-
Notifications
You must be signed in to change notification settings - Fork 30
/
mastodon.js
146 lines (126 loc) · 3.44 KB
/
mastodon.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
136
137
138
139
140
141
142
143
144
145
146
"use strict";
const https = require("https");
const { decrypt, encrypt } = require("./encryption.js");
const DB = require("better-sqlite3-helper");
const toToken = async (domain, app, user_code) => {
return new Promise((resolve) => {
let postData = JSON.stringify({
client_id: app.client_id,
client_secret: app.client_secret,
redirect_uri: "https://" + domain + "/success",
grant_type: "authorization_code",
code: user_code,
});
let options = {
hostname: app.domain,
port: 443,
path: "/oauth/token",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": postData.length,
},
};
const req = https.request(options, (res) => {
let body = "";
res.on("data", (d) => {
body += d;
});
res.on("error", (e) => {
console.error(e);
});
res.on("end", () => {
try {
let data = JSON.parse(body);
resolve(data);
} catch (err) {
console.log(err);
}
return;
});
});
req.write(postData);
req.end();
});
};
const createApp = (domain, remote_domain = false) => {
let fedifinder_domain = process.env.PROJECT_DOMAIN.includes("http")
? process.env.PROJECT_DOMAIN
: `https://${process.env.PROJECT_DOMAIN}.glitch.me`;
remote_domain ? (fedifinder_domain += " " + remote_domain) : void 0;
let postData = JSON.stringify({
client_name: "Fedifinder",
redirect_uris:
fedifinder_domain +
"/success" +
" " +
"https://fedifinder.glitch.me/success" +
" " +
"https://fedifinder-backup.glitch.me/success", //todo remove hardcoded URLs, different apps for each?
scopes: "read:accounts read:follows",
website: fedifinder_domain,
});
let options = {
hostname: domain,
port: 443,
path: "/api/v1/apps",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": postData.length,
},
};
console.log("creating app");
return new Promise((resolve) => {
const req = https.request(options, (res) => {
let body = "";
res.on("data", (d) => {
body += d;
console.log(body);
});
res.on("error", (e) => {
console.error(e);
});
res.on("end", () => {
try {
let data = JSON.parse(body);
data["client_secret"] = encrypt(data["client_secret"]);
DB().insert("mastodonapps", {
domain: domain,
id: data["id"],
client_id: data["client_id"],
client_secret: data["client_secret"],
vapid_key: data["vapid_key"],
});
resolve(data);
} catch (err) {
DB().insert("mastodonapps", {
domain: domain,
working: 0,
});
resolve({
domain: domain,
working: 0,
});
}
});
});
req.write(postData);
req.end();
});
};
const getApp = async (domain, decrypted = false) => {
let app = DB().queryFirstRow(
"SELECT * FROM mastodonapps WHERE domain=?",
domain
);
if (!app) app = await createApp(domain);
if (decrypted) {
app["client_secret"] = decrypt(app["client_secret"]);
}
return app;
};
const getFollowings = (domain, user) => {
let url = domain + "/api/v1/accounts/" + user + "/following?limit=80";
};
module.exports = { getApp, toToken, getFollowings };