This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vlt-modmail.js
102 lines (98 loc) · 3.18 KB
/
vlt-modmail.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
"use strict";
const snoowrap = require("snoowrap");
const request = require("superagent");
function postConversations(context, r, conversations) {
if (conversations.length === 0) {
return Promise.resolve();
}
return Promise.all(conversations.map(conversation => {
return r.oauthRequest({
uri: '/api/mod/conversations/' + conversation.id,
method: 'get'
}).then(response => {
const msgs = Object.keys(response.messages).map(k => response.messages[k]).sort((a, b) => new Date(a.date) - new Date(b.date)).reverse();
return [conversation, msgs[0]];
});
})).then(convMsgs => {
const payload = {
text: "New modmail!"
};
payload.attachments = convMsgs.map(convMsg => {
const conv = convMsg[0];
const msg = convMsg[1];
let color;
if (conv.isHighlighted) {
color = "#ffb000";
} else {
switch(conv.state) {
case 0: // New
color = "#03A9F4";
break;
case 1: // in progress
color = "#388E3C";
break;
case 2: // archived
color = "#949494";
break;
}
}
console.log([conv, msg]);
return {
fallback: `Message from ${msg.author.name.name}: <https://mod.reddit.com/mail/all/${msg.id}>`,
author_name: msg.author.name.name, // what the fuck?
author_link: `https://reddit.com/u/${msg.author.name.name}`,
title: conv.subject,
title_link: `https://mod.reddit.com/mail/all/${conv.id}`,
text: msg.bodyMarkdown,
ts: new Date(msg.date).valueOf() / 1000,
color
};
});
return request
.post(context.secrets.SLACK_URL)
.set('Content-Type', 'application/json')
.send(payload);
});
}
module.exports = function(context, cb) {
context.storage.get((err, data) => {
if (err) {
console.log(`ERROR getting storage: ${err}`);
cb(err);
}
const storage = data || { last: '2017-08-27T22:05:17.330Z' };
const r = new snoowrap({
userAgent: context.secrets.REDDIT_USER_AGENT,
clientId: context.secrets.REDDIT_ID,
clientSecret: context.secrets.REDDIT_SECRET,
username: context.secrets.REDDIT_USERNAME,
password: context.secrets.REDDIT_PASSWORD
});
const body = {
entity: context.secrets.SUBREDDIT,
limit: 25,
};
r.oauthRequest({
uri: '/api/mod/conversations',
method: 'get',
qs: body
}).then(response => {
const last = new Date(storage.last);
const ids = response.conversationIds.filter(x => new Date(response.conversations[x].lastUpdated) > last)
console.log(`Last ID: ${ids[ids.length - 1]}. Num IDs: ${ids.length}`);
const conversations = ids.map(id => response.conversations[id]);
postConversations(context, r, conversations).then(
() => {
context.storage.set({
last: new Date().toJSON()
}, {force: 1}, err => {
if (err) {
cb(err);
}
cb(null, `ok, Last ID ${ids[ids.length - 1]}, num IDs ${ids.length}, last in storage ${storage.last}`);
})
}
).catch(x => cb(x));
}).catch(err => cb(err));
});
};