-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathtelegram_channel.ts
129 lines (103 loc) · 3.01 KB
/
telegram_channel.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
import { action, computed, observable, makeObservable, runInAction } from 'mobx';
import { BaseStore } from 'models/base_store';
import { makeRequest } from 'network/network';
import { RootStore } from 'state/rootStore';
import { TelegramChannel } from './telegram_channel.types';
export class TelegramChannelStore extends BaseStore {
@observable.shallow
items: { [id: string]: TelegramChannel } = {};
@observable
currentTeamToTelegramChannel?: Array<TelegramChannel['id']>;
@observable.shallow
searchResult: { [key: string]: Array<TelegramChannel['id']> } = {};
private autoUpdateTimer?: ReturnType<typeof setTimeout>;
constructor(rootStore: RootStore) {
super(rootStore);
makeObservable(this);
this.path = '/telegram_channels/';
}
@action.bound
async updateTelegramChannels() {
const response = await makeRequest(this.path, {});
const items = response.reduce(
(acc: any, telegramChannel: TelegramChannel) => ({
...acc,
[telegramChannel.id]: telegramChannel,
}),
{}
);
runInAction(() => {
this.items = {
...this.items,
...items,
};
this.currentTeamToTelegramChannel = response.map((telegramChannel: TelegramChannel) => telegramChannel.id);
});
}
@action.bound
async updateById(id: TelegramChannel['id']) {
const response = await this.getById(id);
runInAction(() => {
this.items = {
...this.items,
[id]: response,
};
});
}
@action.bound
async updateItems(query = '') {
const result = await this.getAll();
runInAction(() => {
this.items = {
...this.items,
...result.reduce(
(acc: { [key: number]: TelegramChannel }, item: TelegramChannel) => ({
...acc,
[item.id]: item,
}),
{}
),
};
this.searchResult = {
...this.searchResult,
[query]: result.map((item: TelegramChannel) => item.id),
};
});
}
getSearchResult = (query = '') => {
if (!this.searchResult[query]) {
return undefined;
}
return this.searchResult[query].map((telegramChannelId: TelegramChannel['id']) => this.items[telegramChannelId]);
};
@computed
get hasItems() {
return Boolean(this.getSearchResult('')?.length);
}
async startAutoUpdate() {
this.autoUpdateTimer = setInterval(this.updateTelegramChannels.bind(this), 3000);
}
async stopAutoUpdate() {
if (this.autoUpdateTimer) {
clearInterval(this.autoUpdateTimer);
}
}
async getTelegramVerificationCode() {
return await makeRequest(`/current_team/get_telegram_verification_code/`, {
withCredentials: true,
});
}
@action.bound
async makeTelegramChannelDefault(id: TelegramChannel['id']) {
return makeRequest(`/telegram_channels/${id}/set_default/`, {
method: 'POST',
});
}
@action.bound
async deleteTelegramChannel(id: TelegramChannel['id']) {
return super.delete(id);
}
async getTelegramChannels() {
return super.getAll();
}
}