forked from davecheney/pub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activitypub.go
198 lines (178 loc) · 5.19 KB
/
activitypub.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package activitypub
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/bardic/pub/activitypub/activities"
"github.com/bardic/pub/internal/algorithms"
"github.com/bardic/pub/internal/httpx"
"github.com/bardic/pub/internal/streaming"
"github.com/bardic/pub/internal/to"
"github.com/bardic/pub/models"
"github.com/go-chi/chi/v5"
"gorm.io/gorm"
"golang.org/x/exp/slog"
)
type Env struct {
*gorm.DB
*streaming.Mux
Logger *slog.Logger
}
func (e *Env) Log() *slog.Logger {
return e.Logger
}
func Followers(env *Env, w http.ResponseWriter, r *http.Request) error {
var followers []*models.Relationship
query := env.DB.Joins("JOIN actors ON actors.id = relationships.target_id and actors.name = ? and actors.domain = ?", chi.URLParam(r, "name"), r.Host)
if err := query.Model(&models.Relationship{}).Preload("Actor").Find(&followers, "following = true").Error; err != nil {
return err
}
return to.JSON(w, map[string]any{
"@context": "https://www.w3.org/ns/activitystreams",
"id": fmt.Sprintf("https://%s%s", r.Host, r.URL.Path),
"type": "OrderedCollection",
"totalItems": len(followers),
"orderedItems": algorithms.Map(
followers,
func(r *models.Relationship) string {
return r.Actor.URI
},
),
})
}
func Following(env *Env, w http.ResponseWriter, r *http.Request) error {
var following []*models.Relationship
query := env.DB.Joins("JOIN actors ON actors.id = relationships.actor_id and actors.name = ? and actors.domain = ?", chi.URLParam(r, "name"), r.Host)
if err := query.Model(&models.Relationship{}).Preload("Target").Find(&following, "following = true").Error; err != nil {
return err
}
return to.JSON(w, map[string]any{
"@context": "https://www.w3.org/ns/activitystreams",
"id": fmt.Sprintf("https://%s%s", r.Host, r.URL.Path),
"type": "OrderedCollection",
"totalItems": len(following),
"orderedItems": algorithms.Map(
following,
func(r *models.Relationship) string {
return r.Target.URI
},
),
})
}
func CollectionsShow(env *Env, w http.ResponseWriter, r *http.Request) error {
var actor models.Actor
if err := env.DB.Take(&actor, "name = ? and domain = ?", chi.URLParam(r, "name"), r.Host).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return httpx.Error(http.StatusNotFound, err)
}
return err
}
return to.JSON(w, map[string]any{
"@context": "https://www.w3.org/ns/activitystreams",
"id": fmt.Sprintf("https://%s%s", r.Host, r.URL.Path),
"type": "OrderedCollection",
"totalItems": 0,
"orderedItems": []any{},
})
}
func boolFromAny(v any) bool {
b, _ := v.(bool)
return b
}
func stringFromAny(v any) string {
s, _ := v.(string)
return s
}
func mapFromAny(v any) map[string]any {
m, _ := v.(map[string]any)
return m
}
func timeFromAny(v any) (time.Time, error) {
switch v := v.(type) {
case string:
return time.Parse(time.RFC3339, v)
case time.Time:
return v, nil
default:
return time.Time{}, errors.New("timeFromAny: invalid type")
}
}
func intFromAny(v any) int {
switch v := v.(type) {
case int:
return v
case float64:
// shakes fist at json number type
return int(v)
}
return 0
}
func anyToSlice(v any) []any {
switch v := v.(type) {
case []any:
return v
default:
return nil
}
}
// parseBool parses a boolean value from a request parameter.
// If the parameter is not present, it returns false.
// If the parameter is present but cannot be parsed, it returns false
func parseBool(r *http.Request, key string) bool {
switch r.URL.Query().Get(key) {
case "true", "1":
return true
default:
return false
}
}
// Follow sends a follow request from the Account to the Target Actor's inbox.
func Follow(ctx context.Context, follower *models.Account, target *models.Actor) error {
inbox := target.Inbox()
if inbox == "" {
return fmt.Errorf("no inbox found for %s", target.URI)
}
c, err := NewClient(follower)
if err != nil {
return err
}
return c.Post(ctx, inbox, activities.Follow(follower.Actor, target))
}
// Unfollow sends an unfollow request from the Account to the Target Actor's inbox.
func Unfollow(ctx context.Context, follower *models.Account, target *models.Actor) error {
inbox := target.Inbox()
if inbox == "" {
return fmt.Errorf("no inbox found for %s", target.URI)
}
c, err := NewClient(follower)
if err != nil {
return err
}
return c.Post(ctx, inbox, activities.Unfollow(follower.Actor, target))
}
// Like sends a like request from the Account to the Statuses Actor's inbox.
func Like(ctx context.Context, liker *models.Account, target *models.Status) error {
inbox := target.Actor.Inbox()
if inbox == "" {
return fmt.Errorf("no inbox found for %s", target.Actor.URI)
}
c, err := NewClient(liker)
if err != nil {
return err
}
return c.Post(ctx, inbox, activities.Like(liker.Actor, target.URI))
}
// Unlike sends an undo like request from the Account to the Statuses Actor's inbox.
func Unlike(ctx context.Context, liker *models.Account, target *models.Status) error {
inbox := target.Actor.Inbox()
if inbox == "" {
return fmt.Errorf("no inbox found for %s", target.Actor.URI)
}
c, err := NewClient(liker)
if err != nil {
return err
}
return c.Post(ctx, inbox, activities.Unlike(liker.Actor, target.URI))
}