-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e57ebd
commit 11e60f8
Showing
9 changed files
with
484 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters