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 return $veb.html('../templates/user.html')
116}
117
118@['/post/:post_id']
119fn (mut app App) post(mut ctx Context, post_id int) veb.Result {
120 if !app.config.instance.public_data {
121 _ := app.whoami(mut ctx) or {
122 ctx.error('not logged in')
123 return ctx.redirect('/login')
124 }
125 }
126
127 post := app.get_post_by_id(post_id) or {
128 ctx.error('no such post')
129 return ctx.redirect('/')
130 }
131 ctx.title = '${app.config.instance.name} - ${post.title}'
132 user := app.whoami(mut ctx) or { User{} }
133
134 mut replying_to_post := app.get_unknown_post()
135 mut replying_to_user := app.get_unknown_user()
136
137 if post.replying_to != none {
138 replying_to_post = app.get_post_by_id(post.replying_to) or { app.get_unknown_post() }
139 replying_to_user = app.get_user_by_id(replying_to_post.author_id) or {
140 app.get_unknown_user()
141 }
142 }
143
144 return $veb.html('../templates/post.html')
145}
146
147@['/post/:post_id/edit']
148fn (mut app App) edit(mut ctx Context, post_id int) veb.Result {
149 user := app.whoami(mut ctx) or {
150 ctx.error('not logged in')
151 return ctx.redirect('/login')
152 }
153 post := app.get_post_by_id(post_id) or {
154 ctx.error('no such post')
155 return ctx.redirect('/')
156 }
157 if post.author_id != user.id {
158 ctx.error('insufficient permissions')
159 return ctx.redirect('/post/${post_id}')
160 }
161 ctx.title = '${app.config.instance.name} - editing ${post.title}'
162 return $veb.html('../templates/edit.html')
163}
164
165@['/post/:post_id/reply']
166fn (mut app App) reply(mut ctx Context, post_id int) veb.Result {
167 user := app.whoami(mut ctx) or {
168 ctx.error('not logged in')
169 return ctx.redirect('/login')
170 }
171 post := app.get_post_by_id(post_id) or {
172 ctx.error('no such post')
173 return ctx.redirect('/')
174 }
175 ctx.title = '${app.config.instance.name} - reply to ${post.title}'
176 replying := true
177 replying_to := post_id
178 replying_to_user := app.get_user_by_id(post.author_id) or {
179 ctx.error('no such post')
180 return ctx.redirect('/')
181 }
182 return $veb.html('../templates/new_post.html')
183}
184
185@['/post/new']
186fn (mut app App) new(mut ctx Context) veb.Result {
187 user := app.whoami(mut ctx) or {
188 ctx.error('not logged in')
189 return ctx.redirect('/login')
190 }
191 ctx.title = '${app.config.instance.name} - new post'
192 replying := false
193 replying_to := 0
194 replying_to_user := User{}
195 return $veb.html('../templates/new_post.html')
196}
197
198@['/tag/:tag']
199fn (mut app App) tag(mut ctx Context, tag string) veb.Result {
200 user := app.whoami(mut ctx) or {
201 ctx.error('not logged in')
202 return ctx.redirect('/login')
203 }
204 ctx.title = '${app.config.instance.name} - #${tag}'
205 offset := 0
206 return $veb.html('../templates/tag.html')
207}
208
209@['/tag/:tag/:offset']
210fn (mut app App) tag_with_offset(mut ctx Context, tag string, offset int) veb.Result {
211 user := app.whoami(mut ctx) or {
212 ctx.error('not logged in')
213 return ctx.redirect('/login')
214 }
215 ctx.title = '${app.config.instance.name} - #${tag}'
216 return $veb.html('../templates/tag.html')
217}
218
219@['/search']
220fn (mut app App) search(mut ctx Context, q string, offset int) veb.Result {
221 user := app.whoami(mut ctx) or {
222 ctx.error('not logged in')
223 return ctx.redirect('/login')
224 }
225 ctx.title = '${app.config.instance.name} - search'
226 return $veb.html('../templates/search.html')
227}
228
229@['/about']
230fn (mut app App) about(mut ctx Context) veb.Result {
231 user := app.whoami(mut ctx) or { User{} }
232 ctx.title = '${app.config.instance.name} - about'
233 return $veb.html('../templates/about.html')
234}