-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageQueue.cs
139 lines (121 loc) · 4.65 KB
/
MessageQueue.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
/* 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 Unifiedban.Next.Common.Telegram;
namespace Unifiedban.Next.Terminal.Telegram;
internal class MessageQueue
{
private long TelegramChatId { get; set; }
private DateTime FirstMessageUtc { get; set; }
private int LastMinuteMessagesCount { get; set; }
private short MaxMessagePerMinute { get; set; }
private System.Timers.Timer QueueTimer { get; set; }
private bool _handlingInProgress;
private ActionHandler _actionHandler = new ();
public Queue<ActionRequest> Queue { get; set; } = new();
public MessageQueue(long telegramChatId, short maxMsgPerMinute)
{
TelegramChatId = telegramChatId;
MaxMessagePerMinute = maxMsgPerMinute;
FirstMessageUtc = DateTime.UtcNow;
QueueTimer = new System.Timers.Timer(100);
QueueTimer.Elapsed += QueueTimer_Elapsed;
QueueTimer.AutoReset = true;
QueueTimer.Start();
}
private void QueueTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
// Check if there is any message in queue
if (Queue.Count == 0)
return;
if (_handlingInProgress)
return;
_handlingInProgress = true;
var doResetCount = false;
while (DateTime.UtcNow
.Subtract(FirstMessageUtc)
.Minutes < 1
&& LastMinuteMessagesCount >= MaxMessagePerMinute)
{
System.Threading.Thread.Sleep(100);
doResetCount = true;
}
ProcessAction(Queue.Dequeue());
// Reset counter and time if we waited previously
if (doResetCount)
{
LastMinuteMessagesCount = 1;
FirstMessageUtc = DateTime.UtcNow;
}
else
{
LastMinuteMessagesCount += 1;
}
_handlingInProgress = false;
}
private void ProcessAction(ActionRequest request)
{
switch (request.Action)
{
case ActionRequest.Actions.LeaveChat:
_actionHandler.LeaveChat(request.Message);
break;
case ActionRequest.Actions.SendText:
_actionHandler.SendMessage(request.Message);
break;
case ActionRequest.Actions.SendImage:
break;
case ActionRequest.Actions.SendVideo:
break;
case ActionRequest.Actions.SendGif:
break;
case ActionRequest.Actions.SendAudio:
break;
case ActionRequest.Actions.DeleteMessage:
_actionHandler.DeleteMessage(request.Message);
break;
case ActionRequest.Actions.EditMessage:
break;
case ActionRequest.Actions.EditMessageCaption:
break;
case ActionRequest.Actions.KickUser:
break;
case ActionRequest.Actions.BanUser:
break;
case ActionRequest.Actions.ApplyUserPermissions:
break;
case ActionRequest.Actions.ApplyChatPermissions:
break;
case ActionRequest.Actions.ExportChatInvite:
break;
case ActionRequest.Actions.CreateChatInvite:
break;
case ActionRequest.Actions.RevokeChatInvite:
break;
case ActionRequest.Actions.ApproveChatJoinRequest:
break;
case ActionRequest.Actions.DeclineChatJoinRequest:
break;
case ActionRequest.Actions.PinMessage:
_actionHandler.PinMessage(request.Message);
break;
case ActionRequest.Actions.UnpinMessage:
break;
case ActionRequest.Actions.UnpinAllMessages:
break;
}
}
}