feat(app): get outbox command

master
Xeronith 2022-12-20 16:50:38 +03:30
rodzic 7af27af965
commit 265de276e0
2 zmienionych plików z 74 dodań i 0 usunięć

Wyświetl plik

@ -19,6 +19,7 @@
12. [GetFollowers](#get-followers)
13. [GetFollowing](#get-following)
14. [PostToOutbox](#post-to-outbox)
15. [GetOutbox](#get-outbox)
---
@ -212,3 +213,18 @@ Request:
Result:
```
[Back to List](#apis)
## Get Outbox
```
Request:
string username
Result:
string @context
string id
string type
int32 totalItems
repeated ActivityPubActivity orderedItems
string first
```
[Back to List](#apis)

Wyświetl plik

@ -0,0 +1,58 @@
package spi
import (
"time"
"rail.town/infrastructure/app/activitypub"
. "rail.town/infrastructure/components/constants"
. "rail.town/infrastructure/components/contracts"
)
func GetOutbox(x IDispatcher, username string) (IGetOutboxResult, error) {
identities := x.FilterIdentities(func(identity IIdentity) bool {
return identity.Username() == username
})
x.Assert(identities.HasExactlyOneItem()).Or(ERROR_USER_NOT_FOUND)
identity := identities.First()
actor := x.Format("%s/u/%s", x.PublicUrl(), identity.Username())
activities := x.FilterActivityPubOutgoingActivities(func(activity IActivityPubOutgoingActivity) bool {
return activity.From() == actor && activity.To() == ACTIVITY_PUB_PUBLIC
})
var orderedItems ActivityPubActivities
activities.ForEach(func(outgoingActivity IActivityPubOutgoingActivity) {
published := time.Unix(0, outgoingActivity.Timestamp()).Format("2006-01-02T15:04:05Z")
note := activitypub.NewPublicNote(actor, outgoingActivity.Content())
noteActivity := note.Wrap(username, x.PublicUrl(), outgoingActivity.UniqueIdentifier())
object, _ := x.NewActivityPubObject()
object.SetContext(ACTIVITY_STREAMS)
object.SetType(ACTIVITY_PUB_NOTE)
object.SetId(note.Id)
object.SetContent(note.Content)
activity, _ := x.NewActivityPubActivity()
activity.SetContext(ACTIVITY_STREAMS)
activity.SetType(ACTIVITY_PUB_CREATE)
activity.SetId(x.Format("%s/posts/%s", actor, outgoingActivity.UniqueIdentifier()))
activity.SetActor(actor)
activity.SetTo(noteActivity.To.([]string))
activity.SetPublished(published)
activity.SetObject(object)
orderedItems = append(orderedItems, activity)
})
return x.NewGetOutboxResult(
ACTIVITY_STREAMS, // context
x.Format("%s/outbox", actor), // id
ACTIVITY_PUB_ORDERED_COLLECTION, // type
int32(len(orderedItems)), // totalItems
orderedItems, // orderedItems
"", // first
), nil
}