aboutsummaryrefslogtreecommitdiff
path: root/client.go
blob: beddcad5d04b228ba32b1e14125fa7aa12b2affe (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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
package main

import "fmt"
import "net/http"
import "html/template"
import "database/sql"
import _ "github.com/lib/pq"
import "strings"
import "strconv"
import "math"
import "sort"
import "regexp"
import "io/ioutil"
import "encoding/json"
import "os"

var Key *string = new(string)

var Boards *[]ObjectBase = new([]ObjectBase)

type Board struct{
	Name string
	Actor string
	Summary string
	PrefName string
	InReplyTo string
	Location string
	To string
	RedirectTo string
	Captcha string
	CaptchaCode string
	ModCred string
	Domain string
	TP string
	Restricted bool
}

type PageData struct {
	Title string
	Message string	
	Board Board
	Pages []int
	CurrentPage int
	TotalPage int
	Boards []Board
	Posts []ObjectBase
	Key string
}

type AdminPage struct {
	Title string
	Board Board
	Key string
	Actor string
	Boards []Board	
	Following []string
	Followers []string
	Reported []Report
	Domain string
	IsLocal bool
}

type Report struct {
	ID string
	Count int
}

type Removed struct {
	ID string
	Type string
	Board string
}

func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	t := template.Must(template.ParseFiles("./static/main.html", "./static/index.html"))

	actor := GetActorFromDB(db, Domain)

	var boardCollection []Board

	for _, e := range *Boards {
		var board Board
		boardActor := GetActor(e.Id)
		board.Name = "/" + boardActor.Name + "/"
		board.PrefName = boardActor.PreferredUsername
		board.Location = "/" + boardActor.Name
		boardCollection = append(boardCollection, board)
		board.Restricted = boardActor.Restricted
	}
	
	var data PageData
	data.Title = "Welcome to " + actor.PreferredUsername
	data.Message = fmt.Sprintf("%s is a federated image board based on activitypub. The current version of the code running the server is still a work in progress, expect a bumpy ride for the time being. Get the server code here https://github.com/FChannel0", Domain)
	data.Boards = boardCollection
	data.Board.Name = ""
	data.Key = *Key
	data.Board.Domain = Domain
	data.Board.ModCred, _ = GetPasswordFromSession(r)

	t.ExecuteTemplate(w, "layout",  data)	
}

func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection){

	t := template.Must(template.ParseFiles("./static/main.html", "./static/nposts.html", "./static/top.html", "./static/bottom.html", "./static/posts.html"))	

	actor := collection.Actor

	postNum := strings.Replace(r.URL.EscapedPath(), "/" + actor.Name + "/", "", 1)

	page, _ := strconv.Atoi(postNum)
	
	var returnData PageData

	returnData.Board.Name = actor.Name
	returnData.Board.PrefName = actor.PreferredUsername
	returnData.Board.Summary = actor.Summary
	returnData.Board.InReplyTo = ""
	returnData.Board.To = actor.Outbox
	returnData.Board.Actor = actor.Id
	returnData.Board.ModCred, _ = GetPasswordFromSession(r)
	returnData.Board.Domain = Domain
	returnData.Board.Restricted = actor.Restricted
	returnData.CurrentPage = page

	returnData.Board.Captcha = GetCaptcha(*actor)
	returnData.Board.CaptchaCode = GetCaptchaCode(returnData.Board.Captcha)

	returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername

	returnData.Key = *Key	

	var mergeCollection Collection

	for _, e := range collection.OrderedItems {
		if e.Type != "Tombstone" {
			mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e)
		}
	}

	domainURL := GetDomainURL(*actor)
	
	if domainURL == Domain {
		followCol := GetObjectsFromFollow(db, *actor)
		for _, e := range followCol {
			if e.Type != "Tombstone" {
				mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e)
			}
		}
	}
	
	DeleteRemovedPosts(db, &mergeCollection)
	DeleteTombstoneReplies(&mergeCollection)

	for i, _ := range mergeCollection.OrderedItems {
		sort.Sort(ObjectBaseSortAsc(mergeCollection.OrderedItems[i].Replies.OrderedItems))
	}

	DeleteTombstonePosts(&mergeCollection)
	sort.Sort(ObjectBaseSortDesc(mergeCollection.OrderedItems))	



	returnData.Boards = GetBoardCollection(db)

	offset := 8
	start := page * offset
	for i := 0; i < offset; i++ {
		length := len(mergeCollection.OrderedItems)
		current := start + i
		if(current < length) {
			returnData.Posts = append(returnData.Posts, mergeCollection.OrderedItems[current])
		}
	}


	for i, e := range returnData.Posts {
		var replies []ObjectBase
		for i := 0; i < 5; i++ {
			cur := len(e.Replies.OrderedItems) - i - 1
			if cur > -1 {
				replies = append(replies, e.Replies.OrderedItems[cur])
			}
		}

		var orderedReplies []ObjectBase
		for i := 0; i < 5; i++ {
			cur := len(replies) - i - 1
			if cur > -1 {
				orderedReplies = append(orderedReplies, replies[cur])
			}
		}
		returnData.Posts[i].Replies.OrderedItems = orderedReplies
	}

	var pages []int
	pageLimit := math.Round(float64(len(mergeCollection.OrderedItems) / offset))
	for i := 0.0; i <= pageLimit; i++ {
		pages = append(pages, int(i))
	}

	returnData.Pages = pages
	returnData.TotalPage = len(returnData.Pages) - 1

	t.ExecuteTemplate(w, "layout",  returnData)
}

func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection){

	t := template.Must(template.ParseFiles("./static/main.html", "./static/ncatalog.html", "./static/top.html"))			
	
	actor := collection.Actor

	var mergeCollection Collection

	for _, e := range collection.OrderedItems {
		mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e)
	}

	domainURL := GetDomainURL(*actor)

	if domainURL == Domain {
		followCol := GetObjectsFromFollow(db, *actor)	
		for _, e := range followCol {
			if e.Type != "Tombstone" {			
				mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e)
			}
		}
	}

	DeleteRemovedPosts(db, &mergeCollection)
	DeleteTombstonePosts(&mergeCollection)
	
	sort.Sort(ObjectBaseSortDesc(mergeCollection.OrderedItems))
	
	var returnData PageData
	returnData.Board.Name = actor.Name
	returnData.Board.PrefName = actor.PreferredUsername
	returnData.Board.InReplyTo = ""
	returnData.Board.To = actor.Outbox
	returnData.Board.Actor = actor.Id
	returnData.Board.Summary = actor.Summary
	returnData.Board.ModCred, _ = GetPasswordFromSession(r)
	returnData.Board.Domain = Domain
	returnData.Board.Restricted = actor.Restricted
	returnData.Key = *Key

	returnData.Board.Captcha = GetCaptcha(*actor)
	returnData.Board.CaptchaCode = GetCaptchaCode(returnData.Board.Captcha)	

	returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername

	returnData.Boards = GetBoardCollection(db)

	returnData.Posts = mergeCollection.OrderedItems

	t.ExecuteTemplate(w, "layout",  returnData)
}

func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){

	t := template.Must(template.ParseFiles("./static/main.html", "./static/npost.html", "./static/top.html", "./static/bottom.html", "./static/posts.html"))
	
	path := r.URL.Path
	actor := GetActorFromPath(db, path, "/")
	re := regexp.MustCompile("\\w+$")
	postId := re.FindString(path)

	inReplyTo := actor.Id + "/" + postId

	var returnData PageData

	returnData.Board.Name = actor.Name
	returnData.Board.PrefName = actor.PreferredUsername
	returnData.Board.To = actor.Outbox
	returnData.Board.Actor = actor.Id
	returnData.Board.Summary = actor.Summary
	returnData.Board.ModCred, _ = GetPasswordFromSession(r)
	returnData.Board.Domain = Domain
	returnData.Board.Restricted = actor.Restricted


	if GetDomainURL(actor) != "" {
		returnData.Board.Captcha = GetCaptcha(actor)
		returnData.Board.CaptchaCode = GetCaptchaCode(returnData.Board.Captcha)
	}

	returnData.Title = "/" + returnData.Board.Name + "/ - " + returnData.Board.PrefName

	returnData.Key = *Key	

	returnData.Boards = GetBoardCollection(db)

	re = regexp.MustCompile("f\\w+-\\w+")

	if re.MatchString(path) {
		name := GetActorFollowNameFromPath(path)
		followActors := GetActorsFollowFromName(actor, name)
		followCollection := GetActorsFollowPostFromId(db, followActors, postId)
		
		DeleteRemovedPosts(db, &followCollection)
		DeleteTombstoneReplies(&followCollection)

		for i, _ := range followCollection.OrderedItems {
			sort.Sort(ObjectBaseSortAsc(followCollection.OrderedItems[i].Replies.OrderedItems))
		}

		if len(followCollection.OrderedItems) > 0 {		
			returnData.Board.InReplyTo = followCollection.OrderedItems[0].Id
			returnData.Posts = append(returnData.Posts, followCollection.OrderedItems[0])
			sort.Sort(ObjectBaseSortAsc(returnData.Posts[0].Replies.OrderedItems))			
		}

	} else {
		returnData.Board.InReplyTo = inReplyTo
		collection := GetActorCollectionByID(db, inReplyTo)

		DeleteRemovedPosts(db, &collection)

		for i, e := range collection.OrderedItems {
			var replies CollectionBase
			for _, k := range e.Replies.OrderedItems {
				if k.Type != "Tombstone" {
					replies.OrderedItems = append(replies.OrderedItems, k)
				} else {
					collection.OrderedItems[i].Replies.TotalItems = collection.OrderedItems[i].Replies.TotalItems - 1
					if k.Preview.Id != "" {
						collection.OrderedItems[i].Replies.TotalImgs = collection.OrderedItems[i].Replies.TotalImgs - 1
					}
				}
			}
			collection.TotalItems = collection.OrderedItems[i].Replies.TotalItems
			collection.TotalImgs = collection.OrderedItems[i].Replies.TotalImgs			
			collection.OrderedItems[i].Replies = &replies
			sort.Sort(ObjectBaseSortAsc(e.Replies.OrderedItems))
		}				

		if len(collection.OrderedItems) > 0 {
			returnData.Posts = append(returnData.Posts, collection.OrderedItems[0])
			sort.Sort(ObjectBaseSortAsc(returnData.Posts[0].Replies.OrderedItems))
		}
	}

	t.ExecuteTemplate(w, "layout",  returnData)			
}

func GetRemoteActor(id string) Actor {

	var respActor Actor

	id = StripTransferProtocol(id)

	req, err := http.NewRequest("GET", "http://" + id, nil)

	CheckError(err, "error with getting actor req")

	req.Header.Set("Accept", activitystreams)

	resp, err := http.DefaultClient.Do(req)

	if err != nil || resp.StatusCode != 200 {
		fmt.Println("could not get actor from " + id)		
		return respActor
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)

	err = json.Unmarshal(body, &respActor)

	CheckError(err, "error getting actor from body")

	return respActor
}

func GetBoardCollection(db *sql.DB) []Board {
	var collection []Board
	for _, e := range *Boards {
		var board Board
		boardActor := GetActorFromDB(db, e.Id)
		if boardActor.Id == "" {
			boardActor = GetRemoteActor(e.Id)
		}
		board.Name = "/" + boardActor.Name + "/"
		board.PrefName = boardActor.PreferredUsername
		board.Location = "/" + boardActor.Name
		collection = append(collection, board)
	}
	
	return collection
}

func WantToServe(db *sql.DB, actorName string) (Collection, bool) {

	var collection Collection
	serve := false

	boardActor := GetActorByNameFromDB(db, actorName)

	if boardActor.Id != "" {
		collection = GetActorCollectionDB(db, boardActor)
		return collection, true
	}
	
	for _, e := range *Boards {
		boardActor := GetActorFromDB(db, e.Id)
		
		if boardActor.Id == "" {
			boardActor = GetActor(e.Id)
		}

		if boardActor.Name == actorName {
			serve = true
			if IsActorLocal(db, boardActor.Id) {
				collection = GetActorCollectionDB(db, boardActor)
			} else {
				collection = GetActorCollectionCache(db, boardActor)
			}
			collection.Actor = &boardActor
			return collection, serve
		}
	}

	return collection, serve
}

func StripTransferProtocol(value string) string {
	re := regexp.MustCompile("(http://|https://)?(www.)?")

	value = re.ReplaceAllString(value, "")

	return value
}

func GetCaptcha(actor Actor) string {
	re := regexp.MustCompile("(https://|http://)?(www)?.+/")

	domainURL := re.FindString(actor.Id)

	re = regexp.MustCompile("/$")

	domainURL = re.ReplaceAllString(domainURL, "")

	resp, err := http.Get(domainURL + "/getcaptcha")

	CheckError(err, "error getting captcha")

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)			

	return domainURL + "/" + string(body)
}

func GetCaptchaCode(captcha string) string {
	re := regexp.MustCompile("\\w+\\.\\w+$")

	code := re.FindString(captcha)

	re = regexp.MustCompile("\\w+")

	code = re.FindString(code)

	return code
}

func GetDomainURL(actor Actor) string {
	re := regexp.MustCompile("(https://|http://)?(www)?.+/")

	domainURL := re.FindString(actor.Id)

	re = regexp.MustCompile("/$")

	domainURL = re.ReplaceAllString(domainURL, "")

	return domainURL
}

func DeleteTombstoneReplies(collection *Collection) {

	for i, e := range collection.OrderedItems {
		var replies CollectionBase
		for _, k := range e.Replies.OrderedItems {
			if k.Type != "Tombstone" {
				replies.OrderedItems = append(replies.OrderedItems, k)
			}				
		}
		
		replies.TotalItems = collection.OrderedItems[i].Replies.TotalItems
		replies.TotalImgs = collection.OrderedItems[i].Replies.TotalImgs
		collection.OrderedItems[i].Replies = &replies
	}			
}

func DeleteTombstonePosts(collection *Collection) {
	var nColl Collection
	
	for _, e := range collection.OrderedItems {
		if e.Type != "Tombstone" {
			nColl.OrderedItems = append(nColl.OrderedItems, e)
		}
	}
	collection.OrderedItems = nColl.OrderedItems
}

func DeleteRemovedPosts(db *sql.DB, collection *Collection) {

	removed := GetLocalDeleteDB(db)

	for p, e := range collection.OrderedItems {
		for _, j := range removed {
			if e.Id == j.ID {
				if j.Type == "attachment" {
					collection.OrderedItems[p].Preview.Href = "/public/removed.png"
					collection.OrderedItems[p].Preview.Name = "deleted"
					collection.OrderedItems[p].Preview.MediaType = "image/png"											
					collection.OrderedItems[p].Attachment[0].Href = "/public/removed.png"
					collection.OrderedItems[p].Attachment[0].Name = "deleted"					
					collection.OrderedItems[p].Attachment[0].MediaType = "image/png"
				} else {
					collection.OrderedItems[p].AttributedTo = "deleted"
					collection.OrderedItems[p].Content = ""
					collection.OrderedItems[p].Type = "Tombstone"
					if collection.OrderedItems[p].Attachment != nil {
						collection.OrderedItems[p].Preview.Href = "/public/removed.png"
						collection.OrderedItems[p].Preview.Name = "deleted"
						collection.OrderedItems[p].Preview.MediaType = "image/png"						
						collection.OrderedItems[p].Attachment[0].Href = "/public/removed.png"
						collection.OrderedItems[p].Attachment[0].Name = "deleted"
						collection.OrderedItems[p].Attachment[0].MediaType = "image/png"
					}
				}
			}
		}
		
		for i, r := range e.Replies.OrderedItems {
			for _, k := range removed {
				if r.Id == k.ID {
					if k.Type == "attachment" {
						e.Replies.OrderedItems[i].Preview.Href = "/public/removed.png"
						e.Replies.OrderedItems[i].Preview.Name = "deleted"						
						e.Replies.OrderedItems[i].Preview.MediaType = "image/png"													
						e.Replies.OrderedItems[i].Attachment[0].Href = "/public/removed.png"
						e.Replies.OrderedItems[i].Attachment[0].Name = "deleted"
						e.Replies.OrderedItems[i].Attachment[0].MediaType = "image/png"
						collection.OrderedItems[p].Replies.TotalImgs = collection.OrderedItems[p].Replies.TotalImgs - 1
					} else {
						e.Replies.OrderedItems[i].AttributedTo = "deleted"
						e.Replies.OrderedItems[i].Content = ""
						e.Replies.OrderedItems[i].Type = "Tombstone"
						if e.Replies.OrderedItems[i].Attachment != nil {
							e.Replies.OrderedItems[i].Preview.Href = "/public/removed.png"
							e.Replies.OrderedItems[i].Preview.Name = "deleted"
							e.Replies.OrderedItems[i].Preview.MediaType = "image/png"							
							e.Replies.OrderedItems[i].Attachment[0].Name = "deleted"												
							e.Replies.OrderedItems[i].Attachment[0].Href = "/public/removed.png"						
							e.Replies.OrderedItems[i].Attachment[0].MediaType = "image/png"
							collection.OrderedItems[p].Replies.TotalImgs = collection.OrderedItems[p].Replies.TotalImgs - 1							
						}
						collection.OrderedItems[p].Replies.TotalItems = collection.OrderedItems[p].Replies.TotalItems - 1
					}
				}
			}
		}
	}
}

func CreateLocalDeleteDB(db *sql.DB, id string, _type string)	{
	query := fmt.Sprintf("select id from removed where id='%s'", id)

	rows, err := db.Query(query)

	CheckError(err, "could not query removed")

	defer rows.Close()

	if rows.Next() {
		var i string

		rows.Scan(&i)

		if i != "" {
			query := fmt.Sprintf("update removed set type='%s' where id='%s'", _type, id)

			_, err := db.Exec(query)
			
			CheckError(err, "Could not update removed post")
			
		}
	} else {
		query := fmt.Sprintf("insert into removed (id, type) values ('%s', '%s')", id, _type)
		
		_, err := db.Exec(query)
		
		CheckError(err, "Could not insert removed post")
	}
}

func GetLocalDeleteDB(db *sql.DB) []Removed {
	var deleted []Removed
	
	query := fmt.Sprintf("select id, type from removed")

	rows, err := db.Query(query)

	CheckError(err, "could not query removed")

	defer rows.Close()

	for rows.Next() {
		var r Removed

		rows.Scan(&r.ID, &r.Type)

		deleted = append(deleted, r)
	}

	return deleted
}

func CreateLocalReportDB(db *sql.DB, id string, board string) {
	query := fmt.Sprintf("select id, count from reported where id='%s' and board='%s'", id, board)

	rows, err := db.Query(query)

	CheckError(err, "could not query reported")

	defer rows.Close()

	if rows.Next() {
		var i string
		var count int

		rows.Scan(&i, &count)

		if i != "" {
			count = count + 1
			query := fmt.Sprintf("update reported set count='%d' where id='%s'", count, id)

			_, err := db.Exec(query)
			
			CheckError(err, "Could not update reported post")
		}
	} else {
		query := fmt.Sprintf("insert into reported (id, count, board) values ('%s', '%d', '%s')", id, 1, board)
		
		_, err := db.Exec(query)
		
		CheckError(err, "Could not insert reported post")
	}	

}

func GetLocalReportDB(db *sql.DB, board string) []Report {
	var reported []Report
	
	query := fmt.Sprintf("select id, count from reported where board='%s'", board)

	rows, err := db.Query(query)

	CheckError(err, "could not query reported")

	defer rows.Close()

	for rows.Next() {
		var r Report

		rows.Scan(&r.ID, &r.Count)

		reported = append(reported, r)
	}

	return reported
}

func CloseLocalReportDB(db *sql.DB, id string, board string) {
	query := fmt.Sprintf("delete from reported where id='%s' and board='%s'", id, board)

	_, err := db.Exec(query)

	CheckError(err, "Could not delete local report from db")
}

func GetActorFollowNameFromPath(path string) string{
	var actor string

	re := regexp.MustCompile("f\\w+-")

	actor = re.FindString(path)

	actor = strings.Replace(actor, "f", "", 1)
	actor = strings.Replace(actor, "-", "", 1)	

	return actor
}

func GetActorsFollowFromName(actor Actor, name string) []string {
	var followingActors []string
	follow := GetActorCollection(actor.Following)

	re := regexp.MustCompile("\\w+?$")

	for _, e := range follow.Items {
		if re.FindString(e.Id) == name {
			followingActors = append(followingActors, e.Id)
		}
	}

	return followingActors
}

func GetActorsFollowPostFromId(db *sql.DB, actors []string, id string) Collection{
	var collection Collection

	for _, e := range actors {
		tempCol := GetActorCollectionByID(db, e + "/" + id)
		if len(tempCol.OrderedItems) > 0 {
			collection = tempCol
		}
	}

	return collection
}

func GetActorCollectionByID(db *sql.DB, postID string) Collection {
	collection := GetObjectByIDFromDB(db, postID)
	if len(collection.OrderedItems) < 1 {
		collection = GetObjectByIDFromCache(db, postID)
	}

	return collection
}

func CreateClientKey() string{

	file, err := os.Create("clientkey")

	CheckError(err, "could not create client key in file")

	defer file.Close()

	key := CreateKey(32)
	file.WriteString(key)
	return key
}

type ObjectBaseSortDesc []ObjectBase
func (a ObjectBaseSortDesc) Len() int { return len(a) }
func (a ObjectBaseSortDesc) Less(i, j int) bool { return a[i].Updated > a[j].Updated }
func (a ObjectBaseSortDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

type ObjectBaseSortAsc []ObjectBase
func (a ObjectBaseSortAsc) Len() int { return len(a) }
func (a ObjectBaseSortAsc) Less(i, j int) bool { return a[i].Published < a[j].Published }
func (a ObjectBaseSortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }