-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.cs
244 lines (216 loc) · 9.56 KB
/
Functions.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* unified/ban - Management and protection systems
© fabricators SRL, https://fabricators.ltd , https://unifiedban.solutions
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License with our addition
to Section 7 as published in unified/ban's the GitHub repository.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License and the
additional terms along with this program.
If not, see <https://docs.fabricators.ltd/docs/licenses/unifiedban>.
For more information, see Licensing FAQ:
https://docs.fabricators.ltd/docs/licenses/faq */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Unifiedban.Next.Common.Telegram;
namespace Unifiedban.Next.Terminal.Telegram;
internal partial class TelegramManager
{
private async Task<UserPrivileges> GetUserPrivileges(long chatId, long? userId)
{
if(userId is null)
return new UserPrivileges();
if (!CacheData.UserPermissions.ContainsKey(chatId))
CacheData.UserPermissions[chatId] = new Dictionary<long, UserPrivileges>();
if (CacheData.UserPermissions[chatId].ContainsKey((long)userId))
{
return CacheData.UserPermissions[chatId][(long)userId];
}
if (CacheData.UserPermissions[chatId].ContainsKey(0))
{
return CacheData.UserPermissions[chatId][0];
}
// TODO - reload permissions time by time. No notification is sent by Telegram
// TODO - get chat admins too
var chat = await BotClient!.GetChatAsync(chatId);
CacheData.UserPermissions[chatId][0] = new UserPrivileges
{
CanManageChat = false,
CanPostMessages = chat.Permissions?.CanSendMessages ?? false,
CanEditMessages = false,
CanDeleteMessages = false,
CanManageVoiceChats = false,
CanRestrictMembers = false,
CanPromoteMembers = false,
CanChangeInfo = chat.Permissions?.CanChangeInfo ?? false,
CanInviteUsers = chat.Permissions?.CanInviteUsers ?? false,
CanPinMessages = chat.Permissions?.CanPinMessages ?? false
};
return CacheData.UserPermissions[chatId][0];
}
private void MigrateFromV3(Message message)
{
HandleMessage(message);
}
private async void RegisterNewChat(Message message)
{
if (message.MigrateFromChatId != null)
{
MigrateChat(message);
return;
}
Common.Utils.WriteLine($"Registering new chat {message.Chat.Id}");
ChatMember? creator;
try
{
var admins = await BotClient!.GetChatAdministratorsAsync(message.Chat!);
creator = admins.FirstOrDefault(x => x.Status == ChatMemberStatus.Creator);
if (creator is null)
{
Common.Utils.WriteLine($"Can't register new chat {message.Chat.Id} since can't get creator", 3);
SendToControlChat($"Can't register new chat {message.Chat.Id} since can't get creator");
return;
}
}
catch (Exception ex)
{
Common.Utils.WriteLine($"Can't register new chat {message.Chat.Id}: {ex.Message}", 2);
return;
}
var newChat = _tgChatLogic.Register(message.Chat.Id, message.Chat.Title ?? "",
CacheData.ControlChatId, message.From?.LanguageCode ?? "en",
creator.User.Id, LastVersion);
if (newChat.StatusCode == 200)
{
CacheData.Chats.Add(message.Chat.Id, newChat.Payload);
if (LoadChatPermissions(message.Chat.Id))
{
// TODO - Send welcome to IC message
if(CacheData.Chats[message.Chat.Id].LastVersion == LastVersion)
BotClient!.SendTextMessageAsync(message.Chat.Id, "Welcome to IC!").Wait();
else
BotClient!.SendTextMessageAsync(message.Chat.Id, "Thank you for migrating to IC!").Wait();
}
MessageQueueManager.AddGroupIfNotPresent(CacheData.Chats[message.Chat.Id]);
HandleRegistrationMessages(message.Chat.Id);
return;
}
Common.Utils.WriteLine($"Can't register new chat {message.Chat.Id} with error: {newChat.StatusDescription}", 3);
SendToControlChat($"Can't register new chat {message.Chat.Id} with error: {newChat.StatusDescription}");
}
private void MigrateChat(Message message)
{
if (message.MigrateFromChatId == null)
{
// TODO - log error
return;
}
// get chat from database
var oldChat = CacheData.Chats[message.MigrateFromChatId!.Value];
oldChat.TelegramChatId = message.Chat.Id;
// update record with new id
var newChat = _tgChatLogic.Update(oldChat);
if (newChat.StatusCode != 200)
{
// TODO - log
return;
}
CacheData.Chats.Add(message.Chat.Id, newChat.Payload);
LoadChatPermissions(message.Chat.Id);
if (!CacheData.BotPermissions.ContainsKey(message.MigrateFromChatId!.Value) &&
CacheData.BotPermissions[message.Chat.Id].CanManageChat)
{
BotClient!.SendTextMessageAsync(message.Chat.Id, "Welcome to IC!").Wait();
}
else
{
CacheData.BotPermissions.Remove(message.MigrateFromChatId!.Value);
}
CacheData.Chats.Remove(message.MigrateFromChatId!.Value);
HandleRegistrationMessages(message.Chat.Id);
}
private void HandleRegistrationMessages(long chatId)
{
List<Message> toHandle;
lock (_regInProgObject)
{
toHandle = new List<Message>(_registrationInProgress[chatId]);
_registrationInProgress.Remove(chatId);
}
foreach (var message in toHandle)
HandleMessage(message);
}
private void SendToControlChat(string message)
{
if (CacheData.ControlChatId == 0) return;
BotClient!.SendTextMessageAsync(CacheData.ControlChatId,message);
}
/// <summary>
/// Get Bot's privileges in chat
/// </summary>
/// <param name="chatId">The target chat id</param>
/// <returns>True if bot is chat admin</returns>
private bool LoadChatPermissions(long chatId)
{
Common.Utils.WriteLine("Getting bot's permissions for chat(s)");
var chatMember = BotClient!.GetChatMemberAsync(chatId, _myId).Result;
if (chatMember is ChatMemberAdministrator chatMemberAdministrator)
{
CacheData.BotPermissions[chatId] = new UserPrivileges
{
CanManageChat = chatMemberAdministrator.CanManageChat,
CanPostMessages = chatMemberAdministrator.CanPostMessages ?? false,
CanEditMessages = chatMemberAdministrator.CanEditMessages ?? false,
CanDeleteMessages = chatMemberAdministrator.CanDeleteMessages,
CanManageVoiceChats = chatMemberAdministrator.CanManageVoiceChats,
CanRestrictMembers = chatMemberAdministrator.CanRestrictMembers,
CanPromoteMembers = chatMemberAdministrator.CanPromoteMembers,
CanChangeInfo = chatMemberAdministrator.CanChangeInfo,
CanInviteUsers = chatMemberAdministrator.CanInviteUsers,
CanPinMessages = chatMemberAdministrator.CanPinMessages ?? false
};
return true;
}
if (!CacheData.UserPermissions.ContainsKey(chatId)) return false;
if (!CacheData.UserPermissions[chatId].ContainsKey(0)) return false;
if (CacheData.UserPermissions[chatId][0].CanPostMessages)
{
BotClient!.SendTextMessageAsync(chatId, "Bot must be set as Administrator to work properly").Wait();
}
return false;
}
/// <summary>
/// Get all chat admins and fill <see cref="CacheData.UserPermissions"/> with obtained data
/// </summary>
/// <param name="chatId">The target chat id</param>
private async void GetChatAdmins(long chatId)
{
if (!CacheData.UserPermissions.ContainsKey(chatId))
CacheData.UserPermissions[chatId] = new Dictionary<long, UserPrivileges>();
var chatAdmins = await BotClient!.GetChatAdministratorsAsync(chatId);
foreach (var chatMember in chatAdmins)
{
var chatAdmin = (ChatMemberAdministrator) chatMember;
CacheData.UserPermissions[chatId][chatMember.User.Id] = new UserPrivileges
{
CanManageChat = chatAdmin.CanManageChat,
CanPostMessages = chatAdmin.CanPostMessages ?? false,
CanEditMessages = chatAdmin.CanEditMessages ?? false,
CanDeleteMessages = chatAdmin.CanDeleteMessages,
CanManageVoiceChats = chatAdmin.CanManageVoiceChats,
CanRestrictMembers = chatAdmin.CanRestrictMembers,
CanPromoteMembers = chatAdmin.CanPromoteMembers,
CanChangeInfo = chatAdmin.CanChangeInfo,
CanInviteUsers = chatAdmin.CanInviteUsers,
CanPinMessages = chatAdmin.CanPinMessages ?? false
};
}
}
}