-
Notifications
You must be signed in to change notification settings - Fork 0
/
following.go
93 lines (65 loc) · 2.2 KB
/
following.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
82
83
84
85
86
87
88
89
90
91
92
93
package activitypub
import (
"context"
"fmt"
"log/slog"
"sync/atomic"
"time"
"github.com/sfomuseum/go-activitypub/id"
)
type Following struct {
Id int64 `json:"id"`
AccountId int64 `json:"account_id"`
FollowingAddress string `json:"following_address"`
Created int64 `json:"created"`
}
func CountFollowing(ctx context.Context, db FollowingDatabase, account_id int64) (uint32, error) {
count := uint32(0)
following_cb := func(ctx context.Context, following string) error {
atomic.AddUint32(&count, 1)
return nil
}
err := db.GetFollowingForAccount(ctx, account_id, following_cb)
if err != nil {
return 0, fmt.Errorf("Failed to count following, %w", err)
}
return atomic.LoadUint32(&count), nil
}
func GetFollowing(ctx context.Context, db FollowingDatabase, account_id int64, following_address string) (*Following, error) {
slog.Debug("Get following", "account", account_id, "following", following_address)
return db.GetFollowing(ctx, account_id, following_address)
}
func AddFollowing(ctx context.Context, db FollowingDatabase, account_id int64, following_address string) error {
slog.Debug("Add following", "account", account_id, "following", following_address)
f, err := NewFollowing(ctx, account_id, following_address)
if err != nil {
return fmt.Errorf("Failed to create new following, %w", err)
}
return db.AddFollowing(ctx, f)
}
func NewFollowing(ctx context.Context, account_id int64, following_address string) (*Following, error) {
db_id, err := id.NewId()
if err != nil {
return nil, fmt.Errorf("Failed to create new following ID, %w", err)
}
now := time.Now()
ts := now.Unix()
b := &Following{
Id: db_id,
AccountId: account_id,
FollowingAddress: following_address,
Created: ts,
}
return b, nil
}
// Is account_id following following_address?
func IsFollowing(ctx context.Context, db FollowingDatabase, account_id int64, following_address string) (bool, *Following, error) {
f, err := GetFollowing(ctx, db, account_id, following_address)
if err == nil {
return true, f, nil
}
if err == ErrNotFound {
return false, nil, nil
}
return false, nil, fmt.Errorf("Failed to following record, %w", err)
}