Mercurial Hosting > linkmystyle
diff src/save_pic_title.js.luan @ 0:8f4df159f06b
start public repo
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 11 Jul 2025 20:57:49 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/save_pic_title.js.luan Fri Jul 11 20:57:49 2025 -0600 @@ -0,0 +1,59 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local pairs = Luan.pairs or error() +local String = require "luan:String.luan" +local trim = String.trim or error() +local regex = String.regex or error() +local Parsers = require "luan:Parsers.luan" +local json_string = Parsers.json_string or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local Shared = require "site:/lib/Shared.luan" +local js_error = Shared.js_error or error() +local Pic = require "site:/lib/Pic.luan" +local User = require "site:/lib/User.luan" +local Db = require "site:/lib/Db.luan" +local run_in_transaction = Db.run_in_transaction or error() +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "save_pic_title.js" + + +local hashtags_regex = regex[[^(#?\w+\s*)*$]] +local hashtag_regex = regex[[\w+]] + +return function() + Io.stdout = Http.response.text_writer() + local user = User.current() or error() + local pic_id = Http.request.parameters.pic or error() + local title = Http.request.parameters.title or error() + title = trim(title) + local hashtags = Http.request.parameters.hashtags or error() + hashtags = trim(hashtags) + local is_hidden = Http.request.parameters.visible == nil + if not hashtags_regex.matches(hashtags) then + js_error("hashtags",[[Hashtags may only contain letters, numbers, underscores ("_"), and spaces to separate them]]) + return + end + local set = {} + for hashtag in hashtag_regex.gmatch(hashtags) do + set[hashtag] = true + end + hashtags = {} + for hashtag in pairs(set) do + hashtags[#hashtags+1] = hashtag + end + local pic + run_in_transaction( function() + pic = Pic.get_by_id(pic_id) + pic.user_id == user.id or error() + pic.title = title + pic.is_hidden = is_hidden + pic.hashtags = hashtags + pic.save() + end ) + js_error( "success", "Saved" ) +%> + document.querySelector('div[pic] img').title = <%=json_string(title)%>; + document.querySelector('div[hashtags]').innerHTML = <%=json_string(pic.hashtags_html("pics.html"))%>; +<% +end