-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageQueueManager.cs
150 lines (118 loc) · 4.62 KB
/
MessageQueueManager.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
/* 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.Linq;
using System.Collections.Concurrent;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Unifiedban.Next.Common.Telegram;
namespace Unifiedban.Next.Terminal.Telegram;
internal class MessageQueueManager
{
private static bool _isInitialized;
private static bool _isDisposing;
private static readonly ConcurrentDictionary<long, MessageQueue> PrivateChats = new();
private static readonly ConcurrentDictionary<long, MessageQueue> GroupChats = new();
public static void Initialize()
{
var controlChatAdded = AddChatIfNotPresent(CacheData.ControlChatId);
if (!controlChatAdded)
{
// todo - log
}
_isInitialized = true;
// todo - log initialization
}
public static void Dispose()
{
_isDisposing = true;
// Wait until all queues are dispatched
while (PrivateChats.Values
.Where(x => x.Queue.Count > 0)
.ToList().Count > 0
||
GroupChats.Values
.Where(x => x.Queue.Count > 0)
.ToList().Count > 0)
{
}
}
public static bool AddGroupIfNotPresent(Models.Telegram.TGChat group)
{
// Do not accept new chat if going to shutdown
if (_isDisposing)
return false;
if (GroupChats.ContainsKey(group.TelegramChatId))
return false;
var added = GroupChats.TryAdd(group.TelegramChatId,
new MessageQueue(group.TelegramChatId, 20));
return added;
}
public static bool RemoveGroupIfPresent(long telegramChatId)
{
if (_isDisposing)
return false;
return GroupChats.ContainsKey(telegramChatId) && GroupChats.TryRemove(telegramChatId, out _);
}
private static bool AddChatIfNotPresent(long chatId)
{
// Do not accept new chat if going to shutdown
if (_isDisposing)
return false;
if (PrivateChats.ContainsKey(chatId))
return false;
return PrivateChats.TryAdd(chatId, new MessageQueue(chatId, 60));
}
public static void EnqueueMessage(ActionRequest actionRequest)
{
if (!_isInitialized || _isDisposing)
return;
if (actionRequest.Message.Chat.Type is ChatType.Group or ChatType.Supergroup)
{
if (!GroupChats.ContainsKey(actionRequest.Message.Chat.Id)) return;
GroupChats[actionRequest.Message.Chat.Id]
.Queue
.Enqueue(actionRequest);
}
if (actionRequest.Message.Chat.Type != ChatType.Private &&
actionRequest.Message.Chat.Type != ChatType.Channel) return;
if (!PrivateChats.ContainsKey(actionRequest.Message.Chat.Id))
PrivateChats.TryAdd(actionRequest.Message.Chat.Id, new MessageQueue(actionRequest.Message.Chat.Id, 60));
PrivateChats[actionRequest.Message.Chat.Id]
.Queue
.Enqueue(actionRequest);
}
public static void EnqueueLog(ActionRequest actionRequest)
{
if (!_isInitialized || _isDisposing)
return;
#if DEBUG
actionRequest.Message.Text = actionRequest.Message.Text.Replace("#UB", "#UBB");
#endif
actionRequest.Message.Chat = new Chat()
{
Id = CacheData.ControlChatId,
Type = ChatType.Channel
};
actionRequest.Message.DisableWebPagePreview = true;
PrivateChats[CacheData.ControlChatId]
.Queue
.Enqueue(actionRequest);
if (actionRequest.Message.ControlChatId == default) return;
if (PrivateChats.ContainsKey(actionRequest.Message.ControlChatId))
PrivateChats[CacheData.ControlChatId]
.Queue
.Enqueue(actionRequest);
}
}