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 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('../templates/index.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('../templates/login.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('../templates/register.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 36@['/me/saved'] 37fn (mut app App) me_saved(mut ctx Context) veb.Result { 38 user := app.whoami(mut ctx) or { 39 ctx.error('not logged in') 40 return ctx.redirect('/login') 41 } 42 ctx.title = '${app.config.instance.name} - saved posts' 43 posts := app.get_saved_posts_as_post_for(user.id) 44 return $veb.html('../templates/saved_posts.html') 45} 46 47@['/me/saved_for_later'] 48fn (mut app App) me_saved_for_later(mut ctx Context) veb.Result { 49 user := app.whoami(mut ctx) or { 50 ctx.error('not logged in') 51 return ctx.redirect('/login') 52 } 53 ctx.title = '${app.config.instance.name} - posts saved for later' 54 posts := app.get_saved_for_later_posts_as_post_for(user.id) 55 return $veb.html('../templates/saved_posts_for_later.html') 56} 57 58fn (mut app App) settings(mut ctx Context) veb.Result { 59 user := app.whoami(mut ctx) or { 60 ctx.error('not logged in') 61 return ctx.redirect('/login') 62 } 63 ctx.title = '${app.config.instance.name} - settings' 64 return $veb.html('../templates/settings.html') 65} 66 67fn (mut app App) admin(mut ctx Context) veb.Result { 68 ctx.title = '${app.config.instance.name} dashboard' 69 user := app.whoami(mut ctx) or { User{} } 70 return $veb.html('../templates/admin.html') 71} 72 73fn (mut app App) inbox(mut ctx Context) veb.Result { 74 user := app.whoami(mut ctx) or { 75 ctx.error('not logged in') 76 return ctx.redirect('/login') 77 } 78 ctx.title = '${app.config.instance.name} inbox' 79 notifications := app.get_notifications_for(user.id) 80 return $veb.html('../templates/inbox.html') 81} 82 83fn (mut app App) logout(mut ctx Context) veb.Result { 84 user := app.whoami(mut ctx) or { 85 ctx.error('not logged in') 86 return ctx.redirect('/login') 87 } 88 ctx.title = '${app.config.instance.name} logout' 89 return $veb.html('../templates/logout.html') 90} 91 92@['/user/:username'] 93fn (mut app App) user(mut ctx Context, username string) veb.Result { 94 user := app.whoami(mut ctx) or { User{} } 95 viewing := app.get_user_by_name(username) or { 96 ctx.error('user not found') 97 return ctx.redirect('/') 98 } 99 ctx.title = '${app.config.instance.name} - ${user.get_name()}' 100 posts := app.get_posts_from_user(viewing.id, 10) 101 return $veb.html('../templates/user.html') 102} 103 104@['/post/:post_id'] 105fn (mut app App) post(mut ctx Context, post_id int) veb.Result { 106 post := app.get_post_by_id(post_id) or { 107 ctx.error('no such post') 108 return ctx.redirect('/') 109 } 110 ctx.title = '${app.config.instance.name} - ${post.title}' 111 user := app.whoami(mut ctx) or { User{} } 112 113 mut replying_to_post := app.get_unknown_post() 114 mut replying_to_user := app.get_unknown_user() 115 116 if post.replying_to != none { 117 replying_to_post = app.get_post_by_id(post.replying_to) or { 118 app.get_unknown_post() 119 } 120 replying_to_user = app.get_user_by_id(replying_to_post.author_id) or { 121 app.get_unknown_user() 122 } 123 } 124 125 return $veb.html('../templates/post.html') 126} 127 128@['/post/:post_id/edit'] 129fn (mut app App) edit(mut ctx Context, post_id int) veb.Result { 130 user := app.whoami(mut ctx) or { 131 ctx.error('not logged in') 132 return ctx.redirect('/login') 133 } 134 post := app.get_post_by_id(post_id) or { 135 ctx.error('no such post') 136 return ctx.redirect('/') 137 } 138 if post.author_id != user.id { 139 ctx.error('insufficient permissions') 140 return ctx.redirect('/post/${post_id}') 141 } 142 ctx.title = '${app.config.instance.name} - editing ${post.title}' 143 return $veb.html('../templates/edit.html') 144} 145 146@['/post/:post_id/reply'] 147fn (mut app App) reply(mut ctx Context, post_id int) veb.Result { 148 user := app.whoami(mut ctx) or { 149 ctx.error('not logged in') 150 return ctx.redirect('/login') 151 } 152 post := app.get_post_by_id(post_id) or { 153 ctx.error('no such post') 154 return ctx.redirect('/') 155 } 156 ctx.title = '${app.config.instance.name} - reply to ${post.title}' 157 replying := true 158 replying_to := post_id 159 replying_to_user := app.get_user_by_id(post.author_id) or { 160 ctx.error('no such post') 161 return ctx.redirect('/') 162 } 163 return $veb.html('../templates/new_post.html') 164} 165 166@['/post/new'] 167fn (mut app App) new(mut ctx Context) veb.Result { 168 user := app.whoami(mut ctx) or { 169 ctx.error('not logged in') 170 return ctx.redirect('/login') 171 } 172 ctx.title = '${app.config.instance.name} - new post' 173 replying := false 174 replying_to := 0 175 replying_to_user := User{} 176 return $veb.html('../templates/new_post.html') 177} 178 179@['/tag/:tag'] 180fn (mut app App) tag(mut ctx Context, tag string) veb.Result { 181 user := app.whoami(mut ctx) or { 182 ctx.error('not logged in') 183 return ctx.redirect('/login') 184 } 185 ctx.title = '${app.config.instance.name} - #${tag}' 186 offset := 0 187 return $veb.html('../templates/tag.html') 188} 189 190@['/tag/:tag/:offset'] 191fn (mut app App) tag_with_offset(mut ctx Context, tag string, offset int) veb.Result { 192 user := app.whoami(mut ctx) or { 193 ctx.error('not logged in') 194 return ctx.redirect('/login') 195 } 196 ctx.title = '${app.config.instance.name} - #${tag}' 197 return $veb.html('../templates/tag.html') 198} 199 200@['/search'] 201fn (mut app App) search(mut ctx Context, q string, offset int) veb.Result { 202 user := app.whoami(mut ctx) or { 203 ctx.error('not logged in') 204 return ctx.redirect('/login') 205 } 206 ctx.title = '${app.config.instance.name} - search' 207 return $veb.html('../templates/search.html') 208} 209 210@['/about'] 211fn (mut app App) about(mut ctx Context) veb.Result { 212 user := app.whoami(mut ctx) or { User{} } 213 ctx.title = '${app.config.instance.name} - about' 214 return $veb.html('../templates/about.html') 215}