From 39012c6b17073f6933a5ead8beed64df555f7348 Mon Sep 17 00:00:00 2001
From: FChannel <>
Date: Fri, 6 May 2022 22:45:27 -0700
Subject: converting functions to activitypub object functions
---
routes/actor.go | 14 +++++----
routes/admin.go | 6 ++--
routes/follow.go | 7 +++--
routes/index.go | 2 +-
routes/outbox.go | 4 +--
routes/post.go | 4 +--
routes/util.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
7 files changed, 104 insertions(+), 19 deletions(-)
(limited to 'routes')
diff --git a/routes/actor.go b/routes/actor.go
index 5cd0fa9..ea8656f 100644
--- a/routes/actor.go
+++ b/routes/actor.go
@@ -95,7 +95,7 @@ func ActorInbox(ctx *fiber.Ctx) error {
if err := activitypub.TombstoneObject(activity.Object.Id); err != nil {
return err
}
- if err := db.UnArchiveLast(actor.Id); err != nil {
+ if err := actor.UnArchiveLast(); err != nil {
return err
}
break
@@ -118,12 +118,12 @@ func ActorInbox(ctx *fiber.Ctx) error {
alreadyFollow := false
alreadyFollowing := false
- autoSub, err := activitypub.GetActorAutoSubscribeDB(response.Actor.Id)
+ autoSub, err := response.Actor.GetAutoSubscribe()
if err != nil {
return err
}
- following, err := activitypub.GetActorFollowingDB(response.Actor.Id)
+ following, err := response.Actor.GetFollowing()
if err != nil {
return err
}
@@ -195,7 +195,7 @@ func ActorOutbox(ctx *fiber.Ctx) error {
}
if activitypub.AcceptActivity(ctx.Get("Accept")) {
- activitypub.GetActorOutbox(ctx, actor)
+ actor.GetOutbox(ctx)
return nil
}
@@ -203,11 +203,13 @@ func ActorOutbox(ctx *fiber.Ctx) error {
}
func ActorFollowing(ctx *fiber.Ctx) error {
- return activitypub.GetActorFollowing(ctx, config.Domain+"/"+ctx.Params("actor"))
+ actor, _ := activitypub.GetActorFromDB(config.Domain + "/" + ctx.Params("actor"))
+ return actor.GetFollowingResp(ctx)
}
func ActorFollowers(ctx *fiber.Ctx) error {
- return activitypub.GetActorFollowers(ctx, config.Domain+"/"+ctx.Params("actor"))
+ actor, _ := activitypub.GetActorFromDB(config.Domain + "/" + ctx.Params("actor"))
+ return actor.GetFollowersResp(ctx)
}
func ActorReported(c *fiber.Ctx) error {
diff --git a/routes/admin.go b/routes/admin.go
index 771cda2..ea27792 100644
--- a/routes/admin.go
+++ b/routes/admin.go
@@ -160,7 +160,7 @@ func AdminFollow(ctx *fiber.Ctx) error {
col.Items = append(col.Items, nObj)
for _, e := range col.Items {
- if isFollowing, _ := activitypub.IsAlreadyFollowing(actorId, e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId {
+ if isFollowing, _ := actor.IsAlreadyFollowing(e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId {
followActivity, _ := db.MakeFollowActivity(actorId, e.Id)
if actor, _ := webfinger.FingerActor(e.Id); actor.Id != "" {
@@ -180,7 +180,7 @@ func AdminFollow(ctx *fiber.Ctx) error {
col.Items = append(col.Items, nObj)
for _, e := range col.Items {
- if isFollowing, _ := activitypub.IsAlreadyFollowing(actorId, e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId {
+ if isFollowing, _ := actor.IsAlreadyFollowing(e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId {
followActivity, _ := db.MakeFollowActivity(actorId, e.Id)
if actor, _ := webfinger.FingerActor(e.Id); actor.Id != "" {
db.MakeActivityRequestOutbox(followActivity)
@@ -314,7 +314,7 @@ func AdminActorIndex(ctx *fiber.Ctx) error {
data.Board.Post.Actor = actor.Id
- data.AutoSubscribe, _ = activitypub.GetActorAutoSubscribeDB(actor.Id)
+ data.AutoSubscribe, _ = actor.GetAutoSubscribe()
data.Themes = &config.Themes
diff --git a/routes/follow.go b/routes/follow.go
index 3dbf9cf..f61e405 100644
--- a/routes/follow.go
+++ b/routes/follow.go
@@ -7,10 +7,11 @@ import (
)
func Following(ctx *fiber.Ctx) error {
- return activitypub.GetActorFollowing(ctx, config.Domain)
+ actor, _ := activitypub.GetActorFromDB(config.Domain)
+ return actor.GetFollowingResp(ctx)
}
func Followers(ctx *fiber.Ctx) error {
- // STUB
- return activitypub.GetActorFollowers(ctx, config.Domain)
+ actor, _ := activitypub.GetActorFromDB(config.Domain)
+ return actor.GetFollowersResp(ctx)
}
diff --git a/routes/index.go b/routes/index.go
index c088379..8f12664 100644
--- a/routes/index.go
+++ b/routes/index.go
@@ -16,7 +16,7 @@ func Index(ctx *fiber.Ctx) error {
// this is a activitpub json request return json instead of html page
if activitypub.AcceptActivity(ctx.Get("Accept")) {
- activitypub.GetActorInfo(ctx, actor.Id)
+ actor.GetInfoResp(ctx)
return nil
}
diff --git a/routes/outbox.go b/routes/outbox.go
index 62d99c4..b2ffb42 100644
--- a/routes/outbox.go
+++ b/routes/outbox.go
@@ -19,7 +19,7 @@ func Outbox(ctx *fiber.Ctx) error {
}
if activitypub.AcceptActivity(ctx.Get("Accept")) {
- activitypub.GetActorOutbox(ctx, actor)
+ actor.GetOutbox(ctx)
return nil
}
@@ -31,7 +31,7 @@ func OutboxGet(ctx *fiber.Ctx) error {
actor, _ := activitypub.GetActorByNameFromDB(ctx.Params("actor"))
if activitypub.AcceptActivity(ctx.Get("Accept")) {
- activitypub.GetActorInfo(ctx, actor.Id)
+ actor.GetInfoResp(ctx)
return nil
}
diff --git a/routes/post.go b/routes/post.go
index 7ed9e7d..1634346 100644
--- a/routes/post.go
+++ b/routes/post.go
@@ -31,8 +31,8 @@ func PostGet(ctx *fiber.Ctx) error {
re := regexp.MustCompile("f(\\w|[!@#$%^&*<>])+-(\\w|[!@#$%^&*<>])+")
- if re.MatchString(postId) { // if non local actor post
- name := activitypub.GetActorFollowNameFromPath(postId)
+ if re.MatchString(ctx.Path()) { // if non local actor post
+ name := activitypub.GetActorFollowNameFromPath(ctx.Path())
followActors, err := webfinger.GetActorsFollowFromName(actor, name)
if err != nil {
diff --git a/routes/util.go b/routes/util.go
index 8d36752..ee6d062 100644
--- a/routes/util.go
+++ b/routes/util.go
@@ -3,7 +3,10 @@ package routes
import (
"errors"
"fmt"
+ "html/template"
+ "regexp"
"strings"
+ "time"
"github.com/FChannel0/FChannel-Server/activitypub"
"github.com/FChannel0/FChannel-Server/config"
@@ -12,6 +15,7 @@ import (
"github.com/FChannel0/FChannel-Server/util"
"github.com/FChannel0/FChannel-Server/webfinger"
"github.com/gofiber/fiber/v2"
+ "github.com/gofiber/template/html"
)
var ErrorPageLimit = errors.New("above page limit")
@@ -85,7 +89,7 @@ func wantToServeArchive(actorName string) (activitypub.Collection, bool, error)
}
if actor.Id != "" {
- collection, err = activitypub.GetActorCollectionDBType(actor.Id, "Archive")
+ collection, err = actor.GetCollectionType("Archive")
if err != nil {
return collection, false, err
}
@@ -222,7 +226,8 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error {
}
}
- webfinger.FollowingBoards, err = activitypub.GetActorFollowingDB(config.Domain)
+ actor, _ := activitypub.GetActorFromDB(config.Domain)
+ webfinger.FollowingBoards, err = actor.GetFollowing()
if err != nil {
return err
}
@@ -298,3 +303,80 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error {
return nil
}
+
+func TemplateFunctions(engine *html.Engine) {
+ engine.AddFunc("mod", func(i, j int) bool {
+ return i%j == 0
+ })
+
+ engine.AddFunc("sub", func(i, j int) int {
+ return i - j
+ })
+
+ engine.AddFunc("add", func(i, j int) int {
+ return i + j
+ })
+
+ engine.AddFunc("unixtoreadable", func(u int) string {
+ return time.Unix(int64(u), 0).Format("Jan 02, 2006")
+ })
+
+ engine.AddFunc("timeToReadableLong", func(t time.Time) string {
+ return t.Format("01/02/06(Mon)15:04:05")
+ })
+
+ engine.AddFunc("timeToUnix", func(t time.Time) string {
+ return fmt.Sprint(t.Unix())
+ })
+
+ engine.AddFunc("proxy", util.MediaProxy)
+
+ // previously short
+ engine.AddFunc("shortURL", util.ShortURL)
+
+ engine.AddFunc("parseAttachment", post.ParseAttachment)
+
+ engine.AddFunc("parseContent", post.ParseContent)
+
+ engine.AddFunc("shortImg", util.ShortImg)
+
+ engine.AddFunc("convertSize", util.ConvertSize)
+
+ engine.AddFunc("isOnion", util.IsOnion)
+
+ engine.AddFunc("parseReplyLink", func(actorId string, op string, id string, content string) template.HTML {
+ actor, _ := webfinger.FingerActor(actorId)
+ title := strings.ReplaceAll(post.ParseLinkTitle(actor.Id+"/", op, content), `/\<`, ">")
+ link := ">>" + util.ShortURL(actor.Outbox, id) + ""
+ return template.HTML(link)
+ })
+
+ engine.AddFunc("shortExcerpt", func(post activitypub.ObjectBase) string {
+ var returnString string
+
+ if post.Name != "" {
+ returnString = post.Name + "| " + post.Content
+ } else {
+ returnString = post.Content
+ }
+
+ re := regexp.MustCompile(`(^(.|\r\n|\n){100})`)
+
+ match := re.FindStringSubmatch(returnString)
+
+ if len(match) > 0 {
+ returnString = match[0] + "..."
+ }
+
+ re = regexp.MustCompile(`(^.+\|)`)
+
+ match = re.FindStringSubmatch(returnString)
+
+ if len(match) > 0 {
+ returnString = strings.Replace(returnString, match[0], ""+match[0]+"", 1)
+ returnString = strings.Replace(returnString, "|", ":", 1)
+ }
+
+ return returnString
+ })
+}
--
cgit v1.2.3