0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local pairs = Luan.pairs or error()
|
|
4 local String = require "luan:String.luan"
|
|
5 local trim = String.trim or error()
|
|
6 local regex = String.regex or error()
|
|
7 local Parsers = require "luan:Parsers.luan"
|
|
8 local json_string = Parsers.json_string or error()
|
|
9 local Io = require "luan:Io.luan"
|
|
10 local Http = require "luan:http/Http.luan"
|
|
11 local Shared = require "site:/lib/Shared.luan"
|
|
12 local js_error = Shared.js_error or error()
|
|
13 local Pic = require "site:/lib/Pic.luan"
|
|
14 local User = require "site:/lib/User.luan"
|
|
15 local Db = require "site:/lib/Db.luan"
|
|
16 local run_in_transaction = Db.run_in_transaction or error()
|
|
17 local Logging = require "luan:logging/Logging.luan"
|
|
18 local logger = Logging.logger "save_pic_title.js"
|
|
19
|
|
20
|
|
21 local hashtags_regex = regex[[^(#?\w+\s*)*$]]
|
|
22 local hashtag_regex = regex[[\w+]]
|
|
23
|
|
24 return function()
|
|
25 Io.stdout = Http.response.text_writer()
|
|
26 local user = User.current() or error()
|
|
27 local pic_id = Http.request.parameters.pic or error()
|
|
28 local title = Http.request.parameters.title or error()
|
|
29 title = trim(title)
|
|
30 local hashtags = Http.request.parameters.hashtags or error()
|
|
31 hashtags = trim(hashtags)
|
|
32 local is_hidden = Http.request.parameters.visible == nil
|
|
33 if not hashtags_regex.matches(hashtags) then
|
|
34 js_error("hashtags",[[Hashtags may only contain letters, numbers, underscores ("_"), and spaces to separate them]])
|
|
35 return
|
|
36 end
|
|
37 local set = {}
|
|
38 for hashtag in hashtag_regex.gmatch(hashtags) do
|
|
39 set[hashtag] = true
|
|
40 end
|
|
41 hashtags = {}
|
|
42 for hashtag in pairs(set) do
|
|
43 hashtags[#hashtags+1] = hashtag
|
|
44 end
|
|
45 local pic
|
|
46 run_in_transaction( function()
|
|
47 pic = Pic.get_by_id(pic_id)
|
|
48 pic.user_id == user.id or error()
|
|
49 pic.title = title
|
|
50 pic.is_hidden = is_hidden
|
|
51 pic.hashtags = hashtags
|
|
52 pic.save()
|
|
53 end )
|
|
54 js_error( "success", "Saved" )
|
|
55 %>
|
|
56 document.querySelector('div[pic] img').title = <%=json_string(title)%>;
|
|
57 document.querySelector('div[hashtags]').innerHTML = <%=json_string(pic.hashtags_html("pics.html"))%>;
|
|
58 <%
|
|
59 end
|