Skip to content
This repository was archived by the owner on Apr 12, 2023. It is now read-only.

Commit ea998df

Browse files
committed
Allow toggling reactions
1 parent b1883c2 commit ea998df

File tree

7 files changed

+58
-36
lines changed

7 files changed

+58
-36
lines changed

lib/model/message-list.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ class MessageList {
213213
throw new Error('Should be implemented by subclasses')
214214
}
215215

216+
async setMessageReaction(id, timestamp, name, reacted) {
217+
throw new Error('Should be implemented by subclasses')
218+
}
219+
216220
updateMessageStar(id, timestamp, hasStar) {
217221
const message = this.findMessage(id, timestamp)
218222
if (message && message.hasStar !== hasStar) {
@@ -229,10 +233,13 @@ class MessageList {
229233
if (!message)
230234
return
231235
const existing = message.reactions.find((r) => r.name === reaction.name)
232-
if (existing)
236+
if (existing) {
233237
++existing.count;
234-
else
238+
if (reaction.reacted)
239+
existing.reacted = true
240+
} else {
235241
message.reactions.push(reaction)
242+
}
236243
this.onModifyMessage.dispatch(id, message)
237244
return message
238245
}
@@ -246,10 +253,12 @@ class MessageList {
246253
const i = message.reactions.findIndex((r) => r.name === reaction.name)
247254
if (i === -1)
248255
return
249-
if (--message.reactions[i].count === 0) {
256+
const existing = message.reactions[i]
257+
if (reaction.reacted)
258+
existing.reacted = false
259+
if (--existing.count === 0)
250260
message.reactions.splice(i, 1)
251-
this.onModifyMessage.dispatch(id, message)
252-
}
261+
this.onModifyMessage.dispatch(id, message)
253262
return message
254263
}
255264

lib/service/slack/slack-account.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class SlackAccount extends Account {
7474
this.rtm.on('reconnecting', this.handleConnecting.bind(this))
7575

7676
this.rtm.on('message', this.dispatchMessage.bind(this))
77-
this.rtm.on('reaction_added', this.reactionAdded.bind(this))
78-
this.rtm.on('reaction_removed', this.reactionRemoved.bind(this))
77+
this.rtm.on('reaction_added', this.setReaction.bind(this, true))
78+
this.rtm.on('reaction_removed', this.setReaction.bind(this, false))
7979
this.rtm.on('star_added', this.setStar.bind(this, true))
8080
this.rtm.on('star_removed', this.setStar.bind(this, false))
8181

@@ -276,20 +276,15 @@ class SlackAccount extends Account {
276276
}
277277
}
278278

279-
reactionAdded(event) {
279+
setReaction(add, event) {
280280
const channel = this.findChannelById(event.item.channel)
281281
if (!channel)
282282
return
283-
const reaction = new SlackReaction(this, event.reaction, 1)
284-
channel.reactionAdded(event.item.ts, event.item.ts, reaction)
285-
}
286-
287-
reactionRemoved(event) {
288-
const channel = this.findChannelById(event.item.channel)
289-
if (!channel)
290-
return
291-
const reaction = new SlackReaction(this, event.reaction, -1)
292-
channel.reactionRemoved(event.item.ts, event.item.ts, reaction)
283+
const reaction = new SlackReaction(this, event.reaction, 1, [event.user])
284+
if (add)
285+
channel.reactionAdded(event.item.ts, event.item.ts, reaction)
286+
else
287+
channel.reactionRemoved(event.item.ts, event.item.ts, reaction)
293288
}
294289

295290
setStar(hasStar, event) {

lib/service/slack/slack-channel.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Channel = require('../../model/channel')
22
const SlackMessage = require('./slack-message')
3+
const SlackReaction = require('./slack-reaction')
34
const SlackThread = require('./slack-thread')
45

56
class SlackChannel extends Channel {
@@ -27,20 +28,31 @@ class SlackChannel extends Channel {
2728
return new SlackThread(this, id)
2829
}
2930

30-
async setMessageStar(id, timestamp, hasStar) {
31+
async setMessageStar(id, timestamp, hasStar, channelId = this.id) {
3132
const message = this.findMessage(id, timestamp)
3233
if (message) {
33-
// Slack does not send event after adding star.
34+
// Slack quirk: setting stars does not emit server event.
3435
this.updateMessageStar(id, timestamp, hasStar)
35-
// Send message after we update the message.
36-
const options = {channel: this.id, timestamp: id}
36+
// Send to server after updated locally.
37+
const options = {channel: channelId, timestamp: id}
3738
if (hasStar)
3839
await this.account.rtm.webClient.stars.add(options)
3940
else
4041
await this.account.rtm.webClient.stars.remove(options)
4142
}
4243
}
4344

45+
async setMessageReaction(id, timestamp, name, reacted, channelId = this.id) {
46+
const message = this.findMessage(id, timestamp)
47+
if (message) {
48+
const options = {channel: channelId, timestamp: id, name}
49+
if (reacted)
50+
await this.account.rtm.webClient.reactions.add(options)
51+
else
52+
await this.account.rtm.webClient.reactions.remove(options)
53+
}
54+
}
55+
4456
async readMessagesImpl() {
4557
// Read messages.
4658
const options = {channel: this.id}

lib/service/slack/slack-direct-messsage.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class SlackDirectMessage extends DirectMessage {
3232
return SlackChannel.prototype.setMessageStar.apply(this, arguments)
3333
}
3434

35+
setMessageReaction() {
36+
return SlackChannel.prototype.setMessageReaction.apply(this, arguments)
37+
}
38+
3539
readMessagesImpl() {
3640
return SlackChannel.prototype.readMessagesImpl.apply(this, arguments)
3741
}

lib/service/slack/slack-thread.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@ class SlackThread extends Thread {
66
super(channel, id)
77
}
88

9-
// Like the implementaion in SlackChannel, but use parent channel's id.
109
async setMessageStar(id, timestamp, hasStar) {
11-
const message = this.findMessage(id, timestamp)
12-
if (message) {
13-
// Slack does not send event after adding star.
14-
this.updateMessageStar(id, timestamp, hasStar)
15-
// Send message after we update the message.
16-
const options = {channel: this.channel.id, timestamp: id}
17-
if (hasStar)
18-
await this.account.rtm.webClient.stars.add(options)
19-
else
20-
await this.account.rtm.webClient.stars.remove(options)
21-
}
10+
super.setMessageStar(id, timestamp, hasStar, this.channel.id)
11+
}
12+
13+
async setMessageReaction(id, timestamp, name, reacted) {
14+
super.setMessageReaction(id, timestamp, name, reacted, this.channel.id)
2215
}
2316

2417
async readMessagesImpl() {

lib/view/chat-box.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ handlebars.registerHelper('normalizeId', function(id) {
3636
return 'msg' + id.replace('.', '_')
3737
})
3838

39+
handlebars.registerHelper('revert', function(boolean) {
40+
return boolean ? 'false' : 'true'
41+
})
42+
3943
const fontStyle = fs.readFileSync(path.join(__dirname, 'chat', 'font.css')).toString()
4044
handlebars.registerHelper('fontStyle', function() {
4145
return fontStyle
@@ -79,6 +83,7 @@ class ChatBox {
7983
this.browser.addBinding('openChannel', this.openChannel.bind(this))
8084
this.browser.addBinding('openThread', this.openThread.bind(this))
8185
this.browser.addBinding('setMessageStar', this.setMessageStar.bind(this))
86+
this.browser.addBinding('setMessageReaction', this.setMessageReaction.bind(this))
8287
this.browser.addBinding('notifyDisplaying', this.notifyDisplaying.bind(this))
8388
this.browser.addBinding('notifyNotDisplaying', this.notifyNotDisplaying.bind(this))
8489
this.view.addChildView(this.browser)
@@ -254,6 +259,10 @@ class ChatBox {
254259
this.messageList.setMessageStar(id, timestamp, hasStar)
255260
}
256261

262+
setMessageReaction(id, timestamp, name, reacted) {
263+
this.messageList.setMessageReaction(id, timestamp, name, reacted)
264+
}
265+
257266
notifyDisplaying() {
258267
if (this.messagesLoaded && !this.isDisplaying) {
259268
this.isDisplaying = true

lib/view/chat/message.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="action-menu">
2727
{{~#canStartThread messageList message~}}
2828
<a href="#" onclick="wey.openThread('{{message.id}}'); return false" title="Start thread"><i class="icon-comment"></i></a>
29-
{{~/canStartThread~}}<a class="toggle-star" href="#" onclick="wey.setMessageStar('{{message.id}}', {{message.timestamp}}, {{#if message.hasStar}}false{{else}}true{{/if}}); return false" title="Star message"><i class="{{#if message.hasStar}}icon-star{{else}}icon-star-empty{{/if}}"></i></a>
29+
{{~/canStartThread~}}<a class="toggle-star" href="#" onclick="wey.setMessageStar('{{message.id}}', {{message.timestamp}}, {{revert message.hasStar}}); return false" title="Star message"><i class="{{#if message.hasStar}}icon-star{{else}}icon-star-empty{{/if}}"></i></a>
3030
</div>
3131
{{#unless message.isFolded}}
3232
<div class="sender">
@@ -75,7 +75,7 @@
7575
{{#if message.reactions}}
7676
<div class="reactions">
7777
{{#each message.reactions}}
78-
<button{{#if reacted}} class="reacted"{{/if}}>{{{content}}}<span class="count">{{count}}</span></button>
78+
<button onclick="wey.setMessageReaction('{{../message.id}}', {{../message.timestamp}}, '{{name}}', {{revert reacted}}); return false" {{#if reacted}}class="reacted"{{/if}}>{{{content}}}<span class="count">{{count}}</span></button>
7979
{{/each}}
8080
</div>
8181
{{/if}}

0 commit comments

Comments
 (0)