-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloghelper.coffee
More file actions
173 lines (120 loc) · 3.43 KB
/
bloghelper.coffee
File metadata and controls
173 lines (120 loc) · 3.43 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
ZeroWebsocket= (require "./common.coffee").ZeroWebsocket
Parse = (require "./common.coffee").Parse
WaitGroup=require 'waitgroup'
btoa = require "btoa"
class BlogController extends ZeroWebsocket
constructor:(url,collectFunc,lastAction="publish")->
super url
@changed=false
@collectFunc=collectFunc
@lastAction=lastAction
@wg = new WaitGroup
alreadyHave:(title)=>
for b in @data.post
if b.title==title
return true
return false
toRemoveIfNoRef:(post_id)=>
parse_res= (res)=>
if res[0].counter+res[1].counter is 0
@removeList.push(post_id)
queryRef = "SELECT COUNT(*) AS counter
FROM comment WHERE post_id=#{post_id}
UNION ALL
SELECT COUNT(*) AS counter
FROM post_vote WHERE post_id=#{post_id}"
@wg.add()
@cmd "dbQuery", [queryRef],(res)=>
if res.error
@log res.error
else
parse_res(res)
@wg.done()
return
recycle:(callback,maxTime=10*24*60*60)=>
@log "recycle"
minTime = (new Date()).getTime()/1000 - maxTime
@data.post.sort((lhs,rhs)->
return Math.sign(lhs.date_published - rhs.date_published)
)
@removeList = []
for p in @data.post
if p.date_published < minTime && p.body!=""
@toRemoveIfNoRef(p.post_id)
@wg.wait(()=>
@data.tag=@data.tag.filter((t)=>
idx=@removeList.indexOf(t.post_id)
if idx==-1
return true
@log "recycle tag \"#{t.value}\" of post_id:#{t.post_id}"
return false
)
@data.post.filter((p)=>
idx=@removeList.indexOf(p.post_id)
if idx==-1
return false
p.body=""
@log "recycle ",p.title
@removeList.splice idx,1
return false
)
callback()
)
addPost:(post)->
post.post_id=@data.next_post_id
if !@data.tag
@data.tag=[]
for t in post.tag
@data.tag.push({
value:t,
post_id:post.post_id
})
delete post.tag
@log "add:",post.title
@data.post.unshift(post)
@data.next_post_id++
@changed = true
save:()=>
@data.modified = Math.max((new Date).getTime()/1000,@data.modified+1)
json_raw = unescape(
# Encode to json, encode utf8
encodeURIComponent(JSON.stringify(@data, undefined, '\t')))
# Convert to to base64 and send
#
@cmd "fileWrite", ["data/data.json",btoa(json_raw)], (res)=>
if res != "ok"
@log "write failed",res
@finish()
return
@log "write ok"
if @lastAction is "save"
@log "save stage only"
@finish()
return
@cmd "siteSign", ["stored", "content.json"], (res) =>
@log "Sign result", res
if @lastAction is "sign"
@log "sign stage only"
@finish()
return
@cmd "sitePublish", ["stored"], (res) =>
@log "Publish result:", res
@finish()
onOpenWebsocket: (e)->
@log "web socket opened"
self=@
@log "call fileGet"
@cmd "fileGet", ["data/data.json"], (res)=>
@log "file getted"
@data=JSON.parse(res)
@collectFunc @
finish:()=>
@log "finish, close websocket"
@ws.close()
onCloseWebsocket: (e, reconnect=10000) =>
@log "closed"
exports.BlogHelper =
class BlogHelper
constructor:(option,collectFunc,lastAction)->
new Parse option,(url)->
return new BlogController url,collectFunc,lastAction