Porównaj commity

...

3 Commity

Autor SHA1 Wiadomość Data
Xeronith 856480989c feat(app): improve federation 2023-07-14 16:53:55 +03:30
Xeronith 39e4ae766e refactor(components): improve core components 2023-07-14 16:50:42 +03:30
Xeronith adac16f851 chore(project): ⬆️ upgrade dependencies 2023-07-14 16:49:48 +03:30
10 zmienionych plików z 224 dodań i 91 usunięć

Wyświetl plik

@ -0,0 +1,34 @@
package activitypub
import (
"encoding/json"
"fmt"
)
type Follow struct {
Context string `json:"@context" validate:"activitystream"`
Id string `json:"id"`
Type string `json:"type"`
Actor string `json:"actor"`
Object string `json:"object"`
}
func NewFollow(follower, followee, uuid string) *Follow {
return &Follow{
Context: ActivityStreams,
Id: fmt.Sprintf("%s#follow/%s", follower, uuid),
Type: TypeFollow,
Actor: follower,
Object: followee,
}
}
func UnmarshalFollow(data []byte) (Follow, error) {
var follow Follow
err := json.Unmarshal(data, &follow)
return follow, err
}
func (follow *Follow) Marshal() ([]byte, error) {
return json.Marshal(follow)
}

Wyświetl plik

@ -1,10 +1,8 @@
package commands
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/reiver/greatape/app/activitypub"
@ -26,20 +24,62 @@ func FollowActor(x IDispatcher, username string, acct string) (IFollowActorResul
webfinger, err := activitypub.UnmarshalWebfinger(data)
x.AssertNoError(err)
template := ""
subject := ""
for _, link := range webfinger.Links {
if link.Rel == OSTATUS_SUBSCRIPTION {
template = *link.Template
if link.Rel == "self" {
subject = *link.Href
break
}
}
if template == "" {
return nil, fmt.Errorf("remote_account_lookup_failed")
if x.IsEmpty(subject) {
return nil, ERROR_INVALID_PARAMETERS
}
uri := url.QueryEscape(x.Format("%s/u/%s", x.PublicUrl(), username))
template = strings.Replace(template, "{uri}", uri, -1)
identities := x.FilterIdentities(func(identity IIdentity) bool {
return identity.Username() == username
})
return x.NewFollowActorResult(template), nil
x.Assert(identities.HasExactlyOneItem()).Or(ERROR_USER_NOT_FOUND)
identity := identities.First()
follower := x.GetActorId()
followee := &activitypub.Actor{}
if err := x.GetActivityStreamSigned(subject, nil, followee); err != nil {
return nil, err
}
uniqueIdentifier := x.GenerateUUID()
follow := activitypub.NewFollow(follower, followee.ID, uniqueIdentifier)
x.Atomic(func() error {
activity := x.MarshalJson(follow)
x.AddActivityPubOutgoingActivity(
identity.Id(),
uniqueIdentifier,
x.UnixNano(),
follower,
followee.ID,
activitypub.TypeFollow,
activity,
)
x.AddActivityPubFollower(
follower,
followee.Inbox,
followee.ID,
activity,
false,
)
if err := x.PostActivityStreamSigned(followee.Inbox, follow, nil); err != nil {
return err
}
return nil
})
return x.NewFollowActorResult(follow.Id), nil
}

Wyświetl plik

@ -1,9 +1,6 @@
package commands
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
"github.com/reiver/greatape/app/activitypub"
. "github.com/reiver/greatape/components/constants"
. "github.com/reiver/greatape/components/contracts"
@ -18,54 +15,72 @@ func PostToInbox(x IDispatcher, username string, body []byte) (IPostToInboxResul
identity := identities.First()
object := &activitypub.Object{}
if err := json.Unmarshal(body, object); err != nil {
return nil, ERROR_UNKNOWN_ACTIVITY_PUB_OBJECT
}
keyId := x.Format("%s/u/%s#main-key", x.PublicUrl(), username)
x.UnmarshalJson(body, object)
switch object.Type {
case activitypub.TypeFollow:
case activitypub.TypeAccept:
{
activity := &activitypub.Activity{}
if err := json.Unmarshal(body, activity); err != nil {
return nil, ERROR_UNKNOWN_ACTIVITY_PUB_ACTIVITY
x.UnmarshalJson(body, activity)
switch activity.Object.(map[string]interface{})["type"] {
case activitypub.TypeFollow:
follow := &activitypub.Follow{}
x.DecodeMapStructure(activity.Object, follow)
x.Atomic(func() error {
x.ForEachActivityPubFollower(func(record IActivityPubFollower) {
if record.Handle() == follow.Actor && record.Subject() == follow.Object {
record.UpdateAcceptedAtomic(x.Transaction(), true, x.Identity())
}
})
x.AddActivityPubIncomingActivity(
identity.Id(),
x.GenerateUUID(),
x.UnixNano(),
follow.Object,
follow.Actor,
activitypub.TypeAccept,
string(body),
)
return nil
})
default:
return nil, ERROR_INVALID_PARAMETERS
}
}
case activitypub.TypeFollow:
{
follow := &activitypub.Follow{}
x.UnmarshalJson(body, follow)
url := activity.Actor
var inbox string
url := follow.Actor
{
actor := &activitypub.Actor{}
if err := x.GetActivityStreamSigned(url, keyId, identity.PrivateKey(), nil, actor); err != nil {
return nil, err
}
inbox = actor.Inbox
}
data, err := json.Marshal(activity)
if err != nil {
actor := &activitypub.Actor{}
if err := x.GetActivityStreamSigned(url, nil, actor); err != nil {
return nil, err
}
follower := x.AddActivityPubFollower(
activity.Actor,
inbox,
follow.Actor,
actor.Inbox,
x.Format("%s/u/%s", x.PublicUrl(), username),
string(data),
x.MarshalJson(follow),
false,
)
data, _ = json.Marshal(&activitypub.Activity{
activity := &activitypub.Activity{
Context: activitypub.ActivityStreams,
ID: x.Format("%s/%s", x.PublicUrl(), x.GenerateUUID()),
Type: activitypub.TypeAccept,
Actor: x.Format("%s/u/%s", x.PublicUrl(), username),
Object: activity,
})
Object: follow,
}
if err := x.PostActivityStreamSigned(inbox, keyId, identity.PrivateKey(), data, nil); err != nil {
if err := x.PostActivityStreamSigned(actor.Inbox, activity, nil); err != nil {
return nil, err
}
@ -74,18 +89,12 @@ func PostToInbox(x IDispatcher, username string, body []byte) (IPostToInboxResul
case activitypub.TypeCreate:
{
activity := &activitypub.Activity{}
if err := json.Unmarshal(body, activity); err != nil {
return nil, ERROR_UNKNOWN_ACTIVITY_PUB_ACTIVITY
}
x.UnmarshalJson(body, activity)
switch activity.Object.(map[string]interface{})["type"] {
case activitypub.TypeNote:
note := &activitypub.Note{}
if err := mapstructure.Decode(activity.Object, note); err != nil {
return nil, ERROR_UNKNOWN_ACTIVITY_PUB_ACTIVITY
}
raw, _ := json.Marshal(note)
x.DecodeMapStructure(activity.Object, note)
x.AddActivityPubIncomingActivity(
identity.Id(),
@ -94,16 +103,14 @@ func PostToInbox(x IDispatcher, username string, body []byte) (IPostToInboxResul
note.AttributedTo,
note.To[0],
note.Content,
string(raw),
string(body),
)
default:
return nil, ERROR_INVALID_PARAMETERS
}
}
default:
{
return nil, ERROR_INVALID_PARAMETERS
}
return nil, ERROR_INVALID_PARAMETERS
}
return x.NewPostToInboxResult(body), nil

Wyświetl plik

@ -1,11 +1,9 @@
package commands
import (
"encoding/json"
"fmt"
"time"
ap "github.com/go-ap/activitypub"
"github.com/reiver/greatape/app/activitypub"
. "github.com/reiver/greatape/components/constants"
. "github.com/reiver/greatape/components/contracts"
@ -21,21 +19,21 @@ func PostToOutbox(x IDispatcher, username string, body []byte) (IPostToOutboxRes
item := x.UnmarshalActivityPubObjectOrLink(body)
id := x.Format("%s/u/%s", x.PublicUrl(), identity.Username())
publicKeyId := x.Format("%s#main-key", id)
privateKey := identity.PrivateKey()
actorId := x.GetActorId()
switch item.GetType() {
case ap.NoteType:
case activitypub.TypeNote:
{
note := x.UnmarshalActivityPubNote(body)
note, err := activitypub.UnmarshalNote(body)
if err != nil {
return nil, ERROR_INVALID_PARAMETERS
}
content := note.Content.First().Value.String()
to := note.To.First().GetID().String()
from := note.AttributedTo.GetID().String()
content := note.Content
to := note.To[0]
from := note.AttributedTo
if from != id {
if from != actorId {
return nil, ERROR_INVALID_PARAMETERS
}
@ -51,23 +49,19 @@ func PostToOutbox(x IDispatcher, username string, body []byte) (IPostToOutboxRes
Object: note,
}
if to != activitypub.Public {
if to != ACTIVITY_PUB_PUBLIC {
recipient := &activitypub.Actor{}
if err := x.GetActivityStreamSigned(to, publicKeyId, privateKey, nil, recipient); err != nil {
if err := x.GetActivityStreamSigned(to, 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 {
if err := x.PostActivityStreamSigned(recipient.Inbox, activity, nil); err != nil {
return nil, err
}
}
raw, _ := json.Marshal(note)
x.LogActivityPubOutgoingActivity(
identity.Id(),
uniqueIdentifier,
@ -75,7 +69,7 @@ func PostToOutbox(x IDispatcher, username string, body []byte) (IPostToOutboxRes
from,
to,
content,
string(raw),
string(body),
"PostToOutbox",
EMPTY_JSON,
)

Wyświetl plik

@ -26,7 +26,7 @@ type (
Atomic(handler TransactionHandler) error
Schedule(spec string, callback func()) error
GetSystemComponent(name string) ISystemComponent
RequestActivityStream(method, url, keyId, privateKey string, data []byte, output interface{}) error
RequestActivityStream(method, url, publicKeyId, privateKey string, input, output interface{}) error
LogRemoteCall(context IContext, eventType uint32, source string, input, result interface{}, err error)
// Document

Wyświetl plik

@ -1234,16 +1234,26 @@ type IDispatcher interface {
// Join concatenates the elements of its first argument to create a single string. The separator
// string is placed between elements in the resulting string.
Join(elements []string, separator string) string
// MarshalJson returns the JSON representation of input and panics in case of an error.
MarshalJson(input any) string
// UnmarshalJson parses the JSON-encoded data and stores the result in the
// value pointed to by output. If output is nil or not a pointer, it panics.
UnmarshalJson(data []byte, output any)
// DecodeMapStructure takes an input structure and uses reflection to
// translate it to the output structure. output must be a pointer to a
// map or struct. It panics in case of an error.
DecodeMapStructure(input, output interface{})
IsTestEnvironment() bool
IsDevelopmentEnvironment() bool
IsStagingEnvironment() bool
IsProductionEnvironment() bool
GetActivityStream(url string, data []byte, output interface{}) error
PostActivityStream(url string, data []byte, output interface{}) error
GetActivityStreamSigned(url, keyId, privateKey string, data []byte, output interface{}) error
PostActivityStreamSigned(url, keyId, privateKey string, data []byte, output interface{}) error
GetActorId() string
GetActivityStream(url string, input, output interface{}) error
PostActivityStream(url string, input, output interface{}) error
GetActivityStreamSigned(url string, input, output interface{}) error
PostActivityStreamSigned(url string, input, output interface{}) error
UnmarshalActivityPubObjectOrLink([]byte) activitypub.ObjectOrLink
UnmarshalActivityPubNote([]byte) *activitypub.Note
}

Wyświetl plik

@ -238,7 +238,19 @@ func (conductor *conductor) SignRequest(keyId, privateKey string, data []byte, r
return nil
}
func (conductor *conductor) RequestActivityStream(method, url, keyId, privateKey string, data []byte, output interface{}) error {
func (conductor *conductor) RequestActivityStream(method, url, publicKeyId, privateKey string, input, output interface{}) error {
var (
data []byte
err error
)
if input != nil {
data, err = json.Marshal(input)
if err != nil {
return err
}
}
var reader io.Reader
if data != nil {
reader = bytes.NewBuffer(data)
@ -252,7 +264,7 @@ func (conductor *conductor) RequestActivityStream(method, url, keyId, privateKey
req.Header.Set("Accept", "application/activity+json")
if privateKey != "" {
if err := conductor.SignRequest(keyId, privateKey, data, req); err != nil {
if err := conductor.SignRequest(publicKeyId, privateKey, data, req); err != nil {
return err
}
}

Wyświetl plik

@ -11,6 +11,7 @@ import (
"time"
"github.com/go-ap/activitypub"
"github.com/mitchellh/mapstructure"
. "github.com/reiver/greatape/components/contracts"
"github.com/valyala/fastjson"
. "github.com/xeronith/diamante/contracts/logging"
@ -318,6 +319,27 @@ func (dispatcher *dispatcher) Join(elements []string, separator string) string {
return strings.Join(elements, separator)
}
func (dispatcher *dispatcher) MarshalJson(input any) string {
data, err := json.Marshal(input)
if err != nil {
panic(err.Error())
}
return string(data)
}
func (dispatcher *dispatcher) UnmarshalJson(data []byte, output any) {
if err := json.Unmarshal(data, output); err != nil {
panic(err.Error())
}
}
func (dispatcher *dispatcher) DecodeMapStructure(input, output interface{}) {
if err := mapstructure.Decode(input, output); err != nil {
panic(err.Error())
}
}
func (dispatcher *dispatcher) IsTestEnvironment() bool {
return dispatcher.conductor.IdentityManager().IsTestEnvironment()
}
@ -334,20 +356,34 @@ func (dispatcher *dispatcher) IsProductionEnvironment() bool {
return dispatcher.conductor.IdentityManager().IsProductionEnvironment()
}
func (dispatcher *dispatcher) GetActivityStream(url string, data []byte, output interface{}) error {
return dispatcher.conductor.RequestActivityStream(http.MethodGet, url, "", "", data, output)
func (dispatcher *dispatcher) GetActorId() string {
config := dispatcher.conductor.Configuration().GetServerConfiguration()
return fmt.Sprintf("%s://%s/u/%s", config.GetProtocol(), config.GetFQDN(), dispatcher.identity.Username())
}
func (dispatcher *dispatcher) PostActivityStream(url string, data []byte, output interface{}) error {
return dispatcher.conductor.RequestActivityStream(http.MethodPost, url, "", "", data, output)
func (dispatcher *dispatcher) GetPublicKeyId(identity Identity) string {
config := dispatcher.conductor.Configuration().GetServerConfiguration()
return fmt.Sprintf("%s://%s/u/%s#main-key", config.GetProtocol(), config.GetFQDN(), identity.Username())
}
func (dispatcher *dispatcher) GetActivityStreamSigned(url, keyId, privateKey string, data []byte, output interface{}) error {
return dispatcher.conductor.RequestActivityStream(http.MethodGet, url, keyId, privateKey, data, output)
func (dispatcher *dispatcher) GetActivityStream(url string, input, output interface{}) error {
return dispatcher.conductor.RequestActivityStream(http.MethodGet, url, "", "", input, output)
}
func (dispatcher *dispatcher) PostActivityStreamSigned(url, keyId, privateKey string, data []byte, output interface{}) error {
return dispatcher.conductor.RequestActivityStream(http.MethodPost, url, keyId, privateKey, data, output)
func (dispatcher *dispatcher) PostActivityStream(url string, input, output interface{}) error {
return dispatcher.conductor.RequestActivityStream(http.MethodPost, url, "", "", input, output)
}
func (dispatcher *dispatcher) GetActivityStreamSigned(url string, input, output interface{}) error {
identity := dispatcher.identity
keyId := dispatcher.GetPublicKeyId(identity)
return dispatcher.conductor.RequestActivityStream(http.MethodGet, url, keyId, identity.PrivateKey(), input, output)
}
func (dispatcher *dispatcher) PostActivityStreamSigned(url string, input, output interface{}) error {
identity := dispatcher.identity
keyId := dispatcher.GetPublicKeyId(identity)
return dispatcher.conductor.RequestActivityStream(http.MethodPost, url, keyId, identity.PrivateKey(), input, output)
}
func (dispatcher *dispatcher) UnmarshalActivityPubObjectOrLink(data []byte) activitypub.ObjectOrLink {

2
go.mod
Wyświetl plik

@ -8,7 +8,7 @@ require (
github.com/robfig/cron v1.2.0
github.com/sendgrid/sendgrid-go v3.12.0+incompatible
github.com/valyala/fastjson v1.6.4
github.com/xeronith/diamante v1.15.5
github.com/xeronith/diamante v1.15.6
google.golang.org/protobuf v1.28.1
)

4
go.sum
Wyświetl plik

@ -64,8 +64,8 @@ github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLr
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xeronith/diamante v1.15.5 h1:/VnxmLiG+CFqwMTphjgp2juQcpu0cVxD3DO8bj4rwOQ=
github.com/xeronith/diamante v1.15.5/go.mod h1:7kgdoRJVrQvML7Z9BT6di3XU1o2eTeE0hbwy66dMOrA=
github.com/xeronith/diamante v1.15.6 h1:kC3BMP/vjwW1EOUzLHVeISO9s/O/MCc2kOBV7mn3i5A=
github.com/xeronith/diamante v1.15.6/go.mod h1:7kgdoRJVrQvML7Z9BT6di3XU1o2eTeE0hbwy66dMOrA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=