-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathchannel-details.js
More file actions
236 lines (208 loc) · 5.67 KB
/
channel-details.js
File metadata and controls
236 lines (208 loc) · 5.67 KB
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
229
230
231
232
233
234
235
236
const collect = require('collect-stream')
const timestamp = require('monotonic-timestamp')
const { stableSort, merge } = require('./util')
class ChannelDetailsBase {
constructor (channelName) {
this.name = channelName
this.isPrivate = false
this.members = new Set()
this.mentions = []
/* archived channels are not visible in channel listings */
this.archived = false
this.virtualMessages = []
this.newMessageCount = 0
this.datesSeen = new Set()
/* TODO:
use cursor to remember scrollback state and fetch
from leveldb.
maybe store a negative offset to look up? */
this.lastRead = 0 /* timestamp in epoch time */
this.joined = false
this.focused = false
this.topic = ''
}
toString () {
return this.name
}
archive () {
this.archived = true
}
unarchive () {
this.archived = false
}
addMember (key) {
this.members.add(key)
}
removeMember (key) {
this.members.delete(key)
}
getMembers () {
return Array.from(this.members)
}
addMention (mention) {
if (!this.focused) {
this.mentions.push(mention)
}
}
getMentions () {
return this.mentions.slice() // return copy
}
handleMessage (message) {
if (!this.focused) {
// ++var is an optimization:
// var++ creates a temporary variable while ++var doesn't
++this.newMessageCount
}
}
getNewMessageCount () {
return this.newMessageCount
}
markAsRead () {
this.lastRead = Date.now()
this.newMessageCount = 0
this.mentions = []
}
focus () {
this.focused = true
}
unfocus () {
this.focused = false
}
clearVirtualMessages () {
this.virtualMessages = []
}
getVirtualMessages (opts) {
const limit = opts.limit
const newerThan = parseFloat(opts.gt || 0)
const olderThan = parseFloat(opts.lt || Infinity)
var filtered = this.virtualMessages.filter((m) => {
return (parseFloat(m.value.timestamp) > newerThan && parseFloat(m.value.timestamp) < olderThan)
})
return stableSort(filtered, v => parseFloat(v.value.timestamp)).slice(-limit)
}
interleaveVirtualMessages (messages, opts) {
const virtualMessages = this.getVirtualMessages(opts)
var cmp = (a, b) => {
// sort by timestamp
const diff = parseFloat(a.value.timestamp) - parseFloat(b.value.timestamp)
// if timestamp was the same, and messages are by same author, sort by seqno
if (diff === 0 &&
a.key && b.key && a.key === b.key &&
a.hasOwnProperty('seq') && b.hasOwnProperty('seq')) {
return a.seq - b.seq
}
return diff
}
return virtualMessages.concat(messages).sort(cmp).slice(-opts.limit)
}
/*
addVirtualMessage({ timestamp: Date.now(), type: "status", text: "" }})
*/
addVirtualMessage (msg) {
/*
msg will be on the format of
{timestamp, type, text}
but we convert it to the format that cabal expects messages to conform to
msg = {
key: ''
value: {
timestamp: ''
type: ''
content: {
text: ''
}
}
}
*/
if (!msg.value) {
msg = {
key: this.name,
value: {
timestamp: msg.timestamp || timestamp(),
type: msg.type || 'status',
content: {
text: msg.text
}
}
}
}
this.virtualMessages.push(msg)
}
// returns false if we were already in the channel, otherwise true
join () {
var joined = this.joined
this.joined = true
return joined
}
// returns true if we were previously in the channel, otherwise false
leave () {
var joined = this.joined
this.joined = false
return joined
}
}
class ChannelDetails extends ChannelDetailsBase {
constructor (cabal, channelName) {
super(channelName)
this.messages = cabal.messages
}
getPage (opts) {
opts = opts || {}
const OGopts = Object.assign({}, opts)
return new Promise((resolve, reject) => {
const rs = this.messages.read(this.name, opts)
collect(rs, (err, msgs) => {
if (err) {
return reject(err)
}
const reversed = []
for (let i = msgs.length - 1; i >= 0; --i) {
const msg = msgs[i]
reversed.push(msg)
const msgTime = msg.value.timestamp
const dayTimestamp = msgTime - (msgTime % (24 * 60 * 60 * 1000))
if (!this.datesSeen.has(dayTimestamp)) {
this.datesSeen.add(dayTimestamp)
this.addVirtualMessage({
key: this.name,
value: {
timestamp: dayTimestamp,
type: 'status/date-changed'
}
})
}
}
resolve(this.interleaveVirtualMessages(reversed, OGopts))
})
})
}
}
class VirtualChannelDetails extends ChannelDetailsBase {
constructor (channelName) {
super(channelName)
this.joined = true
}
getPage (opts) {
return Promise.resolve(this.interleaveVirtualMessages([], opts))
}
}
class PMChannelDetails extends ChannelDetails {
constructor (details, cabal, pubkey) {
super(cabal, pubkey)
// change cabal api we read from to be private messages (not messages api)
this.messages = cabal.privateMessages
this.recipient = pubkey
this.isPrivate = true
this.topic = "private message with " + pubkey
Object.defineProperty(this, 'joined', {
get: function() {
return details.settings.joinedPrivateMessages.includes(pubkey)
}
})
this.members.add(this.recipientKey) // makes sure # members > 0 :)
}
toString () {
return `PM-${this.recipient.slice(0,8)}`
}
}
module.exports = { ChannelDetails, VirtualChannelDetails, PMChannelDetails }