aboutsummaryrefslogtreecommitdiff
path: root/routes/util.go
blob: 0a8dc9cee0fcf51bbdf1cbc3e83911e923529040 (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
package routes

import (
	"errors"
	"fmt"
	"strings"

	"github.com/FChannel0/FChannel-Server/activitypub"
	"github.com/FChannel0/FChannel-Server/db"
	"github.com/gofiber/fiber/v2"
)

var ErrorPageLimit = errors.New("above page limit")

func getThemeCookie(c *fiber.Ctx) string {
	cookie := c.Cookies("theme")
	if cookie != "" {
		cookies := strings.SplitN(cookie, "=", 2)
		return cookies[0]
	}

	return "default"
}

func getPassword(r *fiber.Ctx) (string, string) {
	c := r.Cookies("session_token")

	sessionToken := c

	response, err := db.Cache.Do("GET", sessionToken)
	if err != nil {
		return "", ""
	}

	token := fmt.Sprintf("%s", response)

	parts := strings.Split(token, "|")

	if len(parts) > 1 {
		return parts[0], parts[1]
	}

	return "", ""
}

func wantToServePage(actorName string, page int) (activitypub.Collection, bool, error) {
	var collection activitypub.Collection
	serve := false

	// TODO: don't hard code?
	if page > 10 {
		return collection, serve, ErrorPageLimit
	}

	actor, err := db.GetActorByNameFromDB(actorName)
	if err != nil {
		return collection, false, err
	}

	if actor.Id != "" {
		collection, err = db.GetObjectFromDBPage(actor.Id, page)
		if err != nil {
			return collection, false, err
		}

		collection.Actor = &actor
		return collection, true, nil
	}

	return collection, serve, nil
}

func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) {
	var collection activitypub.Collection
	serve := false

	actor, err := db.GetActorByNameFromDB(actorName)
	if err != nil {
		return collection, false, err
	}

	if actor.Id != "" {
		collection, err = db.GetObjectFromDBCatalog(actor.Id)
		if err != nil {
			return collection, false, err
		}

		collection.Actor = &actor
		return collection, true, nil
	}

	return collection, serve, nil
}

func wantToServeArchive(actorName string) (activitypub.Collection, bool, error) {
	var collection activitypub.Collection
	serve := false

	actor, err := db.GetActorByNameFromDB(actorName)
	if err != nil {
		return collection, false, err
	}

	if actor.Id != "" {
		collection, err = db.GetActorCollectionDBType(actor.Id, "Archive")
		if err != nil {
			return collection, false, err
		}

		collection.Actor = &actor
		return collection, true, nil
	}

	return collection, serve, nil
}