-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
165 lines (135 loc) · 4.03 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
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
Comments = require 'comments'
Db = require 'db'
Plugin = require 'plugin'
Event = require 'event'
Timer = require 'timer'
{tr} = require 'i18n'
dayNames = [
tr 'Sun'
tr 'Mon'
tr 'Tue'
tr 'Wed'
tr 'Thu'
tr 'Fri'
tr 'Sat'
]
monthNames = [
tr 'Jan'
tr 'Feb'
tr 'Mar'
tr 'Apr'
tr 'May'
tr 'Jun'
tr 'Jul'
tr 'Aug'
tr 'Sep'
tr 'Oct'
tr 'Nov'
tr 'Dec'
]
# convert daycount to 00:00 unix timestamp
dayToUnix = (day) ->
d = new Date(day*864e5)
offset = d.getTimezoneOffset() * 60 # seconds
day * 86400 + offset
dayToString = (day) ->
d = new Date(day*864e5)
dayNames[d.getUTCDay()]+' '+d.getUTCDate()+' '+monthNames[d.getUTCMonth()]+' '+d.getUTCFullYear()
timeToString = (time) ->
if !time?
tr("None")
else if time<0
tr("All day")
else
minutes = (time/60)%60
minutes = '0' + minutes if minutes.toString().length is 1
(0|(time/3600))+':'+minutes
exports.getTitle = -> # will otherwise be confused with adding an event
exports.onInstall = ->
# anything to do?
exports.client_new = (values, onCreated) !->
event =
title: values.title||"(No title)"
details: values.details
date: values.date
time: values.time
remind: values.remind
rsvp: values.rsvp
created: 0|(new Date()/1000)
by: Plugin.userId()
attendance: {}
maxId = Db.shared.incr('events', 'maxId')
Db.shared.set('events', maxId, event)
if values.notify
whenText = (if values.time>=0 then timeToString(values.time)+' ' else '')
whenText += dayToString(values.date)
Event.create
#unit: 'event'
text: "#{Plugin.userName()} added an event: #{values.title} (#{whenText})"
sender: Plugin.userId()
setRemindTimer maxId
onCreated.reply maxId
setRemindTimer = (eventId) !->
event = Db.shared.get 'events', eventId
remind = event?.remind ? 86400
Timer.cancel 'reminder', eventId
log 'event, remind, Plugin.time()', JSON.stringify(event), remind, Plugin.time()
return if !event or remind<0
# -1 means no reminder
if remind >= 0
startTime = dayToUnix(event.date)*1000 - remind*1000
if remind >= 86400 # a day (or more) in advance
startTime += (12*3600*1000)
else if event.time>0
startTime += event.time*1000
remindTimeout = startTime - Plugin.time()*1000
if remindTimeout>0
log 'setting remindTimeout', remindTimeout
Timer.set remindTimeout, 'reminder', eventId
exports.reminder = (eventId) !->
event = Db.shared.get 'events', eventId
return if !event
whenText = (if event.time>=0 then timeToString(event.time)+' ' else '')
whenText += dayToString(event.date)
include = []
if event.rsvp
for userId in Plugin.userIds()
include.push userId unless event.attendance[userId] is 2
log "event reminder #{event.title} (#{whenText})"
Comments.post
legacyStore: eventId
s: 'remind'
v: whenText
path: eventId
pushText: "Event reminder: #{event.title} (#{whenText})"
for: include
# eventObj =
# unit: 'event'
# text: "Event reminder: #{event.title} (#{whenText})"
# Event.create eventObj unless event.rsvp and include.length is 0
# Db.shared.set 'events', eventId, 'reminded', Math.round(Plugin.time())
exports.client_remove = (eventId) !->
return if !Plugin.userIsAdmin() and Plugin.userId() isnt Db.shared.get('events', eventId, 'by')
log "comments post remove"
Comments.post
s: 'remove'
v: Db.shared.get 'events', eventId, 'title'
Db.shared.remove('events', eventId)
Timer.cancel 'reminder', eventId
exports.client_edit = (eventId, values) !->
return if !Plugin.userIsAdmin() and Plugin.userId() isnt Db.shared.get('events', eventId, 'by')
oldEvent = Db.shared.get('events', eventId)
Db.shared.merge('events', eventId, values)
log "comments post edit"
Comments.post
legacyStore: eventId
s: 'edit'
# possibly re-set the reminder
if values.remind isnt oldEvent.remind or values.date isnt oldEvent.date or values.time isnt oldEvent.time
setRemindTimer eventId
exports.client_attendance = (eventId, value) !->
event = Db.shared.ref 'events', eventId
if value is 0
event.remove 'attendance', Plugin.userId()
else if value in [1, 2, 3] # going, notgoing, maybe
event.set 'attendance', Plugin.userId(), value