a mini social media app for small communities
1module webapp
2
3import veb
4import entity { User }
5
6fn (mut app App) index(mut ctx Context) veb.Result {
7 if !app.config.instance.public_data {
8 _ := app.whoami(mut ctx) or {
9 ctx.error('not logged in')
10 return ctx.redirect('/login')
11 }
12 }
13
14 ctx.title = app.config.instance.name
15 user := app.whoami(mut ctx) or { User{} }
16 recent_posts := app.get_recent_posts()
17 pinned_posts := app.get_pinned_posts()
18 motd := app.get_motd()
19 return $veb.html('../templates/index.html')
20}
21
22fn (mut app App) login(mut ctx Context) veb.Result {
23 ctx.title = 'login to ${app.config.instance.name}'
24 user := app.whoami(mut ctx) or { User{} }
25 return $veb.html('../templates/login.html')
26}
27
28fn (mut app App) register(mut ctx Context) veb.Result {
29 ctx.title = 'register for ${app.config.instance.name}'
30 user := app.whoami(mut ctx) or { User{} }
31 return $veb.html('../templates/register.html')
32}
33
34fn (mut app App) me(mut ctx Context) veb.Result {
35 user := app.whoami(mut ctx) or {
36 ctx.error('not logged in')
37 return ctx.redirect('/login')
38 }
39 ctx.title = '${app.config.instance.name} - ${user.get_name()}'
40 return ctx.redirect('/user/${user.username}')
41}
42
43@['/me/saved']
44fn (mut app App) me_saved(mut ctx Context) veb.Result {
45 user := app.whoami(mut ctx) or {
46 ctx.error('not logged in')
47 return ctx.redirect('/login')
48 }
49 ctx.title = '${app.config.instance.name} - saved posts'
50 posts := app.get_saved_posts_as_post_for(user.id)
51 return $veb.html('../templates/saved_posts.html')
52}
53
54@['/me/saved_for_later']
55fn (mut app App) me_saved_for_later(mut ctx Context) veb.Result {
56 user := app.whoami(mut ctx) or {
57 ctx.error('not logged in')
58 return ctx.redirect('/login')
59 }
60 ctx.title = '${app.config.instance.name} - posts saved for later'
61 posts := app.get_saved_for_later_posts_as_post_for(user.id)
62 return $veb.html('../templates/saved_posts_for_later.html')
63}
64
65fn (mut app App) settings(mut ctx Context) veb.Result {
66 user := app.whoami(mut ctx) or {
67 ctx.error('not logged in')
68 return ctx.redirect('/login')
69 }
70 ctx.title = '${app.config.instance.name} - settings'
71 return $veb.html('../templates/settings.html')
72}
73
74fn (mut app App) admin(mut ctx Context) veb.Result {
75 ctx.title = '${app.config.instance.name} dashboard'
76 user := app.whoami(mut ctx) or { User{} }
77 return $veb.html('../templates/admin.html')
78}
79
80fn (mut app App) inbox(mut ctx Context) veb.Result {
81 user := app.whoami(mut ctx) or {
82 ctx.error('not logged in')
83 return ctx.redirect('/login')
84 }
85 ctx.title = '${app.config.instance.name} inbox'
86 notifications := app.get_notifications_for(user.id)
87 return $veb.html('../templates/inbox.html')
88}
89
90fn (mut app App) logout(mut ctx Context) veb.Result {
91 user := app.whoami(mut ctx) or {
92 ctx.error('not logged in')
93 return ctx.redirect('/login')
94 }
95 ctx.title = '${app.config.instance.name} logout'
96 return $veb.html('../templates/logout.html')
97}
98
99@['/user/:username']
100fn (mut app App) user(mut ctx Context, username string) veb.Result {
101 if !app.config.instance.public_data {
102 _ := app.whoami(mut ctx) or {
103 ctx.error('not logged in')
104 return ctx.redirect('/login')
105 }
106 }
107
108 user := app.whoami(mut ctx) or { User{} }
109 viewing := app.get_user_by_name(username) or {
110 ctx.error('user not found')
111 return ctx.redirect('/')
112 }
113 ctx.title = '${app.config.instance.name} - ${user.get_name()}'
114 posts := app.get_posts_from_user(viewing.id, 10)
115
116 // needed for new_post component
117 replying := false
118 replying_to := 0
119 replying_to_user := User{}
120
121 return $veb.html('../templates/user.html')
122}
123
124@['/post/:post_id']
125fn (mut app App) post(mut ctx Context, post_id int) veb.Result {
126 if !app.config.instance.public_data {
127 _ := app.whoami(mut ctx) or {
128 ctx.error('not logged in')
129 return ctx.redirect('/login')
130 }
131 }
132
133 post := app.get_post_by_id(post_id) or {
134 ctx.error('no such post')
135 return ctx.redirect('/')
136 }
137 ctx.title = '${app.config.instance.name} - ${post.title}'
138 user := app.whoami(mut ctx) or { User{} }
139
140 mut replying_to_post := app.get_unknown_post()
141 mut replying_to_user := app.get_unknown_user()
142
143 if post.replying_to != none {
144 replying_to_post = app.get_post_by_id(post.replying_to) or { app.get_unknown_post() }
145 replying_to_user = app.get_user_by_id(replying_to_post.author_id) or {
146 app.get_unknown_user()
147 }
148 }
149
150 return $veb.html('../templates/post.html')
151}
152
153@['/post/:post_id/edit']
154fn (mut app App) edit(mut ctx Context, post_id int) veb.Result {
155 user := app.whoami(mut ctx) or {
156 ctx.error('not logged in')
157 return ctx.redirect('/login')
158 }
159 post := app.get_post_by_id(post_id) or {
160 ctx.error('no such post')
161 return ctx.redirect('/')
162 }
163 if post.author_id != user.id {
164 ctx.error('insufficient permissions')
165 return ctx.redirect('/post/${post_id}')
166 }
167 ctx.title = '${app.config.instance.name} - editing ${post.title}'
168 return $veb.html('../templates/edit.html')
169}
170
171@['/post/:post_id/reply']
172fn (mut app App) reply(mut ctx Context, post_id int) veb.Result {
173 user := app.whoami(mut ctx) or {
174 ctx.error('not logged in')
175 return ctx.redirect('/login')
176 }
177 post := app.get_post_by_id(post_id) or {
178 ctx.error('no such post')
179 return ctx.redirect('/')
180 }
181 ctx.title = '${app.config.instance.name} - reply to ${post.title}'
182 replying := true
183 replying_to := post_id
184 replying_to_user := app.get_user_by_id(post.author_id) or {
185 ctx.error('no such post')
186 return ctx.redirect('/')
187 }
188 return $veb.html('../templates/new_post.html')
189}
190
191@['/post/new']
192fn (mut app App) new(mut ctx Context) veb.Result {
193 user := app.whoami(mut ctx) or {
194 ctx.error('not logged in')
195 return ctx.redirect('/login')
196 }
197 ctx.title = '${app.config.instance.name} - new post'
198 replying := false
199 replying_to := 0
200 replying_to_user := User{}
201 return $veb.html('../templates/new_post.html')
202}
203
204@['/tag/:tag']
205fn (mut app App) tag(mut ctx Context, tag string) veb.Result {
206 user := app.whoami(mut ctx) or {
207 ctx.error('not logged in')
208 return ctx.redirect('/login')
209 }
210 ctx.title = '${app.config.instance.name} - #${tag}'
211 offset := 0
212 return $veb.html('../templates/tag.html')
213}
214
215@['/tag/:tag/:offset']
216fn (mut app App) tag_with_offset(mut ctx Context, tag string, offset int) veb.Result {
217 user := app.whoami(mut ctx) or {
218 ctx.error('not logged in')
219 return ctx.redirect('/login')
220 }
221 ctx.title = '${app.config.instance.name} - #${tag}'
222 return $veb.html('../templates/tag.html')
223}
224
225@['/search']
226fn (mut app App) search(mut ctx Context, q string, offset int) veb.Result {
227 user := app.whoami(mut ctx) or {
228 ctx.error('not logged in')
229 return ctx.redirect('/login')
230 }
231 ctx.title = '${app.config.instance.name} - search'
232 return $veb.html('../templates/search.html')
233}
234
235@['/about']
236fn (mut app App) about(mut ctx Context) veb.Result {
237 user := app.whoami(mut ctx) or {
238 if !app.config.instance.public_data {
239 ctx.error('not logged in')
240 return ctx.redirect('/login')
241 }
242 User{}
243 }
244 ctx.title = '${app.config.instance.name} - about'
245 return $veb.html('../templates/about.html')
246}