This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
storage.coffee
258 lines (195 loc) · 10.3 KB
/
storage.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
###
Presentz.org - A website to publish presentations with video and slides synchronized.
Copyright (C) 2012 Federico Fissore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
###
"use strict"
utils = require "./utils"
_ = require "underscore"
db = null
init = (database) ->
db = database
@
save = (vertex, callback) ->
db.save vertex, callback
create = (vertex, callback) ->
db.createVertex vertex, callback
link_slide_to_chapter = (slide, chapter, callback) ->
db.createEdge slide, chapter, { label: "slide_of" }, callback
link_chapter_to_presentation = (chapter, presentation, callback) ->
db.createEdge chapter, presentation, { label: "chapter_of" }, callback
link_user_to_presentation = (user, presentation, callback) ->
if presentation.is_new
db.createEdge user, presentation, { label: "authored" }, callback
else
callback()
cascading_save = (vertex, callback) ->
db.cascadingSave vertex, callback
count_presentations_in_catalog = (catalog, callback) ->
db.getInEdges catalog, "part_of", (err, edges) ->
return callback(err) if err?
catalog.presentations_length = edges.length
callback(undefined, catalog)
list_catalogs_with_presentation_count = (callback) ->
db.command "SELECT FROM V WHERE _type = 'catalog' and (hidden is null or hidden = 'false') ORDER BY name", (err, catalogs) ->
return callback(err) if err?
utils.exec_for_each count_presentations_in_catalog, catalogs, (err) ->
return callback(err) if err?
callback(undefined, catalogs)
catalog_name_to_node = (name, callback) ->
db.command "SELECT FROM V WHERE _type = 'catalog' and id = '#{name}'", (err, catalogs) ->
return callback(err) if err?
return callback("no record found") if catalogs.length is 0
callback(undefined, catalogs[0])
from_catalog_to_presentations = (catalog, callback) ->
db.fromVertex(catalog).inVertexes "part_of", (err, presentations) ->
return callback(err) if err?
utils.exec_for_each load_chapters_of, presentations, (err) ->
return callback(err) if err?
callback(undefined, presentations)
from_user_to_presentations = (user, callback) ->
db.fromVertex(user).outVertexes "authored", (err, presentations) ->
return callback(err) if err?
utils.exec_for_each load_chapters_of, presentations, (err) ->
return callback(err) if err?
callback(undefined, presentations)
create_comment = (comment, node_to_link_to, user, callback) ->
db.createVertex comment, (err, comment) ->
return callback(err) if err?
db.createEdge comment, node_to_link_to, { label: "comment_of" }, (err) ->
return callback(err) if err?
db.createEdge user, comment, { label: "authored_comment" }, (err) ->
return callback(err) if err?
callback(undefined, comment)
find_user_by_username_by_social = (social_column, user_name, callback) ->
db.command "select from V where _type = 'user' and #{social_column} is not null and user_name = '#{user_name}'", (err, users) ->
return callback(err) if err?
return callback("no record found") if users.length is 0
return callback("too many records found") if users.length > 1
callback(undefined, users[0])
load_presentation_from_id = (presentation_id, callback) ->
db.command "select from V where _type = 'presentation' and presentation_id = '#{presentation_id}'", (err, presentations) ->
return callback(err) if err?
return callback("no record found") if presentations.length is 0
return callback("too many records found") if presentations.length > 1
callback(undefined, presentations[0])
load_entire_presentation_with_query = (query, callback) ->
db.command query, (err, results) ->
return callback(err) if err?
return callback("no record found") if results.length is 0
presentation = results[0]
load_comments_of presentation, (err) ->
return callback(err) if err?
load_chapters_of presentation, (err) ->
return callback(err) if err?
utils.exec_for_each load_slides_of, presentation.chapters, (err) ->
return callback(err) if err?
return callback(undefined, presentation)
load_entire_presentation_from_id = (presentation_id, callback) ->
load_entire_presentation_with_query "select from V where _type = 'presentation' and id = '#{presentation_id}'", callback
load_entire_presentation_from_catalog = (catalog_name, presentation_id, callback) ->
load_entire_presentation_with_query "select from V where _type = 'presentation' and id = '#{presentation_id}' and out.label CONTAINSALL 'part_of' and out.in.id CONTAINSALL '#{catalog_name}'", callback
load_entire_presentation_from_users_catalog = (social_prefix, user_name, presentation_id, callback) ->
load_entire_presentation_with_query "select from V where _type = 'presentation' and id = '#{presentation_id}' and in.label CONTAINS 'authored' and in.out.#{social_prefix}.size() = 1 and in.out.user_name CONTAINSALL '#{user_name}'", callback
load_user_of = (comment, callback) ->
db.fromVertex(comment).inVertexes "authored_comment", (err, users) ->
return callback(err) if err?
return callback("Too many users") if users.length > 1
comment.user = users[0]
callback()
load_comments_of = (node, callback) ->
db.fromVertex(node).inVertexes "comment_of", (err, comments) ->
return callback(err) if err?
node.comments = _.sortBy comments, (comment) -> -1 * comment.time
utils.exec_for_each load_user_of, node.comments, (err) ->
return callback(err) if err?
callback(undefined, node)
load_chapters_of = (presentation, callback) ->
db.fromVertex(presentation).inVertexes "chapter_of", (err, chapters) ->
return callback(err) if err?
presentation.chapters = _.sortBy chapters, (chapter) -> chapter._index
return callback(undefined, presentation)
load_slides_of = (chapter, callback) ->
db.fromVertex(chapter).inVertexes "slide_of", (err, slides) ->
return callback(err) if err?
chapter.slides = _.sortBy slides, (slide) -> slide.time
utils.exec_for_each load_comments_of, slides, (err) ->
return callback(err) if err?
return callback(undefined, chapter)
delete_slide = (rid, callback) ->
delete_nodes = (nodes, callback) ->
return callback() if nodes.length is 0
deleted = 0
for node in nodes
db.delete node, (err) ->
return callback(err) if err?
deleted++
callback() if nodes.length is deleted
db.loadRecord "##{rid}", (err, node) ->
return callback(err) if err?
db.command "select from (traverse V.in, E.out from ##{rid}) where label = 'authored_comment'", (err, authored_edges) ->
return callback(err) if err?
delete_nodes authored_edges, (err) ->
return callback(err) if err?
db.command "select from (traverse V.in, E.out from ##{rid}) where label = 'comment_of'", (err, comment_edges) ->
return callback(err) if err?
delete_nodes comment_edges, (err) ->
return callback(err) if err?
db.command "select from (traverse V.in, E.out from ##{rid}) where _type = 'comment'", (err, comments) ->
return callback(err) if err?
delete_nodes comments, (err) ->
return callback(err) if err?
db.getOutEdges node, "slide_of", (err, edges) ->
return callback(err) if edges.length isnt 1
edge_rid = edges[0]["@rid"]
db.fromVertex(node).outVertexes "slide_of", (err, chapters) ->
return callback(err) if chapters.length isnt 1
chapter = chapters[0]
chapter.in = _.without(chapter.in, edge_rid)
db.save chapter, (err, chapter) ->
return callback(err) if err?
delete_nodes edges, (err) ->
return callback(err) if err?
db.delete node, callback
is_user_author_of = (user_rid, other_rid, callback) ->
db.command "select gremlin('current.inE.filter({it.label.equals(\"authored\")}).outV.id') as authors from #{other_rid}", (err, results) ->
return callback(err) if err?
authors = results[0].authors
authors = [authors] if !_.isArray(authors)
callback(null, authors.indexOf(user_rid) isnt -1)
is_user_admin_of_catalog_of = (user_rid, other_rid, callback) ->
db.command "select gremlin('current.outE(\"part_of\").inV.filter({it._type.equals(\"catalog\")}).inE(\"admin_of\").outV') as catalog_admins from #{other_rid}", (err, results) ->
return callback(err) if err?
catalog_admins = results[0].catalog_admins
catalog_admins = [catalog_admins] if !_.isArray(catalog_admins)
callback(null, catalog_admins.indexOf(user_rid) isnt -1)
exports.load_chapters_of = load_chapters_of
exports.load_entire_presentation_from_catalog = load_entire_presentation_from_catalog
exports.load_entire_presentation_from_id = load_entire_presentation_from_id
exports.load_entire_presentation_from_users_catalog = load_entire_presentation_from_users_catalog
exports.load_presentation_from_id = load_presentation_from_id
exports.init = init
exports.list_catalogs_with_presentation_count = list_catalogs_with_presentation_count
exports.catalog_name_to_node = catalog_name_to_node
exports.from_user_to_presentations = from_user_to_presentations
exports.from_catalog_to_presentations = from_catalog_to_presentations
exports.create_comment = create_comment
exports.save = save
exports.cascading_save = cascading_save
exports.create = create
exports.link_slide_to_chapter = link_slide_to_chapter
exports.link_chapter_to_presentation = link_chapter_to_presentation
exports.link_user_to_presentation = link_user_to_presentation
exports.find_user_by_username_by_social = find_user_by_username_by_social
exports.delete_slide = delete_slide
exports.is_user_author_of = is_user_author_of
exports.is_user_admin_of_catalog_of = is_user_admin_of_catalog_of