-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscriptionManager.js
59 lines (54 loc) · 2.45 KB
/
subscriptionManager.js
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
import { log } from './util/logger.js';
import SubscriptionDao from './daos/SubscriptionDao.js';
import Constants from './Constants.js';
// -----------------------------------------------------------------------------------------------
// === SUBSCRIPTION MANAGER FUNCTIONS ===
// Function to unsubscribe a user from all entry types.
const blockSubscription = async (subscriber) => {
try {
const res = await SubscriptionDao.blockSubscriptions(subscriber.chat_id);
if (res.nModified !== 0) {
log(`=> Blocked all subscriptions for ${subscriber.chat_id}: ${subscriber.username}.`);
} else {
log(`=> Subscriptions already inactive.`);
}
} catch (err) {
log(`=> Error in blocking subscriptions:`);
log(err);
}
};
// Function to notify a subscriber about updated entries of an entryType.
const notifySubscriber = async (entryType, subscriber, tg, dataObj) => {
log(`=> Sending to ${subscriber.chat_id}: ${subscriber.username}`);
const message = dataObj['savedMessages'][entryType];
try {
await tg.sendMessage(subscriber.chat_id, `🔴 *NEW ${entryType.toUpperCase()} ALERT*\n\n` + message, Constants.MARKDOWN);
log(`=> Successful notification to ${subscriber.chat_id}: ${subscriber.username}.`);
} catch (err) {
log(`=> Failed to notify ${subscriber.chat_id}: ${subscriber.username}`);
log(err);
if (err.code === 403) {
log(`=> User has blocked the bot. Unsubscribing ${subscriber.chat_id}: ${subscriber.username}.`);
await blockSubscription(subscriber);
}
}
};
// Function to notify all subscriber about updated entries of an entryType.
const notifySubscribers = async (entryType, tg, dataObj) => {
log(`=> Notifying all subscribers for ${entryType}.`);
const subscribers = await SubscriptionDao.findActiveSubscriptions(entryType);
return Promise.all(subscribers.map(subscriber => notifySubscriber(entryType, subscriber, tg, dataObj)));
};
// Function to notify all subscribers about updated entries of all entryTypes.
const notifyAllSubscribers = async (tg, dataObj, iterationUpdatesFound) => {
return Promise.all(Constants.ALL_ENTRY_TYPES.map(entryType => {
if (iterationUpdatesFound[entryType])
return notifySubscribers(entryType, tg, dataObj);
})).catch(err => {
log(`=> Error in notifying subscribers:`);
log(err);
});
};
export default {
notifyAllSubscribers
}