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