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 81 mut replying_to_post := app.get_unknown_post() 82 mut replying_to_user := app.get_unknown_user() 83 84 if post.replying_to != none { 85 replying_to_post = app.get_post_by_id(post.replying_to) or { 86 app.get_unknown_post() 87 } 88 replying_to_user = app.get_user_by_id(replying_to_post.author_id) or { 89 app.get_unknown_user() 90 } 91 } 92 93 return $veb.html() 94} 95 96@['/post/:post_id/edit'] 97fn (mut app App) edit(mut ctx Context, post_id int) veb.Result { 98 user := app.whoami(mut ctx) or { 99 ctx.error('not logged in') 100 return ctx.redirect('/login') 101 } 102 post := app.get_post_by_id(post_id) or { 103 ctx.error('no such post') 104 return ctx.redirect('/') 105 } 106 if post.author_id != user.id { 107 ctx.error('insufficient permissions') 108 return ctx.redirect('/post/${post_id}') 109 } 110 ctx.title = '${app.config.instance.name} - editing ${post.title}' 111 return $veb.html() 112} 113 114@['/post/:post_id/reply'] 115fn (mut app App) reply(mut ctx Context, post_id int) veb.Result { 116 user := app.whoami(mut ctx) or { 117 ctx.error('not logged in') 118 return ctx.redirect('/login') 119 } 120 post := app.get_post_by_id(post_id) or { 121 ctx.error('no such post') 122 return ctx.redirect('/') 123 } 124 ctx.title = '${app.config.instance.name} - reply to ${post.title}' 125 replying := true 126 replying_to := post_id 127 replying_to_user := app.get_user_by_id(post.author_id) or { 128 ctx.error('no such post') 129 return ctx.redirect('/') 130 } 131 return $veb.html('templates/new_post.html') 132} 133 134@['/post/new'] 135fn (mut app App) new(mut ctx Context) veb.Result { 136 user := app.whoami(mut ctx) or { 137 ctx.error('not logged in') 138 return ctx.redirect('/login') 139 } 140 ctx.title = '${app.config.instance.name} - new post' 141 replying := false 142 replying_to := 0 143 replying_to_user := User{} 144 return $veb.html('templates/new_post.html') 145}