-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (100 loc) · 2.67 KB
/
app.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
/*jslint node: true, esnext: true */
"use strict";
var Q = require("q");
class App {
startListening() {
this.listener = this.onMessage.bind(this);
this.slapp.on(this.id, this.listener);
}
destroy() {
this.slapp.removeListener(this.id, this.listener);
return this.slackApi("chat.delete", this.message);
}
update() {
if(!(this.text(this.state)=="")) {
return this.slackApi("chat.update", {
channel: this.message.channel,
ts: this.message.ts,
text: this.text(this.state)
});
}
return
}
onMessage(m) {
var userInfo = this.slackApi("users.info", {user: m.user}).then((res) => {
return res.user;
});
var event = {
emoji: m.reaction,
userId: m.user,
type: m.type,
userInfo: userInfo
};
var execute = (fn) => {
Q.when(fn.call(this, event, this.state))
.then((res) => {
if (res !== false) {
this.update().done();
}
});
};
if (this.handlers[m.reaction]) {
execute(this.handlers[m.reaction]);
} else if (this.click) {
execute(this.click);
}
}
sequentialMap(arr, promiseFn) {
return arr.reduce((soFar, a) => {
return soFar.then(() => promiseFn(a));
}, new Q());
}
addButtons() {
return this.sequentialMap(this.buttons(this.state), (emoji) => {
return this.slackApi("reactions.add", {
name: emoji,
channel: this.message.channel,
timestamp: this.message.ts
}, ["already_reacted"]);
});
}
onCreation(messaage) {
this.message = messaage;
this.id = this.slapp.toId(messaage);
this.addButtons().done();
this.startListening();
}
static create(args, state) {
var instance = new this();
instance.state = Object.assign({}, instance.state); // prototype hack
if (state) {
Object.assign(instance.state, state);
}
args = args || {};
instance.slapp.validateField(args, "channel");
args.text = instance.text(instance.state);
args.unfurl_links = args.unfurl_links || false;
args.as_user = true;
if (args.username) {
args.as_user = false;
}
return instance.slackApi("chat.postMessage", args)
.then((res) => {
instance.onCreation(res);
return instance;
});
}
static attach(args, state) {
var instance = new this();
instance.state = Object.assign({}, instance.state); // prototype hack
if (state) {
Object.assign(instance.state, state);
}
args = args || {};
instance.slapp.validateField(args, "id");
instance.onCreation(instance.slapp.fromId(args.id));
instance.update();
return Q.when(instance);
}
}
module.exports = App;