/
per-user-handlers-main.ts
313 lines (279 loc) · 18.6 KB
/
per-user-handlers-main.ts
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
This file is part of botten-nappet -- a Twitch bot and streaming tool.
<https://joelpurra.com/projects/botten-nappet/>
Copyright (c) 2018 Joel Purra <https://joelpurra.com/>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
context,
} from "@botten-nappet/backend-shared/lib/dependency-injection/context/context";
import {
scoped,
} from "@botten-nappet/backend-shared/lib/dependency-injection/scoped/scoped";
import {
within,
} from "@botten-nappet/backend-shared/lib/dependency-injection/within/within";
import Bluebird from "bluebird";
import IStartableStoppable from "@botten-nappet/shared/src/startable-stoppable/istartable-stoppable";
import BackendConfig from "@botten-nappet/backend-shared/src/config/backend-config";
import PinoLogger from "@botten-nappet/shared/src/util/pino-logger";
/* tslint:disable max-line-length */
import TwitchCheeringIrcReplyHandler from "@botten-nappet/backend-twitch/src/handler/cheering-irc-reply-handler";
import TwitchCheeringWithCheermotesHandler from "@botten-nappet/backend-twitch/src/handler/cheering-with-cheermotes-handler";
import TwitchIrcFollowReminderHandler from "@botten-nappet/backend-twitch/src/handler/follow-reminder";
import TwitchFollowingIrcReplyHandler from "@botten-nappet/backend-twitch/src/handler/following-irc-reply-handler";
import TwitchIrcGreetingHandler from "@botten-nappet/backend-twitch/src/handler/greeting";
import TwitchIrcNewChatterHandler from "@botten-nappet/backend-twitch/src/handler/new-chatter";
import TwitchStreamingStatisticsCollectorHandler from "@botten-nappet/backend-twitch/src/handler/streaming-statistics-collector-handler";
import TwitchIrcSubscribingHandler from "@botten-nappet/backend-twitch/src/handler/subscribing";
import TwitchSubscriptionIrcReplyHandler from "@botten-nappet/backend-twitch/src/handler/subscription-irc-reply-handler";
import TwitchIrcTextResponseCommandHandler from "@botten-nappet/backend-twitch/src/handler/text-response-command";
import TwitchIrcVidyCommandHandler from "@botten-nappet/backend-twitch/src/handler/vidy-command";
import TwitchIrcVidyResultEventHandler from "@botten-nappet/backend-twitch/src/handler/vidy-result-event";
import TwitchWhisperIrcReplyHandler from "@botten-nappet/backend-twitch/src/handler/whisper-irc-reply-handler";
import IncomingCheeringCommandEventTranslator from "@botten-nappet/backend-twitch/src/translator/incoming-cheering-event-translator";
import IncomingSubscriptionCommandEventTranslator from "@botten-nappet/backend-twitch/src/translator/incoming-subscription-event-translator";
import IncomingWhisperCommandEventTranslator from "@botten-nappet/backend-twitch/src/translator/incoming-whisper-event-translator";
import TwitchUserIdProvider from "@botten-nappet/backend-twitch/src/authentication/user-id-provider";
import TwitchUserNameProvider from "@botten-nappet/backend-twitch/src/authentication/user-name-provider";
import IncomingCheeringEventTopicPublisher from "@botten-nappet/server-backend/src/topic-publisher/incoming-cheering-event-topic-publisher";
import IncomingCheeringWithCheermotesEventTopicPublisher from "@botten-nappet/server-backend/src/topic-publisher/incoming-cheering-with-cheermotes-event-topic-publisher";
import IncomingSubscriptionEventTopicPublisher from "@botten-nappet/server-backend/src/topic-publisher/incoming-subscription-event-topic-publisher";
import IncomingWhisperEventTopicPublisher from "@botten-nappet/server-backend/src/topic-publisher/incoming-whisper-event-topic-publisher";
import OutgoingIrcCommandTopicPublisher from "@botten-nappet/server-backend/src/topic-publisher/outgoing-irc-command-topic-publisher";
import OutgoingSearchCommandTopicPublisher from "@botten-nappet/server-backend/src/topic-publisher/outgoing-search-command-topic-publisher";
import IncomingCheeringEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-cheering-event-single-item-json-topics-subscriber";
import IncomingCheermotesEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-cheermotes-event-single-item-json-topics-subscriber";
import IncomingFollowingEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-following-event-single-item-json-topics-subscriber";
import IncomingIrcCommandSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-irc-command-single-item-json-topics-subscriber";
import IncomingPubSubEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-pub-sub-event-single-item-json-topics-subscriber";
import IncomingSearchResultEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-search-result-event-single-item-json-topics-subscriber";
import IncomingStreamingEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-streaming-event-single-item-json-topics-subscriber";
import IncomingSubscriptionEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-subscription-event-single-item-json-topics-subscriber";
import IncomingWhisperEventSingleItemJsonTopicsSubscriber from "@botten-nappet/server-backend/src/topics-subscriber/incoming-whisper-event-single-item-json-topics-subscriber";
import BackendTwitchIrcAuthenticatedApplicationApi from "@botten-nappet/server-twitch/src/irc-authenticated-application-api";
import BackendTwitchPollingAuthenticatedApplicationApi from "@botten-nappet/server-twitch/src/polling-authenticated-application-api";
import BackendTwitchPubSubAuthenticatedApplicationApi from "@botten-nappet/server-twitch/src/pubsub-authenticated-application-api";
/* tslint:enable max-line-length */
export default class PerUserHandlersMain implements IStartableStoppable {
private startables: IStartableStoppable[];
private logger: PinoLogger;
constructor(
private readonly backendConfig: BackendConfig,
logger: PinoLogger,
@within(IncomingPubSubEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingPubSubEvent:
IncomingPubSubEventSingleItemJsonTopicsSubscriber,
@within(IncomingIrcCommandSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand:
IncomingIrcCommandSingleItemJsonTopicsSubscriber,
@within(IncomingFollowingEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingFollowingEvent:
IncomingFollowingEventSingleItemJsonTopicsSubscriber,
@within(IncomingStreamingEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingStreamingEvent:
IncomingStreamingEventSingleItemJsonTopicsSubscriber,
@within(IncomingCheermotesEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheermotesEvent:
IncomingCheermotesEventSingleItemJsonTopicsSubscriber,
@within(IncomingCheeringEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringEvent:
IncomingCheeringEventSingleItemJsonTopicsSubscriber,
@within(IncomingWhisperEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingWhisperEvent:
IncomingWhisperEventSingleItemJsonTopicsSubscriber,
@within(IncomingSubscriptionEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSubscriptionEvent:
IncomingSubscriptionEventSingleItemJsonTopicsSubscriber,
@within(IncomingSearchResultEventSingleItemJsonTopicsSubscriber, "BackendAuthenticatedApplicationMain")
private vidyMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSearchResultEvent:
IncomingSearchResultEventSingleItemJsonTopicsSubscriber,
@scoped(OutgoingIrcCommandTopicPublisher)
private readonly messageQueueTopicPublisherForIOutgoingIrcCommand:
OutgoingIrcCommandTopicPublisher,
@scoped(IncomingCheeringEventTopicPublisher)
private readonly messageQueueTopicPublisherForIIncomingCheeringEvent:
IncomingCheeringEventTopicPublisher,
@scoped(IncomingWhisperEventTopicPublisher)
private readonly messageQueueTopicPublisherForIIncomingWhisperEvent:
IncomingWhisperEventTopicPublisher,
@scoped(IncomingSubscriptionEventTopicPublisher)
private readonly messageQueueTopicPublisherForIIncomingSubscriptionEvent:
IncomingSubscriptionEventTopicPublisher,
@scoped(IncomingCheeringWithCheermotesEventTopicPublisher)
private readonly messageQueueTopicPublisherForIIncomingCheeringWithCheermotesEvent:
IncomingCheeringWithCheermotesEventTopicPublisher,
@scoped(OutgoingSearchCommandTopicPublisher)
private readonly messageQueueTopicPublisherForIOutgoingSearchCommand:
OutgoingSearchCommandTopicPublisher,
@scoped(TwitchUserNameProvider)
private readonly twitchUserNameProvider: TwitchUserNameProvider,
@scoped(TwitchUserIdProvider)
private readonly twitchUserIdProvider: TwitchUserIdProvider,
@context(BackendTwitchPubSubAuthenticatedApplicationApi, "BackendTwitchPubSubAuthenticatedApplicationApi")
private readonly backendTwitchPubSubAuthenticatedApplicationApi: BackendTwitchPubSubAuthenticatedApplicationApi,
@context(BackendTwitchIrcAuthenticatedApplicationApi, "BackendTwitchIrcAuthenticatedApplicationApi")
private readonly backendTwitchIrcAuthenticatedApplicationApi: BackendTwitchIrcAuthenticatedApplicationApi,
@context(BackendTwitchPollingAuthenticatedApplicationApi, "BackendTwitchPollingAuthenticatedApplicationApi")
private readonly backendTwitchPollingAuthenticatedApplicationApi:
BackendTwitchPollingAuthenticatedApplicationApi,
) {
// TODO: validate arguments.
this.logger = logger.child(this.constructor.name);
this.startables = [];
}
public async start(): Promise<void> {
const twitchIrcVidyCommandHandler = new TwitchIrcVidyCommandHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIOutgoingSearchCommand,
);
const twitchIrcVidyResultEventHandler = new TwitchIrcVidyResultEventHandler(
this.logger,
this.vidyMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSearchResultEvent,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
this.backendConfig.twitchChannelName,
this.backendConfig.vidyVideoLinkBaseUrl,
);
const twitchIrcTextResponseCommandHandler = new TwitchIrcTextResponseCommandHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
const twitchIrcGreetingHandler = new TwitchIrcGreetingHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
this.twitchUserNameProvider,
);
const twitchIrcNewChatterHandler = new TwitchIrcNewChatterHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
const twitchIrcSubscribingHandler = new TwitchIrcSubscribingHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
const twitchIrcFollowReminderHandler = new TwitchIrcFollowReminderHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
this.backendConfig.twitchChannelName,
);
const twitchFollowingIrcReplyHandler = new TwitchFollowingIrcReplyHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingFollowingEvent,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
const twitchStreamingStatisticsCollector = new TwitchStreamingStatisticsCollectorHandler(
this.logger,
[
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingStreamingEvent,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
],
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
const twitchCheeringWithCheermotesHandler = new TwitchCheeringWithCheermotesHandler(
this.logger,
[
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringEvent,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheermotesEvent,
],
this.messageQueueTopicPublisherForIIncomingCheeringWithCheermotesEvent,
);
const twitchIncomingWhisperCommandEventTranslator = new IncomingWhisperCommandEventTranslator(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingPubSubEvent,
this.messageQueueTopicPublisherForIIncomingWhisperEvent,
);
const twitchWhisperIrcReplyHandler = new TwitchWhisperIrcReplyHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingWhisperEvent,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
this.backendConfig.twitchChannelName,
);
const twitchIncomingCheeringCommandEventTranslator = new IncomingCheeringCommandEventTranslator(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingPubSubEvent,
this.messageQueueTopicPublisherForIIncomingCheeringEvent,
);
const twitchCheeringIrcReplyHandler = new TwitchCheeringIrcReplyHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringEvent,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
const twitchIncomingSubscriptionCommandEventTranslator = new IncomingSubscriptionCommandEventTranslator(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand,
this.messageQueueTopicPublisherForIIncomingSubscriptionEvent,
this.twitchUserNameProvider,
this.twitchUserIdProvider,
);
const twitchSubscriptionIrcReplyHandler = new TwitchSubscriptionIrcReplyHandler(
this.logger,
this.twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSubscriptionEvent,
this.messageQueueTopicPublisherForIOutgoingIrcCommand,
);
this.startables.push(twitchIncomingCheeringCommandEventTranslator);
this.startables.push(twitchIncomingWhisperCommandEventTranslator);
this.startables.push(twitchIncomingSubscriptionCommandEventTranslator);
this.startables.push(twitchIrcGreetingHandler);
this.startables.push(twitchIrcNewChatterHandler);
this.startables.push(twitchIrcSubscribingHandler);
this.startables.push(twitchIrcFollowReminderHandler);
this.startables.push(twitchFollowingIrcReplyHandler);
this.startables.push(twitchStreamingStatisticsCollector);
this.startables.push(twitchCheeringWithCheermotesHandler);
this.startables.push(twitchCheeringIrcReplyHandler);
this.startables.push(twitchWhisperIrcReplyHandler);
this.startables.push(twitchSubscriptionIrcReplyHandler);
this.startables.push(twitchIrcTextResponseCommandHandler);
this.startables.push(twitchIrcVidyCommandHandler);
this.startables.push(twitchIrcVidyResultEventHandler);
await Bluebird.map(this.startables, async (startable) => startable.start());
this.logger.info({
twitchUserId: await this.twitchUserIdProvider.get(),
twitchUserName: await this.twitchUserNameProvider.get(),
}, "Started listening to events");
await Promise.all([
this.backendTwitchPubSubAuthenticatedApplicationApi.start(),
this.backendTwitchIrcAuthenticatedApplicationApi.start(),
this.backendTwitchPollingAuthenticatedApplicationApi.start(),
]);
}
public async stop(): Promise<void> {
// TODO: better cleanup handling.
// TODO: check if each of these have been started successfully.
// TODO: better null handling.
if (this.backendTwitchPubSubAuthenticatedApplicationApi) {
this.backendTwitchPubSubAuthenticatedApplicationApi.stop();
}
if (this.backendTwitchIrcAuthenticatedApplicationApi) {
this.backendTwitchIrcAuthenticatedApplicationApi.stop();
}
if (this.backendTwitchPollingAuthenticatedApplicationApi) {
this.backendTwitchPollingAuthenticatedApplicationApi.stop();
}
await Bluebird.map(
this.startables,
async (startable) => {
try {
await startable.stop();
} catch (error) {
this.logger.error(error, startable, "Swallowed error while stopping.");
}
},
);
}
}