This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Activity.go
81 lines (64 loc) · 1.55 KB
/
Activity.go
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
71
72
73
74
75
76
77
78
79
80
81
package arn
import (
"sort"
"sync"
"time"
"github.com/aerogo/nano"
)
// Activity is a user activity that appears in the follower's feeds.
type Activity interface {
Creator() *User
TypeName() string
GetID() string
GetCreated() string
GetCreatedBy() UserID
GetCreatedTime() time.Time
}
// SortActivitiesLatestFirst puts the latest entries on top.
func SortActivitiesLatestFirst(entries []Activity) {
sort.Slice(entries, func(i, j int) bool {
return entries[i].GetCreated() > entries[j].GetCreated()
})
}
// StreamActivities returns a stream of all activities.
func StreamActivities() <-chan Activity {
channel := make(chan Activity, nano.ChannelBufferSize)
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
for obj := range DB.All("ActivityCreate") {
channel <- obj.(Activity)
}
wg.Done()
}()
go func() {
for obj := range DB.All("ActivityConsumeAnime") {
channel <- obj.(Activity)
}
wg.Done()
}()
go func() {
wg.Wait()
close(channel)
}()
return channel
}
// AllActivities returns a slice of all activities.
func AllActivities() []Activity {
all := make([]Activity, 0, DB.Collection("ActivityCreate").Count()+DB.Collection("ActivityConsumeAnime").Count())
stream := StreamActivities()
for obj := range stream {
all = append(all, obj)
}
return all
}
// FilterActivities filters all Activities by a custom function.
func FilterActivities(filter func(Activity) bool) []Activity {
var filtered []Activity
for obj := range StreamActivities() {
if filter(obj) {
filtered = append(filtered, obj)
}
}
return filtered
}