/
twitch-per-user-polling-api.ts
158 lines (131 loc) · 7.15 KB
/
twitch-per-user-polling-api.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
/*
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 Bluebird from "bluebird";
import IStartableStoppable from "@botten-nappet/shared/startable-stoppable/istartable-stoppable";
import GracefulShutdownManager from "@botten-nappet/shared/util/graceful-shutdown-manager";
import PinoLogger from "@botten-nappet/shared/util/pino-logger";
import Config from "../config/config";
/* tslint:disable max-line-length */
import MessageQueuePublisher from "@botten-nappet/shared/message-queue/publisher";
import MessageQueueTopicPublisher from "@botten-nappet/shared/message-queue/topic-publisher";
import IPollingCheermotesResponse from "@botten-nappet/backend-twitch/interface/response/polling/icheermotes-polling-response";
import IPollingFollowingResponse from "@botten-nappet/backend-twitch/interface/response/polling/ifollowing-polling-response";
import IPollingStreamingResponse from "@botten-nappet/backend-twitch/interface/response/polling/istreaming-polling-response";
import PollingClientIdConnection from "@botten-nappet/backend-twitch/polling/connection/polling-clientid-connection";
import IncomingCheermotesCommandEventTranslator from "@botten-nappet/backend-twitch/translator/incoming-cheermotes-event-translator";
import IncomingFollowingCommandEventTranslator from "@botten-nappet/backend-twitch/translator/incoming-following-event-translator";
import IncomingStreamingCommandEventTranslator from "@botten-nappet/backend-twitch/translator/incoming-streaming-event-translator";
import IIncomingCheermotesEvent from "@botten-nappet/interface-twitch/event/iincoming-cheermotes-event";
import IIncomingFollowingEvent from "@botten-nappet/interface-twitch/event/iincoming-following-event";
import IIncomingStreamingEvent from "@botten-nappet/interface-twitch/event/iincoming-streaming-event";
/* tslint:enable max-line-length */
export default class TwitchPerUserPollingApi {
private startables: IStartableStoppable[];
private twitchUserId: number;
private twitchPollingCheermotesConnection: PollingClientIdConnection<IPollingCheermotesResponse>;
private twitchPollingStreamingConnection: PollingClientIdConnection<IPollingStreamingResponse>;
private twitchPollingFollowingConnection: PollingClientIdConnection<IPollingFollowingResponse>;
private messageQueuePublisher: MessageQueuePublisher;
private gracefulShutdownManager: GracefulShutdownManager;
private logger: PinoLogger;
private config: Config;
constructor(
config: Config,
logger: PinoLogger,
gracefulShutdownManager: GracefulShutdownManager,
messageQueuePublisher: MessageQueuePublisher,
twitchPollingFollowingConnection: PollingClientIdConnection<IPollingFollowingResponse>,
twitchPollingStreamingConnection: PollingClientIdConnection<IPollingStreamingResponse>,
twitchPollingCheermotesConnection: PollingClientIdConnection<IPollingCheermotesResponse>,
twitchUserId: number,
) {
// TODO: validate arguments.
this.config = config;
this.logger = logger.child(this.constructor.name);
this.gracefulShutdownManager = gracefulShutdownManager;
this.messageQueuePublisher = messageQueuePublisher;
this.twitchPollingFollowingConnection = twitchPollingFollowingConnection;
this.twitchPollingStreamingConnection = twitchPollingStreamingConnection;
this.twitchPollingCheermotesConnection = twitchPollingCheermotesConnection;
this.twitchUserId = twitchUserId;
this.startables = [];
}
public async start(): Promise<void> {
const messageQueueTopicPublisherForIIncomingFollowingEvent =
new MessageQueueTopicPublisher<IIncomingFollowingEvent>(
this.logger,
this.messageQueuePublisher,
this.config.topicTwitchIncomingFollowingEvent,
);
const messageQueueTopicPublisherForIIncomingStreamingEvent =
new MessageQueueTopicPublisher<IIncomingStreamingEvent>(
this.logger,
this.messageQueuePublisher,
this.config.topicTwitchIncomingStreamingEvent,
);
const messageQueueTopicPublisherForIIncomingCheermotesEvent =
new MessageQueueTopicPublisher<IIncomingCheermotesEvent>(
this.logger,
this.messageQueuePublisher,
this.config.topicTwitchIncomingCheermotesEvent,
);
const twitchIncomingFollowingCommandEventTranslator = new IncomingFollowingCommandEventTranslator(
this.logger,
this.twitchPollingFollowingConnection,
messageQueueTopicPublisherForIIncomingFollowingEvent,
this.config.twitchUserName,
this.twitchUserId,
);
const twitchIncomingStreamingCommandEventTranslator = new IncomingStreamingCommandEventTranslator(
this.logger,
this.twitchPollingStreamingConnection,
messageQueueTopicPublisherForIIncomingStreamingEvent,
this.config.twitchUserName,
this.twitchUserId,
);
const twitchIncomingCheermotesCommandEventTranslator = new IncomingCheermotesCommandEventTranslator(
this.logger,
this.twitchPollingCheermotesConnection,
messageQueueTopicPublisherForIIncomingCheermotesEvent,
this.config.twitchUserName,
this.twitchUserId,
);
this.startables.push(twitchIncomingFollowingCommandEventTranslator);
this.startables.push(twitchIncomingStreamingCommandEventTranslator);
this.startables.push(twitchIncomingCheermotesCommandEventTranslator);
await Bluebird.map(this.startables, async (startable) => startable.start());
this.logger.info({
twitchUserId: this.twitchUserId,
twitchUserName: this.config.twitchUserName,
}, "Started listening to events");
await this.gracefulShutdownManager.waitForShutdownSignal();
}
public async stop(): Promise<void> {
// TODO: better cleanup handling.
// TODO: check if each of these have been started successfully.
// TODO: better null handling.
await Bluebird.map(
this.startables,
async (startable) => {
try {
await startable.stop();
} catch (error) {
this.logger.error(error, startable, "Swallowed error while stopping.");
}
},
);
}
}