-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.js
135 lines (120 loc) · 3.46 KB
/
helpers.js
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
/**
* getThread gets the entire thread given a Twit object and an id.
* @param {Twit} T Twit object
* @param {string} id Id to get thread
*/
export const getThread = async (T, id) => {
try {
const thread = [];
await getThreadAux(T, id, thread);
return thread;
} catch (e) {
throw e;
}
};
const getThreadAux = async (T, id, thread) => {
try {
if (id) {
const tweetdata = await getTweetById(T, id);
thread.push(tweetdata);
await getThreadAux(T, tweetdata.repliedid, thread);
}
} catch (e) {
throw e;
}
};
const getTweetById = async (T, id) => {
try {
const {
data: {
user,
id_str,
created_at,
text,
in_reply_to_status_id_str: repliedid
}
} = await T.get("statuses/show", { id });
return {
user: {
id_str: user.id_str,
name: user.name,
screen_name: user.screen_name
},
id_str,
created_at,
text,
repliedid
};
} catch (e) {
throw e;
}
};
export const buildDmPost = (id, message) => {
return {
event: {
type: "message_create",
message_create: {
target: { recipient_id: id },
message_data: { text: message }
}
}
};
};
export const stringify = obj => {
if (obj && typeof obj == "object") {
return JSON.stringify(obj);
}
throw new TypeError("Input should be a object");
};
export const encodeToBase64 = input => Buffer.from(input).toString("base64");
export const normalizeDataToDcrtime = input => {
if (input && typeof input == "string") {
return [input];
}
throw new TypeError("Input should be a string");
};
const templates = [
"Have you asked for me Sir? Anyway, this thread is stored with IPFS and should be timestamped within the next hour.",
"I'm glad you called me :D Thread successfully added to IPFS and the timestamp is on its way.",
"This thread is stored on IPFS and it will be timestamped within the next hour.",
"Hello, your thread is timestamped. \nThanks for calling me!",
"Hey there, here is your timestamped thread so you can always remember it!"
];
const getGreeting = () => Math.floor(Math.random() * 5);
export const replyTemplate = (digest, ipfsHash) => `${
// choose greeting message randomly
templates[getGreeting()]
} \n
Timestamping status: https://timestamp.decred.org/results?digests=${digest}×tamp=false \n \n
Thread json: https://ipfs.io/ipfs/${ipfsHash}
`;
export const cleanTweetText = text => {
if (!text || typeof text !== "string") {
throw new TypeError("Input must be a valid string");
}
const undesiredTags = /^(@[a-zA-z_0-9]+[ ])*/.exec(text)[0];
const cleanedText = undesiredTags ? text.replace(undesiredTags, "") : text;
return cleanedText;
};
export const isRetweet = tweet => {
const isRetweet = !!tweet.retweeted_status;
return isRetweet;
};
export const isReplyToBotTweet = (tweet, tag) => {
const { in_reply_to_screen_name, in_reply_to_status_id_str } = tweet;
const isReplyToBot =
in_reply_to_status_id_str && in_reply_to_screen_name === tag;
return isReplyToBot;
};
export const validateTweet = (tweet, tag) => {
if (!tweet || !tweet.text || typeof tweet.text !== "string") {
throw new TypeError("Invalid tweet input");
}
if (isReplyToBotTweet(tweet, tag) || isRetweet(tweet)) {
return false;
}
const { text } = tweet;
const cleanedText = cleanTweetText(text);
const botIsMentioned = cleanedText.toLowerCase().includes(tag);
return botIsMentioned;
};