-
Notifications
You must be signed in to change notification settings - Fork 28
/
site.lua
421 lines (365 loc) · 13.2 KB
/
site.lua
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
-- Community site routes
-- =====================
--
-- Routes for all community website pages. We're in the process of starting to
-- transition the whole site to Lua.
--
-- Written by Bernat Romagosa and Michael Ball
--
-- Copyright (C) 2021 by Bernat Romagosa and Michael Ball
--
-- This file is part of Snap Cloud.
--
-- Snap Cloud is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero 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 Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local app = package.loaded.app
local capture_errors = package.loaded.capture_errors
local cached = package.loaded.cached
local respond_to = package.loaded.respond_to
local Users = package.loaded.Users
local Projects = package.loaded.Projects
local Remixes = package.loaded.Remixes
local Collections = package.loaded.Collections
local FlaggedProjects = package.loaded.FlaggedProjects
local assert_exists = require('validation').assert_exists
require 'controllers.user'
require 'controllers.project'
require 'controllers.collection'
require 'dialogs'
app:enable('etlua')
app.layout = require 'views.layout'
local static_pages = {
'about', 'bjc', 'blog', 'coc', 'contact', 'credits', 'dmca', 'extensions',
'materials', 'mirrors', 'offline', 'partners', 'privacy', 'research',
'snapinator', 'snapp', 'source', 'tos'
}
local views = {
-- Simple pages
'change_email', 'change_password', 'delete_user', 'forgot_password',
'forgot_username', 'sign_up', 'login'
}
-- Temporary during a front-end rewrite.
-- This allows testing anypage with adding ?bootstrap=true to the URL
app:before_filter(function (self)
if self.current_user and self.current_user:isadmin() then
if self.params['bootstrap'] == 'true' then
app.layout = 'layout_bs'
end
end
end)
app:get('index', '/', capture_errors(cached(function (self)
self.snapcloud_id = Users:find({ username = 'snapcloud' }).id
-- return { render = 'index' }
return { render = 'index_bs', layout = 'layout_bs' }
end)))
-- Backwards compatibility.
app:get('/index', function ()
return { redirect_to = '/' }
end)
for _, page in pairs(static_pages) do
app:get('/' .. page, capture_errors(cached(function (self)
return { render = 'static/' .. page, layout = 'layout_bs' }
end)))
end
for _, view in pairs(views) do
app:get('/' .. view, capture_errors(cached(function (self)
if self.params['bootstrap'] then
return { render = view .. '_bs', layout = 'layout_bs'}
else
return { render = view }
end
end)))
end
app:get('/embed', capture_errors(function (self)
-- Backwards compatibility with previous URL params
self.project = Projects:find(
tostring(self.params.user or self.params.username),
self.params.project or self.params.projectname
)
assert_project_exists(self)
return { render = 'embed', layout = false }
end))
app:get('/explore', capture_errors(cached(function (self)
self.items = ProjectController.fetch(self)
return { render = 'explore', layout = 'layout_bs' }
end)))
app:get('/collections', capture_errors(cached(function (self)
self.items = CollectionController.fetch(self)
return { render = 'collections', layout = 'layout_bs' }
end)))
app:get('/all_totms', capture_errors(cached(function (self)
self.items = CollectionController.totms(self)
return { render = 'all_totms', layout = 'layout_bs' }
end)))
app:get('/users', capture_errors(cached(function (self)
self.items_per_page = 51
self.items = UserController.fetch(self)
if not self.params.search_term then self.params.search_term = '' end
return { render = 'users', layout = 'layout_bs' }
end)))
app:get('/my_projects', capture_errors(function (self)
if self.current_user then
self.items = ProjectController.my_projects(self)
return { render = 'my_projects', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end))
app:get('/my_collections', capture_errors(function (self)
if self.current_user then
self.items = CollectionController.my_collections(self)
return { render = 'my_collections', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end))
app:get('/collection', capture_errors(function (self)
assert_user_exists(self)
local creator = self.queried_user
self.collection =
assert_exists(Collections:find(creator.id, self.params.collection))
assert_can_view_collection(self, self.collection)
self.collection.creator = creator
if self.collection.thumbnail_id then
self.collection.thumbnail =
package.loaded.disk:retrieve_thumbnail(
self.collection.thumbnail_id)
end
if self.collection.editor_ids then
self.collection.editors = Users:find_all(
self.collection.editor_ids,
{ fields = 'username, id' })
end
self.items_per_page = 12
self.items = CollectionController.projects(self)
return { render = 'collection' }
end))
app:get('/user', capture_errors(function (self)
assert_user_exists(self)
self.username = self.queried_user.username
self.user_id = self.queried_user.id
return { render = 'user', layout = 'layout_bs' }
end))
app:get('/user_collections/:username', capture_errors(cached(function (self)
assert_user_exists(self)
self.params.user_id = self.queried_user.id
self.items = CollectionController.user_collections(self)
return { render = 'collections', layout = 'layout_bs' }
end)))
app:get('/user_projects/:username', capture_errors(cached(function (self)
assert_user_exists(self)
self.items = ProjectController.user_projects(self)
return { render = 'explore', layout = 'layout_bs' }
end)))
-- Display an embedded collection view.
app:get('/carousel', capture_errors(cached(function (self)
assert_user_exists(self)
local creator = self.queried_user
self.params.items_per_row = self.params.items_per_page or 5
self.params.page_number = self.params.page_number or 1
self.collection = assert_exists(Collections:find(creator.id, self.params.collection))
assert_can_view_collection(self, self.collection)
self.collection.creator = creator
self.items = CollectionController.projects(self)
self.title = self.collection.name
self.show_if_empty = true
return { render = 'carousel', layout = 'embedded' }
end)))
app:get('/followed', capture_errors(cached(function (self)
if self.current_user then
self.items = ProjectController.followed_projects(self)
return { render = 'followed', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end)))
app:get('/followed_users', capture_errors(cached(function (self)
if self.current_user then
self.items = UserController.followed_users(self)
return { render = 'followed_users', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end)))
app:get('/my_followers', capture_errors(cached(function (self)
if self.current_user then
self.items = UserController.follower_users(self)
return { render = 'my_followers', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end)))
app:match('project', '/project', capture_errors(function (self)
-- Backwards compatibility with previous URL params
if self.params.user and self.params.project then
-- Just redirect using the new URL params format
return {
redirect_to =
self:url_for(
'project',
nil,
{
username = self.params.user,
projectname = self.params.project
}
)
}
end
self.project = Projects:find(
tostring(self.params.username),
self.params.projectname
)
assert_project_exists(self)
assert_can_view_project(self)
-- check whether this is a remix of another project
local remix =
Remixes:select('WHERE remixed_project_id = ?', self.project.id)
if remix[1] then
self.remixed_from =
Projects:select('WHERE id = ?', remix[1].original_project_id)[1]
end
-- check whether the current user has already flagged this project
if self.current_user then
self.project.flagged =
FlaggedProjects:select(
'WHERE project_id = ? AND flagger_id = ?',
self.project.id,
self.current_user.id
)[1] ~= nil
end
return { render = 'project' }
end))
-- TODO: Should be able to consolidate these pages.
app:get('/examples', capture_errors(cached(function (self)
self.snapcloud_id = Users:find({ username = 'snapcloud' }).id
return { render = 'examples', layout = 'layout_bs' }
end)))
app:get('/events', capture_errors(cached(function (self)
self.snapcloud_id = Users:find({ username = 'snapcloud' }).id
return { render = 'events', layout = 'layout_bs' }
end)))
app:get('/search', capture_errors(function (self)
self.reviewer_controls =
self.current_user and self.current_user:has_min_role('reviewer')
return { render = 'search', layout = 'layout_bs' }
end))
app:get('/profile', capture_errors(function (self)
if self.current_user then
self.user = self.current_user
return { render = 'profile' }
else
return { redirect_to = self:build_url('/') }
end
end))
-- Administration and data management pages
app:get('/admin', capture_errors(function (self)
if self.current_user then
assert_min_role(self, 'reviewer')
return { render = 'admin/index', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end))
app:get('/flags', capture_errors(function (self)
if self.current_user then
assert_min_role(self, 'reviewer')
items = ProjectController.flagged_projects(self)
return { render = 'admin/flags' }
else
return { redirect_to = self:build_url('/') }
end
end))
app:get('/user_admin', capture_errors(function (self)
self.items_per_page = 150
if self.current_user then
assert_min_role(self, 'moderator')
self.items = UserController.fetch(self)
return { render = 'admin/user_admin', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end))
app:get('/zombie_admin', capture_errors(function (self)
self.items_per_page = 150
if self.current_user then
assert_min_role(self, 'moderator')
self.items = UserController.zombies(self)
return { render = 'admin/zombie_admin' }
else
return { redirect_to = self:build_url('/') }
end
end))
app:match('admin/totm', '/totm', respond_to({
GET = capture_errors(function (self)
if self.current_user then
assert_min_role(self, 'moderator')
return { render = true, layout = 'layout_bs' }
else
return { redirect_to = self:build_url('/') }
end
end),
POST = capture_errors(function (self)
assert_min_role(self, 'moderator')
local file = self.params.uploaded_file
if file then
local disk = package.loaded.disk
if disk:save_totm_banner(file) then
return { render = true, layout = 'layout_bs' }
end
end
return errorResponse(self)
end)
}))
app:get('/carousel_admin', capture_errors(function (self)
assert_min_role(self, 'moderator')
return { render = 'admin/carousel_admin' }
end))
app:get('/ip_admin', capture_errors(function (self)
assert_min_role(self, 'admin')
self.ips = SiteController.banned_ips(self)
return { render = 'admin/ip_admin' }
end))
-- Teachers
app:get('/teacher', capture_errors(function (self)
assert_exists(self.current_user)
if (not self.current_user.is_teacher) then
assert_admin(self)
end
return { render = 'teacher/index', layout = 'layout_bs' }
end))
app:get('/bulk', capture_errors(function (self)
assert_exists(self.current_user)
if (not self.current_user.is_teacher) then
assert_admin(self)
end
return { render = 'teacher/bulk', layout = 'layout_bs' }
end))
app:get('/learners', capture_errors(function (self)
assert_exists(self.current_user)
if (not self.current_user.is_teacher) then
assert_admin(self)
end
self.items_per_page = 150
if self.current_user and self.current_user.is_teacher then
self.items = UserController.learners(self)
return { render = 'teacher/learners', layout = 'layout_bs' }
else
return { redirect_to = self:build_url('index') }
end
end))
-- Tools
--[[
app:get('/localize', capture_errors(function (self)
return { render = 'localize' }
end))
]]--