From dc3c93b1334aecfa1ebaf2f80ebe04c3c56d0499 Mon Sep 17 00:00:00 2001 From: Xeronith Date: Thu, 15 Dec 2022 18:43:24 +0330 Subject: [PATCH] feat(app): :sparkles: outbox post command --- greataped/README.md | 16 +++++ greataped/app/commands/spi/post_to_outbox.go | 75 ++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 greataped/app/commands/spi/post_to_outbox.go diff --git a/greataped/README.md b/greataped/README.md index 32a6749..2e18449 100644 --- a/greataped/README.md +++ b/greataped/README.md @@ -18,6 +18,7 @@ 11. [AuthorizeInteraction](#authorize-interaction) 12. [GetFollowers](#get-followers) 13. [GetFollowing](#get-following) +14. [PostToOutbox](#post-to-outbox) --- @@ -196,3 +197,18 @@ Result: string first ``` [Back to List](#apis) + +## Post To Outbox +``` +Request: + string username + string @context + string type + string to + string attributedTo + string inReplyTo + string content + +Result: +``` +[Back to List](#apis) diff --git a/greataped/app/commands/spi/post_to_outbox.go b/greataped/app/commands/spi/post_to_outbox.go new file mode 100644 index 0000000..13c605c --- /dev/null +++ b/greataped/app/commands/spi/post_to_outbox.go @@ -0,0 +1,75 @@ +package spi + +import ( + "encoding/json" + + "rail.town/infrastructure/app/activitypub" + . "rail.town/infrastructure/components/constants" + . "rail.town/infrastructure/components/contracts" +) + +func PostToOutbox(x IDispatcher, + username string, + context string, + activityType string, + to string, + attributedTo string, + inReplyTo string, + content string, +) (IPostToOutboxResult, error) { + identities := x.FilterIdentities(func(identity IIdentity) bool { + return identity.Username() == username + }) + + x.Assert(identities.HasExactlyOneItem()).Or(ERROR_USER_NOT_FOUND) + identity := identities.First() + + id := x.Format("%s/u/%s", x.PublicUrl(), identity.Username()) + publicKeyId := x.Format("%s#main-key", id) + privateKey := identity.PrivateKey() + + _ = publicKeyId + + switch activityType { + case ACTIVITY_PUB_NOTE: + { + uniqueIdentifier := x.GenerateUUID() + note := activitypub.NewNote(id, to, content) + activity := note.Wrap(identity.Username(), x.PublicUrl(), uniqueIdentifier) + to := activity.To.([]string)[0] + + if to != activitypub.Public { + recipient := &activitypub.Actor{} + if err := x.GetActivityStreamSigned(to, publicKeyId, privateKey, nil, recipient); err != nil { + return nil, err + } + + to = recipient.ID + + data, _ := json.Marshal(activity) + output := &struct{}{} + if err := x.PostActivityStreamSigned(recipient.Inbox, publicKeyId, privateKey, data, output); err != nil { + return nil, err + } + } + + raw, _ := json.Marshal(note) + + x.LogActivityPubOutgoingActivity( + identity.Id(), + uniqueIdentifier, + x.UnixNano(), + note.AttributedTo, + to, + note.Content, + string(raw), + "PostToOutbox", + EMPTY_JSON, + ) + + return x.NewPostToOutboxResult(), nil + } + default: + return nil, ERROR_INVALID_PARAMETERS + } +}