2023-04-06 13:19:55 +02:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/superseriousbusiness/activity/streams"
|
|
|
|
"github.com/superseriousbusiness/activity/streams/vocab"
|
2023-06-27 11:37:42 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2023-04-06 13:19:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResolveStatusable tries to resolve the given bytes into an ActivityPub Statusable representation.
|
2023-04-26 17:17:22 +02:00
|
|
|
// It will then perform normalization on the Statusable.
|
2023-04-06 13:19:55 +02:00
|
|
|
//
|
|
|
|
// Works for: Article, Document, Image, Video, Note, Page, Event, Place, Profile
|
|
|
|
func ResolveStatusable(ctx context.Context, b []byte) (Statusable, error) {
|
|
|
|
rawStatusable := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(b, &rawStatusable); err != nil {
|
2023-06-27 11:37:42 +02:00
|
|
|
return nil, gtserror.Newf("error unmarshalling bytes into json: %w", err)
|
2023-04-06 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := streams.ToType(ctx, rawStatusable)
|
|
|
|
if err != nil {
|
2023-06-27 11:37:42 +02:00
|
|
|
return nil, gtserror.Newf("error resolving json into ap vocab type: %w", err)
|
2023-04-06 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
statusable Statusable
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
switch t.GetTypeName() {
|
|
|
|
case ObjectArticle:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsArticle)
|
|
|
|
case ObjectDocument:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsDocument)
|
|
|
|
case ObjectImage:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsImage)
|
|
|
|
case ObjectVideo:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsVideo)
|
|
|
|
case ObjectNote:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsNote)
|
|
|
|
case ObjectPage:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsPage)
|
|
|
|
case ObjectEvent:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsEvent)
|
|
|
|
case ObjectPlace:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsPlace)
|
|
|
|
case ObjectProfile:
|
|
|
|
statusable, ok = t.(vocab.ActivityStreamsProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2023-06-27 11:37:42 +02:00
|
|
|
err = gtserror.Newf("could not resolve %T to Statusable", t)
|
|
|
|
return nil, gtserror.SetWrongType(err)
|
2023-04-06 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 12:16:10 +02:00
|
|
|
NormalizeIncomingContent(statusable, rawStatusable)
|
|
|
|
NormalizeIncomingAttachments(statusable, rawStatusable)
|
|
|
|
NormalizeIncomingSummary(statusable, rawStatusable)
|
|
|
|
NormalizeIncomingName(statusable, rawStatusable)
|
2023-04-26 17:17:22 +02:00
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
return statusable, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveStatusable tries to resolve the given bytes into an ActivityPub Accountable representation.
|
2023-04-26 17:17:22 +02:00
|
|
|
// It will then perform normalization on the Accountable.
|
2023-04-06 13:19:55 +02:00
|
|
|
//
|
|
|
|
// Works for: Application, Group, Organization, Person, Service
|
|
|
|
func ResolveAccountable(ctx context.Context, b []byte) (Accountable, error) {
|
|
|
|
rawAccountable := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(b, &rawAccountable); err != nil {
|
2023-06-27 11:37:42 +02:00
|
|
|
return nil, gtserror.Newf("error unmarshalling bytes into json: %w", err)
|
2023-04-06 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := streams.ToType(ctx, rawAccountable)
|
|
|
|
if err != nil {
|
2023-06-27 11:37:42 +02:00
|
|
|
return nil, gtserror.Newf("error resolving json into ap vocab type: %w", err)
|
2023-04-06 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
accountable Accountable
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
switch t.GetTypeName() {
|
|
|
|
case ActorApplication:
|
|
|
|
accountable, ok = t.(vocab.ActivityStreamsApplication)
|
|
|
|
case ActorGroup:
|
|
|
|
accountable, ok = t.(vocab.ActivityStreamsGroup)
|
|
|
|
case ActorOrganization:
|
|
|
|
accountable, ok = t.(vocab.ActivityStreamsOrganization)
|
|
|
|
case ActorPerson:
|
|
|
|
accountable, ok = t.(vocab.ActivityStreamsPerson)
|
|
|
|
case ActorService:
|
|
|
|
accountable, ok = t.(vocab.ActivityStreamsService)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2023-06-27 11:37:42 +02:00
|
|
|
err = gtserror.Newf("could not resolve %T to Accountable", t)
|
|
|
|
return nil, gtserror.SetWrongType(err)
|
2023-04-06 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 12:16:10 +02:00
|
|
|
NormalizeIncomingSummary(accountable, rawAccountable)
|
2023-04-26 17:17:22 +02:00
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
return accountable, nil
|
|
|
|
}
|