aboutsummaryrefslogtreecommitdiff
path: root/route/routes/news.go
blob: 8e8047abaa7f0e268e336d428bc94a0616fffe79 (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
package routes

import (
	"html/template"
	"net/http"
	"strconv"

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

func NewsGet(ctx *fiber.Ctx) error {
	timestamp := ctx.Path()[6:]
	ts, err := strconv.Atoi(timestamp)

	if err != nil {
		return ctx.Status(404).Render("404", fiber.Map{})
	}

	actor, err := activitypub.GetActorFromDB(config.Domain)

	if err != nil {
		return util.MakeError(err, "NewsGet")
	}

	var data route.PageData
	data.PreferredUsername = actor.PreferredUsername
	data.Boards = webfinger.Boards
	data.Board.Name = ""
	data.Key = config.Key
	data.Board.Domain = config.Domain
	data.Board.ModCred, _ = util.GetPasswordFromSession(ctx)
	data.Board.Actor = actor
	data.Board.Post.Actor = actor.Id
	data.Board.Restricted = actor.Restricted
	data.NewsItems = make([]db.NewsItem, 1)

	data.NewsItems[0], err = db.GetNewsItem(ts)
	if err != nil {
		return util.MakeError(err, "NewsGet")
	}

	data.Title = actor.PreferredUsername + ": " + data.NewsItems[0].Title

	data.Meta.Description = data.PreferredUsername + " is a federated image board based on ActivityPub. The current version of the code running on the server is still a work-in-progress product, expect a bumpy ride for the time being. Get the server code here: https://git.fchannel.org."
	data.Meta.Url = data.Board.Actor.Id
	data.Meta.Title = data.Title

	data.Themes = &config.Themes
	data.ThemeCookie = route.GetThemeCookie(ctx)

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

func NewsGetAll(ctx *fiber.Ctx) error {
	actor, err := activitypub.GetActorFromDB(config.Domain)
	if err != nil {
		return util.MakeError(err, "NewsGetAll")
	}

	var data route.PageData
	data.PreferredUsername = actor.PreferredUsername
	data.Title = actor.PreferredUsername + " News"
	data.Boards = webfinger.Boards
	data.Board.Name = ""
	data.Key = config.Key
	data.Board.Domain = config.Domain
	data.Board.ModCred, _ = util.GetPasswordFromSession(ctx)
	data.Board.Actor = actor
	data.Board.Post.Actor = actor.Id
	data.Board.Restricted = actor.Restricted

	data.NewsItems, err = db.GetNews(0)

	if err != nil {
		return util.MakeError(err, "NewsGetAll")
	}

	if len(data.NewsItems) == 0 {
		return ctx.Redirect("/", http.StatusSeeOther)
	}

	data.Meta.Description = data.PreferredUsername + " is a federated image board based on ActivityPub. The current version of the code running on the server is still a work-in-progress product, expect a bumpy ride for the time being. Get the server code here: https://git.fchannel.org."
	data.Meta.Url = data.Board.Actor.Id
	data.Meta.Title = data.Title

	data.Themes = &config.Themes
	data.ThemeCookie = route.GetThemeCookie(ctx)

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

func NewsPost(ctx *fiber.Ctx) error {
	actor, err := activitypub.GetActorFromDB(config.Domain)

	if err != nil {
		return util.MakeError(err, "NewPost")
	}

	if has := actor.HasValidation(ctx); !has {
		return nil
	}

	var newsitem db.NewsItem

	newsitem.Title = ctx.FormValue("title")
	newsitem.Content = template.HTML(ctx.FormValue("summary"))

	if err := db.WriteNews(newsitem); err != nil {
		return util.MakeError(err, "NewPost")
	}

	return ctx.Redirect("/", http.StatusSeeOther)
}

func NewsDelete(ctx *fiber.Ctx) error {
	actor, err := activitypub.GetActorFromDB(config.Domain)

	if has := actor.HasValidation(ctx); !has {
		return nil
	}

	timestamp := ctx.Path()[13+len(config.Key):]

	tsint, err := strconv.Atoi(timestamp)

	if err != nil {
		return ctx.Status(404).Render("404", fiber.Map{})
	}

	if err := db.DeleteNewsItem(tsint); err != nil {
		return util.MakeError(err, "NewsDelete")
	}

	return ctx.Redirect("/news/", http.StatusSeeOther)
}