-
Notifications
You must be signed in to change notification settings - Fork 65
/
helpers.js
134 lines (121 loc) · 2.83 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
const prompt = require("prompt");
const readline = require("readline");
const { log } = require("./logger");
/**
* Adds the number of unread messages in the terminal title
*/
function notifyTerminal(unreadMessagesCount) {
const title = unreadMessagesCount
? `messer (${unreadMessagesCount})`
: "messer";
process.stdout.write(
`${String.fromCharCode(27)}]0;${title}${String.fromCharCode(7)}`,
);
}
/**
* Prompts the user for their username and password in the terminal
*/
function promptCredentials() {
log(
"Enter your Facebook credentials - your password will not be visible as you type it in",
);
prompt.start();
return new Promise((resolve, reject) => {
prompt.get(
[
{
name: "email",
required: true,
},
{
name: "password",
required: true,
hidden: true,
replace: "*",
},
],
(err, result) => {
if (err) return reject(err);
return resolve(result);
},
);
});
}
/**
* Prompts the user for a 2-factor authentication code
*/
function promptCode() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => {
log("Enter code > ");
return rl.on("line", line => {
resolve(line);
rl.close();
});
});
}
/**
* Substitute for Object.values
* @param {object} dict - to extract values from
*/
function objectValues(dict) {
return Object.keys(dict).map(key => dict[key]);
}
/**
* Returns the parsed attachment object as a String
* @param {Object} attachment
* @return {String}
*/
function parseAttachment(attachment) {
let attachmentType = attachment.type.replace(/_/g, " ");
let messageBody = "";
switch (attachmentType) {
case "sticker":
try {
messageBody = `<${attachment.caption}>`;
} catch (e) {
messageBody = "- only viewable in browser";
}
break;
case "file":
messageBody = `${attachment.name}: ${attachment.url}`;
break;
case "photo":
messageBody = `${attachment.url}`;
break;
case "share":
// its likely a game
if (attachment.target) {
attachmentType = "game";
messageBody = attachment.title;
} else {
messageBody = `${attachment.url}`;
}
break;
case "video":
messageBody = `${attachment.url}`;
break;
default:
messageBody = `- only viewable in browser`;
break;
}
return `[${attachmentType}] ${messageBody}`;
}
const sortObjects = (arr, key, asc = true) => {
return arr.sort((a, b) => {
if (a[key] < b[key]) return asc ? -1 : 1;
if (a[key] > b[key]) return asc ? 1 : -1;
return 0;
});
};
module.exports = {
promptCredentials,
promptCode,
objectValues,
notifyTerminal,
parseAttachment,
sortObjects,
};