-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathapp.moon
395 lines (305 loc) · 10.3 KB
/
app.moon
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
with require "cloud_storage.http"
.set require "lapis.nginx.http"
db = require "lapis.db"
lapis = require "lapis.init"
import
assert_error
capture_errors
respond_to
yield_error
from require "lapis.application"
import assert_valid, with_params from require "lapis.validate"
types = require "lapis.validate.types"
import
ManifestModules
Manifests
Modules
Rocks
Users
Versions
DownloadsDaily
from require "models"
import
handle_rock_upload
handle_rockspec_upload
from require "helpers.uploaders"
import
assert_csrf
assert_editable
generate_csrf
require_login
require_admin
capture_errors_404
not_found
ensure_https
assert_page
from require "helpers.app"
import concat, insert from table
import load_module, load_manifest from require "helpers.loaders"
import paginated_modules from require "helpers.modules"
logger = require "lapis.logging"
old_query = logger.query
logger.query = (q, time, ...) ->
if ngx and ngx.ctx.query_log
table.insert ngx.ctx.query_log, {q, time}
old_query q, time, ...
class MoonRocks extends lapis.Application
layout: require "views.layout"
Request: require "helpers.request"
@enable "exception_tracking"
@include "applications.api"
@include "applications.user"
@include "applications.manifest"
@include "applications.modules"
@include "applications.github"
@include "applications.admin"
@include "applications.labels"
@before_filter =>
if ngx and ngx.ctx
ngx.ctx.query_log = {}
@current_user, @current_user_session = Users\read_session @
if @current_user
@current_user\update_last_active!
if @current_user_session
@current_user_session\update_last_active!
@csrf_token = generate_csrf @
handle_404: => not_found
"/console": require("lapis.console").make!
[index: "/"]: ensure_https =>
@page_description = "A website for submitting and distributing Lua rocks"
root = Manifests\root!
@recent_modules = Modules\select [[
inner join manifest_modules
on manifest_modules.module_id = modules.id and manifest_modules.manifest_id = ?
order by modules.created_at desc limit 5
]], root.id, fields: "modules.*"
Users\include_in @recent_modules, "user_id"
@popular_modules = Modules\select "order by downloads desc limit 5"
Users\include_in @popular_modules, "user_id"
import ApprovedLabels from require "models"
@labels = ApprovedLabels\select "order by name"
@downloads_daily = DownloadsDaily\fetch true, 30
render: true
[modules: "/modules"]: =>
@title = "All Modules"
paginated_modules @, Modules\paginated "order by name asc", {
per_page: 50
fields: "id, name, display_name, user_id, downloads, summary"
}
render: true
[upload_rockspec: "/upload"]: require_login respond_to {
before: =>
@title = "Upload Rockspec"
GET: =>
render: true
POST: capture_errors {
on_error: =>
if @params.json
{ json: { errors: @errors } }
else
{ render: true }
=>
assert_csrf @
mod, version, is_new = handle_rockspec_upload @
redirect_to = @url_for "module", user: @current_user, module: mod
import UserActivityLogs from require "models"
UserActivityLogs\create_from_request @, {
user_id: @current_user.id
action: if is_new then "module.create" else "module.add_version"
source: "web"
object_type: "module"
object_id: mod.id
data: {
version_id: version.id
version_name: version.version_name
}
}
if @params.json
{ json: { success: true, :redirect_to } }
else
{ :redirect_to }
}
}
[endorse_module: "/endorse/:user/:module"]: require_login capture_errors_404 respond_to {
before: =>
load_module @
PUT: =>
@module\endorse @current_user
redirect_to: @url_for @module
DELETE: =>
endorsement = @module\endorsement @current_user
endorsement and endorsement\delete!
redirect_to: @url_for @module
}
[upload_rock: "/modules/:user/:module/:version/upload"]: capture_errors_404 require_login respond_to {
before: =>
load_module @
@title = "Upload Rock"
GET: =>
assert_editable @, @module
render: true
POST: capture_errors =>
assert_csrf @
rock = assert_error handle_rock_upload @
import UserActivityLogs from require "models"
UserActivityLogs\create_from_request @, {
user_id: @current_user.id
action: "module.version.upload_rock"
source: "web"
object_type: "version"
object_id: @version.id
data: {
rock_id: rock.id
rock_revision: rock.revision
rock_arch: rock.arch
}
}
redirect_to: @url_for "module_version", @
}
[manifest: "/m/:manifest"]: capture_errors_404 =>
load_manifest @, "name"
@title = "#{@manifest.name} Manifest"
paginated_modules @, @manifest
render: true
[manifest_development: "/m/:manifest/development-only"]: capture_errors_404 =>
load_manifest @, "name"
@title = "#{@manifest.name} Manifest Development Only Modules"
@development_only = true
paginated_modules @, @manifest, dev_only: true
render: "manifest"
[manifest_maintainers: "/m/:manifest/maintainers"]: capture_errors_404 =>
load_manifest @, "name"
@title = "#{@manifest.name} Manifest Maintainers"
@pager = @manifest\find_admins!
@admins = @pager\get_page!
render: true
[manifest_recent_versions: "/m/:manifest/recent"]: capture_errors_404 =>
load_manifest @, "name"
@title = "#{@manifest.name} Recent Versions"
@pager = @manifest\find_versions!
assert_page @
@versions = @pager\get_page @page
render: true
[about: "/about"]: =>
@title = "About"
render: true
[changes: "/changes"]: =>
@title = "Changes"
render: true
[search: "/search"]: capture_errors with_params {
{"q", types.empty + types.limited_text(128), label: "query" }
{"non_root", types.empty + types.any / true}
}, (params) =>
if query = params.q
@title = "Search '#{query}'"
@search_query = query
manifests = unless params.non_root
{ Manifests\root!.id }
@results = Modules\search query, manifests
Modules\preload_relation @results, "user", fields: "id, slug, username"
@user_results = Users\search(query)\get_page!
else
@title = "Search"
render: true
[copy_module: "/copy/modules/:user/:module"]: require_admin capture_errors {
on_error: =>
if @module
{ render: true }
else
not_found
respond_to {
before: capture_errors_404 =>
return unless load_module @
for man in *@module\get_manifests!
if man.name == "root"
@in_root = true
break
GET: =>
render: true
POST: capture_errors with_params {
{"username", types.limited_text 256}
{"take_root", types.empty / false + types.any / true}
}, (params) =>
import slugify from require "lapis.util"
target_user = Users\find slug: slugify params.username
assert_error target_user, "could not find user"
assert_error @module.user.id != target_user.id, "users are the same"
new_module = @module\copy_to_user target_user, params.take_root
redirect_to: @url_for("module", user: target_user.slug, module: new_module.name)
}
}
[new_manifest: "/new-manifest"]: require_login respond_to {
before: =>
@title = "New manifest"
GET: =>
render: true
POST: capture_errors with_params {
{"manifest_name", types.limited_text 60}
{"description", types.empty + types.limited_text 1024*5}
{"is_open", types.empty / false + types.any / true}
}, (params) =>
import ManifestAdmins from require "models"
assert_csrf @
manifest = assert_error Manifests\create(
params.manifest_name,
params.is_open,
params.description
)
ManifestAdmins\create manifest, @current_user, true
redirect_to: @url_for(manifest)
}
[stats: "/stats"]: =>
@title = "Stats"
import cumulative_created from require "helpers.stats"
@cumulative_users = cumulative_created Users, nil, "created_at", "week"
@cumulative_modules = cumulative_created Modules, nil, "created_at", "week"
@cumulative_versions = cumulative_created Versions, nil, "created_at", "week"
render: true
[popular_this_week: "/stats/this-week"]: capture_errors_404 =>
@title = "Top this Lua modules this week"
@days = @params.days or 7
@days = math.min 60, math.max 1, @days
@top_versions = DownloadsDaily\select "
where date >= (now() at time zone 'utc' - ?::interval)
group by version_id
order by sum desc
limit 20
", "#{@days} days", fields: "sum(count), version_id", load: false
@top_new_versions = DownloadsDaily\select "
where date >= (now() at time zone 'utc' - ?::interval)
and version_id not in (
select version_id from downloads_daily
where
date >= (now() at time zone 'utc' - ?::interval) and
date < (now() at time zone 'utc' - ?::interval)
group by version_id
order by sum(count) desc
limit 20
)
group by version_id
order by sum desc
limit 20
", "#{@days} days", "#{@days * 2} days", "#{@days} days", {
fields: "sum(count), version_id"
load: false
}
all_tuples = { unpack @top_versions }
for t in *@top_new_versions
table.insert all_tuples, t
Versions\include_in all_tuples, "version_id"
versions = [v.version for v in *all_tuples when v.version]
Modules\include_in versions, "module_id"
Users\include_in [v.module for v in *versions], "user_id"
render: true
[dependency_stats: "/stats/dependencies"]: capture_errors_404 =>
import Dependencies from require "models"
@top_depended = Dependencies\select "
group by dependency_name
order by count desc
limit 30
", fields: "dependency_name, count(*)"
Dependencies\preload_modules @top_depended
@top_depended = [t for t in *@top_depended when t.manifest_module]
render: true
["/security-incident-march-2019"]: =>
render: "security_incident_march_2019"