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) settings(mut ctx Context) veb.Result {
37 user := app.whoami(mut ctx) or {
38 ctx.error('not logged in')
39 return ctx.redirect('/login')
40 }
41 ctx.title = '${app.config.instance.name} - settings'
42 return $veb.html()
43}
44
45fn (mut app App) admin(mut ctx Context) veb.Result {
46 ctx.title = '${app.config.instance.name} dashboard'
47 user := app.whoami(mut ctx) or { User{} }
48 return $veb.html()
49}
50
51fn (mut app App) inbox(mut ctx Context) veb.Result {
52 user := app.whoami(mut ctx) or {
53 ctx.error('not logged in')
54 return ctx.redirect('/login')
55 }
56 ctx.title = '${app.config.instance.name} inbox'
57 notifications := app.get_notifications_for(user.id)
58 return $veb.html()
59}
60
61@['/user/:username']
62fn (mut app App) user(mut ctx Context, username string) veb.Result {
63 user := app.whoami(mut ctx) or { User{} }
64 viewing := app.get_user_by_name(username) or {
65 ctx.error('user not found')
66 return ctx.redirect('/')
67 }
68 ctx.title = '${app.config.instance.name} - ${user.get_name()}'
69 return $veb.html()
70}
71
72@['/post/:post_id']
73fn (mut app App) post(mut ctx Context, post_id int) veb.Result {
74 post := app.get_post_by_id(post_id) or {
75 ctx.error('no such post')
76 return ctx.redirect('/')
77 }
78 ctx.title = '${app.config.instance.name} - ${post.title}'
79 user := app.whoami(mut ctx) or { User{} }
80 return $veb.html()
81}
82
83@['/post/:post_id/edit']
84fn (mut app App) edit(mut ctx Context, post_id int) veb.Result {
85 user := app.whoami(mut ctx) or {
86 ctx.error('not logged in')
87 return ctx.redirect('/login')
88 }
89 post := app.get_post_by_id(post_id) or {
90 ctx.error('no such post')
91 return ctx.redirect('/')
92 }
93 if post.author_id != user.id {
94 ctx.error('insufficient permissions')
95 return ctx.redirect('/post/${post_id}')
96 }
97 ctx.title = '${app.config.instance.name} - editing ${post.title}'
98 return $veb.html()
99}