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

import (
	"errors"
	"fmt"
	"strings"

	"github.com/FChannel0/FChannel-Server/activitypub"
	"github.com/FChannel0/FChannel-Server/config"
	"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
}

func hasValidation(ctx *fiber.Ctx, actor activitypub.Actor) bool {
	id, _ := getPassword(ctx)

	if id == "" || (id != actor.Id && id != config.Domain) {
		//http.Redirect(w, r, "/", http.StatusSeeOther)
		return false
	}

	return true
}