Skip to content

Commit

Permalink
拉历史消息问题修改
Browse files Browse the repository at this point in the history
TODO(@benqi): 历史消息存储机制需要完善
  • Loading branch information
wubenqi committed Dec 3, 2017
1 parent ddf735c commit 08eb8a7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions biz_model/dal/dao/mysql_dao/message_boxes_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func (dao *MessageBoxesDAO) SelectPtsByGtMessageID(user_id int32, message_id int
return values
}

// select user_id, sender_user_id, message_box_type, peer_type, peer_id, pts, message_id, media_unread, state, date2 from message_boxes where user_id = :user_id and peer_type = :peer_type and peer_id = :peer_id and message_id > :message_id order by pts asc limit :limit
// select user_id, sender_user_id, message_box_type, peer_type, peer_id, pts, message_id, media_unread, state, date2 from message_boxes where user_id = :user_id and peer_type = :peer_type and peer_id = :peer_id and message_id < :message_id order by message_id desc limit :limit
// TODO(@benqi): sqlmap
func (dao *MessageBoxesDAO) SelectByPeerOffsetLimit(user_id int32, peer_type int8, peer_id int32, message_id int32, limit int32) []dataobject.MessageBoxesDO {
var query = "select user_id, sender_user_id, message_box_type, peer_type, peer_id, pts, message_id, media_unread, state, date2 from message_boxes where user_id = ? and peer_type = ? and peer_id = ? and message_id > ? order by pts asc limit ?"
var query = "select user_id, sender_user_id, message_box_type, peer_type, peer_id, pts, message_id, media_unread, state, date2 from message_boxes where user_id = ? and peer_type = ? and peer_id = ? and message_id < ? order by message_id desc limit ?"
rows, err := dao.db.Queryx(query, user_id, peer_type, peer_id, message_id, limit)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions biz_model/dal/dao/mysql_dao/messages_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (dao *MessagesDAO) Insert(do *dataobject.MessagesDO) int64 {
return id
}

// select id, sender_user_id, peer_type, peer_id, random_id, message_type, message_data, date2 from messages where id in (:idList)
// select id, sender_user_id, peer_type, peer_id, random_id, message_type, message_data, date2 from messages where id in (:idList) order by id desc
// TODO(@benqi): sqlmap
func (dao *MessagesDAO) SelectByIdList(idList []int32) []dataobject.MessagesDO {
var q = "select id, sender_user_id, peer_type, peer_id, random_id, message_type, message_data, date2 from messages where id in (?)"
var q = "select id, sender_user_id, peer_type, peer_id, random_id, message_type, message_data, date2 from messages where id in (?) order by id desc"
query, a, err := sqlx.In(q, idList)
rows, err := dao.db.Queryx(query, a...)

Expand Down
4 changes: 2 additions & 2 deletions biz_model/dal/tables/message_boxes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
FROM
message_boxes
WHERE
user_id = :user_id AND peer_type = :peer_type AND peer_id = :peer_id AND message_id > :message_id
ORDER BY pts LIMIT :limit
user_id = :user_id AND peer_type = :peer_type AND peer_id = :peer_id AND message_id < :message_id
ORDER BY message_id DESC LIMIT :limit
]]>
</sql>
</operation>
Expand Down
2 changes: 1 addition & 1 deletion biz_model/dal/tables/messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
FROM
messages
WHERE
id IN (:idList)
id IN (:idList) ORDER BY id DESC
</sql>
</operation>
<!--
Expand Down
12 changes: 8 additions & 4 deletions biz_model/model/message_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const (
MESSAGE_TYPE_MESSAGE = 1
MESSAGE_TYPE_MESSAGE_SERVICE = 2
)
const (
MESSAGE_BOX_TYPE_INCOMING = 0
MESSAGE_BOX_TYPE_OUTGOING = 1
)

type messageModel struct {
// messageDAO *dao.MessagesDAO
Expand Down Expand Up @@ -248,7 +252,7 @@ func (m *messageModel) getMessagesByMessageBoxes(boxes []dataobject.MessageBoxes
switch message.Payload.(type) {
case *mtproto.Message_Message:
m2 := message.GetMessage()
if boxDO.MessageBoxType == 1 {
if boxDO.MessageBoxType == MESSAGE_BOX_TYPE_OUTGOING {
m2.Out = true
} else {
m2.Out = false
Expand All @@ -259,7 +263,7 @@ func (m *messageModel) getMessagesByMessageBoxes(boxes []dataobject.MessageBoxes
glog.Infof("message(%d): %v", i, m2)
case *mtproto.Message_MessageService:
m2 := message.GetMessageService()
if boxDO.MessageBoxType == 0 {
if boxDO.MessageBoxType == MESSAGE_BOX_TYPE_OUTGOING {
m2.Out = true
} else {
m2.Out = false
Expand Down Expand Up @@ -299,7 +303,7 @@ func (m *messageModel) CreateMessageBoxes(userId, fromId int32, peerType int32,
if incoming {
messageBox.UserId = userId
messageBox.SenderUserId = fromId
messageBox.MessageBoxType = 0
messageBox.MessageBoxType = MESSAGE_BOX_TYPE_INCOMING
messageBox.PeerType = int8(peerType)
messageBox.PeerId = peerId
messageBox.MessageId = messageId
Expand All @@ -310,7 +314,7 @@ func (m *messageModel) CreateMessageBoxes(userId, fromId int32, peerType int32,
} else {
messageBox.UserId = userId
messageBox.SenderUserId = fromId
messageBox.MessageBoxType = 1
messageBox.MessageBoxType = MESSAGE_BOX_TYPE_OUTGOING
messageBox.PeerType = int8(peerType)
messageBox.PeerId = peerId
messageBox.MessageId = messageId
Expand Down
11 changes: 8 additions & 3 deletions biz_server/messages/rpc/messages_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/nebulaim/telegramd/grpc_util"
delivery2 "github.com/nebulaim/telegramd/biz_server/delivery"
"github.com/nebulaim/telegramd/frontend/id"
"math"
)

type MessagesServiceImpl struct {
Expand Down Expand Up @@ -230,9 +231,12 @@ func (s *MessagesServiceImpl) MessagesGetHistory(ctx context.Context, request *m
peer := base.FromInputPeer(request.Peer)

chatIdList := []int32{}
userIdList := []int32{}
userIdList := []int32{md.UserId}

offsetId := request.OffsetId + request.AddOffset
if offsetId <= 0 {
offsetId = math.MaxInt32
}
messages := model.GetMessageModel().GetMessagesByUserIdPeerOffsetLimit(md.UserId, peer.PeerType, peer.PeerId, offsetId, request.Limit)
for _, message := range messages {
switch message.Payload.(type) {
Expand Down Expand Up @@ -332,7 +336,7 @@ func (s *MessagesServiceImpl) MessagesGetDialogs(ctx context.Context, request *m
messageDialogs := &mtproto.TLMessagesDialogs{}

messageIdList := []int32{}
userIdList := []int32{}
userIdList := []int32{md.UserId}
chatIdList := []int32{}

for _, dialog := range dialogs {
Expand Down Expand Up @@ -559,7 +563,8 @@ func (s *MessagesServiceImpl) MessagesSendMessage(ctx context.Context, request *
}

// 2. MessageBoxes
pts := model.GetMessageModel().CreateMessageBoxes(userId, md.UserId, peer.PeerType, peer.PeerId, false, messageId)
outgoing := userId == md.UserId
pts := model.GetMessageModel().CreateMessageBoxes(userId, md.UserId, peer.PeerType, peer.PeerId, outgoing, messageId)
model.GetDialogModel().CreateOrUpdateByLastMessage(userId, peer.PeerType, peer.PeerId, messageId, message.Mentioned)

// inPts := model.GetMessageModel().CreateMessageBoxes(peer.PeerId, message.FromId, peer, true, messageId)
Expand Down

0 comments on commit 08eb8a7

Please sign in to comment.