Skip to content

Commit

Permalink
msg favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwnloyblog committed Jan 9, 2025
1 parent 8e57ebd commit 11e60f8
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 38 deletions.
17 changes: 17 additions & 0 deletions commons/pbdefines/appbusiness.proto
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,21 @@ message AiBotInfo {
message AiBotInfos{
repeated AiBotInfo items =1;
string offset = 2;
}

//favorite msg
message FavoriteMsg{
string sender_id = 1;
string receiver_id = 2;
int32 channel_type = 3;
string msg_id = 4;
int64 msg_time = 5;
string msg_type = 6;
string msg_content = 7;
int64 created_time = 8;
}

message FavoriteMsgs{
repeated FavoriteMsg items = 1;
string offset = 2;
}
283 changes: 245 additions & 38 deletions commons/pbdefines/pbobjs/appbusiness.pb.go

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions docs/jim.sql
Original file line number Diff line number Diff line change
Expand Up @@ -879,4 +879,21 @@ CREATE TABLE `qrcoderecords` (
UNIQUE KEY `uniq_id` (`app_key`,`code_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT = '二维码扫码记录表';

DROP TABLE IF EXISTS `favoritemsgs`;
CREATE TABLE `favoritemsgs` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR(50) NULL,
`sender_id` VARCHAR(50) NULL,
`receiver_id` VARCHAR(50) NULL,
`channel_type` TINYINT NULL,
`msg_id` VARCHAR(50) NULL,
`msg_time` BIGINT DEFAULT '0',
`msg_type` VARCHAR(50) NULL,
`msg_content` TEXT NULL,
`created_time` DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3),
`app_key` VARCHAR(20) NULL,
PRIMARY KEY (`id`),
INDEX `idx_userid` (`app_key`, `user_id`, `created_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT IGNORE INTO `accounts`(`account`,`password`)VALUES('admin','7c4a8d09ca3762af61e59520943dc26494f8941b');
42 changes: 42 additions & 0 deletions services/appbusiness/apis/favorite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package apis

import (
"im-server/commons/errs"
"im-server/commons/pbdefines/pbobjs"
"im-server/services/appbusiness/httputils"
"im-server/services/appbusiness/services"
"strconv"
)

func AddFavoriteMsg(ctx *httputils.HttpContext) {
req := &pbobjs.FavoriteMsg{}
if err := ctx.BindJson(req); err != nil {
ctx.ResponseErr(errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
return
}
code := services.AddFavoriteMsg(ctx.ToRpcCtx(), req)
if code != errs.IMErrorCode_SUCCESS {
ctx.ResponseErr(code)
return
}
ctx.ResponseSucc(nil)
}

func QryFavoriteMsgs(ctx *httputils.HttpContext) {
offset := ctx.Query("offset")
limit := 20
var err error
limitStr := ctx.Query("limit")
if limitStr != "" {
limit, err = strconv.Atoi(limitStr)
if err != nil {
limit = 20
}
}
code, msgs := services.QryFavoriteMsgs(ctx.ToRpcCtx(), int64(limit), offset)
if code != errs.IMErrorCode_SUCCESS {
ctx.ResponseErr(code)
return
}
ctx.ResponseSucc(msgs)
}
4 changes: 4 additions & 0 deletions services/appbusiness/apis/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func LoadAppApis(mux *http.ServeMux) {
RouteRegiste(mux, http.MethodGet, "/jim/friends/applications", FriendApplications)
RouteRegiste(mux, http.MethodGet, "/jim/friends/myapplications", MyFriendApplications)
RouteRegiste(mux, http.MethodGet, "/jim/friends/mypendingapplications", MyPendingFriendApplications)

//favorite msg
RouteRegiste(mux, http.MethodPost, "/jim/favoritemsgs/add", AddFavoriteMsg)
RouteRegiste(mux, http.MethodGet, "/jim/favoritemsgs/list", QryFavoriteMsgs)
}

func RouteRegiste(mux *http.ServeMux, method, path string, handler func(ctx *httputils.HttpContext)) {
Expand Down
68 changes: 68 additions & 0 deletions services/appbusiness/services/favoritemsgservice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package services

import (
"context"
"im-server/commons/bases"
"im-server/commons/errs"
"im-server/commons/pbdefines/pbobjs"
"im-server/commons/tools"
"im-server/services/appbusiness/storages"
"im-server/services/appbusiness/storages/models"
"im-server/services/commonservices/logs"
"math"
)

func AddFavoriteMsg(ctx context.Context, req *pbobjs.FavoriteMsg) errs.IMErrorCode {
appkey := bases.GetAppKeyFromCtx(ctx)
userId := bases.GetRequesterIdFromCtx(ctx)
storage := storages.NewFavoriteMsgStorage()
err := storage.Create(models.FavoriteMsg{
UserId: userId,
SenderId: req.SenderId,
ReceiverId: req.ReceiverId,
ChannelType: req.ChannelType,
MsgId: req.MsgId,
MsgTime: req.MsgTime,
MsgType: req.MsgType,
MsgContent: req.MsgContent,
AppKey: appkey,
})
if err != nil {
logs.WithContext(ctx).Errorf("save favorite msg fail:%s", err.Error())
return errs.IMErrorCode_APP_DEFAULT
}
return errs.IMErrorCode_SUCCESS
}

func QryFavoriteMsgs(ctx context.Context, limit int64, offset string) (errs.IMErrorCode, *pbobjs.FavoriteMsgs) {
var startId int64 = math.MaxInt64
if offset != "" {
id, err := tools.DecodeInt(offset)
if err == nil && id > 0 {
startId = id
}
}
storage := storages.NewFavoriteMsgStorage()
msgs, err := storage.QueryFavoriteMsgs(bases.GetAppKeyFromCtx(ctx), bases.GetRequesterIdFromCtx(ctx), startId, limit)
if err != nil {
logs.WithContext(ctx).Errorf("failed to query favorite msgs. err:%s", err.Error())
return errs.IMErrorCode_APP_DEFAULT, nil
}
ret := &pbobjs.FavoriteMsgs{
Items: []*pbobjs.FavoriteMsg{},
}
for _, msg := range msgs {
ret.Offset, _ = tools.EncodeInt(msg.ID)
ret.Items = append(ret.Items, &pbobjs.FavoriteMsg{
SenderId: msg.SenderId,
ReceiverId: msg.ReceiverId,
ChannelType: int32(msg.ChannelType),
MsgId: msg.MsgId,
MsgTime: msg.MsgTime,
MsgType: msg.MsgType,
MsgContent: msg.MsgContent,
CreatedTime: msg.CreatedTime.UnixMilli(),
})
}
return errs.IMErrorCode_SUCCESS, ret
}
65 changes: 65 additions & 0 deletions services/appbusiness/storages/dbs/favoritemsgdao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dbs

import (
"im-server/commons/dbcommons"
"im-server/services/appbusiness/storages/models"
"time"
)

type FavoriteMsgDao struct {
ID int64 `gorm:"primary_key"`
UserId string `gorm:"user_id"`
SenderId string `gorm:"user_id"`
ReceiverId string `gorm:"receiver_id"`
ChannelType int32 `gorm:"channel_type"`
MsgId string `gorm:"msg_id"`
MsgTime int64 `gorm:"msg_time"`
MsgType string `gorm:"msg_type"`
MsgContent string `gorm:"msg_content"`
CreatedTime time.Time `gorm:"created_time"`
AppKey string `gorm:"app_key"`
}

func (msg FavoriteMsgDao) TableName() string {
return "favoritemsgs"
}

func (msg FavoriteMsgDao) Create(item models.FavoriteMsg) error {
return dbcommons.GetDb().Create(&FavoriteMsgDao{
UserId: item.UserId,
SenderId: item.SenderId,
ReceiverId: item.ReceiverId,
ChannelType: item.ChannelType,
MsgId: item.MsgId,
MsgTime: item.MsgTime,
MsgType: item.MsgType,
MsgContent: item.MsgContent,
CreatedTime: time.Now(),
AppKey: item.AppKey,
}).Error
}

func (msg FavoriteMsgDao) QueryFavoriteMsgs(appkey, userId string, startId, limit int64) ([]*models.FavoriteMsg, error) {
var items []*FavoriteMsgDao
err := dbcommons.GetDb().Where("app_key=? and user_id=? and id<?", appkey, userId, startId).Order("id desc").Limit(limit).Find(&items).Error
if err != nil {
return nil, err
}
ret := []*models.FavoriteMsg{}
for _, item := range items {
ret = append(ret, &models.FavoriteMsg{
ID: item.ID,
UserId: item.UserId,
SenderId: item.SenderId,
ReceiverId: item.ReceiverId,
ChannelType: item.ChannelType,
MsgId: item.MsgId,
MsgTime: item.MsgTime,
MsgType: item.MsgType,
MsgContent: item.MsgContent,
CreatedTime: item.CreatedTime,
AppKey: item.AppKey,
})
}
return ret, nil
}
22 changes: 22 additions & 0 deletions services/appbusiness/storages/models/favoritemsg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package models

import "time"

type FavoriteMsg struct {
ID int64
UserId string
SenderId string
ReceiverId string
ChannelType int32
MsgId string
MsgTime int64
MsgType string
MsgContent string
CreatedTime time.Time
AppKey string
}

type IFavoriteMsgStorage interface {
Create(item FavoriteMsg) error
QueryFavoriteMsgs(appkey, userId string, startId, limit int64) ([]*FavoriteMsg, error)
}
4 changes: 4 additions & 0 deletions services/appbusiness/storages/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ func NewGrpApplicationStorage() models.IGrpApplicationStorage {
func NewQrCodeRecordStorage() models.IQrCodeRecordStorage {
return &dbs.QrCodeRecordDao{}
}

func NewFavoriteMsgStorage() models.IFavoriteMsgStorage {
return &dbs.FavoriteMsgDao{}
}

0 comments on commit 11e60f8

Please sign in to comment.