From 1a1eb58d094e7579681ded151ae50afaa799b0b3 Mon Sep 17 00:00:00 2001 From: walking devel <104449470+walkingdevel@users.noreply.github.com> Date: Wed, 12 Apr 2023 03:40:40 +0000 Subject: [PATCH] add orm errors handling (#243) --- src/activity_service.v | 8 +- src/admin_routes.v | 4 +- src/admin_service.v | 32 ++++---- src/branch_service.v | 51 ++++++++----- src/cli.v | 4 +- src/comment_routes.v | 8 +- src/comment_service.v | 8 +- src/commit_service.v | 24 +++--- src/file_service.v | 22 +++--- src/git_routes.v | 6 +- src/github_routes.v | 22 ++++-- src/gitly.v | 49 ++++++------ src/issue_routes.v | 10 +-- src/issue_service.v | 26 +++---- src/lang_stats_service.v | 10 +-- src/release_service.v | 12 +-- src/repo_routes.v | 53 +++++++++---- src/repo_service.v | 152 +++++++++++++++++++------------------ src/security_log_service.v | 6 +- src/settings_service.v | 26 +++++-- src/ssh_key_routes.v | 9 ++- src/ssh_key_service.v | 14 ++-- src/star_service.v | 26 +++---- src/tag_service.v | 16 ++-- src/token_service.v | 16 ++-- src/user_routes.v | 64 ++++++++++++---- src/user_service.v | 117 ++++++++++++++-------------- src/watch_service.v | 24 +++--- 28 files changed, 469 insertions(+), 350 deletions(-) diff --git a/src/activity_service.v b/src/activity_service.v index 2348f094..5cb236dd 100644 --- a/src/activity_service.v +++ b/src/activity_service.v @@ -2,7 +2,7 @@ module main import time -fn (app App) add_activity(user_id int, name string) { +fn (app App) add_activity(user_id int, name string) ! { activity := Activity{ user_id: user_id name: name @@ -11,19 +11,19 @@ fn (app App) add_activity(user_id int, name string) { sql app.db { insert activity into Activity - } + }! } fn (mut app App) find_activities(user_id int) []Activity { return sql app.db { select from Activity where user_id == user_id order by created_at desc - } + } or { []Activity{} } } fn (mut app App) has_activity(user_id int, name string) bool { activity_count := sql app.db { select count from Activity where user_id == user_id && name == name - } + } or { 0 } return activity_count > 0 } diff --git a/src/admin_routes.v b/src/admin_routes.v index 4bb6ae67..a3e32d01 100644 --- a/src/admin_routes.v +++ b/src/admin_routes.v @@ -21,7 +21,7 @@ pub fn (mut app App) handle_admin_update_settings(oauth_client_id string, oauth_ return app.redirect_to_index() } - app.update_gitly_settings(oauth_client_id, oauth_client_secret) + app.update_gitly_settings(oauth_client_id, oauth_client_secret) or { app.info(err.str()) } return app.redirect('/admin') } @@ -36,7 +36,7 @@ pub fn (mut app App) handle_admin_edit_user(user_id string) vweb.Result { is_blocked := 'is-blocked' in app.form is_admin := 'is-admin' in app.form - app.edit_user(user_id.int(), clear_session, is_blocked, is_admin) + app.edit_user(user_id.int(), clear_session, is_blocked, is_admin) or { app.info(err.str()) } return app.redirect('/admin') } diff --git a/src/admin_service.v b/src/admin_service.v index d0422eae..7ba06360 100644 --- a/src/admin_service.v +++ b/src/admin_service.v @@ -1,41 +1,41 @@ module main -pub fn (mut app App) edit_user(user_id int, delete_tokens bool, is_blocked bool, is_admin bool) { +pub fn (mut app App) edit_user(user_id int, delete_tokens bool, is_blocked bool, is_admin bool) ! { if is_admin { - app.add_admin(user_id) + app.add_admin(user_id)! } else { - app.remove_admin(user_id) + app.remove_admin(user_id)! } if is_blocked { - app.block_user(user_id) + app.block_user(user_id)! } else { - app.unblock_user(user_id) + app.unblock_user(user_id)! } if delete_tokens { - app.delete_tokens(user_id) + app.delete_tokens(user_id)! } } -pub fn (mut app App) block_user(user_id int) { - app.set_user_block_status(user_id, true) +pub fn (mut app App) block_user(user_id int) ! { + app.set_user_block_status(user_id, true)! } -pub fn (mut app App) unblock_user(user_id int) { - app.set_user_block_status(user_id, false) +pub fn (mut app App) unblock_user(user_id int) ! { + app.set_user_block_status(user_id, false)! } -pub fn (mut app App) add_admin(user_id int) { - app.set_user_admin_status(user_id, true) +pub fn (mut app App) add_admin(user_id int) ! { + app.set_user_admin_status(user_id, true)! } -pub fn (mut app App) remove_admin(user_id int) { - app.set_user_admin_status(user_id, false) +pub fn (mut app App) remove_admin(user_id int) ! { + app.set_user_admin_status(user_id, false)! } -pub fn (mut app App) update_gitly_settings(oauth_client_id string, oauth_client_secret string) { - app.update_settings(oauth_client_id, oauth_client_secret) +pub fn (mut app App) update_gitly_settings(oauth_client_id string, oauth_client_secret string) ! { + app.update_settings(oauth_client_id, oauth_client_secret)! app.load_settings() } diff --git a/src/branch_service.v b/src/branch_service.v index 258a31ad..8ffec62f 100644 --- a/src/branch_service.v +++ b/src/branch_service.v @@ -3,17 +3,17 @@ module main import time import git -fn (mut app App) fetch_branches(repo Repo) { +fn (mut app App) fetch_branches(repo Repo) ! { branches_output := repo.git('branch -a') for branch_output in branches_output.split_into_lines() { branch_name := git.parse_git_branch_output(branch_output) - app.fetch_branch(repo, branch_name) + app.fetch_branch(repo, branch_name)! } } -fn (mut app App) fetch_branch(repo Repo, branch_name string) { +fn (mut app App) fetch_branch(repo Repo, branch_name string) ! { last_commit_hash := repo.get_last_branch_commit_hash(branch_name) branch_data := repo.git('log ${branch_name} -1 --pretty="%aE${log_field_separator}%cD" ${last_commit_hash}') @@ -33,16 +33,17 @@ fn (mut app App) fetch_branch(repo Repo, branch_name string) { } app.create_branch_or_update(repo.id, branch_name, user.username, last_commit_hash, - int(committed_at.unix)) + int(committed_at.unix))! } -fn (mut app App) create_branch_or_update(repository_id int, branch_name string, author string, hash string, date int) { - branch := sql app.db { +fn (mut app App) create_branch_or_update(repository_id int, branch_name string, author string, hash string, date int) ! { + branches := sql app.db { select from Branch where repo_id == repository_id && name == branch_name limit 1 - } + } or { []Branch{} } - if branch.id != 0 { - app.update_branch(branch.id, author, hash, date) + if branches.len != 0 { + branch := branches.first() + app.update_branch(branch.id, author, hash, date)! return } @@ -57,51 +58,63 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string, sql app.db { insert new_branch into Branch - } + }! } -fn (mut app App) update_branch(branch_id int, author string, hash string, date int) { +fn (mut app App) update_branch(branch_id int, author string, hash string, date int) ! { sql app.db { update Branch set author = author, hash = hash, date = date where id == branch_id - } + }! } fn (mut app App) find_repo_branch_by_name(repo_id int, name string) Branch { - return sql app.db { + branches := sql app.db { select from Branch where name == name && repo_id == repo_id limit 1 + } or { []Branch{} } + + if branches.len == 0 { + return Branch{} } + + return branches.first() } fn (mut app App) find_repo_branch_by_id(repo_id int, id int) Branch { - return sql app.db { + branches := sql app.db { select from Branch where id == id && repo_id == repo_id limit 1 + } or { []Branch{} } + + if branches.len == 0 { + return Branch{} } + + return branches.first() } fn (app App) get_all_repo_branches(repo_id int) []Branch { return sql app.db { select from Branch where repo_id == repo_id order by date desc - } + } or { []Branch{} } } fn (mut app App) get_count_repo_branches(repo_id int) int { return sql app.db { select count from Branch where repo_id == repo_id - } + } or { 0 } } fn (mut app App) contains_repo_branch(repo_id int, name string) bool { count := sql app.db { select count from Branch where repo_id == repo_id && name == name - } + } or { 0 } return count == 1 } -fn (mut app App) delete_repo_branches(repo_id int) { +fn (mut app App) delete_repo_branches(repo_id int) ! { sql app.db { delete from Branch where repo_id == repo_id - } + }! } fn (branch Branch) relative() string { diff --git a/src/cli.v b/src/cli.v index 57af5640..fa27ae0b 100644 --- a/src/cli.v +++ b/src/cli.v @@ -4,7 +4,7 @@ module main import os -pub fn (mut app App) command_fetcher() { +pub fn (mut app App) command_fetcher() ! { for { line := os.get_line() @@ -16,7 +16,7 @@ pub fn (mut app App) command_fetcher() { 'adduser' { if args.len > 4 { app.register_user(args[1], args[2], args[3], args[4..], false, - false) + false)! println('Added user ${args[1]}') } else { error('Not enough arguments (3 required but only ${args.len} given)') diff --git a/src/comment_routes.v b/src/comment_routes.v index f06f645e..4c9fb25a 100644 --- a/src/comment_routes.v +++ b/src/comment_routes.v @@ -23,10 +23,14 @@ pub fn (mut app App) handle_add_comment(username string, repo_name string) vweb. return app.issue(username, repo_name, issue_id) } - app.add_issue_comment(app.user.id, issue_id.int(), text) + app.add_issue_comment(app.user.id, issue_id.int(), text) or { + app.error('There was an error while inserting the comment') + + return app.issue(username, repo_name, issue_id) + } // TODO: count comments - app.increment_issue_comments(issue_id.int()) + app.increment_issue_comments(issue_id.int()) or { app.info(err.str()) } return app.redirect('/${username}/${repo_name}/issue/${issue_id}') } diff --git a/src/comment_service.v b/src/comment_service.v index e09bedf0..9bd135be 100644 --- a/src/comment_service.v +++ b/src/comment_service.v @@ -2,7 +2,7 @@ module main import time -fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) { +fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) ! { comment := Comment{ author_id: author_id issue_id: issue_id @@ -12,13 +12,13 @@ fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) { sql app.db { insert comment into Comment - } + }! } fn (mut app App) get_all_issue_comments(issue_id int) []Comment { - mut comments := sql app.db { + comments := sql app.db { select from Comment where issue_id == issue_id - } + } or { []Comment{} } return comments } diff --git a/src/commit_service.v b/src/commit_service.v index a98504d0..f085de17 100644 --- a/src/commit_service.v +++ b/src/commit_service.v @@ -60,12 +60,12 @@ fn (commit Commit) get_changes(repo Repo) []Change { return changes } -fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash string, author string, author_id int, message string, date int) { - commit := sql app.db { +fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash string, author string, author_id int, message string, date int) ! { + commits := sql app.db { select from Commit where repo_id == repo_id && branch_id == branch_id && hash == last_hash limit 1 - } + } or { []Commit{} } - if commit.id > 0 { + if commits.len > 0 { return } @@ -81,25 +81,25 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s sql app.db { insert new_commit into Commit - } + }! } fn (mut app App) find_repo_commits_as_page(repo_id int, branch_id int, offset int) []Commit { return sql app.db { select from Commit where repo_id == repo_id && branch_id == branch_id order by created_at desc limit 35 offset offset - } + } or { []Commit{} } } fn (mut app App) get_repo_commit_count(repo_id int, branch_id int) int { return sql app.db { select count from Commit where repo_id == repo_id && branch_id == branch_id - } + } or { 0 } } fn (mut app App) find_repo_commit_by_hash(repo_id int, hash string) Commit { commits := sql app.db { select from Commit where repo_id == repo_id && hash == hash - } + } or { []Commit{} } if commits.len == 1 { return commits[0] } @@ -107,7 +107,13 @@ fn (mut app App) find_repo_commit_by_hash(repo_id int, hash string) Commit { } fn (mut app App) find_repo_last_commit(repo_id int, branch_id int) Commit { - return sql app.db { + commits := sql app.db { select from Commit where repo_id == repo_id && branch_id == branch_id order by created_at desc limit 1 + } or { []Commit{} } + + if commits.len == 0 { + return Commit{} } + + return commits.first() } diff --git a/src/file_service.v b/src/file_service.v index 94cafd85..a190ac12 100644 --- a/src/file_service.v +++ b/src/file_service.v @@ -53,10 +53,10 @@ fn calculate_lines_of_code(source string) (int, int) { return loc, sloc } -fn (mut app App) add_file(file File) { +fn (mut app App) add_file(file File) ! { sql app.db { insert file into File - } + }! } fn (mut app App) find_repository_items(repo_id int, branch string, parent_path string) []File { @@ -65,7 +65,7 @@ fn (mut app App) find_repository_items(repo_id int, branch string, parent_path s items := sql app.db { select from File where repo_id == repo_id && parent_path == valid_parent_path && branch == branch - } + } or { []File{} } return items } @@ -80,28 +80,28 @@ fn (mut app App) find_repo_file_by_path(repo_id int, item_branch string, path st app.info('find file repo_id=${repo_id} parent_path = ${valid_parent_path} branch=${item_branch} name=${item_branch}') - file := sql app.db { + files := sql app.db { select from File where repo_id == repo_id && parent_path == valid_parent_path && branch == item_branch && name == item_name limit 1 - } + } or { []File{} } - if file.name == '' { + if files.len == 0 { return none } - return file + return files.first() } -fn (mut app App) delete_repository_files(repository_id int) { +fn (mut app App) delete_repository_files(repository_id int) ! { sql app.db { delete from File where repo_id == repository_id - } + }! } -fn (mut app App) delete_repository_files_in_branch(repository_id int, branch_name string) { +fn (mut app App) delete_repository_files_in_branch(repository_id int, branch_name string) ! { sql app.db { delete from File where repo_id == repository_id && branch == branch_name - } + }! } fn (mut app App) delete_repo_folder(path string) { diff --git a/src/git_routes.v b/src/git_routes.v index a7fd33d3..6407d825 100644 --- a/src/git_routes.v +++ b/src/git_routes.v @@ -81,7 +81,11 @@ fn (mut app App) handle_git_receive_pack(username string, git_repo_name string) return app.ok('') } - app.update_repo_after_push(repo.id, branch_name) + app.update_repo_after_push(repo.id, branch_name) or { + app.send_internal_error('There was an error while updating the repo') + + return app.ok('') + } app.set_git_content_type_headers(.receive) diff --git a/src/github_routes.v b/src/github_routes.v index e477216f..18ed687f 100644 --- a/src/github_routes.v +++ b/src/github_routes.v @@ -12,7 +12,9 @@ pub fn (mut app App) handle_oauth() vweb.Result { state := app.query['state'] if code == '' { - app.add_security_log(user_id: app.user.id, kind: .empty_oauth_code) + app.add_security_log(user_id: app.user.id, kind: .empty_oauth_code) or { + app.info(err.str()) + } app.info('Code is empty') return app.redirect_to_index() @@ -25,7 +27,7 @@ pub fn (mut app App) handle_oauth() vweb.Result { kind: .wrong_oauth_state arg1: 'csrf=${csrf}' arg2: 'state=${state}' - ) + ) or { app.info(err.str()) } return app.redirect_to_index() } @@ -67,7 +69,7 @@ pub fn (mut app App) handle_oauth() vweb.Result { user_id: app.user.id kind: .empty_oauth_email arg1: user_response.body - ) + ) or { app.info(err.str()) } app.info('Email is empty') } @@ -79,19 +81,23 @@ pub fn (mut app App) handle_oauth() vweb.Result { user_id: user.id kind: .registered_via_github arg1: user_response.body - ) + ) or { app.info(err.str()) } - app.register_user(github_user.username, '', '', [github_user.email], true, false) + app.register_user(github_user.username, '', '', [github_user.email], true, false) or { + app.info(err.msg()) + } user = app.get_user_by_github_username(github_user.username) or { return app.redirect_to_index() } - app.update_user_avatar(user.id, github_user.avatar) + app.update_user_avatar(user.id, github_user.avatar) or { app.info(err.msg()) } } - app.auth_user(user, app.ip()) - app.add_security_log(user_id: user.id, kind: .logged_in_via_github, arg1: user_response.body) + app.auth_user(user, app.ip()) or { app.info(err.msg()) } + app.add_security_log(user_id: user.id, kind: .logged_in_via_github, arg1: user_response.body) or { + app.info(err.str()) + } return app.redirect_to_index() } diff --git a/src/gitly.v b/src/gitly.v index b918e098..bb7b7593 100644 --- a/src/gitly.v +++ b/src/gitly.v @@ -51,10 +51,10 @@ fn main() { return } - vweb.run(new_app(), http_port) + vweb.run(new_app()!, http_port) } -fn new_app() &App { +fn new_app() !&App { mut app := &App{ db: sqlite.connect('gitly.sqlite') or { panic(err) } started_at: time.now().unix @@ -62,7 +62,7 @@ fn new_app() &App { set_rand_crypto_safe_seed() - app.create_tables() + app.create_tables()! create_directory_if_not_exists('logs') @@ -147,7 +147,8 @@ pub fn (mut app App) before_request() { ['/'] pub fn (mut app App) index() vweb.Result { - no_users := app.get_users_count() == 0 + user_count := app.get_users_count() or { 0 } + no_users := user_count == 0 if no_users { return app.redirect('/register') } @@ -167,68 +168,68 @@ pub fn (mut app App) redirect_to_repository(username string, repo_name string) v return app.redirect('/${username}/${repo_name}') } -fn (mut app App) create_tables() { +fn (mut app App) create_tables() ! { sql app.db { create table Repo - } + }! // unix time default now sql app.db { create table File - } // missing ON CONFLIC REPLACE + }! // missing ON CONFLIC REPLACE //"created_at int default (strftime('%s', 'now'))" sql app.db { create table Issue - } + }! //"created_at int default (strftime('%s', 'now'))" sql app.db { create table Commit - } + }! // author text default '' is to to avoid joins sql app.db { create table LangStat - } + }! sql app.db { create table User - } + }! sql app.db { create table Email - } + }! sql app.db { create table Contributor - } + }! sql app.db { create table Activity - } + }! sql app.db { create table Tag - } + }! sql app.db { create table Release - } + }! sql app.db { create table SshKey - } + }! sql app.db { create table Comment - } + }! sql app.db { create table Branch - } + }! sql app.db { create table Settings - } + }! sql app.db { create table Token - } + }! sql app.db { create table SecurityLog - } + }! sql app.db { create table Star - } + }! sql app.db { create table Watch - } + }! } fn (mut app App) json_success[T](result T) vweb.Result { diff --git a/src/issue_routes.v b/src/issue_routes.v index 741ac87e..f523590a 100644 --- a/src/issue_routes.v +++ b/src/issue_routes.v @@ -77,16 +77,14 @@ pub fn (mut app App) handle_add_repo_issue(username string, repo_name string) vw return app.redirect('/${username}/${repo_name}/issues/new') } - app.increment_user_post(mut app.user) - - app.add_issue(repo.id, app.user.id, title, text) - - app.increment_repo_issues(repo.id) + app.increment_user_post(mut app.user) or { app.info(err.str()) } + app.add_issue(repo.id, app.user.id, title, text) or { app.info(err.str()) } + app.increment_repo_issues(repo.id) or { app.info(err.str()) } has_first_issue_activity := app.has_activity(app.user.id, 'first_issue') if !has_first_issue_activity { - app.add_activity(app.user.id, 'first_issue') + app.add_activity(app.user.id, 'first_issue') or { app.info(err.str()) } } return app.redirect('/${username}/${repo_name}/issues') diff --git a/src/issue_service.v b/src/issue_service.v index 478eaaa5..f081538b 100644 --- a/src/issue_service.v +++ b/src/issue_service.v @@ -4,7 +4,7 @@ module main import time -fn (mut app App) add_issue(repo_id int, author_id int, title string, text string) { +fn (mut app App) add_issue(repo_id int, author_id int, title string, text string) ! { issue := Issue{ title: title text: text @@ -15,48 +15,48 @@ fn (mut app App) add_issue(repo_id int, author_id int, title string, text string sql app.db { insert issue into Issue - } + }! } fn (mut app App) find_issue_by_id(issue_id int) ?Issue { - issue := sql app.db { + issues := sql app.db { select from Issue where id == issue_id limit 1 - } - if issue.id == 0 { + } or { []Issue{} } + if issues.len == 0 { return none } - return issue + return issues.first() } fn (mut app App) find_repo_issues_as_page(repo_id int, page int) []Issue { off := page * commits_per_page return sql app.db { select from Issue where repo_id == repo_id && is_pr == false limit 35 offset off - } + } or { []Issue{} } } fn (mut app App) get_repo_issue_count(repo_id int) int { return sql app.db { select count from Issue where repo_id == repo_id - } + } or { 0 } } fn (mut app App) find_user_issues(user_id int) []Issue { return sql app.db { select from Issue where author_id == user_id && is_pr == false - } + } or { []Issue{} } } -fn (mut app App) delete_repo_issues(repo_id int) { +fn (mut app App) delete_repo_issues(repo_id int) ! { sql app.db { delete from Issue where repo_id == repo_id - } + }! } -fn (mut app App) increment_issue_comments(id int) { +fn (mut app App) increment_issue_comments(id int) ! { sql app.db { update Issue set comments_count = comments_count + 1 where id == id - } + }! } fn (i &Issue) relative_time() string { diff --git a/src/lang_stats_service.v b/src/lang_stats_service.v index 4eeb4e6a..a6c928d3 100644 --- a/src/lang_stats_service.v +++ b/src/lang_stats_service.v @@ -19,10 +19,10 @@ const ( ] ) -fn (app App) add_lang_stat(lang_stat LangStat) { +fn (app App) add_lang_stat(lang_stat LangStat) ! { sql app.db { insert lang_stat into LangStat - } + }! } pub fn (l &LangStat) pct_html() vweb.RawHtml { @@ -39,11 +39,11 @@ pub fn (l &LangStat) pct_html() vweb.RawHtml { pub fn (app App) find_repo_lang_stats(repo_id int) []LangStat { return sql app.db { select from LangStat where repo_id == repo_id order by pct desc - } + } or { []LangStat{} } } -fn (app App) remove_repo_lang_stats(repo_id int) { +fn (app App) remove_repo_lang_stats(repo_id int) ! { sql app.db { delete from LangStat where repo_id == repo_id - } + }! } diff --git a/src/release_service.v b/src/release_service.v index 016b8955..27cb98bd 100644 --- a/src/release_service.v +++ b/src/release_service.v @@ -2,7 +2,7 @@ module main import time -pub fn (mut app App) add_release(tag_id int, repo_id int, date time.Time, notes string) { +pub fn (mut app App) add_release(tag_id int, repo_id int, date time.Time, notes string) ! { release := Release{ tag_id: tag_id repo_id: repo_id @@ -12,24 +12,24 @@ pub fn (mut app App) add_release(tag_id int, repo_id int, date time.Time, notes sql app.db { insert release into Release - } + }! } pub fn (mut app App) find_repo_releases_as_page(repo_id int, offset int) []Release { // FIXME: 20 -> releases_per_page return sql app.db { select from Release where repo_id == repo_id order by date desc limit 20 offset offset - } + } or { []Release{} } } pub fn (app App) get_repo_release_count(repo_id int) int { return sql app.db { select count from Release where repo_id == repo_id - } + } or { 0 } } -pub fn (mut app App) delete_repo_releases(repo_id int) { +pub fn (mut app App) delete_repo_releases(repo_id int) ! { sql app.db { delete from Release where repo_id == repo_id - } + }! } diff --git a/src/repo_routes.v b/src/repo_routes.v index de7353ed..411fa273 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -61,7 +61,7 @@ pub fn (mut app App) handle_update_repo_settings(username string, repo_name stri if webhook_secret != '' && webhook_secret != repo.webhook_secret { webhook := sha1.hexhash(webhook_secret) - app.set_repo_webhook_secret(repo.id, webhook) + app.set_repo_webhook_secret(repo.id, webhook) or { app.info(err.str()) } } return app.redirect_to_repository(username, repo_name) @@ -111,7 +111,10 @@ pub fn (mut app App) handle_repo_move(username string, repo_name string, dest st return app.repo_settings(username, repo_name) } - app.move_repo_to_user(repo.id, dest_user.id, dest_user.username) + app.move_repo_to_user(repo.id, dest_user.id, dest_user.username) or { + app.error('There was an error while moving the repo') + return app.repo_settings(username, repo_name) + } return app.redirect('/${dest_user.username}/${repo.name}') } else { @@ -167,8 +170,8 @@ pub fn (mut app App) handle_repo_update(username string, repo_name string) vweb. } if app.user.is_admin { - app.update_repo_from_remote(mut repo) - app.slow_fetch_files_info(mut repo, 'master', '.') + app.update_repo_from_remote(mut repo) or { app.info(err.str()) } + app.slow_fetch_files_info(mut repo, 'master', '.') or { app.info(err.str()) } } return app.redirect_to_repository(username, repo_name) @@ -261,7 +264,10 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description new_repo.clone() } - app.add_repo(new_repo) + app.add_repo(new_repo) or { + app.error('There was an error while adding the repo') + return app.new() + } new_repo = app.find_repo_by_name_and_user_id(new_repo.name, app.user.id) repo_id := new_repo.id @@ -272,13 +278,19 @@ 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) + app.update_repo_primary_branch(repo_id, primary_branch) or { + app.error('There was an error while adding the repo') + return app.new() + } new_repo = app.find_repo_by_id(repo_id) // Update only cloned repositories if !is_clone_url_empty { - app.update_repo_from_fs(mut new_repo) + app.update_repo_from_fs(mut new_repo) or { + app.error('There was an error while cloning the repo') + return app.new() + } } if no_redirect == '1' { @@ -288,7 +300,7 @@ pub fn (mut app App) handle_new_repo(name string, clone_url string, description has_first_repo_activity := app.has_activity(app.user.id, 'first_repo') if !has_first_repo_activity { - app.add_activity(app.user.id, 'first_repo') + app.add_activity(app.user.id, 'first_repo') or { app.info(err.str()) } } return app.redirect('/${app.user.username}/repos') @@ -312,7 +324,7 @@ pub fn (mut app App) tree(username string, repo_name string, branch_name string, repo_id := repo.id log_prefix := '${username}/${repo_name}' - app.fetch_tags(repo) + app.fetch_tags(repo) or { app.info(err.str()) } app.current_path = '/${path}' if app.current_path.contains('/favicon.svg') { @@ -326,7 +338,7 @@ pub fn (mut app App) tree(username string, repo_name string, branch_name string, app.is_tree = true - app.increment_repo_views(repo.id) + app.increment_repo_views(repo.id) or { app.info(err.str()) } mut up := '/' can_up := path != '' @@ -351,13 +363,20 @@ pub fn (mut app App) tree(username string, repo_name string, branch_name string, // No files in the db, fetch them from git and cache in db app.info('${log_prefix}: caching items in repository with ${repo_id}') - items = app.cache_repository_items(mut repo, branch_name, app.current_path) - app.slow_fetch_files_info(mut repo, branch_name, app.current_path) + items = app.cache_repository_items(mut repo, branch_name, app.current_path) or { + app.info(err.str()) + []File{} + } + app.slow_fetch_files_info(mut repo, branch_name, app.current_path) or { + app.info(err.str()) + } } if items.any(it.last_msg == '') { // If any of the files has a missing `last_msg`, we need to refetch it. - app.slow_fetch_files_info(mut repo, branch_name, app.current_path) + app.slow_fetch_files_info(mut repo, branch_name, app.current_path) or { + app.info(err.str()) + } } // Fetch last commit message for this directory, printed at the top of the tree @@ -437,7 +456,9 @@ pub fn (mut app App) handle_api_repo_star(repo_id_str string) vweb.Result { } user_id := app.user.id - app.toggle_repo_star(repo_id, user_id) + app.toggle_repo_star(repo_id, user_id) or { + return app.json_error('There was an error while starring the repo') + } is_repo_starred := app.check_repo_starred(repo_id, user_id) return app.json_success(is_repo_starred) @@ -454,7 +475,9 @@ pub fn (mut app App) handle_api_repo_watch(repo_id_str string) vweb.Result { } user_id := app.user.id - app.toggle_repo_watcher_status(repo_id, user_id) + app.toggle_repo_watcher_status(repo_id, user_id) or { + return app.json_error('There was an error while toggling to watch') + } is_watching := app.check_repo_watcher_status(repo_id, user_id) return app.json_success(is_watching) diff --git a/src/repo_service.v b/src/repo_service.v index ad6345bb..3c33f426 100644 --- a/src/repo_service.v +++ b/src/repo_service.v @@ -34,7 +34,7 @@ fn (f ArchiveFormat) str() string { } } -fn (mut app App) save_repo(repo Repo) { +fn (mut app App) save_repo(repo Repo) ! { id := repo.id desc := repo.description views_count := repo.views_count @@ -53,18 +53,21 @@ fn (mut app App) save_repo(repo Repo) { webhook_secret = webhook_secret, tags_count = tags_count, open_issues_count = open_issues_count, open_prs_count = open_prs_count, releases_count = releases_count, contributors_count = contributors_count, stars_count = stars_count, branches_count = branches_count where id == id - } + }! } fn (app App) find_repo_by_name_and_user_id(repo_name string, user_id int) Repo { - mut repo := sql app.db { + repos := sql app.db { select from Repo where name == repo_name && user_id == user_id limit 1 - } + } or { []Repo{} } - if repo.id > 0 { - repo.lang_stats = app.find_repo_lang_stats(repo.id) + if repos.len == 0 { + return Repo{} } + mut repo := repos.first() + repo.lang_stats = app.find_repo_lang_stats(repo.id) + return repo } @@ -77,19 +80,19 @@ fn (app App) find_repo_by_name_and_username(repo_name string, username string) R fn (mut app App) get_count_user_repos(user_id int) int { return sql app.db { select count from Repo where user_id == user_id - } + } or { 0 } } fn (mut app App) find_user_repos(user_id int) []Repo { return sql app.db { select from Repo where user_id == user_id - } + } or { []Repo{} } } fn (mut app App) find_user_public_repos(user_id int) []Repo { return sql app.db { select from Repo where user_id == user_id && is_public == true - } + } or { []Repo{} } } fn (app App) search_public_repos(query string) []Repo { @@ -114,133 +117,136 @@ fn (app App) search_public_repos(query string) []Repo { } fn (app App) find_repo_by_id(repo_id int) Repo { - mut repo := sql app.db { + repos := sql app.db { select from Repo where id == repo_id - } + } or { []Repo{} } - if repo.id > 0 { - repo.lang_stats = app.find_repo_lang_stats(repo.id) + if repos.len == 0 { + return Repo{} } + mut repo := repos.first() + repo.lang_stats = app.find_repo_lang_stats(repo.id) + return repo } -fn (mut app App) increment_repo_views(repo_id int) { +fn (mut app App) increment_repo_views(repo_id int) ! { sql app.db { update Repo set views_count = views_count + 1 where id == repo_id - } + }! } -fn (mut app App) increment_repo_stars(repo_id int) { +fn (mut app App) increment_repo_stars(repo_id int) ! { sql app.db { update Repo set stars_count = stars_count + 1 where id == repo_id - } + }! } -fn (mut app App) decrement_repo_stars(repo_id int) { +fn (mut app App) decrement_repo_stars(repo_id int) ! { sql app.db { update Repo set stars_count = stars_count - 1 where id == repo_id - } + }! } -fn (mut app App) increment_file_views(file_id int) { +fn (mut app App) increment_file_views(file_id int) ! { sql app.db { update File set views_count = views_count + 1 where id == file_id - } + }! } -fn (mut app App) set_repo_webhook_secret(repo_id int, secret string) { +fn (mut app App) set_repo_webhook_secret(repo_id int, secret string) ! { sql app.db { update Repo set webhook_secret = secret where id == repo_id - } + }! } -fn (mut app App) increment_repo_issues(repo_id int) { +fn (mut app App) increment_repo_issues(repo_id int) ! { sql app.db { update Repo set open_issues_count = open_issues_count + 1 where id == repo_id - } + }! } -fn (mut app App) add_repo(repo Repo) { +fn (mut app App) add_repo(repo Repo) ! { sql app.db { insert repo into Repo - } + }! } -fn (mut app App) delete_repository(id int, path string, name string) { +fn (mut app App) delete_repository(id int, path string, name string) ! { sql app.db { delete from Repo where id == id - } + }! app.info('Removed repo entry (${id}, ${name})') sql app.db { delete from Commit where repo_id == id - } + }! app.info('Removed repo commits (${id}, ${name})') - app.delete_repo_issues(id) + app.delete_repo_issues(id)! app.info('Removed repo issues (${id}, ${name})') - app.delete_repo_branches(id) + app.delete_repo_branches(id)! app.info('Removed repo branches (${id}, ${name})') - app.delete_repo_releases(id) + app.delete_repo_releases(id)! app.info('Removed repo releases (${id}, ${name})') - app.delete_repository_files(id) + app.delete_repository_files(id)! app.info('Removed repo files (${id}, ${name})') app.delete_repo_folder(path) app.info('Removed repo folder (${id}, ${name})') } -fn (mut app App) move_repo_to_user(repo_id int, user_id int, user_name string) { +fn (mut app App) move_repo_to_user(repo_id int, user_id int, user_name string) ! { sql app.db { update Repo set user_id = user_id, user_name = user_name where id == repo_id - } + }! } fn (mut app App) user_has_repo(user_id int, repo_name string) bool { count := sql app.db { select count from Repo where user_id == user_id && name == repo_name - } + } or { 0 } return count >= 0 } -fn (mut app App) update_repo_from_fs(mut repo Repo) { +fn (mut app App) update_repo_from_fs(mut repo Repo) ! { repo_id := repo.id app.db.exec('BEGIN TRANSACTION') - repo.analyse_lang(app) + repo.analyse_lang(app)! app.info(repo.contributors_count.str()) - app.fetch_branches(repo) + app.fetch_branches(repo)! branches_output := repo.git('branch -a') for branch_output in branches_output.split_into_lines() { branch_name := git.parse_git_branch_output(branch_output) - app.update_repo_branch_from_fs(mut repo, branch_name) + app.update_repo_branch_from_fs(mut repo, branch_name)! } - repo.contributors_count = app.get_count_repo_contributors(repo_id) + repo.contributors_count = app.get_count_repo_contributors(repo_id)! repo.branches_count = app.get_count_repo_branches(repo_id) // TODO: TEMPORARY - UNTIL WE GET PERSISTENT RELEASE INFO for tag in app.get_all_repo_tags(repo_id) { - app.add_release(tag.id, repo_id, time.unix(tag.created_at), tag.message) + app.add_release(tag.id, repo_id, time.unix(tag.created_at), tag.message)! repo.releases_count++ } - app.save_repo(repo) + app.save_repo(repo)! app.db.exec('END TRANSACTION') app.info('Repo updated') } -fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) { +fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) ! { repo_id := repo.id branch := app.find_repo_branch_by_name(repo.id, branch_name) @@ -268,18 +274,18 @@ fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) { user := app.get_user_by_email(commit_author_email) or { User{} } if user.id > 0 { - app.add_contributor(user.id, repo_id) + app.add_contributor(user.id, repo_id)! commit_author_id = user.id } app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author, - commit_author_id, commit_message, int(commit_date.unix)) + commit_author_id, commit_message, int(commit_date.unix))! } } } -fn (mut app App) update_repo_from_remote(mut repo Repo) { +fn (mut app App) update_repo_from_remote(mut repo Repo) ! { repo_id := repo.id repo.git('fetch --all') @@ -287,35 +293,35 @@ fn (mut app App) update_repo_from_remote(mut repo Repo) { app.db.exec('BEGIN TRANSACTION') - repo.analyse_lang(app) + repo.analyse_lang(app)! app.info(repo.contributors_count.str()) - app.fetch_branches(repo) - app.fetch_tags(repo) + app.fetch_branches(repo)! + app.fetch_tags(repo)! branches_output := repo.git('branch -a') for branch_output in branches_output.split_into_lines() { branch_name := git.parse_git_branch_output(branch_output) - app.update_repo_branch_from_fs(mut repo, branch_name) + app.update_repo_branch_from_fs(mut repo, branch_name)! } for tag in app.get_all_repo_tags(repo_id) { - app.add_release(tag.id, repo_id, time.unix(tag.created_at), tag.message) + app.add_release(tag.id, repo_id, time.unix(tag.created_at), tag.message)! repo.releases_count++ } - repo.contributors_count = app.get_count_repo_contributors(repo_id) + repo.contributors_count = app.get_count_repo_contributors(repo_id)! repo.branches_count = app.get_count_repo_branches(repo_id) - app.save_repo(repo) + app.save_repo(repo)! app.db.exec('END TRANSACTION') app.info('Repo updated') } -fn (mut app App) update_repo_branch_data(mut repo Repo, branch_name string) { +fn (mut app App) update_repo_branch_data(mut repo Repo, branch_name string) ! { repo_id := repo.id branch := app.find_repo_branch_by_name(repo.id, branch_name) @@ -343,30 +349,30 @@ fn (mut app App) update_repo_branch_data(mut repo Repo, branch_name string) { user := app.get_user_by_email(commit_author_email) or { User{} } if user.id > 0 { - app.add_contributor(user.id, repo_id) + app.add_contributor(user.id, repo_id)! commit_author_id = user.id } app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author, - commit_author_id, commit_message, int(commit_date.unix)) + commit_author_id, commit_message, int(commit_date.unix))! } } } // TODO: tags and other stuff -fn (mut app App) update_repo_after_push(repo_id int, branch_name string) { +fn (mut app App) update_repo_after_push(repo_id int, branch_name string) ! { mut repo := app.find_repo_by_id(repo_id) if repo.id == 0 { return } - app.update_repo_from_fs(mut repo) - app.delete_repository_files_in_branch(repo_id, branch_name) + app.update_repo_from_fs(mut repo)! + app.delete_repository_files_in_branch(repo_id, branch_name)! } -fn (r &Repo) analyse_lang(app &App) { +fn (r &Repo) analyse_lang(app &App) ! { file_paths := r.get_all_file_paths() mut all_size := 0 @@ -427,10 +433,10 @@ fn (r &Repo) analyse_lang(app &App) { } } - app.remove_repo_lang_stats(r.id) + app.remove_repo_lang_stats(r.id)! for lang_stat in d_lang_stats { - app.add_lang_stat(lang_stat) + app.add_lang_stat(lang_stat)! } } @@ -549,7 +555,7 @@ fn (r &Repo) parse_ls(ls_line string, branch string) ?File { } // Fetches all files via `git ls-tree` and saves them in db -fn (mut app App) cache_repository_items(mut r Repo, branch string, path string) []File { +fn (mut app App) cache_repository_items(mut r Repo, branch string, path string) ![]File { if r.status == .caching { app.info('`${r.name}` is being cached already') return [] @@ -591,7 +597,7 @@ fn (mut app App) cache_repository_items(mut r Repo, branch string, path string) if file.is_dir { dirs << file - app.add_file(file) + app.add_file(file)! } else { files << file } @@ -599,7 +605,7 @@ fn (mut app App) cache_repository_items(mut r Repo, branch string, path string) dirs << files for file in files { - app.add_file(file) + app.add_file(file)! } app.db.exec('END TRANSACTION') @@ -609,7 +615,7 @@ fn (mut app App) cache_repository_items(mut r Repo, branch string, path string) // fetches last message and last time for each file // this is slow, so it's run in the background thread -fn (mut app App) slow_fetch_files_info(mut repo Repo, branch string, path string) { +fn (mut app App) slow_fetch_files_info(mut repo Repo, branch string, path string) ! { files := app.find_repository_items(repo.id, branch, path) for i in 0 .. files.len { @@ -618,7 +624,7 @@ fn (mut app App) slow_fetch_files_info(mut repo Repo, branch string, path string continue } - app.fetch_file_info(repo, files[i]) + app.fetch_file_info(repo, files[i])! } } @@ -699,7 +705,7 @@ fn first_line(s string) string { return s[..pos] } -fn (mut app App) fetch_file_info(r &Repo, file &File) { +fn (mut app App) fetch_file_info(r &Repo, file &File) ! { logs := r.git('log -n1 --format=%B___%at___%H___%an ${file.branch} -- ${file.full_path()}') vals := logs.split('___') if vals.len < 3 { @@ -711,13 +717,13 @@ fn (mut app App) fetch_file_info(r &Repo, file &File) { file_id := file.id sql app.db { update File set last_msg = last_msg, last_time = last_time where id == file_id - } + }! } -fn (mut app App) update_repo_primary_branch(repo_id int, branch string) { +fn (mut app App) update_repo_primary_branch(repo_id int, branch string) ! { sql app.db { update Repo set primary_branch = branch where id == repo_id - } + }! } fn (mut r Repo) clone() { diff --git a/src/security_log_service.v b/src/security_log_service.v index 4d757067..d6625640 100644 --- a/src/security_log_service.v +++ b/src/security_log_service.v @@ -2,7 +2,7 @@ // Use of this source code is governed by a GPL license that can be found in the LICENSE file. module main -fn (mut app App) add_security_log(log SecurityLog) { +fn (mut app App) add_security_log(log SecurityLog) ! { new_log := SecurityLog{ ...log kind_id: int(log.kind) @@ -11,13 +11,13 @@ fn (mut app App) add_security_log(log SecurityLog) { sql app.db { insert new_log into SecurityLog - } + }! } fn (app &App) get_all_user_security_logs(user_id int) []SecurityLog { mut logs := sql app.db { select from SecurityLog where user_id == user_id order by id desc - } + } or { []SecurityLog{} } for i, log in logs { logs[i].kind = unsafe { SecurityLogKind(log.kind_id) } diff --git a/src/settings_service.v b/src/settings_service.v index 93e6d57d..80249008 100644 --- a/src/settings_service.v +++ b/src/settings_service.v @@ -1,14 +1,26 @@ module main fn (mut app App) load_settings() { - app.settings = sql app.db { + settings_result := sql app.db { select from Settings limit 1 + } or { [] } + + app.settings = if settings_result.len == 0 { + Settings{} + } else { + settings_result.first() } } -fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret string) { - old_settings := sql app.db { +fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret string) ! { + settings_result := sql app.db { select from Settings limit 1 + } or { [] } + + old_settings := if settings_result.len == 0 { + Settings{} + } else { + settings_result.first() } github_oauth_client_id := if oauth_client_id != '' { @@ -24,18 +36,18 @@ fn (mut app App) update_settings(oauth_client_id string, oauth_client_secret str } if old_settings.id == 0 { - settings := Settings{ + new_settings := Settings{ oauth_client_id: github_oauth_client_id oauth_client_secret: github_oauth_client_secret } sql app.db { - insert settings into Settings - } + insert new_settings into Settings + }! } else { sql app.db { update Settings set oauth_client_id = github_oauth_client_id, oauth_client_secret = github_oauth_client_secret where id == old_settings.id - } + }! } } diff --git a/src/ssh_key_routes.v b/src/ssh_key_routes.v index 4f72cfee..994eb6e5 100644 --- a/src/ssh_key_routes.v +++ b/src/ssh_key_routes.v @@ -2,6 +2,7 @@ module main import vweb import validation +import api ['/:username/settings/ssh-keys'] pub fn (mut app App) user_ssh_keys_list(username string) vweb.Result { @@ -59,7 +60,13 @@ pub fn (mut app App) handle_remove_ssh_key(username string, id int) vweb.Result return app.redirect_to_index() } - app.remove_ssh_key(app.user.id, id) + app.remove_ssh_key(app.user.id, id) or { + response := api.ApiErrorResponse{ + message: 'There was an error while deleting the SSH key' + } + + return app.json(response) + } return app.ok('') } diff --git a/src/ssh_key_service.v b/src/ssh_key_service.v index 41fb0e1c..c6e11d48 100644 --- a/src/ssh_key_service.v +++ b/src/ssh_key_service.v @@ -3,11 +3,11 @@ module main import time fn (mut app App) add_ssh_key(user_id int, title string, key string) ! { - ssh_key := sql app.db { + ssh_keys := sql app.db { select from SshKey where user_id == user_id && title == title limit 1 - } + } or { [] } - if ssh_key.id != 0 { + if ssh_keys.len != 0 { return error('SSH Key already exists') } @@ -20,17 +20,17 @@ fn (mut app App) add_ssh_key(user_id int, title string, key string) ! { sql app.db { insert new_ssh_key into SshKey - } + }! } fn (mut app App) find_ssh_keys(user_id int) []SshKey { return sql app.db { select from SshKey where user_id == user_id - } + } or { [] } } -fn (mut app App) remove_ssh_key(user_id int, id int) { +fn (mut app App) remove_ssh_key(user_id int, id int) ! { sql app.db { delete from SshKey where id == id && user_id == user_id - } + }! } diff --git a/src/star_service.v b/src/star_service.v index 3f8e9831..e008c952 100644 --- a/src/star_service.v +++ b/src/star_service.v @@ -1,6 +1,6 @@ module main -fn (mut app App) add_star(repo_id int, user_id int) { +fn (mut app App) add_star(repo_id int, user_id int) ! { star := Star{ repo_id: repo_id user_id: user_id @@ -8,13 +8,13 @@ fn (mut app App) add_star(repo_id int, user_id int) { sql app.db { insert star into Star - } + }! } fn (mut app App) find_user_starred_repos(user_id int) []Repo { stars := sql app.db { select from Star where user_id == user_id - } + } or { [] } mut repos := []Repo{} for star in stars { @@ -28,28 +28,28 @@ fn (mut app App) find_user_starred_repos(user_id int) []Repo { return repos } -fn (mut app App) toggle_repo_star(repo_id int, user_id int) { +fn (mut app App) toggle_repo_star(repo_id int, user_id int) ! { is_starred := app.check_repo_starred(repo_id, user_id) if is_starred { - app.remove_star(repo_id, user_id) - app.decrement_repo_stars(repo_id) + app.remove_star(repo_id, user_id)! + app.decrement_repo_stars(repo_id)! } else { - app.add_star(repo_id, user_id) - app.increment_repo_stars(repo_id) + app.add_star(repo_id, user_id)! + app.increment_repo_stars(repo_id)! } } fn (mut app App) check_repo_starred(repo_id int, user_id int) bool { - star := sql app.db { + stars := sql app.db { select from Star where repo_id == repo_id && user_id == user_id limit 1 - } + } or { [] } - return star.id != 0 + return stars.len != 0 } -fn (mut app App) remove_star(repo_id int, user_id int) { +fn (mut app App) remove_star(repo_id int, user_id int) ! { sql app.db { delete from Star where repo_id == repo_id && user_id == user_id - } + }! } diff --git a/src/tag_service.v b/src/tag_service.v index 52c619dc..239568e6 100644 --- a/src/tag_service.v +++ b/src/tag_service.v @@ -4,7 +4,7 @@ module main import time -fn (mut app App) fetch_tags(repo Repo) { +fn (mut app App) fetch_tags(repo Repo) ! { tags_output := repo.git('tag --format="%(refname:lstrip=2)${log_field_separator}%(objectname)${log_field_separator}%(subject)${log_field_separator}%(authoremail)${log_field_separator}%(creatordate:rfc)"') for tag_output in tags_output.split_into_lines() { @@ -24,16 +24,16 @@ fn (mut app App) fetch_tags(repo Repo) { } } - app.add_tag(repo.id, tag_name, commit_hash, commit_message, user.id, int(commit_date.unix)) + app.add_tag(repo.id, tag_name, commit_hash, commit_message, user.id, int(commit_date.unix))! } } -fn (mut app App) add_tag(repo_id int, tag_name string, commit_hash string, commit_message string, user_id int, date int) { - tag := sql app.db { +fn (mut app App) add_tag(repo_id int, tag_name string, commit_hash string, commit_message string, user_id int, date int) ! { + tags := sql app.db { select from Tag where repo_id == repo_id && name == tag_name limit 1 - } + } or { []Tag{} } - if tag.id != 0 { + if tags.len != 0 { return } @@ -48,11 +48,11 @@ fn (mut app App) add_tag(repo_id int, tag_name string, commit_hash string, commi sql app.db { insert new_tag into Tag - } + }! } fn (mut app App) get_all_repo_tags(repo_id int) []Tag { return sql app.db { select from Tag where repo_id == repo_id order by created_at desc - } + } or { [] } } diff --git a/src/token_service.v b/src/token_service.v index 3543b6dc..1a508f40 100644 --- a/src/token_service.v +++ b/src/token_service.v @@ -4,7 +4,7 @@ module main import rand -fn (mut app App) add_token(user_id int, ip string) string { +fn (mut app App) add_token(user_id int, ip string) !string { mut uuid := rand.uuid_v4() token := Token{ @@ -15,25 +15,25 @@ fn (mut app App) add_token(user_id int, ip string) string { sql app.db { insert token into Token - } + }! return uuid } fn (mut app App) get_token(value string) ?Token { - token := sql app.db { + tokens := sql app.db { select from Token where value == value limit 1 - } + } or { []Token{} } - if token.id == 0 { + if tokens.len == 0 { return none } - return token + return tokens.first() } -fn (mut app App) delete_tokens(user_id int) { +fn (mut app App) delete_tokens(user_id int) ! { sql app.db { delete from Token where user_id == user_id - } + }! } diff --git a/src/user_routes.v b/src/user_routes.v index cc362a01..cf55594d 100644 --- a/src/user_routes.v +++ b/src/user_routes.v @@ -31,11 +31,14 @@ pub fn (mut app App) handle_login(username string, password string) vweb.Result } if !compare_password_with_hash(password, user.salt, user.password) { - app.increment_user_login_attempts(user.id) + app.increment_user_login_attempts(user.id) or { + app.error('There was an error while logging in') + return app.login() + } if user.login_attempts == max_login_attempts { app.warn('User ${user.username} got blocked') - app.block_user(user.id) + app.block_user(user.id) or { app.info(err.str()) } } app.error('Wrong username/password') @@ -47,8 +50,11 @@ pub fn (mut app App) handle_login(username string, password string) vweb.Result return app.redirect_to_login() } - app.auth_user(user, app.ip()) - app.add_security_log(user_id: user.id, kind: .logged_in) + app.auth_user(user, app.ip()) or { + app.error('There was an error while logging in') + return app.login() + } + app.add_security_log(user_id: user.id, kind: .logged_in) or { app.info(err.str()) } return app.redirect('/${username}') } @@ -138,7 +144,10 @@ pub fn (mut app App) handle_update_user_settings(username string) vweb.Result { is_new_full_name := full_name != app.user.full_name if is_new_full_name { - app.change_full_name(app.user.id, full_name) + app.change_full_name(app.user.id, full_name) or { + app.error('There was an error while updating the settings') + return app.user_settings(username) + } } if is_new_username { @@ -150,8 +159,14 @@ pub fn (mut app App) handle_update_user_settings(username string) vweb.Result { return app.user_settings(username) } - app.change_username(app.user.id, new_username) - app.incement_namechanges(app.user.id) + app.change_username(app.user.id, new_username) or { + app.error('There was an error while updating the settings') + return app.user_settings(username) + } + app.incement_namechanges(app.user.id) or { + app.error('There was an error while updating the settings') + return app.user_settings(username) + } app.rename_user_directory(username, new_username) } @@ -165,7 +180,8 @@ fn (mut app App) rename_user_directory(old_name string, new_name string) { } pub fn (mut app App) register() vweb.Result { - no_users := app.get_users_count() == 0 + user_count := app.get_users_count() or { 0 } + no_users := user_count == 0 app.current_path = '' @@ -174,7 +190,11 @@ pub fn (mut app App) register() vweb.Result { ['/register'; post] pub fn (mut app App) handle_register(username string, email string, password string, no_redirect string) vweb.Result { - no_users := app.get_users_count() == 0 + user_count := app.get_users_count() or { + app.error('Failed to register') + return app.register() + } + no_users := user_count == 0 if username in ['login', 'register', 'new', 'new_post', 'oauth'] { app.error('Username `${username}` is not available') @@ -227,7 +247,14 @@ pub fn (mut app App) handle_register(username string, email string, password str return app.register() } - if !app.register_user(username, hashed_password, salt, [email], false, no_users) { + // TODO: refactor + is_registered := app.register_user(username, hashed_password, salt, [email], false, + no_users) or { + app.error('Failed to register') + return app.register() + } + + if !is_registered { app.error('Failed to register') return app.register() } @@ -238,13 +265,16 @@ pub fn (mut app App) handle_register(username string, email string, password str } if no_users { - app.add_admin(user.id) + app.add_admin(user.id) or { app.info(err.str()) } } client_ip := app.ip() - app.auth_user(user, client_ip) - app.add_security_log(user_id: user.id, kind: .registered) + app.auth_user(user, client_ip) or { + app.error('Failed to register') + return app.register() + } + app.add_security_log(user_id: user.id, kind: .registered) or { app.info(err.str()) } if no_redirect == '1' { return app.text('ok') @@ -285,7 +315,13 @@ pub fn (mut app App) handle_upload_avatar() vweb.Result { avatar_filename := '${username}.${file_extension}' app.write_user_avatar(avatar_filename, file_content) - app.update_user_avatar(app.user.id, avatar_filename) + app.update_user_avatar(app.user.id, avatar_filename) or { + response := api.ApiErrorResponse{ + message: 'There was an error while updating the avatar' + } + + return app.json(response) + } avatar_file_path := app.build_avatar_file_path(avatar_filename) avatar_file_url := app.build_avatar_file_url(avatar_filename) diff --git a/src/user_service.v b/src/user_service.v index bc09d124..0f216116 100644 --- a/src/user_service.v +++ b/src/user_service.v @@ -4,16 +4,16 @@ import crypto.sha256 import time import os -pub fn (mut app App) set_user_block_status(user_id int, status bool) { +pub fn (mut app App) set_user_block_status(user_id int, status bool) ! { sql app.db { update User set is_blocked = status where id == user_id - } + }! } -pub fn (mut app App) set_user_admin_status(user_id int, status bool) { +pub fn (mut app App) set_user_admin_status(user_id int, status bool) ! { sql app.db { update User set is_admin = status where id == user_id - } + }! } fn hash_password_with_salt(password string, salt string) string { @@ -26,7 +26,7 @@ fn compare_password_with_hash(password string, salt string, hashed string) bool return hash_password_with_salt(password, salt) == hashed } -pub fn (mut app App) register_user(username string, password string, salt string, emails []string, github bool, is_admin bool) bool { +pub fn (mut app App) register_user(username string, password string, salt string, emails []string, github bool, is_admin bool) !bool { mut user := app.get_user_by_username(username) or { User{} } if user.id != 0 && user.is_registered { @@ -49,7 +49,7 @@ pub fn (mut app App) register_user(username string, password string, salt string is_admin: is_admin } - app.add_user(user) + app.add_user(user)! mut u := app.get_user_by_username(user.username) or { app.info('User was not inserted') @@ -61,10 +61,10 @@ pub fn (mut app App) register_user(username string, password string, salt string return false } - app.add_activity(u.id, 'joined') + app.add_activity(u.id, 'joined')! for email in emails { - app.add_email(u.id, email) + app.add_email(u.id, email)! } u.emails = app.find_user_emails(u.id) @@ -79,7 +79,7 @@ pub fn (mut app App) register_user(username string, password string, salt string if user.is_registered { sql app.db { update User set is_github = true where id == user.id - } + }! return true } } @@ -98,19 +98,19 @@ fn (mut app App) create_user_dir(username string) { } } -pub fn (mut app App) update_user_avatar(user_id int, filename_or_url string) { +pub fn (mut app App) update_user_avatar(user_id int, filename_or_url string) ! { sql app.db { update User set avatar = filename_or_url where id == user_id - } + }! } -pub fn (mut app App) add_user(user User) { +pub fn (mut app App) add_user(user User) ! { sql app.db { insert user into User - } + }! } -pub fn (mut app App) add_email(user_id int, email string) { +pub fn (mut app App) add_email(user_id int, email string) ! { user_email := Email{ user_id: user_id email: email @@ -118,10 +118,10 @@ pub fn (mut app App) add_email(user_id int, email string) { sql app.db { insert user_email into Email - } + }! } -pub fn (mut app App) add_contributor(user_id int, repo_id int) { +pub fn (mut app App) add_contributor(user_id int, repo_id int) ! { if !app.contains_contributor(user_id, repo_id) { contributor := Contributor{ user_id: user_id @@ -130,31 +130,32 @@ pub fn (mut app App) add_contributor(user_id int, repo_id int) { sql app.db { insert contributor into Contributor - } + }! } } pub fn (app App) get_username_by_id(id int) ?string { - user := sql app.db { + users := sql app.db { select from User where id == id limit 1 - } + } or { [] } - if user.id == 0 { + if users.len == 0 { return none } - return user.username + return users.first().username } pub fn (app App) get_user_by_username(value string) ?User { - mut user := sql app.db { + users := sql app.db { select from User where username == value limit 1 - } + } or { [] } - if user.id == 0 { + if users.len == 0 { return none } + mut user := users.first() emails := app.find_user_emails(user.id) user.emails = emails @@ -162,14 +163,15 @@ pub fn (app App) get_user_by_username(value string) ?User { } pub fn (app App) get_user_by_id(id int) ?User { - mut user := sql app.db { + users := sql app.db { select from User where id == id - } + } or { [] } - if user.id == 0 { + if users.len == 0 { return none } + mut user := users.first() emails := app.find_user_emails(user.id) user.emails = emails @@ -177,14 +179,15 @@ pub fn (app App) get_user_by_id(id int) ?User { } pub fn (mut app App) get_user_by_github_username(name string) ?User { - mut user := sql app.db { + users := sql app.db { select from User where github_username == name limit 1 - } + } or { [] } - if user.id == 0 { + if users.len == 0 { return none } + mut user := users.first() emails := app.find_user_emails(user.id) user.emails = emails @@ -194,7 +197,7 @@ pub fn (mut app App) get_user_by_github_username(name string) ?User { pub fn (mut app App) get_user_by_email(value string) ?User { emails := sql app.db { select from Email where email == value - } + } or { [] } if emails.len != 1 { return none @@ -206,7 +209,7 @@ pub fn (mut app App) get_user_by_email(value string) ?User { pub fn (app App) find_user_emails(user_id int) []Email { emails := sql app.db { select from Email where user_id == user_id - } + } or { [] } return emails } @@ -214,7 +217,7 @@ pub fn (app App) find_user_emails(user_id int) []Email { pub fn (mut app App) find_repo_registered_contributor(id int) []User { contributors := sql app.db { select from Contributor where repo_id == id - } + } or { [] } mut users := []User{cap: contributors.len} @@ -231,7 +234,7 @@ pub fn (mut app App) get_all_registered_users_as_page(offset int) []User { // FIXME: 30 -> admin_users_per_page mut users := sql app.db { select from User where is_registered == true limit 30 offset offset - } + } or { [] } for i, user in users { users[i].emails = app.find_user_emails(user.id) @@ -243,7 +246,7 @@ pub fn (mut app App) get_all_registered_users_as_page(offset int) []User { pub fn (mut app App) get_all_registered_user_count() int { return sql app.db { select count from User where is_registered == true - } + } or { 0 } } fn (app App) search_users(query string) []User { @@ -263,27 +266,27 @@ fn (app App) search_users(query string) []User { return users } -pub fn (mut app App) get_users_count() int { +pub fn (mut app App) get_users_count() !int { return sql app.db { select count from User - } + } or { 0 } } -pub fn (mut app App) get_count_repo_contributors(id int) int { +pub fn (mut app App) get_count_repo_contributors(id int) !int { return sql app.db { select count from Contributor where repo_id == id - } + } or { 0 } } pub fn (mut app App) contains_contributor(user_id int, repo_id int) bool { contributors := sql app.db { select from Contributor where repo_id == repo_id && user_id == user_id - } + } or { [] } return contributors.len > 0 } -pub fn (mut app App) increment_user_post(mut user User) { +pub fn (mut app App) increment_user_post(mut user User) ! { user.posts_count++ u := *user @@ -295,24 +298,24 @@ pub fn (mut app App) increment_user_post(mut user User) { user.last_post_time = now sql app.db { update User set posts_count = 0, last_post_time = now where id == id - } + }! } sql app.db { update User set posts_count = posts_count + 1 where id == id - } + }! } -pub fn (mut app App) increment_user_login_attempts(user_id int) { +pub fn (mut app App) increment_user_login_attempts(user_id int) ! { sql app.db { update User set login_attempts = login_attempts + 1 where id == user_id - } + }! } -pub fn (mut app App) update_user_login_attempts(user_id int, attempts int) { +pub fn (mut app App) update_user_login_attempts(user_id int, attempts int) ! { sql app.db { update User set login_attempts = attempts where id == user_id - } + }! } pub fn (mut app App) check_user_blocked(user_id int) bool { @@ -321,29 +324,29 @@ pub fn (mut app App) check_user_blocked(user_id int) bool { return user.is_blocked } -fn (mut app App) change_username(user_id int, username string) { +fn (mut app App) change_username(user_id int, username string) ! { sql app.db { update User set username = username where id == user_id - } + }! sql app.db { update Repo set user_name = username where user_id == user_id - } + }! } -fn (mut app App) change_full_name(user_id int, full_name string) { +fn (mut app App) change_full_name(user_id int, full_name string) ! { sql app.db { update User set full_name = full_name where id == user_id - } + }! } -fn (mut app App) incement_namechanges(user_id int) { +fn (mut app App) incement_namechanges(user_id int) ! { now := int(time.now().unix) sql app.db { update User set namechanges_count = namechanges_count + 1, last_namechange_time = now where id == user_id - } + }! } fn (mut app App) check_username(username string) (bool, User) { @@ -356,10 +359,10 @@ fn (mut app App) check_username(username string) (bool, User) { return user.is_registered, user } -pub fn (mut app App) auth_user(user User, ip string) { - token := app.add_token(user.id, ip) +pub fn (mut app App) auth_user(user User, ip string) ! { + token := app.add_token(user.id, ip)! - app.update_user_login_attempts(user.id, 0) + app.update_user_login_attempts(user.id, 0)! expire_date := time.now().add_days(200) diff --git a/src/watch_service.v b/src/watch_service.v index c09e70b4..a69ab0e4 100644 --- a/src/watch_service.v +++ b/src/watch_service.v @@ -1,6 +1,6 @@ module main -fn (mut app App) watch_repo(repo_id int, user_id int) { +fn (mut app App) watch_repo(repo_id int, user_id int) ! { watch := Watch{ repo_id: repo_id user_id: user_id @@ -8,43 +8,43 @@ fn (mut app App) watch_repo(repo_id int, user_id int) { sql app.db { insert watch into Watch - } + }! } fn (mut app App) get_count_repo_watchers(repo_id int) int { return sql app.db { select count from Watch where repo_id == repo_id - } + } or { 0 } } fn (mut app App) find_watching_repo_ids(user_id int) []int { watch_list := sql app.db { select from Watch where user_id == user_id - } + } or { [] } return watch_list.map(it.repo_id) } -fn (mut app App) toggle_repo_watcher_status(repo_id int, user_id int) { +fn (mut app App) toggle_repo_watcher_status(repo_id int, user_id int) ! { is_watching := app.check_repo_watcher_status(repo_id, user_id) if is_watching { - app.unwatch_repo(repo_id, user_id) + app.unwatch_repo(repo_id, user_id)! } else { - app.watch_repo(repo_id, user_id) + app.watch_repo(repo_id, user_id)! } } fn (mut app App) check_repo_watcher_status(repo_id int, user_id int) bool { - watch := sql app.db { + watches := sql app.db { select from Watch where repo_id == repo_id && user_id == user_id limit 1 - } + } or { [] } - return watch.id != 0 + return watches.len != 0 } -fn (mut app App) unwatch_repo(repo_id int, user_id int) { +fn (mut app App) unwatch_repo(repo_id int, user_id int) ! { sql app.db { delete from Watch where repo_id == repo_id && user_id == user_id - } + }! }