Skip to content

Commit

Permalink
add user functions & repo functions (vlang#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisSchmieder authored Jul 20, 2020
1 parent d996264 commit 165291a
Show file tree
Hide file tree
Showing 21 changed files with 468 additions and 82 deletions.
4 changes: 2 additions & 2 deletions api.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import json

['/api/:user/:repo/issues']
pub fn (mut app App) api_issues(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.json('{}')
}
issues := app.find_repo_issues(app.repo.id)
Expand All @@ -16,7 +16,7 @@ pub fn (mut app App) api_issues(user, repo string) vweb.Result {

['/api/:user/:repo/commits']
pub fn (mut app App) api_commits(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.json('{}')
}
commits := app.find_repo_commits(app.repo.id)
Expand Down
6 changes: 6 additions & 0 deletions branch.v
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ fn (mut app App) contains_repo_branch(name string, repo_id int) bool {
}
return nr == 1
}

fn (mut app App) delete_repo_branches(repo_id int) {
sql app.db {
delete from Branch where repo_id == repo_id
}
}
10 changes: 10 additions & 0 deletions file.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ fn (mut app App) find_repo_file_by_path(repo_id int, branch, path string) ?File
}
return file
}

fn (mut app App) delete_repo_files(repo_id int) {
sql app.db {
delete from File where repo_id == repo_id
}
}

fn (mut app App) delete_repo_folder(path string) {
os.rmdir_all(os.real_path(path))
}
118 changes: 71 additions & 47 deletions gitly.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const (
max_login_attempts = 5
repo_storage_path = './repos'
max_user_repos = 5
max_repo_name_len = 20
max_repo_name_len = 20
max_namechanges = 3
namechange_period = time.hour * 24
)

struct App {
Expand Down Expand Up @@ -139,6 +141,7 @@ pub fn (mut app App) init_once() {

pub fn (mut app App) init() {
url := app.vweb.req.url
app.show_menu = false
app.page_gen_time = ''
app.info('\n\ninit() url=$url')
app.path = ''
Expand Down Expand Up @@ -212,7 +215,7 @@ pub fn (mut app App) repo_settings(user, repo string) vweb.Result {
if !app.logged_in {
return app.vweb.redirect('/$user/$repo')
}
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.redirect('/$user/$repo')
}
if app.repo.user_id != app.user.id {
Expand All @@ -223,18 +226,16 @@ pub fn (mut app App) repo_settings(user, repo string) vweb.Result {
return $vweb.html()
}

// Helper function
fn (mut app App) repo_belongs_to(user, repo string) bool {
return app.logged_in && app.exists_user_repo(user, repo) && app.repo.user_id == app.user.id
}

['/:user/:repo/repo_settings_post']
pub fn (mut app App) repo_settings_post(user, repo string) vweb.Result {
if !app.logged_in {
return app.r_repo()
}
if !app.find_repo(user, repo) {
if !app.repo_belongs_to(user, repo) {
return app.r_repo()
}
if app.repo.user_id != app.user.id {
return app.r_repo()
}

if 'webhook_secret' in app.vweb.form && app.vweb.form['webhook_secret'] != app.repo.webhook_secret && app.vweb.form['webhook_secret'] != '' {
webhook := sha1.hexhash(app.vweb.form['webhook_secret'])
app.update_repo_webhook(app.repo.id, webhook)
Expand All @@ -243,9 +244,52 @@ pub fn (mut app App) repo_settings_post(user, repo string) vweb.Result {
return app.r_repo()
}

['/:user/:repo/delete_repo_post']
pub fn (mut app App) repo_delete_post(user, repo string) vweb.Result {
if !app.repo_belongs_to(user, repo) {
return app.r_repo()
}
if 'verify' in app.vweb.form && app.vweb.form['verify'] == '$user/$repo' {
go app.delete_repo(app.repo.id, app.repo.git_dir, app.repo.name)
} else {
app.vweb.error('Verification failed')
return app.repo_settings(user, repo)
}

return app.r_home()
}

['/:user/:repo/move_repo_post']
pub fn (mut app App) repo_move_post(user, repo string) vweb.Result {
if !app.repo_belongs_to(user, repo) {
return app.r_repo()
}
if 'verify' in app.vweb.form && 'dest' in app.vweb.form && app.vweb.form['verify'] == '$user/$repo' {
uname := app.vweb.form['dest']
dest_user := app.find_user_by_username(uname) or {
app.vweb.error('Unknown user $uname')
return app.repo_settings(user, repo)
}
if app.user_has_repo(dest_user.id, app.repo.name) {
app.vweb.error('User already owns repo $app.repo.name')
return app.repo_settings(user, repo)
}
if app.nr_user_repos(dest_user.id) >= max_user_repos {
app.vweb.error('User already reached the repo limit')
return app.repo_settings(user, repo)
}
app.move_repo_to_user(app.repo.id, dest_user.id, dest_user.username)
return app.vweb.redirect('/$dest_user.username/$app.repo.name')
} else {
app.vweb.error('Verification failed')
return app.repo_settings(user, repo)
}
return app.r_home()
}

['/:user/:repo']
pub fn (mut app App) tree2(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
return app.tree(user, repo, app.repo.primary_branch, '')
Expand All @@ -254,7 +298,7 @@ pub fn (mut app App) tree2(user, repo string) vweb.Result {
// pub fn (mut app App) tree(path string) {
['/:user/:repo/tree/:branch/:path...']
pub fn (mut app App) tree(user, repo, branch, path string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
println('\n\n\ntree() user="$user" repo="' + repo + '"')
Expand Down Expand Up @@ -335,7 +379,7 @@ pub fn (mut app App) index() vweb.Result {

['/:user/:repo/update']
pub fn (mut app App) update(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
/*secret := if 'X-Hub-Signature' in app.vweb.req.headers { app.vweb.req.headers['X-Hub-Signature'][5..] } else { '' }
Expand All @@ -348,7 +392,7 @@ pub fn (mut app App) update(user, repo string) vweb.Result {
if app.user.is_admin {
go app.update_repo_data(app.repo)
}
return app.r_home()
return app.r_repo()
}

pub fn (mut app App) new() vweb.Result {
Expand Down Expand Up @@ -398,27 +442,7 @@ pub fn (mut app App) new_post() vweb.Result {
}
go app.update_repo()
println('end go')
return app.vweb.redirect('/$app.user.username')
}

['/:username']
pub fn (mut app App) user(username string) vweb.Result {
println('user() name=$username')
app.show_menu = false
mut user := User{}
if username.len != 0 {
user = app.find_user_by_username(username) or {
return app.vweb.not_found()
}
} else {
return app.vweb.not_found()
}
user.b_avatar = user.avatar != ''
if !user.b_avatar {
user.avatar = user.username.bytes()[0].str()
}
repos := app.find_user_repos(user.id)
return $vweb.html()
return app.vweb.redirect('/$app.user.username/repos')
}

['/:user/:repo/commits']
Expand All @@ -428,7 +452,7 @@ pub fn (mut app App) commits2(user, repo string) vweb.Result {

['/:user/:repo/commits/:page_str']
pub fn (mut app App) commits(user, repo, page_str string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
app.show_menu = true
Expand Down Expand Up @@ -501,7 +525,7 @@ pub fn (mut app App) commits(user, repo, page_str string) vweb.Result {

['/:user/:repo/commit/:hash']
pub fn (mut app App) commit(user, repo, hash string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
commit := app.find_repo_commit_by_hash(app.repo.id, hash)
Expand All @@ -525,7 +549,7 @@ pub fn (mut app App) issues2(user, repo string) vweb.Result {

['/:user/:repo/issues/:page_str']
pub fn (mut app App) issues(user, repo, page_str string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
app.vweb.not_found()
}
app.show_menu = true
Expand Down Expand Up @@ -560,7 +584,7 @@ pub fn (mut app App) issues(user, repo, page_str string) vweb.Result {

['/:user/:repo/issue/:id']
pub fn (mut app App) issue(user, repo, id_str string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
app.show_menu = true
Expand All @@ -579,7 +603,7 @@ pub fn (mut app App) issue(user, repo, id_str string) vweb.Result {

['/:user/:repo/pull/:id']
pub fn (mut app App) pull(user, repo, id_str string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
_ := app.path.split('/')
Expand All @@ -599,7 +623,7 @@ pub fn (mut app App) pulls() vweb.Result {

['/:user/:repo/contributors']
pub fn (mut app App) contributors(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
contributors := app.find_repo_registered_contributor(app.repo.id)
Expand All @@ -608,7 +632,7 @@ pub fn (mut app App) contributors(user, repo string) vweb.Result {

['/:user/:repo/branches']
pub fn (mut app App) branches(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
app.show_menu = true
Expand All @@ -618,7 +642,7 @@ pub fn (mut app App) branches(user, repo string) vweb.Result {

['/:user/:repo/releases']
pub fn (mut app App) releases(user_str, repo string) vweb.Result {
if !app.find_repo(user_str, repo) {
if !app.exists_user_repo(user_str, repo) {
return app.vweb.not_found()
}
app.show_menu = true
Expand Down Expand Up @@ -652,7 +676,7 @@ pub fn (mut app App) releases(user_str, repo string) vweb.Result {

['/:user/:repo/blob/:branch/:path...']
pub fn (mut app App) blob(user, repo, branch, path string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
app.path = path
Expand Down Expand Up @@ -696,7 +720,7 @@ pub fn (mut app App) blob(user, repo, branch, path string) vweb.Result {

['/:user/:repo/issues/new']
pub fn (mut app App) new_issue(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
if !app.logged_in {
Expand All @@ -707,7 +731,7 @@ pub fn (mut app App) new_issue(user, repo string) vweb.Result {

['/:user/:repo/new_issue_post']
pub fn (mut app App) new_issue_post(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
if !app.logged_in || (app.logged_in && app.user.nr_posts >= posts_per_day) {
Expand All @@ -733,7 +757,7 @@ pub fn (mut app App) new_issue_post(user, repo string) vweb.Result {

['/:user/:repo/comment_post']
pub fn (mut app App) comment_post(user, repo string) vweb.Result {
if !app.find_repo(user, repo) {
if !app.exists_user_repo(user, repo) {
return app.vweb.not_found()
}
text := app.vweb.form['text']
Expand Down
6 changes: 3 additions & 3 deletions hl/hl.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn highlight_text(st, ext string, commit bool) (string, int, int) {
res << '<table class="hl_table">'.bytes()
res << `\n`
if !is_single_line(st) {
res << '<tr><td><a id="1" href="#1">1</a></td><td>'.bytes()
res << '<tr><td><a id="1" class="no_select" href="#1">1</a></td><td>'.bytes()
lines++
}
mut in_comment := false
Expand All @@ -52,9 +52,9 @@ pub fn highlight_text(st, ext string, commit bool) (string, int, int) {
class = 'class="d"'
}
res <<
'</td></tr>\n<tr><td><a id="$lines" href="#$lines">$lines</a></td><td $class>'.bytes()
'</td></tr>\n<tr><td><a id="$lines" class="no_select" href="#$lines">$lines</a></td><td $class>'.bytes()
} else {
res << '</td></tr>\n<tr><td><a id="$lines" href="#$lines">$lines</a></td><td>'.bytes()
res << '</td></tr>\n<tr><td><a id="$lines" class="no_select" href="#$lines">$lines</a></td><td>'.bytes()
}
if in_line_comment {
in_line_comment = false
Expand Down
14 changes: 14 additions & 0 deletions issue.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mut:
status IssueStatus [skip]
linked_issues []int [skip]
author_name string [skip]
repo_author string [skip]
repo_name string [skip]
}

enum IssueStatus {
Expand Down Expand Up @@ -81,6 +83,18 @@ fn (mut app App) find_repo_prs(repo_id int) []Issue {
return issues
}

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
}
}

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) inc_issue_comments(id int) {
sql app.db {
update Issue set nr_comments = nr_comments + 1 where id == id
Expand Down
2 changes: 1 addition & 1 deletion lang_stat.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn (l &LangStat) pct_html() vweb.RawHtml {

pub fn (mut app App) find_repo_lang_stats(repo_id int) []LangStat {
lang_stats := sql app.db {
select from LangStat where repo_id == repo_id
select from LangStat where repo_id == repo_id order by pct desc
}
return lang_stats
}
6 changes: 3 additions & 3 deletions login.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import vweb
import time
import rand
import math
import strings

//['/login']
pub fn (mut app App) login() vweb.Result {
Expand Down Expand Up @@ -58,8 +57,9 @@ 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)
//println('cookie: setting token=$token id=$user.id')
app.vweb.set_cookie(name: 'id', value:user.id.str())
app.vweb.set_cookie(name:'token', value:token)
expire_date := time.now().add_days(200)
app.vweb.set_cookie(name: 'id', value:user.id.str(), expires: expire_date)
app.vweb.set_cookie(name:'token', value:token, expires: expire_date)
//app.vweb.set_cookie_with_expire_date('id', user.id.str(), expires)
//app.vweb.set_cookie_with_expire_date('token', token, expires)
}
Expand Down
Loading

0 comments on commit 165291a

Please sign in to comment.