| 1 | module main |
| 2 | |
| 3 | import time |
| 4 | |
| 5 | // now only for commits |
| 6 | struct FeedItem { |
| 7 | id int |
| 8 | author_name string |
| 9 | created_at time.Time |
| 10 | repo_name string |
| 11 | repo_owner string |
| 12 | branch_name string |
| 13 | message string |
| 14 | } |
| 15 | |
| 16 | const feed_items_per_page = 30 |
| 17 | |
| 18 | fn (mut app App) build_user_feed_as_page(user_id int, offset int) []FeedItem { |
| 19 | mut feed := []FeedItem{} |
| 20 | repo_ids := app.find_watching_repo_ids(user_id) |
| 21 | if repo_ids.len == 0 { |
| 22 | return [] |
| 23 | } |
| 24 | where_repo_ids := repo_ids.map(it.str()).join(', ') |
| 25 | |
| 26 | commits := db_exec_values(app.db, ' |
| 27 | select author, hash, created_at, repo_id, branch_id, message from ${sql_table('Commit')} |
| 28 | where repo_id in (${where_repo_ids}) order by created_at desc |
| 29 | limit ${feed_items_per_page} offset ${offset}') or { |
| 30 | return [] |
| 31 | } |
| 32 | mut item_id := 0 |
| 33 | |
| 34 | println(commits) |
| 35 | |
| 36 | for commit in commits { |
| 37 | vals := commit |
| 38 | author_name := vals[0] |
| 39 | commit_hash := vals[1] |
| 40 | created_at_unix := vals[2].int() |
| 41 | repo_id := vals[3].int() |
| 42 | branch_id := vals[4].int() |
| 43 | commit_message := vals[5] |
| 44 | |
| 45 | repo := app.find_repo_by_id(repo_id) or { continue } |
| 46 | repo_owner := app.get_username_by_id(repo.user_id) or { '' } |
| 47 | branch := app.find_repo_branch_by_id(repo_id, branch_id) |
| 48 | created_at := time.unix(created_at_unix) |
| 49 | |
| 50 | item := FeedItem{ |
| 51 | id: item_id++ |
| 52 | author_name: author_name |
| 53 | created_at: created_at |
| 54 | repo_name: repo.name |
| 55 | repo_owner: repo_owner |
| 56 | branch_name: branch.name |
| 57 | message: '${commit_message} (${commit_hash})' |
| 58 | } |
| 59 | |
| 60 | feed << item |
| 61 | } |
| 62 | |
| 63 | return feed |
| 64 | } |
| 65 | |
| 66 | fn (mut app App) get_feed_items_count(user_id int) int { |
| 67 | repo_ids := app.find_watching_repo_ids(user_id) |
| 68 | if repo_ids.len == 0 { |
| 69 | return 0 |
| 70 | } |
| 71 | where_repo_ids := repo_ids.map(it.str()).join(', ') |
| 72 | |
| 73 | count_result := db_exec_values(app.db, |
| 74 | 'select count(id) from ${sql_table('Commit')} where repo_id in (${where_repo_ids})') or { |
| 75 | return 0 |
| 76 | } |
| 77 | |
| 78 | return count_result.first().first().int() |
| 79 | } |
| 80 | |