-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchathelper.coffee
More file actions
185 lines (134 loc) · 4.19 KB
/
chathelper.coffee
File metadata and controls
185 lines (134 loc) · 4.19 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
ZeroWebsocket= (require "./common.coffee").ZeroWebsocket
Parse = (require "./common.coffee").Parse
btoa = require "btoa"
class ChatController extends ZeroWebsocket
constructor:(url,userInfo,needOldFile,collectFunc,lastAction="publish")->
super url
@userInfo = userInfo
@collectFunc=collectFunc
@lastAction=lastAction
@needOldFile = needOldFile
recycle:(maxTime=30*24*60*60*1000)=>
@log "recycle"
minTime = (new Date()).getTime() - maxTime
@data.message.sort((lhs,rhs)->
return Math.sign(lhs.date_published - rhs.date_published)
)
@data.message=@data.message.filter((e)=>
if e.date_added<=minTime
@log "recycle:",e
@changed=true
return e.date_added>minTime
)
receiveUserCallback:(inner_path)=>
@log "user file ",inner_path," changed"
addMessage:(message)->
@log "add:",message
@data.message.push(message)
@changed = true
save:(exit=true)=>
finishIfExit=()=>
if exit
@finish()
if !@changed
@log "not changed"
finishIfExit()
return
# body...
json_raw = unescape(
# Encode to json, encode utf8
encodeURIComponent(JSON.stringify(@data, undefined, '\t')))
# Convert to to base64 and send
#
@log "write to ",@inner_path
@cmd "fileWrite", [@inner_path,btoa(json_raw)], (res)=>
if res != "ok"
@log "write failed"
finishIfExit()
return
@log "write ok"
@changed = false
if @lastAction is "save"
@log "save stage only"
finishIfExit()
return
@cmd "sitePublish", {"inner_path": @inner_path}, (res) =>
@log "Publish result:", res
finishIfExit()
getAndCollect:()=>
if @run
return
@run = true
path = "data/users/#{@site_info.auth_address}/"
@cmd "fileRules",path+"content.json",(rules)=>
@log "file rules getted"
@rules = rules
@inner_path = path+"data.json"
@log "inner_path is :",@inner_path
if !@needOldFile
@data = { "message": [] }
@log "no need old file"
@collectFunc @
return
@log "need old file,getting..."
@cmd "fileGet", {"inner_path": @inner_path, "required": false}, (data) =>
@log "file getted"
if data # Parse if already exits
@data = JSON.parse(data)
else # Not exits yet, use default data
@data = { "message": [] }
@log "call collect func"
@collectFunc @
route: (cmd, message) ->
if cmd is "setSiteInfo"
site_info = message.params
if site_info.event?[0] == "file_done"
@log "file done",site_info.event[1]
if site_info.event?[0] == "file_done" and
site_info.event[1].match /.*users.*data.json$/
@log "file_done",site_info.event[1]
@receiveUserCallback(site_info.event[1])
if message.params.cert_user_id
@log "get cert_user_id:",message.params.cert_user_id
@site_info = message.params
@getAndCollect()
return
if !@userInfo
@log "user info not provided,exit"
@finish()
return
@cmd "certAdd",@userInfo,(res)=>
@log "certAdd result:",res
if res.error
@log "error:",res.error
@finish()
return
@getAndCollect()
else if cmd is "notification"
if message.params[0] is "ask"
@log "ask",message.params[1]
@cmd "certSet",["zeroid.bit"]
else
@log message
else
@log "wtf:",cmd,"__",message
onOpenWebsocket: (e)->
@log "web socket opened"
@cmd "siteInfo",{},(site_info)=>
if site_info.cert_user_id
@site_info=site_info
@log "cert_user_id:",site_info.cert_user_id
@getAndCollect()
return
@log "certSelect"
@cmd "certSelect",[["zeroid.bit"]]
finish:()=>
@log "finish, close websocket"
@ws.close()
onCloseWebsocket: (e, reconnect=10000) =>
@log "closed"
exports.ChatHelper =
class ChatHelper
constructor:(option,userInfo,needOldFile,collectFunc,lastAction)->
new Parse option,(url)->
return new ChatController url,userInfo,needOldFile,collectFunc,lastAction