aboutsummaryrefslogtreecommitdiff
path: root/routes/post.go
blob: 64df600349d9b83d91cb50bc41a1f1bfbed83636 (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
package routes

import (
	"regexp"

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

func PostGet(ctx *fiber.Ctx) error {
	actor, err := activitypub.GetActorByNameFromDB(ctx.Params("actor"))
	if err != nil {
		return err
	}

	// this is a activitpub json request return json instead of html page
	if activitypub.AcceptActivity(ctx.Get("Accept")) {
		activitypub.GetActorPost(ctx, ctx.Path())
		return nil
	}

	postId := ctx.Params("post")

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

	var data PageData

	re := regexp.MustCompile("f(\\w|[!@#$%^&*<>])+-(\\w|[!@#$%^&*<>])+")

	if re.MatchString(postId) { // if non local actor post
		name := activitypub.GetActorFollowNameFromPath(postId)

		followActors, err := webfinger.GetActorsFollowFromName(actor, name)
		if err != nil {
			return err
		}

		followCollection, err := activitypub.GetActorsFollowPostFromId(followActors, postId)
		if err != nil {
			return err
		}

		if len(followCollection.OrderedItems) > 0 {
			data.Board.InReplyTo = followCollection.OrderedItems[0].Id
			data.Posts = append(data.Posts, followCollection.OrderedItems[0])

			actor, err := webfinger.FingerActor(data.Board.InReplyTo)
			if err != nil {
				return err
			}

			data.Board.Post.Actor = actor.Id
		}
	} else {
		collection, err := activitypub.GetObjectByIDFromDB(inReplyTo)
		if err != nil {
			return err
		}

		if collection.Actor != nil {
			data.Board.Post.Actor = collection.Actor.Id
			data.Board.InReplyTo = inReplyTo

			if len(collection.OrderedItems) > 0 {
				data.Posts = append(data.Posts, collection.OrderedItems[0])
			}
		}
	}

	if len(data.Posts) > 0 {
		data.PostId = util.ShortURL(data.Board.To, data.Posts[0].Id)
	}

	data.Board.Name = actor.Name
	data.Board.PrefName = actor.PreferredUsername
	data.Board.To = actor.Outbox
	data.Board.Actor = actor
	data.Board.Summary = actor.Summary
	data.Board.ModCred, _ = getPassword(ctx)
	data.Board.Domain = config.Domain
	data.Board.Restricted = actor.Restricted
	data.ReturnTo = "feed"

	capt, err := db.GetRandomCaptcha()
	if err != nil {
		return err
	}
	data.Board.Captcha = config.Domain + "/" + capt
	data.Board.CaptchaCode = util.GetCaptchaCode(data.Board.Captcha)

	data.Instance, err = activitypub.GetActorFromDB(config.Domain)
	if err != nil {
		return err
	}

	data.Key = config.Key
	data.Boards = webfinger.Boards

	data.Title = "/" + data.Board.Name + "/ - " + data.PostId

	if len(data.Posts) > 0 {
		data.Meta.Description = data.Posts[0].Content
		data.Meta.Url = data.Posts[0].Id
		data.Meta.Title = data.Posts[0].Name
		data.Meta.Preview = data.Posts[0].Preview.Href
	}

	data.Themes = &config.Themes
	data.ThemeCookie = getThemeCookie(ctx)

	return ctx.Render("npost", fiber.Map{
		"page": data,
	}, "layouts/main")
}

func CatalogGet(ctx *fiber.Ctx) error {
	actorName := ctx.Params("actor")
	actor, err := activitypub.GetActorByNameFromDB(actorName)
	if err != nil {
		return err
	}

	collection, err := activitypub.GetObjectFromDBCatalog(actor.Id)

	// TODO: implement this in template functions
	//	"showArchive": func() bool {
	//	col, err := db.GetActorCollectionDBTypeLimit(collection.Actor.Id, "Archive", 1)
	//	if err != nil {
	//		// TODO: figure out what to do here
	//		panic(err)
	//	}
	//
	//	if len(col.OrderedItems) > 0 {
	//		return true
	//	}
	//	return false
	//},

	var data PageData
	data.Board.Name = actor.Name
	data.Board.PrefName = actor.PreferredUsername
	data.Board.InReplyTo = ""
	data.Board.To = actor.Outbox
	data.Board.Actor = actor
	data.Board.Summary = actor.Summary
	data.Board.ModCred, _ = getPassword(ctx)
	data.Board.Domain = config.Domain
	data.Board.Restricted = actor.Restricted
	data.Key = config.Key
	data.ReturnTo = "catalog"

	data.Board.Post.Actor = actor.Id

	data.Instance, err = activitypub.GetActorFromDB(config.Domain)
	if err != nil {
		return err
	}

	capt, err := db.GetRandomCaptcha()
	if err != nil {
		return err
	}

	data.Board.Captcha = config.Domain + "/" + capt
	data.Board.CaptchaCode = util.GetCaptchaCode(data.Board.Captcha)

	data.Title = "/" + data.Board.Name + "/ - catalog"

	data.Boards = webfinger.Boards
	data.Posts = collection.OrderedItems

	data.Meta.Description = data.Board.Summary
	data.Meta.Url = data.Board.Actor.Id
	data.Meta.Title = data.Title

	data.Themes = &config.Themes
	data.ThemeCookie = getThemeCookie(ctx)

	return ctx.Render("catalog", fiber.Map{
		"page": data,
	}, "layouts/main")
}