diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Pic.luan	Fri Jul 11 20:57:49 2025 -0600
@@ -0,0 +1,144 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Number = require "luan:Number.luan"
+local long = Number.long or error()
+local float = Number.float or error()
+local Table = require "luan:Table.luan"
+local concat = Table.concat or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Db = require "site:/lib/Db.luan"
+local Uploadcare = require "site:/lib/Uploadcare.luan"
+local uploadcare_url = Uploadcare.url or error()
+local uploadcare_thumb_url = Uploadcare.thumb_url or error()
+local Utils = require "site:/lib/Utils.luan"
+local to_list = Utils.to_list or error()
+
+
+local Pic = {}
+
+local function from_doc(doc)
+	doc.type == "pic" or error "wrong type"
+	return Pic.new {
+		id = doc.id
+		uuid = doc.uuid
+		filename = doc.filename
+		user_id = doc.pic_user_id
+		order = doc.pic_order
+		title = doc.title
+		is_hidden = doc.is_hidden=="true"
+		hashtags = doc.hashtags
+	}
+end
+
+local function to_doc(pic)
+	return {
+		type = "pic"
+		id = pic.id
+		uuid = pic.uuid or error()
+		filename = pic.filename or error()
+		pic_user_id = long(pic.user_id)
+		pic_order = float(pic.order)
+		title = pic.title or error()
+		is_hidden = pic.is_hidden and "true" or nil
+		hashtags = pic.hashtags
+	}
+end
+
+function Pic.new(pic)
+
+	function pic.save()
+		local doc = to_doc(pic)
+		Db.save(doc)
+		pic.id = doc.id
+	end
+
+	function pic.reload()
+		return Pic.get_by_id(pic.id) or error(pic.id)
+	end
+
+	function pic.delete()
+		Db.run_in_transaction( function()
+			local id = pic.id
+			Db.delete("link_owner_id:"..id)
+			Db.delete("id:"..id)
+		end )
+	end
+
+	function pic.get_url()
+		return uploadcare_url(pic.uuid)
+	end
+
+	function pic.get_thumb_url()
+		return uploadcare_thumb_url(pic.uuid)
+	end
+
+	function pic.title_attr()
+		local title = pic.title
+		return [[title="]]..html_encode(title)..[["]]
+	end
+
+	function pic.move_after(prev)
+		local pics = Pic.get_user_pics(pic.user_id)
+		if prev == nil then
+			pic.order = pics[1].order - 1
+		else
+			pic.user_id==prev.user_id or error()
+			for i, x in ipairs(pics) do
+				if prev.id == x.id then
+					if i == #pics then
+						pic.order = prev.order + 1
+					else
+						local next = pics[i+1]
+						pic.order = (prev.order + next.order) / 2
+					end
+					return
+				end
+			end
+			error()
+		end
+	end
+
+	function pic.hashtags_html(base_path)
+		local hashtags = pic.hashtags
+		if hashtags == nil then
+			return ""
+		end
+		hashtags = to_list(hashtags)
+		local t = {}
+		for _, hashtag in ipairs(hashtags) do
+			t[#t+1] = `%><a href="<%=base_path%>#<%=hashtag%>">#<%=hashtag%></a><%`
+		end
+		return concat(t," ")
+	end
+
+	return pic
+end
+
+function Pic.get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and from_doc(doc)
+end
+
+local function from_docs(docs)
+	local pics = {}
+	for _, doc in ipairs(docs) do
+		local pic = from_doc(doc)
+		pics[#pics+1] = pic
+	end
+	return pics
+end
+
+function Pic.get_user_pics(user_id)
+	local docs = Db.search("pic_user_id:"..user_id,1,1000,{sort="pic_order"})
+	return from_docs(docs)
+end
+
+function Pic.search(query,sort,rows)
+	rows = rows or 1000000
+	local docs = Db.search(query,1,rows,{sort=sort})
+	return from_docs(docs)
+end
+
+return Pic