aboutsummaryrefslogtreecommitdiff
path: root/Follow.go
blob: 8fc6200b2bd75904341bf903908d71f15628f7e1 (plain)
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
package main

import "net/http"
import "database/sql"
import _ "github.com/lib/pq"
import "encoding/json"

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, _ = GetActorFollowDB(db, id)

	enc, _ := json.MarshalIndent(following, "", "\t")							
	w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/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", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
	w.Write(enc)
}

func SetActorFollowDB(db *sql.DB, activity Activity, actor string) Activity {
	var query string
	alreadyFollow := false
	following, follower := GetActorFollowDB(db, actor)
	
	if activity.Actor.Id == actor {
		for _, e := range following {
			if e.Id == activity.Object.Id {
				alreadyFollow = true
			}
		}
		if alreadyFollow {
			query = `delete from following where id=$1 and following=$2`
			activity.Summary = activity.Actor.Id + " Unfollow " + activity.Object.Id
		} else {
			query = `insert into following (id, following) values ($1, $2)`
			activity.Summary = activity.Actor.Id + " Follow " + activity.Object.Id			
		}
	} else {
		for _, e := range follower {
			if e.Id == activity.Actor.Id {
				alreadyFollow = true
			}
		}
		if alreadyFollow {
			query = `delete from follower where id=$1 and follower=$2`
			activity.Summary = activity.Actor.Id + " Unfollow " + activity.Object.Id			
		} else {		
			query = `insert into follower (id, follower) values ($1, $2)`
			activity.Summary = activity.Actor.Id + " Follow " + activity.Object.Id						
		}
	}
	
	_, err := db.Exec(query, activity.Actor.Id, activity.Object.Id)

	CheckError(err, "error with follow db insert/delete")

	return activity
}

func GetActorFollowDB(db *sql.DB, id string) ([]ObjectBase, []ObjectBase) {
	var followingCollection []ObjectBase
	var followerCollection []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)
	}

	query = `select follower from follower where id=$1`

	rows, err = db.Query(query, id)

	CheckError(err, "error with followers 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 followingCollection, 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, actor Actor) Activity {
	var accept Activity
	var obj ObjectBase

	obj.Type = activity.Type
	obj.Actor = activity.Actor

	var nobj NestedObjectBase
	obj.Object = &nobj
	obj.Object.Id = activity.Object.Id

	accept.AtContext.Context = activity.AtContext.Context
	accept.Type = "Accept"

	var nactor Actor
	accept.Actor = &nactor
	accept.Actor.Id = actor.Id
	accept.Object = &obj
	accept.To = append(accept.To, activity.Object.Id)

	return accept
}

func RejectFollow(activity Activity, actor Actor) Activity {
	var accept Activity
	var obj ObjectBase

	obj.Type = activity.Type
	obj.Actor = activity.Actor
	obj.Object = new(NestedObjectBase)
	obj.Object.Id = activity.Object.Id

	accept.AtContext.Context = activity.AtContext.Context
	accept.Type = "Reject"
	accept.Actor = &actor
	accept.Object = &obj

	return accept
}

func SetActorFollowingDB(db *sql.DB, activity Activity) Activity{
	var query string
	alreadyFollow := false
	_, follower := GetActorFollowDB(db, activity.Object.Id)

	for _, e := range follower {
		if e.Id == activity.Object.Id {
			alreadyFollow = true
		}
	}
	
	if alreadyFollow {
		query = `delete from follower where id=$1 and follower=$2`
		activity.Summary = activity.Actor.Id + " Unfollow " + activity.Object.Id			
	} else {		
		query = `insert into follower (id, follower) values ($1, $2)`
		activity.Summary = activity.Actor.Id + " Follow " + activity.Object.Id						
	}
	
	_, err := db.Exec(query, activity.Object.Id, activity.Actor.Id)

	if err != nil {
		CheckError(err, "error with follow db insert/delete")
		activity.Type = "Reject"
		return activity
	}
	
	activity.Type = "Accept"
	return	activity
}