-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.addChatUser_handler.go
executable file
·97 lines (82 loc) · 3.81 KB
/
messages.addChatUser_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2018, https://github.com/airwide-code/airwide.datacenter
* All rights reserved.
*
*
*
*/
package rpc
import (
"github.com/golang/glog"
"github.com/airwide-code/airwide.datacenter/baselib/logger"
"github.com/airwide-code/airwide.datacenter/baselib/grpc_util"
"github.com/airwide-code/airwide.datacenter/mtproto"
"golang.org/x/net/context"
"github.com/airwide-code/airwide.datacenter/biz/core/chat"
update2 "github.com/airwide-code/airwide.datacenter/biz/core/update"
// "github.com/airwide-code/airwide.datacenter/biz_server/sync_client"
"github.com/airwide-code/airwide.datacenter/biz/base"
"github.com/airwide-code/airwide.datacenter/biz/core/message"
// "github.com/airwide-code/airwide.datacenter/biz/core/user"
"github.com/airwide-code/airwide.datacenter/biz/core/user"
"github.com/airwide-code/airwide.datacenter/biz_server/sync_client"
)
// messages.addChatUser#f9a0aa09 chat_id:int user_id:InputUser fwd_limit:int = Updates;
func (s *MessagesServiceImpl) MessagesAddChatUser(ctx context.Context, request *mtproto.TLMessagesAddChatUser) (*mtproto.Updates, error) {
md := grpc_util.RpcMetadataFromIncoming(ctx)
glog.Infof("messages.addChatUser#f9a0aa09 - metadata: %s, request: %s", logger.JsonDebugData(md), logger.JsonDebugData(request))
var (
err error
addChatUserId int32
)
if request.GetUserId().GetConstructor() == mtproto.TLConstructor_CRC32_inputUserEmpty {
err = mtproto.NewRpcError2(mtproto.TLRpcErrorCodes_BAD_REQUEST)
glog.Error("messages.sendMessage#fa88427a - invalid peer", err)
return nil, err
}
switch request.GetUserId().GetConstructor() {
case mtproto.TLConstructor_CRC32_inputUserEmpty:
case mtproto.TLConstructor_CRC32_inputUserSelf:
addChatUserId = md.UserId
case mtproto.TLConstructor_CRC32_inputUser:
addChatUserId = request.GetUserId().GetData2().GetUserId()
}
chatLogic, _ := chat.NewChatLogicById(request.GetChatId())
chatLogic.AddChatUser(md.UserId, addChatUserId)
peer := &base.PeerUtil{
PeerType: base.PEER_CHAT,
PeerId: chatLogic.GetChatId(),
}
addUserMessage := chatLogic.MakeAddUserMessage(md.UserId, addChatUserId)
randomId := base.NextSnowflakeId()
outbox := message.CreateMessageOutboxByNew(md.UserId, peer, randomId, addUserMessage, func(messageId int32) {
user.CreateOrUpdateByOutbox(md.UserId, peer.PeerType, peer.PeerId, messageId, false, false)
})
_ = outbox
syncUpdates := update2.NewUpdatesLogic(md.UserId)
updateChatParticipants := &mtproto.TLUpdateChatParticipants{Data2: &mtproto.Update_Data{
Participants: chatLogic.GetChatParticipants().To_ChatParticipants(),
}}
syncUpdates.AddUpdate(updateChatParticipants.To_Update())
syncUpdates.AddUpdateNewMessage(addUserMessage)
syncUpdates.AddUsers(user.GetUsersBySelfAndIDList(md.UserId, chatLogic.GetChatParticipantIdList()))
syncUpdates.AddChat(chatLogic.ToChat(md.UserId))
state, _ := sync_client.GetSyncClient().SyncUpdatesData(md.AuthId, md.SessionId, md.UserId, syncUpdates.ToUpdates())
syncUpdates.AddUpdateMessageId(outbox.MessageId, outbox.RandomId)
syncUpdates.SetupState(state)
replyUpdates := syncUpdates.ToUpdates()
inboxList, _ := outbox.InsertMessageToInbox(md.UserId, peer, func(inBoxUserId, messageId int32) {
user.CreateOrUpdateByInbox(inBoxUserId, base.PEER_CHAT, peer.PeerId, messageId, false)
})
for _, inbox := range inboxList {
updates := update2.NewUpdatesLogic(md.UserId)
updates.AddUpdate(updateChatParticipants.To_Update())
updates.AddUpdateNewMessage(inbox.Message)
updates.AddUsers(user.GetUsersBySelfAndIDList(md.UserId, chatLogic.GetChatParticipantIdList()))
updates.AddChat(chatLogic.ToChat(inbox.UserId))
sync_client.GetSyncClient().PushToUserUpdatesData(inbox.UserId, updates.ToUpdates())
}
// TODO(@benqi): Add后需要将聊天历史消息导入
glog.Infof("messages.addChatUser#f9a0aa09 - reply: {%v}", replyUpdates)
return replyUpdates, nil
}