wrap.go (4737B)
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 typeutils 19 20 import ( 21 "net/url" 22 23 "github.com/superseriousbusiness/activity/pub" 24 "github.com/superseriousbusiness/activity/streams" 25 "github.com/superseriousbusiness/activity/streams/vocab" 26 "github.com/superseriousbusiness/gotosocial/internal/ap" 27 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 28 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 29 "github.com/superseriousbusiness/gotosocial/internal/id" 30 "github.com/superseriousbusiness/gotosocial/internal/uris" 31 ) 32 33 func (c *converter) WrapPersonInUpdate(person vocab.ActivityStreamsPerson, originAccount *gtsmodel.Account) (vocab.ActivityStreamsUpdate, error) { 34 update := streams.NewActivityStreamsUpdate() 35 36 // set the actor 37 actorURI, err := url.Parse(originAccount.URI) 38 if err != nil { 39 return nil, gtserror.Newf("error parsing url %s: %w", originAccount.URI, err) 40 } 41 actorProp := streams.NewActivityStreamsActorProperty() 42 actorProp.AppendIRI(actorURI) 43 update.SetActivityStreamsActor(actorProp) 44 45 // set the ID 46 47 newID, err := id.NewRandomULID() 48 if err != nil { 49 return nil, err 50 } 51 52 idString := uris.GenerateURIForUpdate(originAccount.Username, newID) 53 idURI, err := url.Parse(idString) 54 if err != nil { 55 return nil, gtserror.Newf("error parsing url %s: %w", idString, err) 56 } 57 idProp := streams.NewJSONLDIdProperty() 58 idProp.SetIRI(idURI) 59 update.SetJSONLDId(idProp) 60 61 // set the person as the object here 62 objectProp := streams.NewActivityStreamsObjectProperty() 63 objectProp.AppendActivityStreamsPerson(person) 64 update.SetActivityStreamsObject(objectProp) 65 66 // to should be public 67 toURI, err := url.Parse(pub.PublicActivityPubIRI) 68 if err != nil { 69 return nil, gtserror.Newf("error parsing url %s: %w", pub.PublicActivityPubIRI, err) 70 } 71 toProp := streams.NewActivityStreamsToProperty() 72 toProp.AppendIRI(toURI) 73 update.SetActivityStreamsTo(toProp) 74 75 // bcc followers 76 followersURI, err := url.Parse(originAccount.FollowersURI) 77 if err != nil { 78 return nil, gtserror.Newf("error parsing url %s: %w", originAccount.FollowersURI, err) 79 } 80 bccProp := streams.NewActivityStreamsBccProperty() 81 bccProp.AppendIRI(followersURI) 82 update.SetActivityStreamsBcc(bccProp) 83 84 return update, nil 85 } 86 87 func (c *converter) WrapNoteInCreate(note vocab.ActivityStreamsNote, objectIRIOnly bool) (vocab.ActivityStreamsCreate, error) { 88 create := streams.NewActivityStreamsCreate() 89 90 // Object property 91 objectProp := streams.NewActivityStreamsObjectProperty() 92 if objectIRIOnly { 93 objectProp.AppendIRI(note.GetJSONLDId().GetIRI()) 94 } else { 95 objectProp.AppendActivityStreamsNote(note) 96 } 97 create.SetActivityStreamsObject(objectProp) 98 99 // ID property 100 idProp := streams.NewJSONLDIdProperty() 101 createID := note.GetJSONLDId().GetIRI().String() + "/activity" 102 createIDIRI, err := url.Parse(createID) 103 if err != nil { 104 return nil, err 105 } 106 idProp.SetIRI(createIDIRI) 107 create.SetJSONLDId(idProp) 108 109 // Actor Property 110 actorProp := streams.NewActivityStreamsActorProperty() 111 actorIRI, err := ap.ExtractAttributedToURI(note) 112 if err != nil { 113 return nil, gtserror.Newf("couldn't extract AttributedTo: %w", err) 114 } 115 actorProp.AppendIRI(actorIRI) 116 create.SetActivityStreamsActor(actorProp) 117 118 // Published Property 119 publishedProp := streams.NewActivityStreamsPublishedProperty() 120 published, err := ap.ExtractPublished(note) 121 if err != nil { 122 return nil, gtserror.Newf("couldn't extract Published: %w", err) 123 } 124 publishedProp.Set(published) 125 create.SetActivityStreamsPublished(publishedProp) 126 127 // To Property 128 toProp := streams.NewActivityStreamsToProperty() 129 if toURIs := ap.ExtractToURIs(note); len(toURIs) != 0 { 130 for _, toURI := range toURIs { 131 toProp.AppendIRI(toURI) 132 } 133 create.SetActivityStreamsTo(toProp) 134 } 135 136 // Cc Property 137 ccProp := streams.NewActivityStreamsCcProperty() 138 if ccURIs := ap.ExtractCcURIs(note); len(ccURIs) != 0 { 139 for _, ccURI := range ccURIs { 140 ccProp.AppendIRI(ccURI) 141 } 142 create.SetActivityStreamsCc(ccProp) 143 } 144 145 return create, nil 146 }