2023-03-12 15:00:57 +00: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/>.
|
2021-05-08 12:25:55 +00:00
|
|
|
|
|
|
|
package typeutils
|
|
|
|
|
|
|
|
import (
|
2024-08-13 09:01:50 +00:00
|
|
|
"cmp"
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2021-05-08 12:25:55 +00:00
|
|
|
"errors"
|
2023-01-25 10:12:27 +00:00
|
|
|
"net/url"
|
2021-12-20 17:42:19 +00:00
|
|
|
|
2022-09-23 19:27:35 +00:00
|
|
|
"github.com/miekg/dns"
|
2021-08-10 11:32:39 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2023-01-25 10:12:27 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-06-17 15:49:11 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 08:47:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2023-01-25 10:12:27 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/uris"
|
2023-08-07 17:38:11 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-05-08 12:25:55 +00:00
|
|
|
)
|
|
|
|
|
2024-08-13 09:01:50 +00:00
|
|
|
// ASRepresentationToAccount converts a remote account / person
|
|
|
|
// / application representation into a gts model account.
|
2023-09-23 16:44:11 +00:00
|
|
|
//
|
2024-08-13 09:01:50 +00:00
|
|
|
// If accountDomain is provided then this value will be
|
|
|
|
// used as the account's Domain, else the AP ID host.
|
|
|
|
//
|
|
|
|
// If accountUsername is provided then this is used as
|
|
|
|
// a fallback when no preferredUsername is provided. Else
|
|
|
|
// a lack of username will result in error return.
|
|
|
|
func (c *Converter) ASRepresentationToAccount(
|
|
|
|
ctx context.Context,
|
|
|
|
accountable ap.Accountable,
|
|
|
|
accountDomain string,
|
|
|
|
accountUsername string,
|
|
|
|
) (
|
|
|
|
*gtsmodel.Account,
|
|
|
|
error,
|
|
|
|
) {
|
2023-11-30 16:22:34 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// Extract URI from accountable
|
|
|
|
uriObj := ap.GetJSONLDId(accountable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
|
|
|
|
|
|
|
// Create DB account with URI
|
|
|
|
var acct gtsmodel.Account
|
|
|
|
acct.URI = uri
|
|
|
|
|
|
|
|
// Check whether account is a usable actor type.
|
|
|
|
switch acct.ActorType = accountable.GetTypeName(); acct.ActorType {
|
|
|
|
|
|
|
|
// people, groups, and organizations aren't bots
|
|
|
|
case ap.ActorPerson, ap.ActorGroup, ap.ActorOrganization:
|
|
|
|
acct.Bot = util.Ptr(false)
|
|
|
|
|
|
|
|
// apps and services are
|
|
|
|
case ap.ActorApplication, ap.ActorService:
|
|
|
|
acct.Bot = util.Ptr(true)
|
|
|
|
|
|
|
|
// we don't know what this is!
|
|
|
|
default:
|
|
|
|
err := gtserror.Newf("unusable actor type for %s", uri)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
}
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2024-08-13 09:01:50 +00:00
|
|
|
// Set account username.
|
|
|
|
acct.Username = cmp.Or(
|
|
|
|
|
|
|
|
// Prefer the AP model provided username.
|
|
|
|
ap.ExtractPreferredUsername(accountable),
|
|
|
|
|
|
|
|
// Fallback username.
|
|
|
|
accountUsername,
|
|
|
|
)
|
|
|
|
if acct.Username == "" {
|
|
|
|
err := gtserror.Newf("missing username for %s", uri)
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
}
|
|
|
|
|
2024-01-26 13:17:10 +00:00
|
|
|
// Extract published time if possible.
|
|
|
|
//
|
|
|
|
// This denotes original creation time
|
|
|
|
// of the account on the remote instance.
|
|
|
|
//
|
|
|
|
// Not every implementation uses this property;
|
|
|
|
// so don't bother warning if we can't find it.
|
|
|
|
if pub := ap.GetPublished(accountable); !pub.IsZero() {
|
|
|
|
acct.CreatedAt = pub
|
|
|
|
acct.UpdatedAt = pub
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract a preferred name (display name), fallback to username.
|
|
|
|
if displayName := ap.ExtractName(accountable); displayName != "" {
|
|
|
|
acct.DisplayName = displayName
|
|
|
|
} else {
|
|
|
|
acct.DisplayName = acct.Username
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Check for separaate account
|
|
|
|
// domain to the instance hostname.
|
2022-06-11 09:01:34 +00:00
|
|
|
if accountDomain != "" {
|
|
|
|
acct.Domain = accountDomain
|
|
|
|
} else {
|
2023-11-30 16:22:34 +00:00
|
|
|
acct.Domain = uriObj.Host
|
2022-06-11 09:01:34 +00:00
|
|
|
}
|
2021-05-08 12:25:55 +00:00
|
|
|
|
|
|
|
// avatar aka icon
|
|
|
|
// if this one isn't extractable in a format we recognise we'll just skip it
|
2023-11-30 16:22:34 +00:00
|
|
|
avatarURL, err := ap.ExtractIconURI(accountable)
|
|
|
|
if err == nil {
|
2021-05-08 12:25:55 +00:00
|
|
|
acct.AvatarRemoteURL = avatarURL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// header aka image
|
|
|
|
// if this one isn't extractable in a format we recognise we'll just skip it
|
2023-11-30 16:22:34 +00:00
|
|
|
headerURL, err := ap.ExtractImageURI(accountable)
|
|
|
|
if err == nil {
|
2021-05-08 12:25:55 +00:00
|
|
|
acct.HeaderRemoteURL = headerURL.String()
|
|
|
|
}
|
|
|
|
|
2022-09-26 09:56:01 +00:00
|
|
|
// account emojis (used in bio, display name, fields)
|
2023-11-30 16:22:34 +00:00
|
|
|
acct.Emojis, err = ap.ExtractEmojis(accountable)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf(ctx, "error(s) extracting account emojis for %s: %v", uri, err)
|
2022-09-26 09:56:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract account attachments (key-value fields).
|
2023-05-09 10:16:10 +00:00
|
|
|
acct.Fields = ap.ExtractFields(accountable)
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract account note (bio / summary).
|
2023-02-02 15:41:02 +00:00
|
|
|
acct.Note = ap.ExtractSummary(accountable)
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2024-03-22 13:03:46 +00:00
|
|
|
// Assume not memorial (todo)
|
2023-11-30 16:22:34 +00:00
|
|
|
acct.Memorial = util.Ptr(false)
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2024-01-19 13:02:04 +00:00
|
|
|
// Extract 'manuallyApprovesFollowers' aka locked account (default = true).
|
|
|
|
manuallyApprovesFollowers := ap.GetManuallyApprovesFollowers(accountable)
|
|
|
|
acct.Locked = &manuallyApprovesFollowers
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract account discoverability (default = false).
|
|
|
|
discoverable := ap.GetDiscoverable(accountable)
|
2022-08-15 10:35:05 +00:00
|
|
|
acct.Discoverable = &discoverable
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract the URL property.
|
|
|
|
urls := ap.GetURL(accountable)
|
|
|
|
if len(urls) == 0 {
|
|
|
|
// just use account uri string
|
|
|
|
acct.URL = uri
|
2021-06-13 16:42:28 +00:00
|
|
|
} else {
|
2023-11-30 16:22:34 +00:00
|
|
|
// else use provided URL string
|
|
|
|
acct.URL = urls[0].String()
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract the inbox IRI property.
|
|
|
|
inboxIRI := ap.GetInbox(accountable)
|
|
|
|
if inboxIRI != nil {
|
|
|
|
acct.InboxURI = inboxIRI.String()
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract the outbox IRI property.
|
|
|
|
outboxIRI := ap.GetOutbox(accountable)
|
|
|
|
if outboxIRI != nil {
|
|
|
|
acct.OutboxURI = outboxIRI.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract a SharedInboxURI, but only trust if equal to / subdomain of account's domain.
|
2023-02-03 20:03:05 +00:00
|
|
|
if sharedInboxURI := ap.ExtractSharedInbox(accountable); // nocollapse
|
|
|
|
sharedInboxURI != nil && dns.CompareDomainName(acct.Domain, sharedInboxURI.Host) >= 2 {
|
|
|
|
sharedInbox := sharedInboxURI.String()
|
2022-09-23 19:27:35 +00:00
|
|
|
acct.SharedInboxURI = &sharedInbox
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract the following IRI property.
|
|
|
|
followingURI := ap.GetFollowing(accountable)
|
|
|
|
if followingURI != nil {
|
|
|
|
acct.FollowingURI = followingURI.String()
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract the following IRI property.
|
|
|
|
followersURI := ap.GetFollowers(accountable)
|
|
|
|
if followersURI != nil {
|
|
|
|
acct.FollowersURI = followersURI.String()
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract a FeaturedURI, but only trust if equal to / subdomain of account's domain.
|
|
|
|
if featuredURI := ap.GetFeatured(accountable); // nocollapse
|
|
|
|
featuredURI != nil && dns.CompareDomainName(acct.Domain, featuredURI.Host) >= 2 {
|
|
|
|
acct.FeaturedCollectionURI = featuredURI.String()
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FeaturedTagsURI
|
|
|
|
|
2024-02-06 09:45:46 +00:00
|
|
|
// Moved and AlsoKnownAsURIs,
|
|
|
|
// needed for account migrations.
|
|
|
|
movedToURI := ap.GetMovedTo(accountable)
|
|
|
|
if movedToURI != nil {
|
|
|
|
acct.MovedToURI = movedToURI.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
alsoKnownAsURIs := ap.GetAlsoKnownAs(accountable)
|
|
|
|
for i, uri := range alsoKnownAsURIs {
|
|
|
|
// Don't store more than
|
|
|
|
// 20 AKA URIs for remotes,
|
|
|
|
// to prevent people playing
|
|
|
|
// silly buggers.
|
|
|
|
if i >= 20 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
acct.AlsoKnownAsURIs = append(acct.AlsoKnownAsURIs, uri.String())
|
|
|
|
}
|
2021-05-08 12:25:55 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract account public key and verify ownership to account.
|
2024-04-02 10:30:10 +00:00
|
|
|
pkey, pkeyURL, pkeyOwnerID, err := ap.ExtractPubKeyFromActor(accountable)
|
2021-05-08 12:25:55 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
err := gtserror.Newf("error extracting public key for %s: %w", uri, err)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
} else if pkeyOwnerID.String() != acct.URI {
|
|
|
|
err := gtserror.Newf("public key not owned by account %s", uri)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2023-06-13 14:47:56 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
acct.PublicKey = pkey
|
|
|
|
acct.PublicKeyURI = pkeyURL.String()
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
return &acct, nil
|
2021-05-08 12:25:55 +00:00
|
|
|
}
|
2021-05-15 09:58:11 +00:00
|
|
|
|
2023-09-23 16:44:11 +00:00
|
|
|
// ASStatus converts a remote activitystreams 'status' representation into a gts model status.
|
|
|
|
func (c *Converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) {
|
2023-10-04 12:09:42 +00:00
|
|
|
var err error
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract URI from statusable
|
|
|
|
uriObj := ap.GetJSONLDId(statusable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
|
|
|
|
|
|
|
// Create DB status with URI
|
|
|
|
var status gtsmodel.Status
|
|
|
|
status.URI = uri
|
2021-08-29 10:03:08 +00:00
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.URL
|
|
|
|
//
|
|
|
|
// Web URL of this status (optional).
|
2022-05-23 15:10:48 +00:00
|
|
|
if statusURL, err := ap.ExtractURL(statusable); err == nil {
|
2021-05-15 09:58:11 +00:00
|
|
|
status.URL = statusURL.String()
|
2022-05-23 15:10:48 +00:00
|
|
|
} else {
|
2023-06-24 07:32:10 +00:00
|
|
|
status.URL = status.URI // Fall back to the URI.
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Content
|
2023-11-21 14:13:30 +00:00
|
|
|
// status.Language
|
2023-06-24 07:32:10 +00:00
|
|
|
//
|
2023-11-21 14:13:30 +00:00
|
|
|
// Many implementations set both content
|
|
|
|
// and contentMap; we can use these to
|
|
|
|
// infer the language of the status.
|
|
|
|
status.Content, status.Language = ContentToContentLanguage(
|
|
|
|
ctx,
|
|
|
|
ap.ExtractContent(statusable),
|
|
|
|
)
|
2021-05-15 09:58:11 +00:00
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Attachments
|
|
|
|
//
|
|
|
|
// Media attachments for later dereferencing.
|
2023-10-04 12:09:42 +00:00
|
|
|
status.Attachments, err = ap.ExtractAttachments(statusable)
|
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
log.Warnf(ctx, "error(s) extracting attachments for %s: %v", uri, err)
|
2023-10-04 12:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// status.Poll
|
|
|
|
//
|
|
|
|
// Attached poll information (the statusable will actually
|
|
|
|
// be a Pollable, as a Question is a subset of our Status).
|
|
|
|
if pollable, ok := ap.ToPollable(statusable); ok {
|
2023-11-08 14:32:17 +00:00
|
|
|
status.Poll, err = ap.ExtractPoll(pollable)
|
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
log.Warnf(ctx, "error(s) extracting poll for %s: %v", uri, err)
|
2023-11-08 14:32:17 +00:00
|
|
|
}
|
2023-10-04 12:09:42 +00:00
|
|
|
}
|
2021-05-15 09:58:11 +00:00
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Hashtags
|
|
|
|
//
|
|
|
|
// Hashtags for later dereferencing.
|
2021-08-29 10:03:08 +00:00
|
|
|
if hashtags, err := ap.ExtractHashtags(statusable); err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
log.Warnf(ctx, "error extracting hashtags for %s: %v", uri, err)
|
2021-08-29 10:03:08 +00:00
|
|
|
} else {
|
2021-08-20 10:26:56 +00:00
|
|
|
status.Tags = hashtags
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Emojis
|
|
|
|
//
|
|
|
|
// Custom emojis for later dereferencing.
|
2021-08-29 10:03:08 +00:00
|
|
|
if emojis, err := ap.ExtractEmojis(statusable); err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
log.Warnf(ctx, "error extracting emojis for %s: %v", uri, err)
|
2021-08-29 10:03:08 +00:00
|
|
|
} else {
|
2021-08-20 10:26:56 +00:00
|
|
|
status.Emojis = emojis
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Mentions
|
|
|
|
//
|
|
|
|
// Mentions of other accounts for later dereferencing.
|
2021-08-29 10:03:08 +00:00
|
|
|
if mentions, err := ap.ExtractMentions(statusable); err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
log.Warnf(ctx, "error extracting mentions for %s: %v", uri, err)
|
2021-08-29 10:03:08 +00:00
|
|
|
} else {
|
2021-08-20 10:26:56 +00:00
|
|
|
status.Mentions = mentions
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.ContentWarning
|
|
|
|
//
|
|
|
|
// Topic or content warning for this status;
|
|
|
|
// prefer Summary, fall back to Name.
|
2023-02-02 15:41:02 +00:00
|
|
|
if summary := ap.ExtractSummary(statusable); summary != "" {
|
|
|
|
status.ContentWarning = summary
|
2021-08-29 10:03:08 +00:00
|
|
|
} else {
|
2023-02-02 15:41:02 +00:00
|
|
|
status.ContentWarning = ap.ExtractName(statusable)
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Published
|
|
|
|
//
|
2024-01-26 13:17:10 +00:00
|
|
|
// Extract published time for the status,
|
2023-11-30 16:22:34 +00:00
|
|
|
// zero-time will fall back to db defaults.
|
|
|
|
if pub := ap.GetPublished(statusable); !pub.IsZero() {
|
|
|
|
status.CreatedAt = pub
|
|
|
|
status.UpdatedAt = pub
|
2021-08-29 10:03:08 +00:00
|
|
|
} else {
|
2023-11-30 16:22:34 +00:00
|
|
|
log.Warnf(ctx, "unusable published property on %s", uri)
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.AccountURI
|
|
|
|
// status.AccountID
|
|
|
|
// status.Account
|
|
|
|
//
|
|
|
|
// Account that created the status. Assume we have
|
|
|
|
// this in the db by the time this function is called,
|
|
|
|
// error if we don't.
|
2023-11-30 16:22:34 +00:00
|
|
|
status.Account, err = c.getASAttributedToAccount(ctx,
|
|
|
|
status.URI,
|
|
|
|
statusable,
|
|
|
|
)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2023-06-24 07:32:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-30 16:22:34 +00:00
|
|
|
|
|
|
|
// Set the related status<->account fields.
|
|
|
|
status.AccountURI = status.Account.URI
|
|
|
|
status.AccountID = status.Account.ID
|
2023-06-24 07:32:10 +00:00
|
|
|
|
|
|
|
// status.InReplyToURI
|
|
|
|
// status.InReplyToID
|
|
|
|
// status.InReplyTo
|
|
|
|
// status.InReplyToAccountID
|
|
|
|
// status.InReplyToAccount
|
|
|
|
//
|
|
|
|
// Status that this status replies to, if applicable.
|
|
|
|
// If we don't have this status in the database, we
|
|
|
|
// just set the URI and assume we can deref it later.
|
2023-11-30 16:22:34 +00:00
|
|
|
inReplyTo := ap.GetInReplyTo(statusable)
|
|
|
|
if len(inReplyTo) > 0 {
|
|
|
|
|
|
|
|
// Extract the URI from inReplyTo slice.
|
|
|
|
inReplyToURI := inReplyTo[0].String()
|
2023-06-24 07:32:10 +00:00
|
|
|
status.InReplyToURI = inReplyToURI
|
|
|
|
|
|
|
|
// Check if we already have the replied-to status.
|
2023-09-23 16:44:11 +00:00
|
|
|
inReplyTo, err := c.state.DB.GetStatusByURI(ctx, inReplyToURI)
|
2023-06-24 07:32:10 +00:00
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2023-11-30 16:22:34 +00:00
|
|
|
err := gtserror.Newf("error getting reply %s from db: %w", inReplyToURI, err)
|
2023-06-24 07:32:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if inReplyTo != nil {
|
|
|
|
// We have it in the DB! Set
|
|
|
|
// appropriate fields here and now.
|
|
|
|
status.InReplyToID = inReplyTo.ID
|
|
|
|
status.InReplyTo = inReplyTo
|
|
|
|
status.InReplyToAccountID = inReplyTo.AccountID
|
|
|
|
status.InReplyToAccount = inReplyTo.Account
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Calculate intended visibility of the status.
|
|
|
|
status.Visibility, err = ap.ExtractVisibility(
|
2023-06-24 07:32:10 +00:00
|
|
|
statusable,
|
|
|
|
status.Account.FollowersURI,
|
|
|
|
)
|
2021-05-15 09:58:11 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
err := gtserror.Newf("error extracting status visibility for %s: %w", uri, err)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 10:04:28 +00:00
|
|
|
// Status was sent to us or dereffed
|
|
|
|
// by us so it must be federated.
|
2023-08-07 17:38:11 +00:00
|
|
|
status.Federated = util.Ptr(true)
|
2023-06-24 07:32:10 +00:00
|
|
|
|
2024-07-26 10:04:28 +00:00
|
|
|
// Derive interaction policy for this status.
|
|
|
|
status.InteractionPolicy = ap.ExtractInteractionPolicy(
|
|
|
|
statusable,
|
|
|
|
status.Account,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set approvedByURI if present,
|
|
|
|
// for later dereferencing.
|
|
|
|
approvedByURI := ap.GetApprovedBy(statusable)
|
|
|
|
if approvedByURI != nil {
|
|
|
|
status.ApprovedByURI = approvedByURI.String()
|
|
|
|
}
|
|
|
|
|
2023-06-24 07:32:10 +00:00
|
|
|
// status.Sensitive
|
2023-11-30 16:22:34 +00:00
|
|
|
sensitive := ap.ExtractSensitive(statusable)
|
|
|
|
status.Sensitive = &sensitive
|
2021-05-15 09:58:11 +00:00
|
|
|
|
|
|
|
// ActivityStreamsType
|
2021-05-21 13:48:26 +00:00
|
|
|
status.ActivityStreamsType = statusable.GetTypeName()
|
2021-05-15 09:58:11 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
return &status, nil
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 16:44:11 +00:00
|
|
|
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request.
|
|
|
|
func (c *Converter) ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) {
|
2023-11-30 16:22:34 +00:00
|
|
|
uriObj := ap.GetJSONLDId(followable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
2021-05-15 09:58:11 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
origin, err := c.getASActorAccount(ctx, uri, followable)
|
2021-05-15 09:58:11 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
2023-11-30 16:22:34 +00:00
|
|
|
|
|
|
|
target, err := c.getASObjectAccount(ctx, uri, followable)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
followRequest := >smodel.FollowRequest{
|
|
|
|
URI: uri,
|
2023-11-30 16:22:34 +00:00
|
|
|
AccountID: origin.ID,
|
|
|
|
TargetAccountID: target.ID,
|
2021-05-15 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return followRequest, nil
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:44:11 +00:00
|
|
|
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow.
|
|
|
|
func (c *Converter) ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) {
|
2023-11-30 16:22:34 +00:00
|
|
|
uriObj := ap.GetJSONLDId(followable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-05-21 13:48:26 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
2021-05-21 13:48:26 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
origin, err := c.getASActorAccount(ctx, uri, followable)
|
2021-05-21 13:48:26 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-05-21 13:48:26 +00:00
|
|
|
}
|
2023-11-30 16:22:34 +00:00
|
|
|
|
|
|
|
target, err := c.getASObjectAccount(ctx, uri, followable)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-05-21 13:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
follow := >smodel.Follow{
|
|
|
|
URI: uri,
|
2023-11-30 16:22:34 +00:00
|
|
|
AccountID: origin.ID,
|
|
|
|
TargetAccountID: target.ID,
|
2021-05-21 13:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return follow, nil
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:44:11 +00:00
|
|
|
// ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave.
|
|
|
|
func (c *Converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) {
|
2023-11-30 16:22:34 +00:00
|
|
|
uriObj := ap.GetJSONLDId(likeable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
2021-05-27 14:06:24 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
origin, err := c.getASActorAccount(ctx, uri, likeable)
|
2021-05-27 14:06:24 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
target, err := c.getASObjectStatus(ctx, uri, likeable)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return >smodel.StatusFave{
|
2023-11-30 16:22:34 +00:00
|
|
|
AccountID: origin.ID,
|
|
|
|
Account: origin,
|
|
|
|
TargetAccountID: target.AccountID,
|
|
|
|
TargetAccount: target.Account,
|
|
|
|
StatusID: target.ID,
|
|
|
|
Status: target,
|
2021-08-20 10:26:56 +00:00
|
|
|
URI: uri,
|
2021-05-27 14:06:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:44:11 +00:00
|
|
|
// ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block.
|
|
|
|
func (c *Converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) {
|
2023-11-30 16:22:34 +00:00
|
|
|
uriObj := ap.GetJSONLDId(blockable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2021-07-11 14:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
2021-07-11 14:22:21 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
origin, err := c.getASActorAccount(ctx, uri, blockable)
|
2021-07-11 14:22:21 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-07-11 14:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
target, err := c.getASObjectAccount(ctx, uri, blockable)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2021-07-11 14:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return >smodel.Block{
|
2023-11-30 16:22:34 +00:00
|
|
|
AccountID: origin.ID,
|
|
|
|
Account: origin,
|
|
|
|
TargetAccountID: target.ID,
|
|
|
|
TargetAccount: target,
|
2021-07-11 14:22:21 +00:00
|
|
|
URI: uri,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 14:27:15 +00:00
|
|
|
// ASAnnounceToStatus converts an activitystreams 'announce' into a boost
|
|
|
|
// wrapper status. The returned bool indicates whether this boost is new
|
|
|
|
// (true) or not. If new, callers should use `status.BoostOfURI` to see the
|
|
|
|
// status being boosted, and do dereferencing on it as appropriate. If not
|
|
|
|
// new, then the boost has already been fully processed and can be ignored.
|
|
|
|
func (c *Converter) ASAnnounceToStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
announceable ap.Announceable,
|
|
|
|
) (*gtsmodel.Status, bool, error) {
|
2023-11-30 16:22:34 +00:00
|
|
|
// Default assume
|
|
|
|
// we already have.
|
|
|
|
isNew := false
|
|
|
|
|
|
|
|
// Extract uri ID from announceable.
|
|
|
|
uriObj := ap.GetJSONLDId(announceable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, isNew, gtserror.SetMalformed(err)
|
2023-06-17 15:49:11 +00:00
|
|
|
}
|
2021-05-28 17:57:04 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
2023-06-17 15:49:11 +00:00
|
|
|
|
|
|
|
// Check if we already have this boost in the database.
|
2023-12-01 14:27:15 +00:00
|
|
|
boost, err := c.state.DB.GetStatusByURI(ctx, uri)
|
2023-06-17 15:49:11 +00:00
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2023-11-30 16:22:34 +00:00
|
|
|
err = gtserror.Newf("db error trying to get status with uri %s: %w", uri, err)
|
2023-06-17 15:49:11 +00:00
|
|
|
return nil, isNew, err
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 14:27:15 +00:00
|
|
|
if boost != nil {
|
|
|
|
// We already have this boost,
|
2023-06-17 15:49:11 +00:00
|
|
|
// no need to proceed further.
|
2023-12-01 14:27:15 +00:00
|
|
|
return boost, isNew, nil
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 14:27:15 +00:00
|
|
|
// Create boost with URI
|
|
|
|
boost = new(gtsmodel.Status)
|
|
|
|
boost.URI = uri
|
2023-11-30 16:22:34 +00:00
|
|
|
isNew = true
|
2023-06-17 15:49:11 +00:00
|
|
|
|
|
|
|
// Get the URI of the boosted status.
|
2023-11-30 16:22:34 +00:00
|
|
|
boostOf := ap.GetObjectIRIs(announceable)
|
|
|
|
if len(boostOf) == 0 {
|
|
|
|
err := gtserror.Newf("unusable object property iri for %s", uri)
|
|
|
|
return nil, isNew, gtserror.SetMalformed(err)
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 15:49:11 +00:00
|
|
|
// Set the URI of the boosted status on
|
2023-12-01 14:27:15 +00:00
|
|
|
// the boost, for later dereferencing.
|
|
|
|
boost.BoostOfURI = boostOf[0].String()
|
2023-11-30 16:22:34 +00:00
|
|
|
|
|
|
|
// Extract published time for the boost,
|
|
|
|
// zero-time will fall back to db defaults.
|
|
|
|
if pub := ap.GetPublished(announceable); !pub.IsZero() {
|
2023-12-01 14:27:15 +00:00
|
|
|
boost.CreatedAt = pub
|
|
|
|
boost.UpdatedAt = pub
|
2023-11-30 16:22:34 +00:00
|
|
|
} else {
|
|
|
|
log.Warnf(ctx, "unusable published property on %s", uri)
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Extract and load the boost actor account,
|
|
|
|
// (this MUST already be in database by now).
|
2023-12-01 14:27:15 +00:00
|
|
|
boost.Account, err = c.getASActorAccount(ctx,
|
2023-11-30 16:22:34 +00:00
|
|
|
uri,
|
|
|
|
announceable,
|
|
|
|
)
|
2021-05-28 17:57:04 +00:00
|
|
|
if err != nil {
|
2023-06-17 15:49:11 +00:00
|
|
|
return nil, isNew, err
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Set the related status<->account fields.
|
2023-12-01 14:27:15 +00:00
|
|
|
boost.AccountURI = boost.Account.URI
|
|
|
|
boost.AccountID = boost.Account.ID
|
2021-05-28 17:57:04 +00:00
|
|
|
|
2023-06-17 15:49:11 +00:00
|
|
|
// Calculate intended visibility of the boost.
|
2023-12-01 14:27:15 +00:00
|
|
|
boost.Visibility, err = ap.ExtractVisibility(
|
2023-11-30 16:22:34 +00:00
|
|
|
announceable,
|
2023-12-01 14:27:15 +00:00
|
|
|
boost.Account.FollowersURI,
|
2023-11-30 16:22:34 +00:00
|
|
|
)
|
2021-05-28 17:57:04 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
err := gtserror.Newf("error extracting status visibility for %s: %w", uri, err)
|
|
|
|
return nil, isNew, gtserror.SetMalformed(err)
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 15:49:11 +00:00
|
|
|
// Below IDs will all be included in the
|
|
|
|
// boosted status, so set them empty here.
|
2023-12-01 14:27:15 +00:00
|
|
|
boost.AttachmentIDs = make([]string, 0)
|
|
|
|
boost.TagIDs = make([]string, 0)
|
|
|
|
boost.MentionIDs = make([]string, 0)
|
|
|
|
boost.EmojiIDs = make([]string, 0)
|
|
|
|
|
|
|
|
// Remaining fields on the boost will be
|
|
|
|
// taken from the target status; it's not
|
|
|
|
// our job to do all that dereferencing here.
|
|
|
|
return boost, isNew, nil
|
2021-05-28 17:57:04 +00:00
|
|
|
}
|
2023-01-25 10:12:27 +00:00
|
|
|
|
2023-09-23 16:44:11 +00:00
|
|
|
// ASFlagToReport converts a remote activitystreams 'flag' representation into a gts model report.
|
|
|
|
func (c *Converter) ASFlagToReport(ctx context.Context, flaggable ap.Flaggable) (*gtsmodel.Report, error) {
|
2023-11-30 16:22:34 +00:00
|
|
|
uriObj := ap.GetJSONLDId(flaggable)
|
|
|
|
if uriObj == nil {
|
|
|
|
err := gtserror.New("unusable iri property")
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Stringify uri obj.
|
|
|
|
uri := uriObj.String()
|
|
|
|
|
|
|
|
// Extract the origin (actor) account for report.
|
|
|
|
origin, err := c.getASActorAccount(ctx, uri, flaggable)
|
2023-01-25 10:12:27 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, err
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
var (
|
|
|
|
// Gathered from objects
|
|
|
|
// (+ content for misskey).
|
|
|
|
statusURIs []*url.URL
|
|
|
|
targetAccURI *url.URL
|
|
|
|
|
|
|
|
// Get current hostname.
|
|
|
|
host = config.GetHost()
|
|
|
|
)
|
|
|
|
|
2023-01-25 10:12:27 +00:00
|
|
|
// Get the content of the report.
|
|
|
|
// For Mastodon, this will just be a string, or nothing.
|
|
|
|
// In Misskey's case, it may also contain the URLs of
|
|
|
|
// one or more reported statuses, so extract these too.
|
2023-11-21 14:13:30 +00:00
|
|
|
content := ap.ExtractContent(flaggable).Content
|
2023-11-30 16:22:34 +00:00
|
|
|
statusURIs = misskeyReportInlineURLs(content)
|
2023-01-25 10:12:27 +00:00
|
|
|
|
|
|
|
// Extract account and statuses targeted by the flag / report.
|
|
|
|
//
|
|
|
|
// Incoming flags from mastodon usually have a target account uri as
|
|
|
|
// first entry in objects, followed by URIs of one or more statuses.
|
|
|
|
// Misskey on the other hand will just contain the target account uri.
|
|
|
|
// We shouldn't assume the order of the objects will correspond to this,
|
|
|
|
// but we can check that he objects slice contains just one account, and
|
|
|
|
// maybe some statuses.
|
|
|
|
//
|
|
|
|
// Throw away anything that's not relevant to us.
|
2023-11-30 16:22:34 +00:00
|
|
|
objects := ap.GetObjectIRIs(flaggable)
|
2023-01-25 10:12:27 +00:00
|
|
|
if len(objects) == 0 {
|
2023-11-30 16:22:34 +00:00
|
|
|
err := gtserror.Newf("unusable object property iris for %s", uri)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, object := range objects {
|
|
|
|
switch {
|
2023-11-30 16:22:34 +00:00
|
|
|
case object.Host != host:
|
|
|
|
// object doesn't belong
|
|
|
|
// to us, just ignore it
|
2023-01-25 10:12:27 +00:00
|
|
|
continue
|
2023-11-30 16:22:34 +00:00
|
|
|
|
2023-01-25 10:12:27 +00:00
|
|
|
case uris.IsUserPath(object):
|
2023-11-30 16:22:34 +00:00
|
|
|
if targetAccURI != nil {
|
|
|
|
err := gtserror.Newf("multiple target account uris for %s", uri)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
2023-11-30 16:22:34 +00:00
|
|
|
targetAccURI = object
|
|
|
|
|
2023-01-25 10:12:27 +00:00
|
|
|
case uris.IsStatusesPath(object):
|
|
|
|
statusURIs = append(statusURIs, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Ensure we have a target.
|
|
|
|
if targetAccURI == nil {
|
|
|
|
err := gtserror.Newf("missing target account uri for %s", uri)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
2023-11-30 16:22:34 +00:00
|
|
|
|
|
|
|
// Fetch target account from the database by its URI.
|
|
|
|
targetAcc, err := c.state.DB.GetAccountByURI(ctx, targetAccURI.String())
|
2023-01-25 10:12:27 +00:00
|
|
|
if err != nil {
|
2023-11-30 16:22:34 +00:00
|
|
|
return nil, gtserror.Newf("error getting target account %s from database: %w", targetAccURI, err)
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-11-30 16:22:34 +00:00
|
|
|
// Preallocate expected status + IDs slice lengths.
|
2023-01-25 10:12:27 +00:00
|
|
|
statusIDs = make([]string, 0, len(statusURIs))
|
|
|
|
statuses = make([]*gtsmodel.Status, 0, len(statusURIs))
|
|
|
|
)
|
2023-11-30 16:22:34 +00:00
|
|
|
|
2023-01-25 10:12:27 +00:00
|
|
|
for _, statusURI := range statusURIs {
|
2023-11-30 16:22:34 +00:00
|
|
|
// Rescope as just the URI string.
|
|
|
|
statusURI := statusURI.String()
|
2023-01-25 10:12:27 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Try getting status by URI from database.
|
|
|
|
status, err := c.state.DB.GetStatusByURI(ctx, statusURI)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
err := gtserror.Newf("error getting target status %s from database: %w", statusURI, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-25 10:12:27 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
if status == nil {
|
|
|
|
// Status was not found, try again with URL.
|
|
|
|
status, err = c.state.DB.GetStatusByURL(ctx, statusURI)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
err := gtserror.Newf("error getting target status %s from database: %w", statusURI, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-25 10:12:27 +00:00
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
if status == nil {
|
|
|
|
log.Warnf(ctx, "missing target status %s for %s", statusURI, uri)
|
2023-01-25 10:12:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
if status.AccountID != targetAcc.ID {
|
|
|
|
// status doesn't belong
|
|
|
|
// to target, ignore it.
|
2023-01-25 10:12:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Append the discovered status to slices.
|
2023-01-25 10:12:27 +00:00
|
|
|
statusIDs = append(statusIDs, status.ID)
|
|
|
|
statuses = append(statuses, status)
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// id etc should be handled the caller,
|
|
|
|
// so just return what we got
|
2023-01-25 10:12:27 +00:00
|
|
|
return >smodel.Report{
|
|
|
|
URI: uri,
|
2023-11-30 16:22:34 +00:00
|
|
|
AccountID: origin.ID,
|
|
|
|
Account: origin,
|
|
|
|
TargetAccountID: targetAcc.ID,
|
|
|
|
TargetAccount: targetAcc,
|
2023-01-25 10:12:27 +00:00
|
|
|
Comment: content,
|
|
|
|
StatusIDs: statusIDs,
|
|
|
|
Statuses: statuses,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-11-30 16:22:34 +00:00
|
|
|
|
|
|
|
func (c *Converter) getASActorAccount(ctx context.Context, id string, with ap.WithActor) (*gtsmodel.Account, error) {
|
|
|
|
// Get actor IRIs from type.
|
|
|
|
actor := ap.GetActorIRIs(with)
|
|
|
|
if len(actor) == 0 {
|
|
|
|
err := gtserror.Newf("unusable actor property iri for %s", id)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for account in database with provided actor URI.
|
|
|
|
account, err := c.state.DB.GetAccountByURI(ctx, actor[0].String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.Newf("error getting actor account from database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Converter) getASAttributedToAccount(ctx context.Context, id string, with ap.WithAttributedTo) (*gtsmodel.Account, error) {
|
|
|
|
// Get attribTo IRIs from type.
|
|
|
|
attribTo := ap.GetAttributedTo(with)
|
|
|
|
if len(attribTo) == 0 {
|
|
|
|
err := gtserror.Newf("unusable attributedTo property iri for %s", id)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for account in database with provided attributedTo URI.
|
|
|
|
account, err := c.state.DB.GetAccountByURI(ctx, attribTo[0].String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.Newf("error getting actor account from database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Converter) getASObjectAccount(ctx context.Context, id string, with ap.WithObject) (*gtsmodel.Account, error) {
|
|
|
|
// Get object IRIs from type.
|
|
|
|
object := ap.GetObjectIRIs(with)
|
|
|
|
if len(object) == 0 {
|
|
|
|
err := gtserror.Newf("unusable object property iri for %s", id)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for account in database with provided object URI.
|
|
|
|
account, err := c.state.DB.GetAccountByURI(ctx, object[0].String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.Newf("error getting object account from database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Converter) getASObjectStatus(ctx context.Context, id string, with ap.WithObject) (*gtsmodel.Status, error) {
|
|
|
|
// Get object IRIs from type.
|
|
|
|
object := ap.GetObjectIRIs(with)
|
|
|
|
if len(object) == 0 {
|
|
|
|
err := gtserror.Newf("unusable object property iri for %s", id)
|
|
|
|
return nil, gtserror.SetMalformed(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for status in database with provided object URI.
|
|
|
|
status, err := c.state.DB.GetStatusByURI(ctx, object[0].String())
|
|
|
|
if err != nil {
|
2024-09-16 15:49:40 +00:00
|
|
|
return nil, gtserror.Newf("error getting object status from database: %w", err)
|
2023-11-30 16:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|