2021-05-08 14:25:55 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 12:43:00 +01:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2021-05-08 14:25:55 +02:00
|
|
|
|
|
|
|
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 typeutils
|
|
|
|
|
|
|
|
import (
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2021-05-08 14:25:55 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-20 18:42:19 +01:00
|
|
|
|
2022-09-23 21:27:35 +02:00
|
|
|
"github.com/miekg/dns"
|
2021-08-10 13:32:39 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-05-08 14:25:55 +02:00
|
|
|
)
|
|
|
|
|
2022-06-11 11:01:34 +02:00
|
|
|
func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, accountDomain string, update bool) (*gtsmodel.Account, error) {
|
2021-05-08 14:25:55 +02:00
|
|
|
// first check if we actually already know this account
|
|
|
|
uriProp := accountable.GetJSONLDId()
|
|
|
|
if uriProp == nil || !uriProp.IsIRI() {
|
|
|
|
return nil, errors.New("no id property found on person, or id was not an iri")
|
|
|
|
}
|
|
|
|
uri := uriProp.GetIRI()
|
|
|
|
|
2021-05-23 18:07:04 +02:00
|
|
|
if !update {
|
2021-08-25 15:34:33 +02:00
|
|
|
acct, err := c.db.GetAccountByURI(ctx, uri.String())
|
2021-05-23 18:07:04 +02:00
|
|
|
if err == nil {
|
|
|
|
// we already know this account so we can skip generating it
|
|
|
|
return acct, nil
|
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != db.ErrNoEntries {
|
2021-05-23 18:07:04 +02:00
|
|
|
// we don't know the account and there's been a real error
|
|
|
|
return nil, fmt.Errorf("error getting account with uri %s from the database: %s", uri.String(), err)
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 18:07:04 +02:00
|
|
|
// we don't know the account, or we're being told to update it, so we need to generate it from the person -- at least we already have the URI!
|
2021-08-20 12:26:56 +02:00
|
|
|
acct := >smodel.Account{}
|
2021-05-08 14:25:55 +02:00
|
|
|
acct.URI = uri.String()
|
|
|
|
|
|
|
|
// Username aka preferredUsername
|
|
|
|
// We need this one so bail if it's not set.
|
2021-08-10 13:32:39 +02:00
|
|
|
username, err := ap.ExtractPreferredUsername(accountable)
|
2021-05-08 14:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't extract username: %s", err)
|
|
|
|
}
|
|
|
|
acct.Username = username
|
|
|
|
|
|
|
|
// Domain
|
2022-06-11 11:01:34 +02:00
|
|
|
if accountDomain != "" {
|
|
|
|
acct.Domain = accountDomain
|
|
|
|
} else {
|
|
|
|
acct.Domain = uri.Host
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
|
|
|
|
// avatar aka icon
|
|
|
|
// if this one isn't extractable in a format we recognise we'll just skip it
|
2021-08-10 13:32:39 +02:00
|
|
|
if avatarURL, err := ap.ExtractIconURL(accountable); err == nil {
|
2021-05-08 14:25:55 +02:00
|
|
|
acct.AvatarRemoteURL = avatarURL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// header aka image
|
|
|
|
// if this one isn't extractable in a format we recognise we'll just skip it
|
2021-08-10 13:32:39 +02:00
|
|
|
if headerURL, err := ap.ExtractImageURL(accountable); err == nil {
|
2021-05-08 14:25:55 +02:00
|
|
|
acct.HeaderRemoteURL = headerURL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// display name aka name
|
|
|
|
// we default to the username, but take the more nuanced name property if it exists
|
|
|
|
acct.DisplayName = username
|
2021-08-10 13:32:39 +02:00
|
|
|
if displayName, err := ap.ExtractName(accountable); err == nil {
|
2021-05-08 14:25:55 +02:00
|
|
|
acct.DisplayName = displayName
|
|
|
|
}
|
|
|
|
|
2022-09-26 11:56:01 +02:00
|
|
|
// account emojis (used in bio, display name, fields)
|
|
|
|
if emojis, err := ap.ExtractEmojis(accountable); err != nil {
|
|
|
|
log.Infof("ASRepresentationToAccount: error extracting account emojis: %s", err)
|
|
|
|
} else {
|
|
|
|
acct.Emojis = emojis
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
// TODO: fields aka attachment array
|
|
|
|
|
|
|
|
// note aka summary
|
2021-08-10 13:32:39 +02:00
|
|
|
note, err := ap.ExtractSummary(accountable)
|
2021-05-08 14:25:55 +02:00
|
|
|
if err == nil && note != "" {
|
|
|
|
acct.Note = note
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for bot and actor type
|
2021-05-21 15:48:26 +02:00
|
|
|
switch accountable.GetTypeName() {
|
2021-08-31 15:59:12 +02:00
|
|
|
case ap.ActorPerson, ap.ActorGroup, ap.ActorOrganization:
|
2021-05-08 14:25:55 +02:00
|
|
|
// people, groups, and organizations aren't bots
|
2022-08-15 12:35:05 +02:00
|
|
|
bot := false
|
|
|
|
acct.Bot = &bot
|
2021-05-08 14:25:55 +02:00
|
|
|
// apps and services are
|
2021-08-31 15:59:12 +02:00
|
|
|
case ap.ActorApplication, ap.ActorService:
|
2022-08-15 12:35:05 +02:00
|
|
|
bot := true
|
|
|
|
acct.Bot = &bot
|
2021-05-08 14:25:55 +02:00
|
|
|
default:
|
|
|
|
// we don't know what this is!
|
2021-08-31 15:59:12 +02:00
|
|
|
return nil, fmt.Errorf("type name %s not recognised or not convertible to ap.ActivityStreamsActor", accountable.GetTypeName())
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
2021-05-21 15:48:26 +02:00
|
|
|
acct.ActorType = accountable.GetTypeName()
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2022-08-15 12:35:05 +02:00
|
|
|
// assume not memorial (todo)
|
|
|
|
memorial := false
|
|
|
|
acct.Memorial = &memorial
|
|
|
|
|
|
|
|
// assume not sensitive (todo)
|
|
|
|
sensitive := false
|
|
|
|
acct.Sensitive = &sensitive
|
|
|
|
|
|
|
|
// assume not hide collections (todo)
|
|
|
|
hideCollections := false
|
|
|
|
acct.HideCollections = &hideCollections
|
|
|
|
|
2021-08-23 12:46:05 +02:00
|
|
|
// locked aka manuallyApprovesFollowers
|
2022-08-15 12:35:05 +02:00
|
|
|
locked := true
|
|
|
|
acct.Locked = &locked // assume locked by default
|
2021-08-23 12:46:05 +02:00
|
|
|
maf := accountable.GetActivityStreamsManuallyApprovesFollowers()
|
|
|
|
if maf != nil && maf.IsXMLSchemaBoolean() {
|
2022-08-15 12:35:05 +02:00
|
|
|
locked = maf.Get()
|
|
|
|
acct.Locked = &locked
|
2021-08-23 12:46:05 +02:00
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
|
|
|
|
// discoverable
|
|
|
|
// default to false -- take custom value if it's set though
|
2022-08-15 12:35:05 +02:00
|
|
|
discoverable := false
|
|
|
|
acct.Discoverable = &discoverable
|
|
|
|
d, err := ap.ExtractDiscoverable(accountable)
|
2021-05-08 14:25:55 +02:00
|
|
|
if err == nil {
|
2022-08-15 12:35:05 +02:00
|
|
|
acct.Discoverable = &d
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 14:00:39 +02:00
|
|
|
// assume not rss feed
|
|
|
|
enableRSS := false
|
|
|
|
acct.EnableRSS = &enableRSS
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
// url property
|
2021-08-10 13:32:39 +02:00
|
|
|
url, err := ap.ExtractURL(accountable)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err == nil {
|
|
|
|
// take the URL if we can find it
|
|
|
|
acct.URL = url.String()
|
|
|
|
} else {
|
|
|
|
// otherwise just take the account URI as the URL
|
|
|
|
acct.URL = uri.String()
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InboxURI
|
2021-05-21 23:04:59 +02:00
|
|
|
if accountable.GetActivityStreamsInbox() != nil && accountable.GetActivityStreamsInbox().GetIRI() != nil {
|
2021-05-09 20:34:27 +02:00
|
|
|
acct.InboxURI = accountable.GetActivityStreamsInbox().GetIRI().String()
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 21:27:35 +02:00
|
|
|
// SharedInboxURI
|
|
|
|
if sharedInboxURI := ap.ExtractSharedInbox(accountable); sharedInboxURI != nil {
|
|
|
|
var sharedInbox string
|
|
|
|
|
|
|
|
// only trust shared inbox if it has at least two domains,
|
|
|
|
// from the right, in common with the domain of the account
|
|
|
|
if dns.CompareDomainName(acct.Domain, sharedInboxURI.Host) >= 2 {
|
|
|
|
sharedInbox = sharedInboxURI.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
acct.SharedInboxURI = &sharedInbox
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
// OutboxURI
|
2021-05-09 20:34:27 +02:00
|
|
|
if accountable.GetActivityStreamsOutbox() != nil && accountable.GetActivityStreamsOutbox().GetIRI() != nil {
|
|
|
|
acct.OutboxURI = accountable.GetActivityStreamsOutbox().GetIRI().String()
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FollowingURI
|
2021-05-09 20:34:27 +02:00
|
|
|
if accountable.GetActivityStreamsFollowing() != nil && accountable.GetActivityStreamsFollowing().GetIRI() != nil {
|
|
|
|
acct.FollowingURI = accountable.GetActivityStreamsFollowing().GetIRI().String()
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FollowersURI
|
2021-05-09 20:34:27 +02:00
|
|
|
if accountable.GetActivityStreamsFollowers() != nil && accountable.GetActivityStreamsFollowers().GetIRI() != nil {
|
|
|
|
acct.FollowersURI = accountable.GetActivityStreamsFollowers().GetIRI().String()
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FeaturedURI
|
|
|
|
if accountable.GetTootFeatured() != nil && accountable.GetTootFeatured().GetIRI() != nil {
|
|
|
|
acct.FeaturedCollectionURI = accountable.GetTootFeatured().GetIRI().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FeaturedTagsURI
|
|
|
|
|
|
|
|
// TODO: alsoKnownAs
|
|
|
|
|
|
|
|
// publicKey
|
2021-08-10 13:32:39 +02:00
|
|
|
pkey, pkeyURL, err := ap.ExtractPublicKeyForOwner(accountable, uri)
|
2021-05-08 14:25:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get public key for person %s: %s", uri.String(), err)
|
|
|
|
}
|
|
|
|
acct.PublicKey = pkey
|
|
|
|
acct.PublicKeyURI = pkeyURL.String()
|
|
|
|
|
|
|
|
return acct, nil
|
|
|
|
}
|
2021-05-15 11:58:11 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) {
|
2021-05-15 11:58:11 +02:00
|
|
|
status := >smodel.Status{}
|
|
|
|
|
|
|
|
// uri at which this status is reachable
|
|
|
|
uriProp := statusable.GetJSONLDId()
|
|
|
|
if uriProp == nil || !uriProp.IsIRI() {
|
|
|
|
return nil, errors.New("no id property found, or id was not an iri")
|
|
|
|
}
|
|
|
|
status.URI = uriProp.GetIRI().String()
|
|
|
|
|
2022-07-19 10:47:55 +02:00
|
|
|
l := log.WithField("statusURI", status.URI)
|
2021-08-29 12:03:08 +02:00
|
|
|
|
2021-05-15 11:58:11 +02:00
|
|
|
// web url for viewing this status
|
2022-05-23 17:10:48 +02:00
|
|
|
if statusURL, err := ap.ExtractURL(statusable); err == nil {
|
2021-05-15 11:58:11 +02:00
|
|
|
status.URL = statusURL.String()
|
2022-05-23 17:10:48 +02:00
|
|
|
} else {
|
|
|
|
// if no URL was set, just take the URI
|
|
|
|
status.URL = status.URI
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the html-formatted content of this status
|
2022-05-23 17:12:46 +02:00
|
|
|
status.Content = ap.ExtractContent(statusable)
|
2021-05-15 11:58:11 +02:00
|
|
|
|
|
|
|
// attachments to dereference and fetch later on (we don't do that here)
|
2021-08-29 12:03:08 +02:00
|
|
|
if attachments, err := ap.ExtractAttachments(statusable); err != nil {
|
|
|
|
l.Infof("ASStatusToStatus: error extracting status attachments: %s", err)
|
|
|
|
} else {
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Attachments = attachments
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// hashtags to dereference later on
|
2021-08-29 12:03:08 +02:00
|
|
|
if hashtags, err := ap.ExtractHashtags(statusable); err != nil {
|
|
|
|
l.Infof("ASStatusToStatus: error extracting status hashtags: %s", err)
|
|
|
|
} else {
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Tags = hashtags
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// emojis to dereference and fetch later on
|
2021-08-29 12:03:08 +02:00
|
|
|
if emojis, err := ap.ExtractEmojis(statusable); err != nil {
|
|
|
|
l.Infof("ASStatusToStatus: error extracting status emojis: %s", err)
|
|
|
|
} else {
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Emojis = emojis
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// mentions to dereference later on
|
2021-08-29 12:03:08 +02:00
|
|
|
if mentions, err := ap.ExtractMentions(statusable); err != nil {
|
|
|
|
l.Infof("ASStatusToStatus: error extracting status mentions: %s", err)
|
|
|
|
} else {
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Mentions = mentions
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// cw string for this status
|
2021-08-29 12:03:08 +02:00
|
|
|
if cw, err := ap.ExtractSummary(statusable); err != nil {
|
|
|
|
l.Infof("ASStatusToStatus: error extracting status summary: %s", err)
|
|
|
|
} else {
|
2021-05-15 11:58:11 +02:00
|
|
|
status.ContentWarning = cw
|
|
|
|
}
|
|
|
|
|
|
|
|
// when was this status created?
|
2021-08-10 13:32:39 +02:00
|
|
|
published, err := ap.ExtractPublished(statusable)
|
2021-08-29 12:03:08 +02:00
|
|
|
if err != nil {
|
|
|
|
l.Infof("ASStatusToStatus: error extracting status published: %s", err)
|
|
|
|
} else {
|
2021-05-15 11:58:11 +02:00
|
|
|
status.CreatedAt = published
|
2021-08-25 15:34:33 +02:00
|
|
|
status.UpdatedAt = published
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// which account posted this status?
|
|
|
|
// if we don't know the account yet we can dereference it later
|
2021-08-10 13:32:39 +02:00
|
|
|
attributedTo, err := ap.ExtractAttributedTo(statusable)
|
2021-05-15 11:58:11 +02:00
|
|
|
if err != nil {
|
2021-10-06 18:18:02 +02:00
|
|
|
return nil, errors.New("ASStatusToStatus: attributedTo was empty")
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
2021-06-17 18:02:33 +02:00
|
|
|
status.AccountURI = attributedTo.String()
|
2021-05-15 11:58:11 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
statusOwner, err := c.db.GetAccountByURI(ctx, attributedTo.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-10-06 18:18:02 +02:00
|
|
|
return nil, fmt.Errorf("ASStatusToStatus: couldn't get status owner from db: %s", err)
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
status.AccountID = statusOwner.ID
|
2021-06-17 18:02:33 +02:00
|
|
|
status.AccountURI = statusOwner.URI
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Account = statusOwner
|
2021-05-15 11:58:11 +02:00
|
|
|
|
|
|
|
// check if there's a post that this is a reply to
|
2021-08-10 13:32:39 +02:00
|
|
|
inReplyToURI := ap.ExtractInReplyToURI(statusable)
|
|
|
|
if inReplyToURI != nil {
|
2021-05-15 11:58:11 +02:00
|
|
|
// something is set so we can at least set this field on the
|
|
|
|
// status and dereference using this later if we need to
|
2021-06-17 18:02:33 +02:00
|
|
|
status.InReplyToURI = inReplyToURI.String()
|
2021-05-15 11:58:11 +02:00
|
|
|
|
|
|
|
// now we can check if we have the replied-to status in our db already
|
2021-08-25 15:34:33 +02:00
|
|
|
if inReplyToStatus, err := c.db.GetStatusByURI(ctx, inReplyToURI.String()); err == nil {
|
2021-05-15 11:58:11 +02:00
|
|
|
// we have the status in our database already
|
2021-08-20 12:26:56 +02:00
|
|
|
// so we can set these fields here and now...
|
2021-05-15 11:58:11 +02:00
|
|
|
status.InReplyToID = inReplyToStatus.ID
|
|
|
|
status.InReplyToAccountID = inReplyToStatus.AccountID
|
2021-08-20 12:26:56 +02:00
|
|
|
status.InReplyTo = inReplyToStatus
|
|
|
|
if status.InReplyToAccount == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
if inReplyToAccount, err := c.db.GetAccountByID(ctx, inReplyToStatus.AccountID); err == nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
status.InReplyToAccount = inReplyToAccount
|
|
|
|
}
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility entry for this status
|
2021-10-06 18:18:02 +02:00
|
|
|
visibility, err := ap.ExtractVisibility(statusable, status.Account.FollowersURI)
|
2021-05-15 11:58:11 +02:00
|
|
|
if err != nil {
|
2021-10-06 18:18:02 +02:00
|
|
|
return nil, fmt.Errorf("ASStatusToStatus: error extracting visibility: %s", err)
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
status.Visibility = visibility
|
|
|
|
|
|
|
|
// advanced visibility for this status
|
|
|
|
// TODO: a lot of work to be done here -- a new type needs to be created for this in go-fed/activity using ASTOOL
|
2021-09-09 16:15:25 +02:00
|
|
|
// for now we just set everything to true
|
2022-08-15 12:35:05 +02:00
|
|
|
pinned := false
|
|
|
|
federated := true
|
|
|
|
boostable := true
|
|
|
|
replyable := true
|
|
|
|
likeable := true
|
|
|
|
|
|
|
|
status.Pinned = &pinned
|
|
|
|
status.Federated = &federated
|
|
|
|
status.Boostable = &boostable
|
|
|
|
status.Replyable = &replyable
|
|
|
|
status.Likeable = &likeable
|
2021-05-15 11:58:11 +02:00
|
|
|
// sensitive
|
2022-08-15 12:35:05 +02:00
|
|
|
sensitive := ap.ExtractSensitive(statusable)
|
|
|
|
status.Sensitive = &sensitive
|
2021-05-15 11:58:11 +02:00
|
|
|
|
|
|
|
// language
|
|
|
|
// we might be able to extract this from the contentMap field
|
|
|
|
|
|
|
|
// ActivityStreamsType
|
2021-05-21 15:48:26 +02:00
|
|
|
status.ActivityStreamsType = statusable.GetTypeName()
|
2021-05-15 11:58:11 +02:00
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) {
|
2021-05-15 11:58:11 +02:00
|
|
|
idProp := followable.GetJSONLDId()
|
|
|
|
if idProp == nil || !idProp.IsIRI() {
|
|
|
|
return nil, errors.New("no id property set on follow, or was not an iri")
|
|
|
|
}
|
|
|
|
uri := idProp.GetIRI().String()
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
origin, err := ap.ExtractActor(followable)
|
2021-05-15 11:58:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error extracting actor property from follow")
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-15 11:58:11 +02:00
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
target, err := ap.ExtractObject(followable)
|
2021-05-15 11:58:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error extracting object property from follow")
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
targetAccount, err := c.db.GetAccountByURI(ctx, target.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-15 11:58:11 +02:00
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
followRequest := >smodel.FollowRequest{
|
|
|
|
URI: uri,
|
|
|
|
AccountID: originAccount.ID,
|
|
|
|
TargetAccountID: targetAccount.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return followRequest, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *converter) ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) {
|
2021-05-21 15:48:26 +02:00
|
|
|
idProp := followable.GetJSONLDId()
|
|
|
|
if idProp == nil || !idProp.IsIRI() {
|
|
|
|
return nil, errors.New("no id property set on follow, or was not an iri")
|
|
|
|
}
|
|
|
|
uri := idProp.GetIRI().String()
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
origin, err := ap.ExtractActor(followable)
|
2021-05-21 15:48:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error extracting actor property from follow")
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-21 15:48:26 +02:00
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
target, err := ap.ExtractObject(followable)
|
2021-05-21 15:48:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error extracting object property from follow")
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
targetAccount, err := c.db.GetAccountByURI(ctx, target.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-21 15:48:26 +02:00
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
follow := >smodel.Follow{
|
|
|
|
URI: uri,
|
|
|
|
AccountID: originAccount.ID,
|
|
|
|
TargetAccountID: targetAccount.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return follow, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) {
|
2021-05-27 16:06:24 +02:00
|
|
|
idProp := likeable.GetJSONLDId()
|
|
|
|
if idProp == nil || !idProp.IsIRI() {
|
|
|
|
return nil, errors.New("no id property set on like, or was not an iri")
|
|
|
|
}
|
|
|
|
uri := idProp.GetIRI().String()
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
origin, err := ap.ExtractActor(likeable)
|
2021-05-27 16:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error extracting actor property from like")
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-27 16:06:24 +02:00
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
target, err := ap.ExtractObject(likeable)
|
2021-05-27 16:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error extracting object property from like")
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
targetStatus, err := c.db.GetStatusByURI(ctx, target.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-27 16:06:24 +02:00
|
|
|
return nil, fmt.Errorf("error extracting status with uri %s from the database: %s", target.String(), err)
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
var targetAccount *gtsmodel.Account
|
|
|
|
if targetStatus.Account != nil {
|
|
|
|
targetAccount = targetStatus.Account
|
|
|
|
} else {
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := c.db.GetAccountByID(ctx, targetStatus.AccountID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error extracting account with id %s from the database: %s", targetStatus.AccountID, err)
|
|
|
|
}
|
|
|
|
targetAccount = a
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return >smodel.StatusFave{
|
2021-08-20 12:26:56 +02:00
|
|
|
AccountID: originAccount.ID,
|
|
|
|
Account: originAccount,
|
|
|
|
TargetAccountID: targetAccount.ID,
|
|
|
|
TargetAccount: targetAccount,
|
|
|
|
StatusID: targetStatus.ID,
|
|
|
|
Status: targetStatus,
|
|
|
|
URI: uri,
|
2021-05-27 16:06:24 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) {
|
2021-07-11 16:22:21 +02:00
|
|
|
idProp := blockable.GetJSONLDId()
|
|
|
|
if idProp == nil || !idProp.IsIRI() {
|
|
|
|
return nil, errors.New("ASBlockToBlock: no id property set on block, or was not an iri")
|
|
|
|
}
|
|
|
|
uri := idProp.GetIRI().String()
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
origin, err := ap.ExtractActor(blockable)
|
2021-07-11 16:22:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("ASBlockToBlock: error extracting actor property from block")
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
originAccount, err := c.db.GetAccountByURI(ctx, origin.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
2021-07-11 16:22:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
target, err := ap.ExtractObject(blockable)
|
2021-07-11 16:22:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("ASBlockToBlock: error extracting object property from block")
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
targetAccount, err := c.db.GetAccountByURI(ctx, target.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
|
2021-07-11 16:22:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return >smodel.Block{
|
|
|
|
AccountID: originAccount.ID,
|
|
|
|
Account: originAccount,
|
|
|
|
TargetAccountID: targetAccount.ID,
|
|
|
|
TargetAccount: targetAccount,
|
|
|
|
URI: uri,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (*gtsmodel.Status, bool, error) {
|
2021-05-28 19:57:04 +02:00
|
|
|
status := >smodel.Status{}
|
|
|
|
isNew := true
|
|
|
|
|
|
|
|
// check if we already have the boost in the database
|
|
|
|
idProp := announceable.GetJSONLDId()
|
|
|
|
if idProp == nil || !idProp.IsIRI() {
|
|
|
|
return nil, isNew, errors.New("no id property set on announce, or was not an iri")
|
|
|
|
}
|
|
|
|
uri := idProp.GetIRI().String()
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if status, err := c.db.GetStatusByURI(ctx, uri); err == nil {
|
2021-05-28 19:57:04 +02:00
|
|
|
// we already have it, great, just return it as-is :)
|
|
|
|
isNew = false
|
|
|
|
return status, isNew, nil
|
|
|
|
}
|
|
|
|
status.URI = uri
|
|
|
|
|
|
|
|
// get the URI of the announced/boosted status
|
2021-08-10 13:32:39 +02:00
|
|
|
boostedStatusURI, err := ap.ExtractObject(announceable)
|
2021-05-28 19:57:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error getting object from announce: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the URI on the new status for dereferencing later
|
2021-08-20 12:26:56 +02:00
|
|
|
status.BoostOf = >smodel.Status{
|
2021-05-28 19:57:04 +02:00
|
|
|
URI: boostedStatusURI.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the published time for the announce
|
2021-08-10 13:32:39 +02:00
|
|
|
published, err := ap.ExtractPublished(announceable)
|
2021-05-28 19:57:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error extracting published time: %s", err)
|
|
|
|
}
|
|
|
|
status.CreatedAt = published
|
|
|
|
status.UpdatedAt = published
|
|
|
|
|
|
|
|
// get the actor's IRI (ie., the person who boosted the status)
|
2021-08-10 13:32:39 +02:00
|
|
|
actor, err := ap.ExtractActor(announceable)
|
2021-05-28 19:57:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error extracting actor: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the boosting account based on the URI
|
|
|
|
// this should have been dereferenced already before we hit this point so we can confidently error out if we don't have it
|
2021-08-25 15:34:33 +02:00
|
|
|
boostingAccount, err := c.db.GetAccountByURI(ctx, actor.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-28 19:57:04 +02:00
|
|
|
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error in db fetching account with uri %s: %s", actor.String(), err)
|
|
|
|
}
|
|
|
|
status.AccountID = boostingAccount.ID
|
2021-06-17 18:02:33 +02:00
|
|
|
status.AccountURI = boostingAccount.URI
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Account = boostingAccount
|
2021-05-28 19:57:04 +02:00
|
|
|
|
|
|
|
// these will all be wrapped in the boosted status so set them empty here
|
2021-08-20 12:26:56 +02:00
|
|
|
status.AttachmentIDs = []string{}
|
|
|
|
status.TagIDs = []string{}
|
|
|
|
status.MentionIDs = []string{}
|
|
|
|
status.EmojiIDs = []string{}
|
2021-05-28 19:57:04 +02:00
|
|
|
|
2021-10-06 18:18:02 +02:00
|
|
|
visibility, err := ap.ExtractVisibility(announceable, boostingAccount.FollowersURI)
|
2021-05-28 19:57:04 +02:00
|
|
|
if err != nil {
|
2021-10-06 18:18:02 +02:00
|
|
|
return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error extracting visibility: %s", err)
|
2021-05-28 19:57:04 +02:00
|
|
|
}
|
|
|
|
status.Visibility = visibility
|
|
|
|
|
|
|
|
// the rest of the fields will be taken from the target status, but it's not our job to do the dereferencing here
|
|
|
|
return status, isNew, nil
|
|
|
|
}
|