-
Notifications
You must be signed in to change notification settings - Fork 21
/
StoreService.ts
158 lines (138 loc) · 4.92 KB
/
StoreService.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
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
import {error as StoreEngineError} from '@wireapp/store-engine';
import {FileEngine} from '@wireapp/store-engine/dist/commonjs/engine';
import * as logdown from 'logdown';
import * as path from 'path';
import {SubscriberOptions, TravisData} from './Interfaces';
const defaultStorePath = path.join(__dirname, '.temp');
class StoreService {
private readonly STORE_PATH: string;
private readonly TABLE_NAME_SUBSCRIBERS = 'subscribers';
private readonly TABLE_NAME_Data = 'travis-data';
private readonly logger: logdown.Logger;
private readonly storeEngine: FileEngine;
constructor(storePath?: string) {
this.STORE_PATH = storePath ? path.resolve(storePath) : defaultStorePath;
this.storeEngine = new FileEngine(this.STORE_PATH);
this.logger = logdown('@wireapp/travis-status-bot/StoreService', {
logger: console,
markdown: false,
});
this.logger.state.isEnabled = true;
}
public async saveDataToCache(data: TravisData): Promise<void> {
try {
await this.storeEngine.updateOrCreate(this.TABLE_NAME_Data, 'full', data);
} catch (error) {
this.logger.error(error);
}
}
public async loadDataFromCache(): Promise<TravisData | null> {
try {
const data = await this.storeEngine.read<TravisData>(this.TABLE_NAME_Data, 'full');
return data;
} catch (error) {
const recordNotFound =
error instanceof StoreEngineError.RecordNotFoundError ||
error.constructor.name !== StoreEngineError.RecordNotFoundError.name;
if (!recordNotFound) {
this.logger.error(error);
}
return null;
}
}
public async addSubscriber(subscriberId: string): Promise<void> {
try {
await this.storeEngine.updateOrCreate(this.TABLE_NAME_SUBSCRIBERS, subscriberId, {isSubscribed: true});
} catch (error) {
this.logger.error(error);
}
}
public async init(): Promise<void> {
try {
await this.storeEngine.init('travis-status-bot');
} catch (error) {
this.logger.error(error);
}
const subscriberIds = await this.getSubscribers();
if (subscriberIds.length) {
this.logger.info(`Loaded ${subscriberIds.length} subscriber${subscriberIds.length === 1 ? '' : 's'} from cache.`);
} else {
this.logger.info(`No subscribers found in cache.`);
}
const jsonData = await this.loadDataFromCache();
if (jsonData && jsonData.incidents && jsonData.incidents.length) {
this.logger.info(
`Loaded Travis JSON data with ${jsonData.incidents.length} incident${
jsonData.incidents.length === 1 ? '' : 's'
} from cache.`
);
} else {
this.logger.info(`No Travis JSON data found in cache.`);
}
}
public async checkSubscription(conversationId: string): Promise<boolean> {
try {
const {isSubscribed} = await this.storeEngine.read<SubscriberOptions>(
this.TABLE_NAME_SUBSCRIBERS,
conversationId
);
return isSubscribed;
} catch (error) {
const recordNotFound =
error instanceof StoreEngineError.RecordNotFoundError ||
error.constructor.name !== StoreEngineError.RecordNotFoundError.name;
if (!recordNotFound) {
this.logger.error(error);
}
return false;
}
}
public async getSubscribers(): Promise<string[]> {
let conversationIds: string[] = [];
try {
conversationIds = await this.storeEngine.readAllPrimaryKeys(this.TABLE_NAME_SUBSCRIBERS);
} catch (error) {
const recordNotFound =
error instanceof StoreEngineError.RecordNotFoundError ||
error.constructor.name !== StoreEngineError.RecordNotFoundError.name;
if (!recordNotFound) {
this.logger.error(error);
}
return conversationIds;
}
const subscriberIds = [];
for (const conversationId of conversationIds) {
const isSubscribed = await this.checkSubscription(conversationId);
if (isSubscribed) {
subscriberIds.push(conversationId);
}
}
return subscriberIds;
}
public async removeSubscriber(conversationId: string): Promise<void> {
try {
await this.storeEngine.updateOrCreate(this.TABLE_NAME_SUBSCRIBERS, conversationId, {isSubscribed: false});
} catch (error) {
this.logger.error(error);
}
}
}
export {StoreService};