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