gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

resolve.go (4075B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package ap
     19 
     20 import (
     21 	"context"
     22 	"encoding/json"
     23 	"fmt"
     24 
     25 	"github.com/superseriousbusiness/activity/streams"
     26 	"github.com/superseriousbusiness/activity/streams/vocab"
     27 )
     28 
     29 // ResolveStatusable tries to resolve the given bytes into an ActivityPub Statusable representation.
     30 // It will then perform normalization on the Statusable.
     31 //
     32 // Works for: Article, Document, Image, Video, Note, Page, Event, Place, Profile
     33 func ResolveStatusable(ctx context.Context, b []byte) (Statusable, error) {
     34 	rawStatusable := make(map[string]interface{})
     35 	if err := json.Unmarshal(b, &rawStatusable); err != nil {
     36 		return nil, fmt.Errorf("ResolveStatusable: error unmarshalling bytes into json: %w", err)
     37 	}
     38 
     39 	t, err := streams.ToType(ctx, rawStatusable)
     40 	if err != nil {
     41 		return nil, fmt.Errorf("ResolveStatusable: error resolving json into ap vocab type: %w", err)
     42 	}
     43 
     44 	var (
     45 		statusable Statusable
     46 		ok         bool
     47 	)
     48 
     49 	switch t.GetTypeName() {
     50 	case ObjectArticle:
     51 		statusable, ok = t.(vocab.ActivityStreamsArticle)
     52 	case ObjectDocument:
     53 		statusable, ok = t.(vocab.ActivityStreamsDocument)
     54 	case ObjectImage:
     55 		statusable, ok = t.(vocab.ActivityStreamsImage)
     56 	case ObjectVideo:
     57 		statusable, ok = t.(vocab.ActivityStreamsVideo)
     58 	case ObjectNote:
     59 		statusable, ok = t.(vocab.ActivityStreamsNote)
     60 	case ObjectPage:
     61 		statusable, ok = t.(vocab.ActivityStreamsPage)
     62 	case ObjectEvent:
     63 		statusable, ok = t.(vocab.ActivityStreamsEvent)
     64 	case ObjectPlace:
     65 		statusable, ok = t.(vocab.ActivityStreamsPlace)
     66 	case ObjectProfile:
     67 		statusable, ok = t.(vocab.ActivityStreamsProfile)
     68 	}
     69 
     70 	if !ok {
     71 		err = fmt.Errorf("ResolveStatusable: could not resolve %T to Statusable", t)
     72 		return nil, newErrWrongType(err)
     73 	}
     74 
     75 	NormalizeIncomingContent(statusable, rawStatusable)
     76 	NormalizeIncomingAttachments(statusable, rawStatusable)
     77 	NormalizeIncomingSummary(statusable, rawStatusable)
     78 	NormalizeIncomingName(statusable, rawStatusable)
     79 
     80 	return statusable, nil
     81 }
     82 
     83 // ResolveStatusable tries to resolve the given bytes into an ActivityPub Accountable representation.
     84 // It will then perform normalization on the Accountable.
     85 //
     86 // Works for: Application, Group, Organization, Person, Service
     87 func ResolveAccountable(ctx context.Context, b []byte) (Accountable, error) {
     88 	rawAccountable := make(map[string]interface{})
     89 	if err := json.Unmarshal(b, &rawAccountable); err != nil {
     90 		return nil, fmt.Errorf("ResolveAccountable: error unmarshalling bytes into json: %w", err)
     91 	}
     92 
     93 	t, err := streams.ToType(ctx, rawAccountable)
     94 	if err != nil {
     95 		return nil, fmt.Errorf("ResolveAccountable: error resolving json into ap vocab type: %w", err)
     96 	}
     97 
     98 	var (
     99 		accountable Accountable
    100 		ok          bool
    101 	)
    102 
    103 	switch t.GetTypeName() {
    104 	case ActorApplication:
    105 		accountable, ok = t.(vocab.ActivityStreamsApplication)
    106 	case ActorGroup:
    107 		accountable, ok = t.(vocab.ActivityStreamsGroup)
    108 	case ActorOrganization:
    109 		accountable, ok = t.(vocab.ActivityStreamsOrganization)
    110 	case ActorPerson:
    111 		accountable, ok = t.(vocab.ActivityStreamsPerson)
    112 	case ActorService:
    113 		accountable, ok = t.(vocab.ActivityStreamsService)
    114 	}
    115 
    116 	if !ok {
    117 		err = fmt.Errorf("ResolveAccountable: could not resolve %T to Accountable", t)
    118 		return nil, newErrWrongType(err)
    119 	}
    120 
    121 	NormalizeIncomingSummary(accountable, rawAccountable)
    122 
    123 	return accountable, nil
    124 }