Compare commits
10 Commits
c3e02f2596
...
b08e96fe6b
Author | SHA1 | Date | |
---|---|---|---|
|
b08e96fe6b | ||
|
a6fa9a7149 | ||
|
bfe5084eef | ||
|
624bd9c2f6 | ||
|
1fda7ef7fe | ||
|
be734d28b1 | ||
|
0bad9154c6 | ||
|
dbedb15725 | ||
|
5b92aa7510 | ||
|
aab578ecec |
18
app.moon
18
app.moon
@ -2,9 +2,9 @@ lapis = require "lapis"
|
||||
console = require "lapis.console"
|
||||
|
||||
import respond_to from require "lapis.application"
|
||||
import escape_identifier from require "lapis.db"
|
||||
import unescape from require "lapis.util"
|
||||
import create_track, update_track, escape_tag from require "helpers"
|
||||
import escape_literal, escape_identifier from require "lapis.db"
|
||||
import escape, unescape from require "lapis.util"
|
||||
import create_track, update_track from require "helpers"
|
||||
|
||||
import Tracks, Tags from require "models"
|
||||
|
||||
@ -52,6 +52,7 @@ class extends lapis.Application
|
||||
return render: true
|
||||
|
||||
[tag: "/tag/:name(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
|
||||
update_track(@params) if @params.id -- update a track if needed
|
||||
-- validate order
|
||||
@order = tostring(@params.order)\lower!
|
||||
unless Tags.fields[@order]
|
||||
@ -62,7 +63,14 @@ class extends lapis.Application
|
||||
@asc_desc = "ASC"
|
||||
@page = tonumber(@params.page) or 1
|
||||
|
||||
tracks = Tracks\paginated "WHERE tags LIKE ? ORDER BY #{escape_identifier @order} #{@asc_desc}", escape_tag(unescape(@params.name)), per_page: 32
|
||||
-- create tag pattern
|
||||
tag_pattern = unescape(@params.name)
|
||||
if tag_pattern != tag_pattern\lower!
|
||||
return redirect_to: @url_for "tag", name: escape(tag_pattern\lower!), order: @order, asc_desc: @asc_desc, page: @page
|
||||
tag_pattern = escape_literal tag_pattern
|
||||
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
||||
|
||||
tracks = Tracks\paginated "WHERE tags LIKE #{tag_pattern} ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
|
||||
@last_page = 1
|
||||
if tag = Tags\find name: unescape(@params.name)
|
||||
@last_page = 1 + math.floor tag.count / 32
|
||||
@ -72,7 +80,7 @@ class extends lapis.Application
|
||||
if @page > @last_page
|
||||
return redirect_to: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: @last_page
|
||||
|
||||
@tracks = tags\get_page(@page)
|
||||
@tracks = tracks\get_page(@page)
|
||||
return render: true
|
||||
|
||||
[tags: "/tags(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
|
||||
|
@ -14,9 +14,6 @@ process_tags = (tags_str) ->
|
||||
table.sort taglist
|
||||
return " #{table.concat taglist, " "} "
|
||||
|
||||
escape_tag = (str) ->
|
||||
return str\gsub "[%%_]", "\\%1"
|
||||
|
||||
update_track = (user_input) ->
|
||||
updates = {}
|
||||
-- automatic tags
|
||||
@ -49,7 +46,6 @@ update_track = (user_input) ->
|
||||
|
||||
return {
|
||||
:process_tags
|
||||
:escape_tag
|
||||
:update_track
|
||||
create_track: update_track
|
||||
}
|
||||
|
@ -11,6 +11,11 @@ http {
|
||||
charset UTF-8;
|
||||
include mime.types;
|
||||
|
||||
lua_shared_dict worker 1m;
|
||||
init_worker_by_lua '
|
||||
require("worker")
|
||||
';
|
||||
|
||||
server {
|
||||
listen ${{PORT}};
|
||||
lua_code_cache ${{CODE_CACHE}};
|
||||
|
@ -44,7 +44,7 @@ class extends Widget
|
||||
for track in *@tracks
|
||||
tr class: Tracks.statuses[track.status], ->
|
||||
form {
|
||||
action: @url_for "tracks", order: @order, asc_desc: @asc_desc, page: @page
|
||||
action: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: @page
|
||||
method: "POST"
|
||||
enctype: "multipart/form-data"
|
||||
}, ->
|
||||
|
33
worker.moon
Normal file
33
worker.moon
Normal file
@ -0,0 +1,33 @@
|
||||
lock = require "resty.lock"
|
||||
|
||||
import Tags, Tracks from require "models"
|
||||
import escape_literal from require "lapis.db"
|
||||
|
||||
next_id = (id=0) ->
|
||||
next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
|
||||
unless next_tag
|
||||
next_tag = Tags\select "WHERE id > '0' ORDER BY id ASC LIMIT 1"
|
||||
if next_tag
|
||||
return next_tag[1].id
|
||||
|
||||
update_tag_counts = (premature, tag_id) ->
|
||||
if premature return
|
||||
|
||||
thread_lock = lock\new "worker", { timeout: 0, exptime: 2 }
|
||||
if thread_lock\lock "update_tag_counts.thread"
|
||||
time_lock = lock\new "worker", { exptime: 2 }
|
||||
if time_lock\lock "update_tag_counts.time"
|
||||
|
||||
if tag_id
|
||||
if tag = Tags\find id: tag_id
|
||||
tag_pattern = escape_literal tag.name
|
||||
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
||||
count = Tracks\count "tags LIKE #{tag_pattern}"
|
||||
tag\update(:count)
|
||||
|
||||
tag_id = next_id(tag_id) or tag_id
|
||||
thread_lock\unlock!
|
||||
|
||||
ngx.timer.at(2, update_tag_counts, tag_id)
|
||||
|
||||
ngx.timer.at(0, update_tag_counts)
|
Reference in New Issue
Block a user