-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
107 lines (98 loc) · 3.53 KB
/
utils.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
import {IncomingMessage} from "http";
import Chat from "./chat";
import Question from "./question";
export default class Utils {
public static validateURL(link: string) {
if (link.indexOf("http://") == 0) {
return false;
}
return link.indexOf("https://") == 0;
}
public static async isImageValid(url: string) {
let statusCode = await this.getStatusCode(url);
return statusCode === 200;
}
public static async getStatusCode(url: string) {
return new Promise((resolve, reject) => {
const http = require('http');
const https = require('https');
let client = http;
//Switch to HTTPS
if (url.toString().indexOf("https") === 0)
client = https;
//Getting StatusCode
client.get(url, (res: IncomingMessage) => {
resolve(res.statusCode);
}).on("error", (err: any) => {
reject(err);
});
});
}
public static userAutocomplete(users: Chat[], query: string, opts?: {
skipTopics?: boolean,
}) {
const results: any[] = [];
users.forEach((chat) => {
let type = "Unknown";
//0 Chat, 1 Group, 2 Supergroup
if (chat.type != undefined) {
if (chat.type == 0) type = "User";
else if (chat.type == 1) type = "Group";
else if (chat.type == 2) type = "Supergroup";
}
results.push({
name: chat.chatName,
description: type,
id: chat.chatId,
});
});
if (!opts || !opts.skipTopics) {
const topics = this.topicAutocomplete(users, query);
results.push(...topics);
}
return results.filter((result: any) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
public static topicAutocomplete(users: Chat[], query: string) {
const results: any[] = [];
users.forEach((chat) => {
if (chat.topics && chat.topics.length > 0)
chat.topics.forEach((topic) => {
results.push({
name: topic.topicName,
description: `➡️ ${chat.chatName}`,
id: chat.chatId,
topic: topic.topicId
});
})
});
return results.filter((result: any) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
public static questionAutocomplete(questions: Question[], query: string) {
let results: { name: string, id: string }[] = [];
questions.forEach((question) => {
results.push({
name: question.question,
id: question.UUID,
});
});
return results.filter((result: { name: string, id: string }) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
public static answerAutocomplete(question: Question, query: string) {
let results: { name: string, id: number }[] = [];
question.buttons.forEach((answer) => {
results.push({
name: answer,
id: question?.buttons.indexOf(answer)
})
});
return results.filter((result: { name: string, id: number }) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
}