-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport.ts
283 lines (264 loc) · 20.8 KB
/
report.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import * as TelegramBot from "node-telegram-bot-api";
import {getFederationChannel, getRule,getAllowance, setAllowance, addReport, getRecordCount, getQuestionId, getForgiveness} from "../../db";
import { groupSettings } from "../../../types";
import {upload} from "./addEvidence"
import {reportUser} from "../../bot-core";
import langJson from "../assets/langNew.json";
/*
* /report
*/
const regexp = /^\/report\s?(.+)?/
// cacheIndex => groupID,reported message id => [pending report message id]
const NodeCache = require( "node-cache" );
const myCache = new NodeCache( { stdTTL: 900, checkperiod: 1200 } );
const myCacheGarbageCollection = new NodeCache( { stdTTL: 90, checkperiod: 120 } );
const myCacheGarbageCollectionSlow = new NodeCache( { stdTTL: 900, checkperiod: 1200 } );
var myBot;
var myQueue;
myCacheGarbageCollection.on("expired",function(key,value){
myQueue.add(async () => {try{await myBot.deleteMessage(value, key)}catch{}});
});
myCacheGarbageCollectionSlow.on("expired",function(key,value){
myQueue.add(async () => {try{await myBot.deleteMessage(value, key)}catch{}});
});
const callback = async (queue: any, db:any, settings: groupSettings, bot: TelegramBot, botId: number, msg: TelegramBot.Message, match: string[]) => {
try{
if(msg.text.substring(0,11) === '/info')
return
if (!myBot)
myBot = bot
if (!myQueue)
myQueue = queue
if (!msg.reply_to_message) {
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, langJson[settings.lang].error.reply, msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})
return val}catch{}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
}
const channelUserSusie = await queue.add(async () => {try{const val = await bot.getChatMember(msg.chat.id, botId)
return val}catch(e){console.log(e)}});
if(!channelUserSusie)
return
const isAdmin = channelUserSusie.status === "administrator"
if (isAdmin && msg.reply_to_message.date < Date.now()/1000-86400*7){
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, langJson[settings.lang].report.expired, msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})
return val}catch{}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
}
const forgiveness = getForgiveness(db,'telegram', String(msg.chat.id),String(msg.reply_to_message.from.id))
const msg_date = new Date(forgiveness*1000).toUTCString()
if (forgiveness > msg.reply_to_message.date){
queue.add(async () => {try{await bot.sendMessage(msg.chat.id, langJson[settings.lang].forgiven+msg_date, msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})}catch{}});
return
}
// WHO WATCHES THE WATCHMEN??
// can't ban bots
if (msg.reply_to_message.from.is_bot && msg.reply_to_message.from?.id == Number(process.env.BOT_USERID)){
let resp;
if(msg.reply_to_message.from.username === "GroupAnonymousBot")
resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, langJson[settings.lang].report.anon, msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})
return val}catch{}});
else
resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].report.bot}`, msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})
return val}catch{}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return
}
let report: TelegramBot.ChatMember
if(!settings.admin_reportable){
report = await queue.add(async () => {try{const val = await bot.getChatMember(msg.chat.id,msg.reply_to_message.from.id)
return val}catch{}})
if (!report)
return
if(report.status === "administrator" || report.status === "creator"){
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].report.admin}`, msg.chat.is_forum? {message_thread_id: msg.message_thread_id, parse_mode: "Markdown"}: {parse_mode: "Markdown"})
return val}catch(e){console.log(e)}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return
}
}
const fromUsername = (msg.reply_to_message.from.username || msg.reply_to_message.from.first_name || `no-username-set`);
const reportedUserID = String(msg.reply_to_message.from.id);
const currentTimeMs = Date.now()/1000;
/*
const cachedReportRequestMessage = myCache.get([msg.chat.id, msg.reply_to_message.message_id].toString())
if (cachedReportRequestMessage){ // message already reported
const msgLinkReport = 'https://t.me/c/' + String(msg.chat.id).substring(4) + '/' + cachedReportRequestMessage;
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].report.reported}(${msgLinkReport})`, msg.chat.is_forum? {message_thread_id: msg.message_thread_id, parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})
return val}catch{}});
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
}*/
const msgId = (msg.chat.is_forum? (msg.is_topic_message? String(msg.message_thread_id)+'/': '1/') : '')+String(msg.reply_to_message.message_id)
const reportedQuestionId = getQuestionId(db, 'telegram', String(msg.chat.id), reportedUserID, msgId);
if (reportedQuestionId){
console.log(msg)
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].report.reported}(https://reality.eth.limo/app/#!/network/${process.env.CHAIN_ID}/question/${process.env.REALITY_ETH_V30}-${reportedQuestionId})`, (msg.is_topic_message)? {message_thread_id: msg.message_thread_id, parse_mode: 'Markdown', disable_web_page_preview: true}: {parse_mode: 'Markdown', disable_web_page_preview: true})
return val}catch{}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
}
const rules = getRule(db, 'telegram', String(msg.chat.id), msg.reply_to_message.date)?.rules;
if (!rules){
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, langJson[settings.lang].report.norules, msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})
return val}catch{}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
}
const chatobj = await queue.add(async () => {try{const val = await bot.getChat(msg.chat.id)
return val}catch{}});
if (!chatobj)
return
const isPrivate = !chatobj.active_usernames;
const evidencepath = await upload(queue, bot, settings.lang, msg, isPrivate);
const msgBackup = 'https://cdn.kleros.link'+evidencepath;
// TODO report
const reportAllowance = getAllowance(db, 'telegram', String(msg.chat.id), String(msg.from.id));
console.log(reportAllowance)
const reporter = await queue.add(async () => {try{const val = await bot.getChatMember(msg.chat.id,msg.from.id)
return val}catch(e){console.log(e)}})
if((reporter.status === "administrator" || reporter.status === "creator")){
console.log(reporter.status)
} else if (!reportAllowance){
setAllowance(db, 'telegram', String(msg.chat.id), String(msg.from.id), 9, 30, currentTimeMs);
} else if (currentTimeMs < reportAllowance.timestamp_refresh + 28800 && reportAllowance.report_allowance < 1 ){
console.log(msg)
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, langJson[settings.lang].report.noallowance, (msg.chat.is_forum && msg.is_topic_message)? {message_thread_id: msg.message_thread_id}: {})
return val}catch (e){console.log(e)}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
} else{
const newReportAllowance = reportAllowance.report_allowance + Math.floor((currentTimeMs - reportAllowance.timestamp_refresh)/28800)*3 - 1;
const newEvidenceAllowance = reportAllowance.evidence_allowance + Math.floor((currentTimeMs - reportAllowance.timestamp_refresh)/28800)*10;
const newRefreshTimestamp = reportAllowance.timestamp_refresh + Math.floor((currentTimeMs - reportAllowance.timestamp_refresh)/28800)*28800;
setAllowance(db, 'telegram', String(msg.chat.id), String(msg.from.id), Math.min(newReportAllowance,9), Math.min(newEvidenceAllowance,30), newRefreshTimestamp);
}
console.log(msg)
console.log(msgId)
const opts = {
parse_mode: "Markdown" as TelegramBot.ParseMode,
reply_to_message_id: msg.reply_to_message.message_id,
...(msg.is_topic_message && {message_thread_id: msg.message_thread_id}),
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{
text: langJson[settings.lang].socialConsensus.confirm + ' (1/3)',
callback_data: '2|'+String(msg.reply_to_message.from.id)+'|'+msgId+'|'+String(msg.reply_to_message.date)+'|'+String(msg.from.id)
}
]
]
}
};
const msgLink = 'https://t.me/c/' + String(msg.chat.id).substring(4) + '/'+`${msg.chat.is_forum? msg.is_topic_message? msg.message_thread_id: 1 : ''}/`+msg.reply_to_message.message_id;
const reportRequestMsg: TelegramBot.Message = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].socialConsensus.consensus2} [${fromUsername}](tg://user?id=${reportedUserID}) ${langJson[settings.lang].socialConsensus.consensus3}(${rules}) ${langJson[settings.lang].socialConsensus.consensus4}(${msgLink}) ([${langJson[settings.lang].socialConsensus.consensus5}](${msgBackup}))?`, opts)
return val}catch (e){}});
if (!reportRequestMsg)
return
myCache.set([msg.chat.id, msg.reply_to_message.message_id].toString(),`${msg.chat.is_forum? `${msg.message_thread_id}/${reportRequestMsg.message_id}`:''}${reportRequestMsg.message_id}`) ;
//myCacheGarbageCollectionSlow.set(reportRequestMsg.message_id, msg.chat.id)
return;
} catch(e){
console.log(e)
}
}
const reportMsg = async (queue: any, settings: groupSettings, db: any, bot: any, msg: any, fromUsername: string, reportedUserID: string, rules: string, msgId: string, msgBackup: string, reportedBy: string, msgDate: string, batchedSend: any) => {
try {
const reportedQuestionId = getQuestionId(db, 'telegram', String(msg.chat.id), reportedUserID, msgId);
if (reportedQuestionId){
const resp = await queue.add(async () => {try{const val = await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].report.reported}(https://reality.eth.limo/app/#!/network/${process.env.CHAIN_ID}/question/${process.env.REALITY_ETH_V30}-${reportedQuestionId})`, (msg.chat.is_forum && msg.is_topic_message)? {message_thread_id: msg.message_thread_id, parse_mode: 'Markdown', disable_web_page_preview: true}: {parse_mode: 'Markdown', disable_web_page_preview: true})
return val}catch{}});
if (!resp)
return resp
myCacheGarbageCollection.set(resp.message_id, msg.chat.id)
return;
}
const chatobj = await queue.add(async () => {try{const val = await bot.getChat(msg.chat.id)
return val}catch{}});
if (!chatobj)
return
const isPrivate: boolean = !chatobj.active_usernames;
var inviteURL = isPrivate ? '': myCache.get(msg.chat.id)
if (!isPrivate && !inviteURL){
inviteURL = 'https://t.me/'+chatobj.active_usernames[0]
// TODO Handle private, but invites allowed
/*
inviteURL = getInviteURL(db, 'telegram', String(msg.chat.id));
if (!inviteURL){
inviteURL = await queue.add(async () => {try{const val = await bot.exportChatInviteLink(msg.chat.id)
return val}catch{}});
if (!inviteURL)
return
setInviteURL(db, 'telegram', String(msg.chat.id), inviteURL);
}
*/
myCache.set(msg.chat.id, inviteURL)
}
const msgLink = 'https://t.me/c/' + String(msg.chat.id).substring(4) + `/${msgId}`;
const cachedEvidenceIndex = myCache.get("evidence"+msg.chat.id);
const evidenceIndex = cachedEvidenceIndex? cachedEvidenceIndex+1:getRecordCount(db, 'telegram', String(msg.chat.id))+1
myCache.set("evidence"+msg.chat.id, evidenceIndex);
const {questionId, questionUrl: appealUrl} = reportUser(
batchedSend,
settings.lang,
false,
fromUsername,
reportedUserID,
'Telegram',
msg.chat.title,
inviteURL,
String(msg.chat.id),
rules,
msgLink,
msgBackup,
reportedBy,
isPrivate);
addReport(db, questionId, 'telegram', String(msg.chat.id), reportedUserID, fromUsername , msgId, Number(msgDate),evidenceIndex, msgBackup);
const fedNotificationChannel = settings.federation_id ? getFederationChannel(db, 'telegram',settings.federation_id) : ""
if (settings.lang === "en"){
if (fedNotificationChannel){
queue.add(async () => {try{bot.sendMessage(fedNotificationChannel, `[${fromUsername}](tg://user?id=${reportedUserID})'s conduct due to this [message](${msgLink}) ([backup](${msgBackup})) is reported for breaking the [rules](${rules}).\n\nDid *${fromUsername}* break the rules? The [question](${appealUrl}) can be answered with a minimum bond of 1 DAI. Need assistance answering the question? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) me for help : )\n\nTo save a record, reply to messages you want saved with \`/evidence ${evidenceIndex}\``, {parse_mode: 'Markdown'})}catch{}});
/*if (inviteurl)
queue.add(async () => {try{bot.sendMessage(settings.channelID, `[${fromUsername}](tg://user?id=${reportedUserID})'s conduct due to this [message](${msgLink}) ([backup](${msgBackup})) is reported for breaking the [rules](${rules}).\n\nDid *${fromUsername}* break the rules? The [question](${appealUrl}) can be answered with a minimum bond of 1 DAI. Need assistance answering the question? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) me for help : )\n\nTo save a record, reply to messages you want saved with \`/evidence ${evidenceIndex}\`\n\nJoin the [notification channel](${inviteurl}) to follow updates for this moderation question.`, msg.chat.is_forum? {message_thread_id: settings.thread_id_notifications , parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})}catch{}});
else
queue.add(async () => {try{bot.sendMessage(settings.channelID, `[${fromUsername}](tg://user?id=${reportedUserID})'s conduct due to this [message](${msgLink}) ([backup](${msgBackup})) is reported for breaking the [rules](${rules}).\n\nDid *${fromUsername}* break the rules? The [question](${appealUrl}) can be answered with a minimum bond of 1 DAI. Need assistance answering the question? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) me for help : )\n\nTo save a record, reply to messages you want saved with \`/evidence ${evidenceIndex}\``, msg.chat.is_forum? {message_thread_id: settings.thread_id_notifications , parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})}catch{}});*/
} else
queue.add(async () => {try{bot.sendMessage(settings.channelID, `[${fromUsername}](tg://user?id=${reportedUserID})'s conduct due to this [message](${msgLink}) ([backup](${msgBackup})) is reported for breaking the [rules](${rules}).\n\nDid *${fromUsername}* break the rules? The [question](${appealUrl}) can be answered with a minimum bond of 1 DAI. Need assistance answering the question? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) me for help : )\n\nTo save a record, reply to messages you want saved with \`/evidence ${evidenceIndex}\``, msg.chat.is_forum? {message_thread_id: settings.thread_id_notifications , parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})}catch{}});
}
else if (settings.lang === "es"){
if (fedNotificationChannel){
queue.add(async () => {try{bot.sendMessage(settings.channelID, `La conducta de [${fromUsername}](tg://user?id=${reportedUserID}) a este [mensaje](${msgLink}) ([backup](${msgBackup})) es denunciada por infringir las [reglas](${rules}).\n\nHa infringido el usuario *${fromUsername}* las reglas? La [pregunta](${appealUrl}) puede responderse con un bono mínimo de 1 DAI. Necesitas ayuda para responder a la pregunta? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) para obetener ayuda : )\n\nPara guardar un mensaje, responda a los mensajes que desee guardar con \`/evidence ${evidenceIndex}\``, {parse_mode: 'Markdown'})}catch{}}); /*if (inviteurl)
queue.add(async () => {try{bot.sendMessage(settings.channelID, `[${fromUsername}](tg://user?id=${reportedUserID})'s conduct due to this [message](${msgLink}) ([backup](${msgBackup})) is reported for breaking the [rules](${rules}).\n\nDid *${fromUsername}* break the rules? The [question](${appealUrl}) can be answered with a minimum bond of 1 DAI. Need assistance answering the question? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) me for help : )\n\nTo save a record, reply to messages you want saved with \`/evidence ${evidenceIndex}\`\n\nJoin the [notification channel](${inviteurl}) to follow updates for this moderation question.`, msg.chat.is_forum? {message_thread_id: settings.thread_id_notifications , parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})}catch{}});
else
queue.add(async () => {try{bot.sendMessage(settings.channelID, `[${fromUsername}](tg://user?id=${reportedUserID})'s conduct due to this [message](${msgLink}) ([backup](${msgBackup})) is reported for breaking the [rules](${rules}).\n\nDid *${fromUsername}* break the rules? The [question](${appealUrl}) can be answered with a minimum bond of 1 DAI. Need assistance answering the question? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) me for help : )\n\nTo save a record, reply to messages you want saved with \`/evidence ${evidenceIndex}\``, msg.chat.is_forum? {message_thread_id: settings.thread_id_notifications , parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})}catch{}});*/
} else
queue.add(async () => {try{bot.sendMessage(settings.channelID, `La conducta de [${fromUsername}](tg://user?id=${reportedUserID}) a este [mensaje](${msgLink}) ([backup](${msgBackup})) es denunciada por infringir las [reglas](${rules}).\n\nHa infringido el usuario *${fromUsername}* las reglas? La [pregunta](${appealUrl}) puede responderse con un bono mínimo de 1 DAI. Necesitas ayuda para responder a la pregunta? [DM](https://t.me/${process.env.BOT_USERNAME}?start=helpgnosis) para obetener ayuda : )\n\nPara guardar un mensaje, responda a los mensajes que desee guardar con \`/evidence ${evidenceIndex}\``, msg.chat.is_forum? {message_thread_id: settings.thread_id_notifications , parse_mode: 'Markdown'}: {parse_mode: 'Markdown'})}catch{}});
}
return [appealUrl, evidenceIndex];
} catch (e) {
console.log(e);
try{
queue.add(async () => {try{await bot.sendMessage(msg.chat.id, `${langJson[settings.lang].error.txn}. ${e.reason}. `,msg.chat.is_forum? {message_thread_id: msg.message_thread_id}: {})}catch{}});
} catch(e){
console.log(e)
}
return;
}
}
export {regexp, callback, reportMsg};