forked from slackapi/node-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.coffee
228 lines (161 loc) · 5.23 KB
/
channel.coffee
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Message = require './message'
class Channel
constructor: (@_client, data = {}) ->
@_typing = {}
@_history = {}
for k of (data or {})
@[k] = data[k]
# TODO: Emit event for unread history
getType: ->
return @constructor.name
addMessage: (message) ->
switch message.subtype
when undefined, "channel_archive", "channel_unarchive", "group_archive", "group_unarchive"
@_history[message.ts] = message
when "message_changed"
@_history[message.message.ts] = message.message
when "message_deleted"
delete @_history[message.deleted_ts]
when "channel_topic", "group_topic"
@topic.value = message.topic
@topic.creator = message.user
@topic.last_set = message.ts
@_history[message.ts] = message
when "channel_purpose", "group_purpose"
@purpose.value = message.purpose
@purpose.creator = message.user
@purpose.last_set = message.ts
@_history[message.ts] = message
when "channel_name", "group_name"
@name = message.name
@_history[message.ts] = message
when "bot_message"
# TODO: Make a new message type before storing
@_history[message.ts] = message
when "channel_join", "group_join"
@members.push message.user
@_history[message.ts] = message
when "channel_leave", "group_leave"
index = @members.indexOf message.user
if index isnt -1
@members.splice index
@_history[message.ts] = message
else
@_client.logger.debug "Unknown message subtype: %s", message.subtype
@_history[message.ts] = message
if message.ts and not message.hidden and @latest? and @latest.ts? and message.ts > @latest.ts
@unread_count++
@latest = message
if @_client.autoMark then @mark message.ts
getHistory: ->
@_history
startedTyping: (user_id) ->
# Start timer to clear this
if @_typing[user_id] then clearTimeout @_typing[user_id]
@_typing[user_id] = setTimeout @_typingTimeout, 5000, user_id
_typingTimeout: (user_id) =>
delete @_typing[user_id]
getTyping: ->
for k of @_typing
k
send: (text) ->
m = new Message @_client, {text: text}
@sendMessage m
postMessage: (data) ->
params = data
params.channel = @id
if data.attachments
params.attachments = JSON.stringify(data.attachments)
@_client.logger.debug data
@_client.logger.debug params
@_client._apiCall "chat.postMessage", params, @_onPostMessage
_onPostMessage: (data) =>
@_client.logger.debug data
sendMessage: (message) ->
message.channel = @id
@_client._send(message)
fetchHistory: (latest, oldest) ->
params = {
"channel": @id
}
if latest? then params.latest = latest
if oldest? then params.oldest = oldest
method = 'channels.history'
if @getType() == 'Group' then method = 'groups.history'
if @getType() == 'DM' then method = 'im.history'
@_client._apiCall method, params, @_onFetchHistory
_onFetchHistory: (data) =>
@_client.logger.debug data
mark: (ts) ->
params = {
"channel": @id,
"ts": ts
}
method = 'channels.mark'
if @getType() == 'Group' then method = 'groups.mark'
if @getType() == 'DM' then method = 'im.mark'
@_client._apiCall method, params, @_onMark
_onMark: (data) =>
@_client.logger.debug data
# TODO: Update @unread_count based on ts
leave: ->
if @getType() == 'DM' then return null
params = {
"channel": @id
}
method = 'channels.leave'
if @getType() == 'Group' then method = 'groups.leave'
@_client._apiCall method, params, @_onLeave
_onLeave: (data) =>
@_client.logger.debug data
setTopic: (topic) ->
if @getType() == 'DM' then return null
params = {
"channel": @id,
"topic": topic
}
method = 'channels.setTopic'
if @getType() == 'Group' then method = 'groups.setTopic'
@_client._apiCall method, params, @_onSetTopic
_onSetTopic: (data) =>
@_client.logger.debug data
setPurpose: (purpose) ->
if @getType() == 'DM' then return null
params = {
"channel": @id,
"purpose": purpose
}
method = 'channels.setPurpose'
if @getType() == 'Group' then method = 'groups.setPurpose'
@_client._apiCall method, params, @_onSetPurpose
_onSetPurpose: (data) =>
@_client.logger.debug data
rename: (name) ->
if @getType() == 'DM' then return null
params = {
"channel": @id,
"name": name
}
method = 'channels.rename'
if @getType() == 'Group' then method = 'groups.rename'
@_client._apiCall method, params, @_onRename
_onRename: (data) =>
@_client.logger.debug data
invite: (user_id) ->
if @getType() == 'DM' then return null
params = {
"channel": @id,
"user": user_id
}
method = 'channels.invite'
if @getType() == 'Group' then method = 'groups.invite'
@_client._apiCall method, params, @_onInvite
_onInvite: (data) =>
@_client.logger.debug data
_recalcUnreads: ->
unreads = 0
# Iterate through history and count messages greater than last_read
for ts of @history
if ts > @last_read then unreads++
@unread_count = unreads
module.exports = Channel