-
Notifications
You must be signed in to change notification settings - Fork 362
/
messages.addChatUser_handler.go
143 lines (127 loc) · 4.31 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2022 Teamgram Authors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: teamgramio (teamgram.io@gmail.com)
//
package core
import (
"math/rand"
"github.com/teamgram/proto/mtproto"
msgpb "github.com/teamgram/teamgram-server/app/messenger/msg/msg/msg"
chatpb "github.com/teamgram/teamgram-server/app/service/biz/chat/chat"
userpb "github.com/teamgram/teamgram-server/app/service/biz/user/user"
)
// MessagesAddChatUser
// messages.addChatUser#f24753e3 chat_id:long user_id:InputUser fwd_limit:int = Updates;
func (c *ChatsCore) MessagesAddChatUser(in *mtproto.TLMessagesAddChatUser) (*mtproto.Updates, error) {
var (
err error
addUser = mtproto.FromInputUser(c.MD.UserId, in.UserId)
chat *mtproto.MutableChat
inviterId int64
isBot = false
)
if !addUser.IsUser() || addUser.PeerId == 0 {
err = mtproto.ErrPeerIdInvalid
c.Logger.Errorf("messages.addChatUser - error: %v", err)
return nil, err
}
if !c.MD.IsAdmin {
inviterId = c.MD.UserId
// 400 USERS_TOO_MUCH The maximum number of users has been exceeded (to create a chat, for example)
// 400 USER_ALREADY_PARTICIPANT The user is already in the group
// 400 USER_ID_INVALID The provided user ID is invalid
// 403 USER_NOT_MUTUAL_CONTACT The provided user is not a mutual contact
// 403 USER_PRIVACY_RESTRICTED The user's privacy settings do not allow you to do this
users, _ := c.svcCtx.Dao.UserClient.UserGetMutableUsers(c.ctx, &userpb.TLUserGetMutableUsers{
Id: []int64{inviterId, addUser.PeerId},
})
me, _ := users.GetImmutableUser(inviterId)
added, _ := users.GetImmutableUser(addUser.PeerId)
if me == nil || added == nil {
err = mtproto.ErrPeerIdInvalid
c.Logger.Errorf("messages.addChatUser - error: %v", err)
return nil, err
}
if added.IsBot() && added.BotNochats() {
err = mtproto.ErrBotGroupsBlocked
c.Logger.Errorf("messages.addChatUser - error: %v", err)
return nil, err
}
rules, _ := c.svcCtx.Dao.UserClient.UserGetPrivacy(c.ctx, &userpb.TLUserGetPrivacy{
UserId: addUser.PeerId,
KeyType: userpb.CHAT_INVITE,
})
if len(rules.Datas) > 0 {
// return true
allowAddChat := userpb.CheckPrivacyIsAllow(
addUser.PeerId,
rules.Datas,
inviterId,
func(id, checkId int64) bool {
contact, _ := c.svcCtx.Dao.UserClient.UserCheckContact(c.ctx, &userpb.TLUserCheckContact{
UserId: id,
Id: checkId,
})
return mtproto.FromBool(contact)
},
func(checkId int64, idList []int64) bool {
chatIdList, _ := mtproto.SplitChatAndChannelIdList(idList)
return c.svcCtx.Dao.ChatClient.CheckParticipantIsExist(c.ctx, checkId, chatIdList)
})
if !allowAddChat {
err = mtproto.ErrUserPrivacyRestricted
c.Logger.Errorf("not allow addChat: %v", err)
return nil, err
}
}
isBot = added.IsBot()
} else {
inviterId = 0
}
chat, err = c.svcCtx.Dao.ChatClient.Client().ChatAddChatUser(c.ctx, &chatpb.TLChatAddChatUser{
ChatId: in.ChatId,
InviterId: inviterId,
UserId: addUser.PeerId,
IsBot: isBot,
})
//request.ChatId, md.UserId, peer.PeerId)
if err != nil {
c.Logger.Errorf("addChatUser error: %v", err)
return nil, err
}
fromId := c.MD.UserId
if c.MD.IsAdmin {
fromId = chat.Creator()
}
rUpdates, err := c.svcCtx.Dao.MsgClient.MsgSendMessage(c.ctx, &msgpb.TLMsgSendMessage{
UserId: fromId,
AuthKeyId: c.MD.AuthId,
PeerType: mtproto.PEER_CHAT,
PeerId: in.ChatId,
Message: msgpb.MakeTLOutboxMessage(&msgpb.OutboxMessage{
NoWebpage: true,
Background: false,
RandomId: rand.Int63(),
Message: chat.MakeMessageService(fromId, mtproto.MakeMessageActionChatAddUser(addUser.PeerId)),
ScheduleDate: nil,
}).To_OutboxMessage(),
})
if err != nil {
c.Logger.Errorf("addChatUser error: %v", err)
return nil, err
}
return rUpdates, nil
}