Mercurial Hosting > linkmystyle
comparison src/lib/Pic.luan @ 0:8f4df159f06b
start public repo
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 11 Jul 2025 20:57:49 -0600 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:8f4df159f06b |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | |
| 2 local error = Luan.error | |
| 3 local ipairs = Luan.ipairs or error() | |
| 4 local Number = require "luan:Number.luan" | |
| 5 local long = Number.long or error() | |
| 6 local float = Number.float or error() | |
| 7 local Table = require "luan:Table.luan" | |
| 8 local concat = Table.concat or error() | |
| 9 local Html = require "luan:Html.luan" | |
| 10 local html_encode = Html.encode or error() | |
| 11 local Db = require "site:/lib/Db.luan" | |
| 12 local Uploadcare = require "site:/lib/Uploadcare.luan" | |
| 13 local uploadcare_url = Uploadcare.url or error() | |
| 14 local uploadcare_thumb_url = Uploadcare.thumb_url or error() | |
| 15 local Utils = require "site:/lib/Utils.luan" | |
| 16 local to_list = Utils.to_list or error() | |
| 17 | |
| 18 | |
| 19 local Pic = {} | |
| 20 | |
| 21 local function from_doc(doc) | |
| 22 doc.type == "pic" or error "wrong type" | |
| 23 return Pic.new { | |
| 24 id = doc.id | |
| 25 uuid = doc.uuid | |
| 26 filename = doc.filename | |
| 27 user_id = doc.pic_user_id | |
| 28 order = doc.pic_order | |
| 29 title = doc.title | |
| 30 is_hidden = doc.is_hidden=="true" | |
| 31 hashtags = doc.hashtags | |
| 32 } | |
| 33 end | |
| 34 | |
| 35 local function to_doc(pic) | |
| 36 return { | |
| 37 type = "pic" | |
| 38 id = pic.id | |
| 39 uuid = pic.uuid or error() | |
| 40 filename = pic.filename or error() | |
| 41 pic_user_id = long(pic.user_id) | |
| 42 pic_order = float(pic.order) | |
| 43 title = pic.title or error() | |
| 44 is_hidden = pic.is_hidden and "true" or nil | |
| 45 hashtags = pic.hashtags | |
| 46 } | |
| 47 end | |
| 48 | |
| 49 function Pic.new(pic) | |
| 50 | |
| 51 function pic.save() | |
| 52 local doc = to_doc(pic) | |
| 53 Db.save(doc) | |
| 54 pic.id = doc.id | |
| 55 end | |
| 56 | |
| 57 function pic.reload() | |
| 58 return Pic.get_by_id(pic.id) or error(pic.id) | |
| 59 end | |
| 60 | |
| 61 function pic.delete() | |
| 62 Db.run_in_transaction( function() | |
| 63 local id = pic.id | |
| 64 Db.delete("link_owner_id:"..id) | |
| 65 Db.delete("id:"..id) | |
| 66 end ) | |
| 67 end | |
| 68 | |
| 69 function pic.get_url() | |
| 70 return uploadcare_url(pic.uuid) | |
| 71 end | |
| 72 | |
| 73 function pic.get_thumb_url() | |
| 74 return uploadcare_thumb_url(pic.uuid) | |
| 75 end | |
| 76 | |
| 77 function pic.title_attr() | |
| 78 local title = pic.title | |
| 79 return [[title="]]..html_encode(title)..[["]] | |
| 80 end | |
| 81 | |
| 82 function pic.move_after(prev) | |
| 83 local pics = Pic.get_user_pics(pic.user_id) | |
| 84 if prev == nil then | |
| 85 pic.order = pics[1].order - 1 | |
| 86 else | |
| 87 pic.user_id==prev.user_id or error() | |
| 88 for i, x in ipairs(pics) do | |
| 89 if prev.id == x.id then | |
| 90 if i == #pics then | |
| 91 pic.order = prev.order + 1 | |
| 92 else | |
| 93 local next = pics[i+1] | |
| 94 pic.order = (prev.order + next.order) / 2 | |
| 95 end | |
| 96 return | |
| 97 end | |
| 98 end | |
| 99 error() | |
| 100 end | |
| 101 end | |
| 102 | |
| 103 function pic.hashtags_html(base_path) | |
| 104 local hashtags = pic.hashtags | |
| 105 if hashtags == nil then | |
| 106 return "" | |
| 107 end | |
| 108 hashtags = to_list(hashtags) | |
| 109 local t = {} | |
| 110 for _, hashtag in ipairs(hashtags) do | |
| 111 t[#t+1] = `%><a href="<%=base_path%>#<%=hashtag%>">#<%=hashtag%></a><%` | |
| 112 end | |
| 113 return concat(t," ") | |
| 114 end | |
| 115 | |
| 116 return pic | |
| 117 end | |
| 118 | |
| 119 function Pic.get_by_id(id) | |
| 120 local doc = Db.get_document("id:"..id) | |
| 121 return doc and from_doc(doc) | |
| 122 end | |
| 123 | |
| 124 local function from_docs(docs) | |
| 125 local pics = {} | |
| 126 for _, doc in ipairs(docs) do | |
| 127 local pic = from_doc(doc) | |
| 128 pics[#pics+1] = pic | |
| 129 end | |
| 130 return pics | |
| 131 end | |
| 132 | |
| 133 function Pic.get_user_pics(user_id) | |
| 134 local docs = Db.search("pic_user_id:"..user_id,1,1000,{sort="pic_order"}) | |
| 135 return from_docs(docs) | |
| 136 end | |
| 137 | |
| 138 function Pic.search(query,sort,rows) | |
| 139 rows = rows or 1000000 | |
| 140 local docs = Db.search(query,1,rows,{sort=sort}) | |
| 141 return from_docs(docs) | |
| 142 end | |
| 143 | |
| 144 return Pic |
