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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
package main
import (
"database/sql"
"encoding/json"
"net/http"
_ "github.com/lib/pq"
)
func GetActorFollowing(w http.ResponseWriter, db *sql.DB, id string) {
var following Collection
following.AtContext.Context = "https://www.w3.org/ns/activitystreams"
following.Type = "Collection"
following.TotalItems, _ = GetActorFollowTotal(db, id)
following.Items = GetActorFollowingDB(db, id)
enc, _ := json.MarshalIndent(following, "", "\t")
w.Header().Set("Content-Type", activitystreams)
w.Write(enc)
}
func GetActorFollowers(w http.ResponseWriter, db *sql.DB, id string) {
var following Collection
following.AtContext.Context = "https://www.w3.org/ns/activitystreams"
following.Type = "Collection"
_, following.TotalItems = GetActorFollowTotal(db, id)
following.Items = GetActorFollowDB(db, id)
enc, _ := json.MarshalIndent(following, "", "\t")
w.Header().Set("Content-Type", activitystreams)
w.Write(enc)
}
func GetActorFollowingDB(db *sql.DB, id string) []ObjectBase {
var followingCollection []ObjectBase
query := `select following from following where id=$1`
rows, err := db.Query(query, id)
CheckError(err, "error with following db query")
defer rows.Close()
for rows.Next() {
var obj ObjectBase
err := rows.Scan(&obj.Id)
CheckError(err, "error with following db scan")
followingCollection = append(followingCollection, obj)
}
return followingCollection
}
func GetActorFollowDB(db *sql.DB, id string) []ObjectBase {
var followerCollection []ObjectBase
query := `select follower from follower where id=$1`
rows, err := db.Query(query, id)
CheckError(err, "error with follower db query")
defer rows.Close()
for rows.Next() {
var obj ObjectBase
err := rows.Scan(&obj.Id)
CheckError(err, "error with followers db scan")
followerCollection = append(followerCollection, obj)
}
return followerCollection
}
func GetActorFollowTotal(db *sql.DB, id string) (int, int) {
var following int
var followers int
query := `select count(following) from following where id=$1`
rows, err := db.Query(query, id)
CheckError(err, "error with following total db query")
defer rows.Close()
for rows.Next() {
err := rows.Scan(&following)
CheckError(err, "error with following total db scan")
}
query = `select count(follower) from follower where id=$1`
rows, err = db.Query(query, id)
CheckError(err, "error with followers total db query")
defer rows.Close()
for rows.Next() {
err := rows.Scan(&followers)
CheckError(err, "error with followers total db scan")
}
return following, followers
}
func AcceptFollow(activity Activity) Activity {
var accept Activity
accept.AtContext.Context = activity.AtContext.Context
accept.Type = "Accept"
var nActor Actor
accept.Actor = &nActor
accept.Actor.Id = activity.Object.Actor
var nObj ObjectBase
accept.Object = &nObj
accept.Object.Actor = activity.Actor.Id
var nNested NestedObjectBase
accept.Object.Object = &nNested
accept.Object.Object.Actor = activity.Object.Actor
accept.Object.Object.Type = "Follow"
accept.To = append(accept.To, activity.Object.Actor)
return accept
}
func RejectActivity(activity Activity) Activity {
var accept Activity
accept.AtContext.Context = activity.AtContext.Context
accept.Type = "Reject"
var nObj ObjectBase
accept.Object = &nObj
var nActor Actor
accept.Actor = &nActor
accept.Actor.Id = activity.Object.Actor
accept.Object.Actor = activity.Actor.Id
var nNested NestedObjectBase
accept.Object.Object = &nNested
accept.Object.Object.Actor = activity.Object.Actor
accept.Object.Object.Type = "Follow"
accept.To = append(accept.To, activity.Actor.Id)
return accept
}
func IsAlreadyFollowing(db *sql.DB, actor string, follow string) bool {
followers := GetActorFollowingDB(db, actor)
for _, e := range followers {
if e.Id == follow {
return true
}
}
return false;
}
func IsAlreadyFollower(db *sql.DB, actor string, follow string) bool {
followers := GetActorFollowDB(db, actor)
for _, e := range followers {
if e.Id == follow {
return true
}
}
return false;
}
func SetActorFollowerDB(db *sql.DB, activity Activity) Activity {
var query string
alreadyFollow := IsAlreadyFollower(db, activity.Actor.Id, activity.Object.Actor)
activity.Type = "Reject"
if activity.Actor.Id == activity.Object.Actor {
return activity
}
if alreadyFollow {
query = `delete from follower where id=$1 and follower=$2`
activity.Summary = activity.Object.Actor + " Unfollow " + activity.Actor.Id
_, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor)
if CheckError(err, "error with follower db delete") != nil {
activity.Type = "Reject"
return activity
}
activity.Type = "Accept"
return activity
} else {
query = `insert into follower (id, follower) values ($1, $2)`
activity.Summary = activity.Object.Actor + " Follow " + activity.Actor.Id
_, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor)
if CheckError(err, "error with follower db insert") != nil {
activity.Type = "Reject"
return activity
}
activity.Type = "Accept"
return activity
}
return activity
}
func SetActorFollowingDB(db *sql.DB, activity Activity) Activity {
var query string
alreadyFollowing := false
alreadyFollower := false
following := GetActorFollowingDB(db, activity.Object.Actor)
actor := FingerActor(activity.Actor.Id)
remoteActorFollowerCol := GetCollectionFromReq(actor.Followers)
for _, e := range following {
if e.Id == activity.Actor.Id {
alreadyFollowing = true
}
}
for _, e := range remoteActorFollowerCol.Items {
if e.Id == activity.Object.Actor {
alreadyFollower = true
}
}
activity.Type = "Reject"
if activity.Actor.Id == activity.Object.Actor {
return activity
}
if alreadyFollowing && alreadyFollower {
query = `delete from following where id=$1 and following=$2`
activity.Summary = activity.Object.Actor + " Unfollowing " + activity.Actor.Id
if !IsActorLocal(db, activity.Actor.Id) {
go DeleteActorCache(db, activity.Actor.Id)
}
_, err := db.Exec(query, activity.Object.Actor, activity.Actor.Id)
if CheckError(err, "error with following db delete") != nil {
activity.Type = "Reject"
return activity
}
activity.Type = "Accept"
return activity
}
if !alreadyFollowing && !alreadyFollower {
query = `insert into following (id, following) values ($1, $2)`
activity.Summary = activity.Object.Actor + " Following " + activity.Actor.Id
if !IsActorLocal(db, activity.Actor.Id) {
go WriteActorToCache(db, activity.Actor.Id)
}
_, err := db.Exec(query, activity.Object.Actor, activity.Actor.Id)
if CheckError(err, "error with following db insert") != nil {
activity.Type = "Reject"
return activity
}
activity.Type = "Accept"
return activity
}
return activity
}
func AutoFollow(db *sql.DB, actor string) {
following := GetActorFollowingDB(db, actor)
follower := GetActorFollowDB(db, actor)
isFollowing := false
for _, e := range follower {
for _, k := range following {
if e.Id == k.Id {
isFollowing = true
}
}
if !isFollowing && e.Id != Domain && e.Id != actor {
followActivity := MakeFollowActivity(db, actor, e.Id)
nActor := FingerActor(e.Id)
if nActor.Id != "" {
MakeActivityRequestOutbox(db, followActivity)
}
}
}
}
func MakeFollowActivity(db *sql.DB, actor string, follow string) Activity {
var followActivity Activity
followActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams"
followActivity.Type = "Follow"
var obj ObjectBase
var nactor Actor
if actor == Domain {
nactor = GetActorFromDB(db, actor)
} else {
nactor = FingerActor(actor)
}
followActivity.Actor = &nactor
followActivity.Object = &obj
followActivity.Object.Actor = follow
followActivity.To = append(followActivity.To, follow)
return followActivity
}
|