a mini social media app for small communities
1module main
2
3import veb
4import entity { User }
5
6fn (mut app App) index(mut ctx Context) veb.Result {
7 ctx.title = app.config.instance.name
8 user := app.whoami(mut ctx) or { User{} }
9 recent_posts := app.get_recent_posts()
10 pinned_posts := app.get_pinned_posts()
11 motd := app.get_motd()
12 return $veb.html()
13}
14
15fn (mut app App) login(mut ctx Context) veb.Result {
16 ctx.title = 'login to ${app.config.instance.name}'
17 user := app.whoami(mut ctx) or { User{} }
18 return $veb.html()
19}
20
21fn (mut app App) register(mut ctx Context) veb.Result {
22 ctx.title = 'register for ${app.config.instance.name}'
23 user := app.whoami(mut ctx) or { User{} }
24 return $veb.html()
25}
26
27fn (mut app App) me(mut ctx Context) veb.Result {
28 user := app.whoami(mut ctx) or {
29 ctx.error('not logged in')
30 return ctx.redirect('/login')
31 }
32 ctx.title = '${app.config.instance.name} - ${user.get_name()}'
33 return ctx.redirect('/user/${user.username}')
34}
35
36fn (mut app App) admin(mut ctx Context) veb.Result {
37 ctx.title = '${app.config.instance.name} dashboard'
38 user := app.whoami(mut ctx) or { User{} }
39 return $veb.html()
40}
41
42@['/user/:username']
43fn (mut app App) user(mut ctx Context, username string) veb.Result {
44 user := app.whoami(mut ctx) or { User{} }
45 viewing := app.get_user_by_name(username) or {
46 ctx.error('user not found')
47 return ctx.redirect('/')
48 }
49 ctx.title = '${app.config.instance.name} - ${user.get_name()}'
50 return $veb.html()
51}
52
53@['/post/:post_id']
54fn (mut app App) post(mut ctx Context, post_id int) veb.Result {
55 post := app.get_post_by_id(post_id) or {
56 ctx.error('no such post')
57 return ctx.redirect('/')
58 }
59 ctx.title = '${app.config.instance.name} - ${post.title}'
60 user := app.whoami(mut ctx) or { User{} }
61 return $veb.html()
62}