annotate src/lib/Icon.luan @ 2:e32e4120dc70

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Jul 2025 21:23:39 -0600
parents 8f4df159f06b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Number = require "luan:Number.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local long = Number.long or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local float = Number.float or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Db = require "site:/lib/Db.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Icon = {}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local icon_names = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 applemusic = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 title = "Apple Music"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 placeholder = "https://music.apple.com/"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 cashapp = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 title = "Cashapp"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 placeholder = "https://cash.app/$username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 discord = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 title = "Discord"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 placeholder = "https://discord.com/invite/yourchannel"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 email = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 title = "Email"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 placeholder = "you@email.com"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 type = "email"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 facebook = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 title = "Facebook"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 placeholder = "https://facebook.com/facebookpageurl"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 instagram = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 title = "Instagram"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 placeholder = "https://www.instagram.com/username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 patreon = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 title = "Patreon"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 placeholder = "https://patreon.com/"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 pinterest = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 title = "Pinterest"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 placeholder = "https://www.pinterest.com/username/"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 paypal = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 title = "PayPal"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 placeholder = "https://www.paypal.me/username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 reddit = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 title = "Reddit"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 placeholder = "https://www.reddit.com/"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 snapchat = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 title = "Snapchat"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 placeholder = "https://www.snapchat.com/add/username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 soundcloud = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 title = "SoundCloud"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 placeholder = "https://soundcloud.com/username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 spotify = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 title = "Spotify"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 placeholder = "https://open.spotify.com/artist/artistname"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 threads = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 title = "Threads"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 placeholder = "https://www.threads.net/@username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 tiktok = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 title = "TikTok"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 placeholder = "https://www.tiktok.com/@username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 twitch = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 title = "Twitch"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 placeholder = "https://twitch.tv/"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 twitter = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 title = "Twitter"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 placeholder = "https://twitter.com/username"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 youtube = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 title = "YouTube"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 placeholder = "https://youtube.com/channel/youtubechannelurl"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 Icon.icon_names = icon_names
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 local function from_doc(doc)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 doc.type == "icon" or error "wrong type"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 return Icon.new {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 id = doc.id
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 name = doc.icon_name
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 url = doc.url
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 user_id = doc.icon_user_id
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 order = doc.icon_order
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 Icon.from_doc = from_doc
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 local function to_doc(icon)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 return {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 type = "icon"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 id = icon.id
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 icon_name = icon.name or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 url = icon.url or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 icon_user_id = long(icon.user_id)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 icon_order = float(icon.order)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 function Icon.new(icon)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 function icon.save()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 local doc = to_doc(icon)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 Db.save(doc)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 icon.id = doc.id
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 function icon.title_attr()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 return [[title="]]..icon_names[icon.name].title..[["]]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 function icon.move_after(prev)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 local icons = Icon.get_user_icons(icon.user_id)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 if prev == nil then
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 icon.order = icons[1].order - 1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 else
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 icon.owner_id==prev.owner_id or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 for i, x in ipairs(icons) do
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 if prev.id == x.id then
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 if i == #icons then
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 icon.order = prev.order + 1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 else
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 local next = icons[i+1]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 icon.order = (prev.order + next.order) / 2
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 return
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 return icon
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 function Icon.get_by_id(id)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 local doc = Db.get_document("id:"..id)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 return doc and from_doc(doc)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 local function from_docs(docs)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 local icons = {}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 for _, doc in ipairs(docs) do
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 local icon = from_doc(doc)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 icons[#icons+1] = icon
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 return icons
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 function Icon.get_user_icons(user_id)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 local docs = Db.search("icon_user_id:"..user_id,1,1000,{sort="icon_order"})
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 return from_docs(docs)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 return Icon