diff options
author | FChannel <> | 2022-05-06 22:45:27 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 39012c6b17073f6933a5ead8beed64df555f7348 (patch) | |
tree | 7784d092fe196a9051c81cf6833997607ad2d268 /routes | |
parent | af542e339e5a611d2a1b5876450bee841b577640 (diff) |
converting functions to activitypub object functions
Diffstat (limited to 'routes')
-rw-r--r-- | routes/actor.go | 14 | ||||
-rw-r--r-- | routes/admin.go | 6 | ||||
-rw-r--r-- | routes/follow.go | 7 | ||||
-rw-r--r-- | routes/index.go | 2 | ||||
-rw-r--r-- | routes/outbox.go | 4 | ||||
-rw-r--r-- | routes/post.go | 4 | ||||
-rw-r--r-- | routes/util.go | 86 |
7 files changed, 104 insertions, 19 deletions
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 := "<a href=\"/" + actor.Name + "/" + util.ShortURL(actor.Outbox, op) + "#" + util.ShortURL(actor.Outbox, id) + "\" title=\"" + title + "\" class=\"replyLink\">>>" + util.ShortURL(actor.Outbox, id) + "</a>" + 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], "<b>"+match[0]+"</b>", 1) + returnString = strings.Replace(returnString, "|", ":", 1) + } + + return returnString + }) +} |