kopia lustrzana https://github.com/reiver/greatape
feat(app): ✨ outbox post command
rodzic
0a08d86163
commit
dc3c93b133
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue