diff --git a/highlight/java.v b/highlight/java.v index a66da95..d9894bc 100644 --- a/highlight/java.v +++ b/highlight/java.v @@ -3,13 +3,13 @@ module highlight fn init_java() Lang { return Lang{ - name: 'Java' + name: 'Java' lang_extensions: ['java'] - line_comments: '//' - mline_comments: ['/*', '*/'] - string_start: ['"', "'"] - color: '#f1e05a' - keywords: [ + line_comments: '//' + mline_comments: ['/*', '*/'] + string_start: ['"', "'"] + color: '#f1e05a' + keywords: [ 'abstract', 'continue', 'for', diff --git a/src/admin_routes.v b/src/admin_routes.v index 350d128..86d5811 100644 --- a/src/admin_routes.v +++ b/src/admin_routes.v @@ -7,7 +7,7 @@ const admin_users_per_page = 30 // TODO move to admin controller @['/admin/settings'] -pub fn (mut app App) admin_settings() veb.Result { +pub fn (mut app App) admin_settings(mut ctx Context) veb.Result { if !ctx.is_admin() { return ctx.redirect_to_index() } @@ -42,12 +42,12 @@ pub fn (mut app App) handle_admin_edit_user(user_id string) veb.Result { } @['/admin/users'] -pub fn (mut app App) admin_users_default() veb.Result { - return app.admin_users(0) +pub fn (mut app App) admin_users_default(mut ctx Context) veb.Result { + return app.admin_users(mut ctx, 0) } @['/admin/users/:page'] -pub fn (mut app App) admin_users(page int) veb.Result { +pub fn (mut app App) admin_users(mut ctx Context, page int) veb.Result { if !ctx.is_admin() { return ctx.redirect_to_index() } diff --git a/src/comment.v b/src/comment.v index a424842..b2641e4 100644 --- a/src/comment.v +++ b/src/comment.v @@ -24,11 +24,11 @@ pub fn (mut app App) handle_add_comment(username string, repo_name string) veb.R is_issue_id_empty := validation.is_string_empty(issue_id) if is_text_empty || is_issue_id_empty || !ctx.logged_in { ctx.error('Issue comment is not valid') - return app.issue(username, repo_name, issue_id) + return app.issue(mut ctx, username, repo_name, issue_id) } app.add_issue_comment(ctx.user.id, issue_id.int(), text) or { ctx.error('There was an error while inserting the comment') - return app.issue(username, repo_name, issue_id) + return app.issue(mut ctx, username, repo_name, issue_id) } // TODO: count comments app.increment_issue_comments(issue_id.int()) or { app.info(err.str()) } diff --git a/src/feed_routes.v b/src/feed_routes.v index ae3233d..03ff040 100644 --- a/src/feed_routes.v +++ b/src/feed_routes.v @@ -3,12 +3,12 @@ module main import veb @['/:username/feed'] -pub fn (mut app App) user_feed_default(username string) veb.Result { - return app.user_feed(username, 0) +pub fn (mut app App) user_feed_default(mut ctx Context, username string) veb.Result { + return app.user_feed(mut ctx, username, 0) } @['/:username/feed/:page'] -pub fn (mut app App) user_feed(username string, page int) veb.Result { +pub fn (mut app App) user_feed(mut ctx Context, username string, page int) veb.Result { exists, user := app.check_username(username) if !exists || ctx.user.username != user.username { diff --git a/src/gitly.v b/src/gitly.v index 4adab95..f0ff38d 100644 --- a/src/gitly.v +++ b/src/gitly.v @@ -128,12 +128,12 @@ pub fn (mut app App) init_server() { } pub fn (mut app App) before_request(mut ctx Context) { - ctx.logged_in = app.is_logged_in() + ctx.logged_in = app.is_logged_in(mut ctx) app.load_settings() if ctx.logged_in { - ctx.user = app.get_user_from_cookies() or { + ctx.user = app.get_user_from_cookies(ctx) or { ctx.logged_in = false User{} } diff --git a/src/issue_routes.v b/src/issue_routes.v index 823fd41..94c9622 100644 --- a/src/issue_routes.v +++ b/src/issue_routes.v @@ -39,12 +39,12 @@ pub fn (mut app App) new_issue(username string, repo_name string) veb.Result { } @['/:username/issues'] -pub fn (mut app App) handle_get_user_issues(username string) veb.Result { - return app.user_issues(username, 0) +pub fn (mut app App) handle_get_user_issues(mut ctx Context, username string) veb.Result { + return app.user_issues(mut ctx, username, 0) } @['/:username/:repo_name/issues'; post] -pub fn (mut app App) handle_add_repo_issue(username string, repo_name string) veb.Result { +pub fn (mut app App) handle_add_repo_issue(mut ctx Context, username string, repo_name string) veb.Result { // TODO: use captcha instead of user restrictions if !ctx.logged_in || (ctx.logged_in && ctx.user.posts_count >= posts_per_day) { return ctx.redirect_to_index() @@ -68,12 +68,12 @@ pub fn (mut app App) handle_add_repo_issue(username string, repo_name string) ve } @['/:username/:repo_name/issues'] -pub fn (mut app App) handle_get_repo_issues(username string, repo_name string) veb.Result { - return app.issues(username, repo_name, 0) +pub fn (mut app App) handle_get_repo_issues(mut ctx Context, username string, repo_name string) veb.Result { + return app.issues(mut ctx, username, repo_name, 0) } @['/:username/:repo_name/issues/:page'] -pub fn (mut app App) issues(username string, repo_name string, page int) veb.Result { +pub fn (mut app App) issues(mut ctx Context, username string, repo_name string, page int) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } mut issues_with_users := []IssueWithUser{} for issue in app.find_repo_issues_as_page(repo.id, page) { @@ -105,7 +105,7 @@ pub fn (mut app App) issues(username string, repo_name string, page int) veb.Res } @['/:username/:repo_name/issue/:id'] -pub fn (mut app App) issue(username string, repo_name string, id string) veb.Result { +pub fn (mut app App) issue(mut ctx Context, username string, repo_name string, id string) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() } issue_author := app.get_user_by_id(issue.author_id) or { return ctx.not_found() } @@ -121,7 +121,7 @@ pub fn (mut app App) issue(username string, repo_name string, id string) veb.Res } @['/:username/issues/:page'] -pub fn (mut app App) user_issues(username string, page int) veb.Result { +pub fn (mut app App) user_issues(mut ctx Context, username string, page int) veb.Result { if !ctx.logged_in { return ctx.not_found() } diff --git a/src/main.v b/src/main.v index ac4f69e..f38b405 100644 --- a/src/main.v +++ b/src/main.v @@ -1,7 +1,11 @@ import os import veb -const http_port = os.getenv_opt('GITLY_PORT') or { '8080' }.int() +const http_port = get_port() + +fn get_port() int { + return os.getenv_opt('GITLY_PORT') or { '8080' }.int() +} fn main() { if os.args.contains('ci_run') { diff --git a/src/release_routes.v b/src/release_routes.v index a5f7b92..28d55af 100644 --- a/src/release_routes.v +++ b/src/release_routes.v @@ -6,12 +6,12 @@ import time const releases_per_page = 20 @['/:username/:repo_name/releases'] -pub fn (mut app App) releases_default(username string, repo_name string) veb.Result { - return app.releases(username, repo_name, 0) +pub fn (mut app App) releases_default(mut ctx Context, username string, repo_name string) veb.Result { + return app.releases(mut ctx, username, repo_name, 0) } @['/:username/:repo_name/releases/:page'] -pub fn (mut app App) releases(username string, repo_name string, page int) veb.Result { +pub fn (mut app App) releases(mut ctx Context, username string, repo_name string, page int) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } repo_id := repo.id diff --git a/src/repo_routes.v b/src/repo_routes.v index ca77275..a6f9100 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -86,7 +86,7 @@ pub fn (mut app App) handle_repo_delete(username string, repo_name string) veb.R spawn app.delete_repository(repo.id, repo.git_dir, repo.name) } else { ctx.error('Verification failed') - return app.repo_settings(username, repo_name, mut ctx) + return app.repo_settings(mut ctx, username, repo_name) } return ctx.redirect_to_index() @@ -106,64 +106,64 @@ pub fn (mut app App) handle_repo_move(username string, repo_name string, dest st if dest != '' && verify == '${username}/${repo_name}' { dest_user := app.get_user_by_username(dest) or { ctx.error('Unknown user ${dest}') - return app.repo_settings(username, repo_name, mut ctx) + return app.repo_settings(mut ctx, username, repo_name) } if app.user_has_repo(dest_user.id, repo.name) { ctx.error('User already owns repo ${repo.name}') - return app.repo_settings(username, repo_name, mut ctx) + return app.repo_settings(mut ctx, username, repo_name) } if app.get_count_user_repos(dest_user.id) >= max_user_repos { ctx.error('User already reached the repo limit') - return app.repo_settings(username, repo_name, mut ctx) + return app.repo_settings(mut ctx, username, repo_name) } app.move_repo_to_user(repo.id, dest_user.id, dest_user.username) or { ctx.error('There was an error while moving the repo') - return app.repo_settings(username, repo_name, mut ctx) + return app.repo_settings(mut ctx, username, repo_name) } return ctx.redirect('/${dest_user.username}/${repo.name}') } else { ctx.error('Verification failed') - return app.repo_settings(username, repo_name, mut ctx) + return app.repo_settings(mut ctx, username, repo_name) } return ctx.redirect_to_index() } @['/:username/:repo_name'] -pub fn (mut app App) handle_tree(username string, repo_name string) veb.Result { +pub fn (mut app App) handle_tree(mut ctx Context, username string, repo_name string) veb.Result { println('handle tree()') match repo_name { 'repos' { - return app.user_repos(username, mut ctx) + return app.user_repos(mut ctx, username) } 'issues' { - return app.handle_get_user_issues(username, mut ctx) + return app.handle_get_user_issues(mut ctx, username) } 'settings' { - return app.user_settings(username) + return app.user_settings(mut ctx, username) } else {} } repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } - return app.tree(username, repo_name, repo.primary_branch, '') + return app.tree(mut ctx, username, repo_name, repo.primary_branch, '') } @['/:username/:repo_name/tree/:branch_name'] -pub fn (mut app App) handle_branch_tree(username string, repo_name string, branch_name string) veb.Result { +pub fn (mut app App) handle_branch_tree(mut ctx Context, username string, repo_name string, branch_name string) veb.Result { app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } - return app.tree(username, repo_name, branch_name, '') + return app.tree(mut ctx, username, repo_name, branch_name, '') } @['/:username/:repo_name/update'] -pub fn (mut app App) handle_repo_update(username string, repo_name string) veb.Result { +pub fn (mut app App) handle_repo_update(mut ctx Context, username string, repo_name string) veb.Result { mut repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } @@ -185,7 +185,7 @@ pub fn (mut app App) new() veb.Result { } @['/new'; post] -pub fn (mut app App) handle_new_repo(name string, clone_url string, description string, no_redirect string) veb.Result { +pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url string, description string, no_redirect string) veb.Result { mut valid_clone_url := clone_url is_clone_url_empty := validation.is_string_empty(clone_url) is_public := ctx.form['repo_visibility'] == 'public' @@ -194,24 +194,24 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description } if !ctx.is_admin() && app.get_count_user_repos(ctx.user.id) >= max_user_repos { ctx.error('You have reached the limit for the number of repositories') - return app.new() + return app.new(mut ctx) } if name.len > max_repo_name_len { ctx.error('The repository name is too long (should be fewer than ${max_repo_name_len} characters)') - return app.new() + return app.new(mut ctx) } if _ := app.find_repo_by_name_and_username(name, ctx.user.username) { ctx.error('A repository with the name "${name}" already exists') - return app.new() + return app.new(mut ctx) } if name.contains(' ') { ctx.error('Repository name cannot contain spaces') - return app.new() + return app.new(mut ctx) } is_repo_name_valid := validation.is_repository_name_valid(name) if !is_repo_name_valid { ctx.error('The repository name is not valid') - return app.new() + return app.new(mut ctx) } has_clone_url_https_prefix := clone_url.starts_with('https://') if !is_clone_url_empty { @@ -221,7 +221,7 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description is_git_repo := git.check_git_repo_url(valid_clone_url) if !is_git_repo { ctx.error('The repository URL does not contain any git repository or the server does not respond') - return app.new() + return app.new(mut ctx) } } repo_path := os.join_path(app.config.repo_storage_path, ctx.user.username, name) @@ -249,7 +249,7 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description } app.add_repo(new_repo) or { ctx.error('There was an error while adding the repo') - return app.new() + return app.new(mut ctx) } new_repo2 := app.find_repo_by_name_and_user_id(new_repo.name, ctx.user.id) or { app.info('Repo was not inserted') @@ -259,15 +259,15 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description primary_branch := git.get_repository_primary_branch(repo_path) app.update_repo_primary_branch(repo_id, primary_branch) or { ctx.error('There was an error while adding the repo') - return app.new() + return app.new(mut ctx) } - app.find_repo_by_id(repo_id) or { return app.new() } + app.find_repo_by_id(repo_id) or { return app.new(mut ctx) } // Update only cloned repositories /* if !is_clone_url_empty { app.update_repo_from_fs(mut new_repo) or { ctx.error('There was an error while cloning the repo') - return app.new() + return app.new(mut ctx) } } */ @@ -289,7 +289,7 @@ pub fn (mut app App) foo(mut new_repo Repo) { } @['/:username/:repo_name/tree/:branch_name/:path...'] -pub fn (mut app App) tree(username string, repo_name string, branch_name string, path string) veb.Result { +pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, branch_name string, path string) veb.Result { mut repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } @@ -431,7 +431,7 @@ pub fn (mut app App) tree(username string, repo_name string, branch_name string, } @['/api/v1/repos/:repo_id/star'; 'post'] -pub fn (mut app App) handle_api_repo_star(repo_id_str string) veb.Result { +pub fn (mut app App) handle_api_repo_star(mut ctx Context, repo_id_str string) veb.Result { repo_id := repo_id_str.int() has_access := app.has_user_repo_read_access(ctx, ctx.user.id, repo_id) @@ -450,7 +450,7 @@ pub fn (mut app App) handle_api_repo_star(repo_id_str string) veb.Result { } @['/api/v1/repos/:repo_id/watch'; 'post'] -pub fn (mut app App) handle_api_repo_watch(repo_id_str string) veb.Result { +pub fn (mut app App) handle_api_repo_watch(mut ctx Context, repo_id_str string) veb.Result { repo_id := repo_id_str.int() has_access := app.has_user_repo_read_access(ctx, ctx.user.id, repo_id) @@ -469,7 +469,7 @@ pub fn (mut app App) handle_api_repo_watch(repo_id_str string) veb.Result { } @['/:username/:repo_name/contributors'] -pub fn (mut app App) contributors(username string, repo_name string) veb.Result { +pub fn (mut app App) contributors(mut ctx Context, username string, repo_name string) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } contributors := app.find_repo_registered_contributor(repo.id) @@ -478,7 +478,7 @@ pub fn (mut app App) contributors(username string, repo_name string) veb.Result } @['/:username/:repo_name/blob/:branch_name/:path...'] -pub fn (mut app App) blob(username string, repo_name string, branch_name string, path string) veb.Result { +pub fn (mut app App) blob(mut ctx Context, username string, repo_name string, branch_name string, path string) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } mut path_parts := path.split('/') @@ -505,7 +505,7 @@ pub fn (mut app App) blob(username string, repo_name string, branch_name string, } @['/:user/:repository/raw/:branch_name/:path...'] -pub fn (mut app App) handle_raw(username string, repo_name string, branch_name string, path string) veb.Result { +pub fn (mut app App) handle_raw(mut ctx Context, username string, repo_name string, branch_name string, path string) veb.Result { user := app.get_user_by_username(username) or { return ctx.not_found() } repo := app.find_repo_by_name_and_user_id(repo_name, user.id) or { return ctx.not_found() } diff --git a/src/ssh_key_routes.v b/src/ssh_key_routes.v index 619207a..04c84c8 100644 --- a/src/ssh_key_routes.v +++ b/src/ssh_key_routes.v @@ -5,7 +5,7 @@ import validation import api @['/:username/settings/ssh-keys'] -pub fn (mut app App) user_ssh_keys_list(username string) veb.Result { +pub fn (mut app App) user_ssh_keys_list(mut ctx Context, username string) veb.Result { is_users_settings := username == ctx.user.username if !ctx.logged_in || !is_users_settings { @@ -18,7 +18,7 @@ pub fn (mut app App) user_ssh_keys_list(username string) veb.Result { } @['/:username/settings/ssh-keys'; 'post'] -pub fn (mut app App) handle_add_ssh_key(username string) veb.Result { +pub fn (mut app App) handle_add_ssh_key(mut ctx Context, username string) veb.Result { is_users_settings := username == ctx.user.username if !ctx.logged_in || !is_users_settings { @@ -34,26 +34,26 @@ pub fn (mut app App) handle_add_ssh_key(username string) veb.Result { if is_title_empty { ctx.error('Title is empty') - return app.user_ssh_keys_new(username) + return app.user_ssh_keys_new(mut ctx, username) } if is_ssh_key_empty { ctx.error('SSH key is empty') - return app.user_ssh_keys_new(username) + return app.user_ssh_keys_new(mut ctx, username) } app.add_ssh_key(ctx.user.id, title, ssh_key) or { ctx.error(err.str()) - return app.user_ssh_keys_new(username) + return app.user_ssh_keys_new(mut ctx, username) } return ctx.redirect('/${username}/settings/ssh-keys') } @['/:username/settings/ssh-keys/:id'; 'delete'] -pub fn (mut app App) handle_remove_ssh_key(username string, id int) veb.Result { +pub fn (mut app App) handle_remove_ssh_key(mut ctx Context, username string, id int) veb.Result { is_users_settings := username == ctx.user.username if !ctx.logged_in || !is_users_settings { @@ -72,7 +72,7 @@ pub fn (mut app App) handle_remove_ssh_key(username string, id int) veb.Result { } @['/:username/settings/ssh-keys/new'] -pub fn (mut app App) user_ssh_keys_new(username string) veb.Result { +pub fn (mut app App) user_ssh_keys_new(mut ctx Context, username string) veb.Result { is_users_settings := username == ctx.user.username if !ctx.logged_in || !is_users_settings { diff --git a/src/tag_routes.v b/src/tag_routes.v index 7a2416d..abd3304 100644 --- a/src/tag_routes.v +++ b/src/tag_routes.v @@ -22,5 +22,5 @@ pub fn (mut app App) handle_download_tag_archive(username string, repo_name stri archive_content := os.read_file(archive_path) or { return ctx.not_found() } - return app.send_file(snapshot_name, archive_content) + return app.send_file(mut ctx, snapshot_name, archive_content) } diff --git a/src/user.v b/src/user.v index 9b07791..d721564 100644 --- a/src/user.v +++ b/src/user.v @@ -391,12 +391,12 @@ pub fn (mut app App) auth_user(mut ctx Context, user User, ip string) ! { ctx.set_cookie(name: 'token', value: token, expires: expire_date) } -pub fn (mut app App) is_logged_in(ctx &Context) bool { +pub fn (mut app App) is_logged_in(mut ctx Context) bool { token_cookie := ctx.get_cookie('token') or { return false } token := app.get_token(token_cookie) or { return false } is_user_blocked := app.check_user_blocked(token.user_id) if is_user_blocked { - app.handle_logout() + app.handle_logout(mut ctx) return false } return true diff --git a/src/user_routes.v b/src/user_routes.v index 58870ba..3bffdd6 100644 --- a/src/user_routes.v +++ b/src/user_routes.v @@ -7,11 +7,11 @@ import rand import validation import api -pub fn (mut app App) login() veb.Result { +pub fn (mut app App) login(mut ctx Context) veb.Result { csrf := rand.string(30) ctx.set_cookie(name: 'csrf', value: csrf) - if app.is_logged_in() { + if app.is_logged_in(mut ctx) { return ctx.not_found() } @@ -19,7 +19,7 @@ pub fn (mut app App) login() veb.Result { } @['/login'; post] -pub fn (mut app App) handle_login(username string, password string) veb.Result { +pub fn (mut app App) handle_login(mut ctx Context, username string, password string) veb.Result { if username == '' || password == '' { return ctx.redirect_to_login() } @@ -30,34 +30,34 @@ pub fn (mut app App) handle_login(username string, password string) veb.Result { if !compare_password_with_hash(password, user.salt, user.password) { app.increment_user_login_attempts(user.id) or { ctx.error('There was an error while logging in') - return app.login() + return app.login(mut ctx) } if user.login_attempts == max_login_attempts { app.warn('User ${user.username} got blocked') app.block_user(user.id) or { app.info(err.str()) } } ctx.error('Wrong username/password') - return app.login() + return app.login(mut ctx) } if !user.is_registered { return ctx.redirect_to_login() } app.auth_user(mut ctx, user, ctx.ip()) or { ctx.error('There was an error while logging in') - return app.login() + return app.login(mut ctx) } app.add_security_log(user_id: user.id, kind: .logged_in) or { app.info(err.str()) } return ctx.redirect('/${username}') } @['/logout'] -pub fn (mut app App) handle_logout() veb.Result { +pub fn (mut app App) handle_logout(mut ctx Context) veb.Result { ctx.set_cookie(name: 'token', value: '') return ctx.redirect_to_index() } @['/:username'] -pub fn (mut app App) user(username string) veb.Result { +pub fn (mut app App) user(mut ctx Context, username string) veb.Result { exists, user := app.check_username(username) if !exists { return ctx.not_found() @@ -73,7 +73,7 @@ pub fn (mut app App) user(username string) veb.Result { } @['/:username/settings'] -pub fn (mut app App) user_settings(username string) veb.Result { +pub fn (mut app App) user_settings(mut ctx Context, username string) veb.Result { is_users_settings := username == ctx.user.username if !ctx.logged_in || !is_users_settings { @@ -84,7 +84,7 @@ pub fn (mut app App) user_settings(username string) veb.Result { } @['/:username/settings'; post] -pub fn (mut app App) handle_update_user_settings(username string) veb.Result { +pub fn (mut app App) handle_update_user_settings(mut ctx Context, username string) veb.Result { is_users_settings := username == ctx.user.username if !ctx.logged_in || !is_users_settings { @@ -100,13 +100,13 @@ pub fn (mut app App) handle_update_user_settings(username string) veb.Result { if is_username_empty { ctx.error('New name is empty') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } if ctx.user.namechanges_count > max_namechanges { ctx.error('You can not change your username, limit reached') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } is_username_valid := validation.is_username_valid(new_username) @@ -114,7 +114,7 @@ pub fn (mut app App) handle_update_user_settings(username string) veb.Result { if !is_username_valid { ctx.error('New username is not valid') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } is_first_namechange := ctx.user.last_namechange_time == 0 @@ -123,7 +123,7 @@ pub fn (mut app App) handle_update_user_settings(username string) veb.Result { if !(is_first_namechange || can_change_usernane) { ctx.error('You need to wait until you can change the name again') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } is_new_username := new_username != username @@ -132,7 +132,7 @@ pub fn (mut app App) handle_update_user_settings(username string) veb.Result { if is_new_full_name { app.change_full_name(ctx.user.id, full_name) or { ctx.error('There was an error while updating the settings') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } } @@ -142,16 +142,16 @@ pub fn (mut app App) handle_update_user_settings(username string) veb.Result { if user.id != 0 { ctx.error('Name already exists') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } app.change_username(ctx.user.id, new_username) or { ctx.error('There was an error while updating the settings') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } app.incement_namechanges(ctx.user.id) or { ctx.error('There was an error while updating the settings') - return app.user_settings(username) + return app.user_settings(mut ctx, username) } app.rename_user_directory(username, new_username) } @@ -165,7 +165,7 @@ fn (mut app App) rename_user_directory(old_name string, new_name string) { } } -pub fn (mut app App) register() veb.Result { +pub fn (mut app App) register(mut ctx Context) veb.Result { user_count := app.get_users_count() or { 0 } no_users := user_count == 0 @@ -175,39 +175,39 @@ pub fn (mut app App) register() veb.Result { } @['/register'; post] -pub fn (mut app App) handle_register(username string, email string, password string, no_redirect string) veb.Result { +pub fn (mut app App) handle_register(mut ctx Context, username string, email string, password string, no_redirect string) veb.Result { user_count := app.get_users_count() or { ctx.error('Failed to register') - return app.register() + return app.register(mut ctx) } no_users := user_count == 0 if username in ['login', 'register', 'new', 'new_post', 'oauth'] { ctx.error('Username `${username}` is not available') - return app.register() + return app.register(mut ctx) } user_chars := username.bytes() if user_chars.len > max_username_len { ctx.error('Username is too long (max. ${max_username_len})') - return app.register() + return app.register(mut ctx) } if username.contains('--') { ctx.error('Username cannot contain two hyphens') - return app.register() + return app.register(mut ctx) } if user_chars[0] == `-` || user_chars.last() == `-` { ctx.error('Username cannot begin or end with a hyphen') - return app.register() + return app.register(mut ctx) } for ch in user_chars { if !ch.is_letter() && !ch.is_digit() && ch != `-` { ctx.error('Username cannot contain special characters') - return app.register() + return app.register(mut ctx) } } @@ -216,13 +216,13 @@ pub fn (mut app App) handle_register(username string, email string, password str if !is_username_valid { ctx.error('Username is not valid') - return app.register() + return app.register(mut ctx) } if password == '' { ctx.error('Password cannot be empty') - return app.register() + return app.register(mut ctx) } salt := generate_salt() @@ -230,24 +230,24 @@ pub fn (mut app App) handle_register(username string, email string, password str if username == '' || email == '' { ctx.error('Username or Email cannot be emtpy') - return app.register() + return app.register(mut ctx) } // TODO: refactor is_registered := app.register_user(username, hashed_password, salt, [email], false, no_users) or { ctx.error('Failed to register') - return app.register() + return app.register(mut ctx) } if !is_registered { ctx.error('Failed to register') - return app.register() + return app.register(mut ctx) } user := app.get_user_by_username(username) or { ctx.error('User already exists') - return app.register() + return app.register(mut ctx) } if no_users { @@ -258,7 +258,7 @@ pub fn (mut app App) handle_register(username string, email string, password str app.auth_user(mut ctx, user, client_ip) or { ctx.error('Failed to register') - return app.register() + return app.register(mut ctx) } app.add_security_log(user_id: user.id, kind: .registered) or { app.info(err.str()) } @@ -270,7 +270,7 @@ pub fn (mut app App) handle_register(username string, email string, password str } @['/api/v1/users/avatar'; post] -pub fn (mut app App) handle_upload_avatar() veb.Result { +pub fn (mut app App) handle_upload_avatar(mut ctx Context) veb.Result { if !ctx.logged_in { return ctx.not_found() } diff --git a/tests/first_run.v b/tests/first_run.v index 8291fc1..1f33d32 100644 --- a/tests/first_run.v +++ b/tests/first_run.v @@ -192,7 +192,7 @@ fn test_login_with_token(username string, token string) { cookies: { 'token': token } - url: prepare_url(username) + url: prepare_url(username) ) or { exit_with_message(err.str()) } ilog('Ensure that after login, there is a signed in as `${username}` message') @@ -210,8 +210,8 @@ fn test_create_repo(token string, name string, clone_url string) { cookies: { 'token': token } - url: prepare_url('new') - data: 'name=${name}&description=${description}&clone_url=${clone_url}&repo_visibility=${repo_visibility}&no_redirect=1' + url: prepare_url('new') + data: 'name=${name}&description=${description}&clone_url=${clone_url}&repo_visibility=${repo_visibility}&no_redirect=1' ) or { exit_with_message(err.str()) } assert response.status_code == 200 @@ -224,7 +224,7 @@ fn get_repo_commit_count(token string, username string, repo_name string, branch cookies: { 'token': token } - url: prepare_url('api/v1/${username}/${repo_name}/${branch_name}/commits/count') + url: prepare_url('api/v1/${username}/${repo_name}/${branch_name}/commits/count') ) or { exit_with_message(err.str()) } response_json := json.decode(api.ApiCommitCount, response.body) or { @@ -240,7 +240,7 @@ fn get_repo_issue_count(token string, username string, repo_name string) int { cookies: { 'token': token } - url: prepare_url('api/v1/${username}/${repo_name}/issues/count') + url: prepare_url('api/v1/${username}/${repo_name}/issues/count') ) or { exit_with_message(err.str()) } response_json := json.decode(api.ApiIssueCount, response.body) or { @@ -256,7 +256,7 @@ fn get_repo_branch_count(token string, username string, repo_name string) int { cookies: { 'token': token } - url: prepare_url('api/v1/${username}/${repo_name}/branches/count') + url: prepare_url('api/v1/${username}/${repo_name}/branches/count') ) or { exit_with_message(err.str()) } response_json := json.decode(api.ApiBranchCount, response.body) or {