-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.coffee
executable file
·71 lines (56 loc) · 1.83 KB
/
server.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
Db = require 'db'
Event = require 'event'
Photo = require 'photo'
Plugin = require 'plugin'
Subscription = require 'subscription'
{tr} = require 'i18n'
exports.client_msg = exports.client_chat = (text) !->
post {text}, text
exports.onPhoto = (info) !->
post {photo: info.key}, tr("(photo)")
post = (msg, text) !->
msg.time = 0|(new Date()/1000)
msg.by = Plugin.userId()
id = Db.shared.modify 'maxId', (v) -> (v||0)+1
log "#{id} / #{0|id/100} #{id%100}"
Db.shared.set 0|id/100, id%100, msg
name = Plugin.userName()
Event.create
text: if Plugin.groupId() < 0 then text else "#{name}: #{text}"
senderText: "You: #{text}" if Plugin.groupId() > 0
sender: Plugin.userId()
exports.onJoin = onJoin = (userId, left = false) !->
msg =
time: 0|(new Date()/1000)
by: userId
type: if left then 12 else 11
id = Db.shared.modify 'maxId', (v) -> (v||0)+1
Db.shared.set 0|id/100, id%100, msg
if !left
Event.create
text: "#{Plugin.userName(userId)} joined"
senderText: "You joined"
ticker: "#{Plugin.userName(userId)} joined '#{Plugin.groupName()}'"
sender: userId
exports.onLeave = (userId) !->
onJoin userId, true
exports.client_typingSub = (cb) !->
cb.subscribe 'typing'
exports.client_typing = (typing) !->
patch = {}
patch[Plugin.userId()] = if typing then true else null
Subscription.push 'typing', patch
exports.client_getRead = (id, cb) !->
maxId = Db.shared.get('maxId')
byUserId = Db.shared.get 0|id/100, id%100, 'by'
read = {}
for userId in Plugin.userIds()
continue if userId is byUserId
unread = Event.getUnread(userId)
read[userId] = true if maxId - unread >= id
cb.reply read
exports.client_removePhoto = (num) !->
msg = Db.shared.ref 0|num/100, num%100
return if !msg.get('photo') or (msg.get('by') isnt Plugin.userId() and !Plugin.userIsAdmin())
Photo.remove msg.get('photo')
msg.set 'photo', ''