-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkSubscriptions.ts
179 lines (148 loc) · 6.73 KB
/
checkSubscriptions.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
import * as Discord from 'discord.js';
import BoardRoomApiService from "./services/boardRoomApiService";
import { ISubscription } from "./models";
import SubscriptionService from './services/subscriptionService';
interface Params {
client: Discord.Client;
subscriptions: ISubscription[];
boardroomApi: BoardRoomApiService;
subscriptionService: SubscriptionService;
}
const checkSubscriptions = async ({ subscriptions, boardroomApi, client, subscriptionService }: Params) => {
const now = new Date();
for (let i = 0; i < subscriptions.length; i++) {
const subscription = subscriptions[i];
const channel = client.channels.cache.find(channel => channel.id === subscription.channelId);
if (! channel || ! channel.isText()) {
return;
}
const diffInHours = (now.valueOf() - subscription.lastCheck.valueOf()) / 1000 / 3600;
if (diffInHours < subscription.frequency) {
return;
}
subscription.lastCheck = now;
switch (subscription.type) {
case 'new_protocol': {
const oldProtocols = subscription.protocols;
const currentProtocols = await boardroomApi.listProtocols();
const difference = currentProtocols.data.filter(protocol => ! oldProtocols.find(protocol2 => protocol2.cname === protocol.cname));
if (difference.length > 0) {
const message = `
New protocols for Boardroom!
{${difference.map(protocol => `- ${protocol.name} (${protocol.cname})`).join("\n")}}
`.trim();
channel.send(message);
}
subscription.protocols = currentProtocols.data;
break;
}
case 'new_proposal': {
const newProposals = await boardroomApi.listProtocolProposals(subscription.cname);
const difference = newProposals.data.filter(proposal => {
return proposal.startTime.timestamp > subscription.lastCheck.valueOf() / 1000;
});
if (difference.length > 0) {
const message = `
New proposals for ${subscription.cname}}!
{${difference.map(proposal => `- \`${proposal.title}\` by (${proposal.proposer})`).join("\n")}}
`.trim();
channel.send(message);
}
break;
}
case 'new_proposal_vote': {
const newVotes = await boardroomApi.listProposalVotes(subscription.refId);
const difference = newVotes.data.filter(vote => {
return vote.timestamp > subscription.lastCheck.valueOf() / 1000;
});
if (difference.length > 0) {
const message = `
New votes in ${subscription.proposal.title}}!
{${difference.map(proposalVote => `- \`${proposalVote.address}\` voted ${subscription.proposal.choices[proposalVote.choice]}`).join("\n")}}
`.trim();
channel.send(message);
}
break;
}
case 'new_voter_vote': {
const newVotes = await boardroomApi.listVoterVotes(subscription.address);
const difference = newVotes.data.filter(vote => {
return vote.timestamp > subscription.lastCheck.valueOf() / 1000;
});
if (difference.length > 0) {
const message = `
New votes by ${subscription.address}}!
{${difference.map(voterVote => `- \`${subscription.address}\` voted \`${voterVote.proposalInfo.choices[voterVote.choice]} \` in ${voterVote.proposalInfo.title} (${voterVote.proposalInfo.refId})`).join("\n")}}
`.trim();
channel.send(message);
}
break;
}
case 'proposal_state': {
const oldProposal = subscription.proposal;
const newProposal = await boardroomApi.getProposalDetails(subscription.refId);
if (oldProposal.currentState !== newProposal.data.currentState) {
const message = `
Proposal ${newProposal.data.title} went from \`${oldProposal.currentState}\` to \`${newProposal.data.currentState}\`!
`.trim();
channel.send(message);
}
subscription.proposal = newProposal.data;
break;
}
case 'stats': {
const request = await boardroomApi.getStats();
const oldStats = subscription.stats;
const newStats = request.data;
const oldTotalProposals = oldStats.totalProposals;
const oldTotalProtocols = oldStats.totalProtocols;
const oldTotalUniqueVoters = oldStats.totalUniqueVoters;
const oldTotalVotesCast = oldStats.totalVotesCast;
const totalProposals = newStats.totalProposals;
const totalProtocols = newStats.totalProtocols;
const totalUniqueVoters = newStats.totalUniqueVoters;
const totalVotesCast = newStats.totalVotesCast;
const totalProposalsDifference = totalProposals - oldTotalProposals;
const totalProtocolsDifference = totalProtocols - oldTotalProtocols;
const totalUniqueVotersDifference = totalUniqueVoters - oldTotalUniqueVoters;
const totalVotesCastDifference = totalVotesCast - oldTotalVotesCast;
let totalProposalsDifferenceText = '';
let totalProtocolsDifferenceText = '';
let totalUniqueVotersDifferenceText = '';
let totalVotesCastDifferenceText = '';
if (totalProposalsDifference > 0) {
totalProposalsDifferenceText = '+' + totalProposalsDifference;
} else if (totalProposalsDifference < 0) {
totalProposalsDifferenceText = '-' + totalProposalsDifference;
}
if (totalProtocolsDifference > 0) {
totalProtocolsDifferenceText = '+' + totalProtocolsDifference;
} else if (totalProtocolsDifference < 0) {
totalProtocolsDifferenceText = '-' + totalProtocolsDifference;
}
if (totalUniqueVotersDifference > 0) {
totalUniqueVotersDifferenceText = '+' + totalUniqueVotersDifference;
} else if (totalUniqueVotersDifference < 0) {
totalUniqueVotersDifferenceText = '-' + totalUniqueVotersDifference;
}
if (totalVotesCastDifference > 0) {
totalVotesCastDifferenceText = '+' + totalVotesCastDifference;
} else if (totalVotesCastDifference < 0) {
totalVotesCastDifferenceText = '-' + totalVotesCastDifference;
}
const message = `
BoardRoom's statistics:
Total Proposals: ${totalProposals.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} ${totalProposalsDifferenceText}
Total Protocols: ${totalProtocols.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} ${totalProtocolsDifferenceText}
Total Unique Voters: ${totalUniqueVoters.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} ${totalUniqueVotersDifferenceText}
Total Votes Cast: ${totalVotesCast.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} ${totalVotesCastDifferenceText}
`.trim();
channel.send(message);
subscription.stats = newStats;
break;
}
}
await subscriptionService.update(subscription);
}
};
export default checkSubscriptions;