-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
executable file
·104 lines (83 loc) · 2.19 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
Db = require 'db'
Event = require 'event'
Plugin = require 'plugin'
exports.getTitle = ->
if Db.shared.get('single')
Db.shared.get 'stories', 1, 'title'
exports.onInstall = (config) !->
if config?.title
newStory
title: config.title
text: config.text||''
, Plugin.ownerId()
Db.shared.set 'single', true
exports.onConfig = (config) !->
Db.shared.merge 'stories', 1,
title: config?.title
text: config?.text
exports.client_new = newStory = (d, userId = Plugin.userId()) !->
id = Db.shared.incr 'storyId'
Db.shared.set 'stories', id,
text: d.text
title: d.title
time: 0|(new Date()/1000)
comments: 0
user: userId
Event.create
unit: "msg"
text: "#{Plugin.userName(userId)}: #{if d.text then d.text else d.title}"
read: [userId]
checkPath = (path) !->
for x in path
if ''+(0|x) != ''+x
throw new Error("invalid path term: "+x)
makePath = (path) ->
res = []
for x,n in path
res.push 'c' if n
res.push x
res
exports.client_up = (path) !->
checkPath path
up = Db.personal().createRef 'up'
args = path.concat(['voted', (v) -> !v])
value = up.modify.apply up, args
args = makePath path
args.push 'votes', if value then 1 else -1
stories = Db.shared.ref 'stories'
stories.incr.apply stories, args
exports.client_delete = (path, undelete = false) !->
checkPath path
stories = Db.shared.ref 'stories'
args = makePath path
comment = stories.get.apply stories, args
return if !Plugin.userIsAdmin() and +comment.user isnt Plugin.userId()
args = args.concat(['deleted', (if undelete then null else true)])
stories.set.apply stories, args
exports.client_reply = (path, text) !->
checkPath path
stories = Db.shared.ref 'stories'
commentId = stories.incr(path[0], 'comments')
userId = Plugin.userId()
parentIds = []
parentsSeen = {}
parentsSeen[userId] = true
args = []
for x,n in path
args.push 'c' if n
args.push x
args.push 'user'
pId = stories.get.apply(stories,args)
args.pop()
unless parentsSeen[pId]
parentIds.push pId
parentsSeen[pId] = true
args.push 'c', commentId,
text: text
time: 0|(new Date()/1000)
user: userId
stories.set.apply stories, args
Event.create
path: path
text: "#{Plugin.userName()}: #{text}"
sender: userId