aboutsummaryrefslogtreecommitdiff
path: root/db/cache.go
blob: d1c1fe78a474cc29561a50cd0444ed9ecf048dca (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
package db

import (
	"github.com/FChannel0/FChannel-Server/activitypub"
	"github.com/FChannel0/FChannel-Server/config"
	"github.com/FChannel0/FChannel-Server/webfinger"
	_ "github.com/lib/pq"
)

func WriteObjectReplyToCache(obj activitypub.ObjectBase) error {
	for i, e := range obj.InReplyTo {
		res, err := IsReplyInThread(obj.InReplyTo[0].Id, e.Id)
		if err != nil {
			return err
		}

		if i == 0 || res {
			query := `select id from replies where id=$1`

			rows, err := config.DB.Query(query, obj.Id)
			if err != nil {
				return err
			}
			defer rows.Close()

			var id string
			rows.Next()
			err = rows.Scan(&id)
			if err != nil {
				return err
			} else if id != "" {
				return nil // TODO: error?
			}

			query = `insert into cachereplies (id, inreplyto) values ($1, $2)`

			_, err = config.DB.Exec(query, obj.Id, e.Id)
			if err != nil {
				return err
			}
		}
	}

	if len(obj.InReplyTo) < 1 {
		query := `insert into cachereplies (id, inreplyto) values ($1, $2)`

		_, err := config.DB.Exec(query, obj.Id, "")
		return err
	}

	return nil
}

func WriteActorToCache(actorID string) error {
	actor, err := webfinger.FingerActor(actorID)
	if err != nil {
		return err
	}

	collection, err := webfinger.GetActorCollection(actor.Outbox)
	if err != nil {
		return err
	}

	for _, e := range collection.OrderedItems {
		if err := e.WriteCache(); err != nil {
			return err
		}
	}

	return nil
}