2021-04-19 19:42:19 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 18:42:19 +01:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-04-19 19:42:19 +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/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
package typeutils
|
2021-04-19 19:42:19 +02:00
|
|
|
|
|
|
|
import (
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2022-11-08 18:11:06 +01:00
|
|
|
"errors"
|
2021-04-19 19:42:19 +02:00
|
|
|
"fmt"
|
2021-05-17 19:06:58 +02:00
|
|
|
"strings"
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-12-07 13:31:39 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-04-19 19:42:19 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2022-06-26 10:58:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
2022-05-24 18:21:27 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-04-19 19:42:19 +02:00
|
|
|
)
|
|
|
|
|
2022-09-08 12:36:42 +02:00
|
|
|
const (
|
|
|
|
instanceStatusesCharactersReservedPerURL = 25
|
|
|
|
instanceMediaAttachmentsImageMatrixLimit = 16777216 // width * height
|
|
|
|
instanceMediaAttachmentsVideoMatrixLimit = 16777216 // width * height
|
|
|
|
instanceMediaAttachmentsVideoFrameRateLimit = 60
|
|
|
|
instancePollsMinExpiration = 300 // seconds
|
|
|
|
instancePollsMaxExpiration = 2629746 // seconds
|
|
|
|
)
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) AccountToAPIAccountSensitive(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
|
2021-04-19 19:42:19 +02:00
|
|
|
// we can build this sensitive account easily by first getting the public account....
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAccount, err := c.AccountToAPIAccountPublic(ctx, a)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// then adding the Source object to it...
|
|
|
|
|
|
|
|
// check pending follow requests aimed at this account
|
2021-08-25 15:34:33 +02:00
|
|
|
frs, err := c.db.GetAccountFollowRequests(ctx, a.ID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
2021-04-19 19:42:19 +02:00
|
|
|
return nil, fmt.Errorf("error getting follow requests: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var frc int
|
2021-08-20 12:26:56 +02:00
|
|
|
if frs != nil {
|
|
|
|
frc = len(frs)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 12:09:21 +02:00
|
|
|
statusFormat := string(model.StatusFormatDefault)
|
|
|
|
if a.StatusFormat != "" {
|
|
|
|
statusFormat = a.StatusFormat
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAccount.Source = &model.Source{
|
|
|
|
Privacy: c.VisToAPIVis(ctx, a.Privacy),
|
2022-08-15 12:35:05 +02:00
|
|
|
Sensitive: *a.Sensitive,
|
2021-04-19 19:42:19 +02:00
|
|
|
Language: a.Language,
|
2022-08-06 12:09:21 +02:00
|
|
|
StatusFormat: statusFormat,
|
2022-05-07 17:55:27 +02:00
|
|
|
Note: a.NoteRaw,
|
2021-10-04 15:24:19 +02:00
|
|
|
Fields: apiAccount.Fields,
|
2021-04-19 19:42:19 +02:00
|
|
|
FollowRequestsCount: frc,
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
return apiAccount, nil
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
|
2021-08-25 15:34:33 +02:00
|
|
|
if a == nil {
|
|
|
|
return nil, fmt.Errorf("given account was nil")
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
// count followers
|
2021-08-25 15:34:33 +02:00
|
|
|
followersCount, err := c.db.CountAccountFollowedBy(ctx, a.ID, false)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error counting followers: %s", err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// count following
|
2021-08-25 15:34:33 +02:00
|
|
|
followingCount, err := c.db.CountAccountFollows(ctx, a.ID, false)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error counting following: %s", err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// count statuses
|
2021-08-25 15:34:33 +02:00
|
|
|
statusesCount, err := c.db.CountAccountStatuses(ctx, a.ID)
|
2021-05-17 19:06:58 +02:00
|
|
|
if err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
return nil, fmt.Errorf("error counting statuses: %s", err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check when the last status was
|
2022-11-13 21:38:01 +01:00
|
|
|
var lastStatusAt *string
|
2022-10-08 14:00:39 +02:00
|
|
|
lastPosted, err := c.db.GetAccountLastPosted(ctx, a.ID, false)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err == nil && !lastPosted.IsZero() {
|
2022-11-13 21:38:01 +01:00
|
|
|
lastStatusAtTemp := util.FormatISO8601(lastPosted)
|
|
|
|
lastStatusAt = &lastStatusAtTemp
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:12:17 +01:00
|
|
|
// set account avatar fields if available
|
2021-08-20 12:26:56 +02:00
|
|
|
var aviURL string
|
|
|
|
var aviURLStatic string
|
|
|
|
if a.AvatarMediaAttachmentID != "" {
|
|
|
|
if a.AvatarMediaAttachment == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
avi, err := c.db.GetAttachmentByID(ctx, a.AvatarMediaAttachmentID)
|
2022-01-24 13:12:17 +01:00
|
|
|
if err == nil {
|
|
|
|
a.AvatarMediaAttachment = avi
|
2022-02-08 12:37:58 +01:00
|
|
|
} else {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("AccountToAPIAccountPublic: error getting Avatar with id %s: %s", a.AvatarMediaAttachmentID, err)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2022-01-25 12:03:25 +01:00
|
|
|
if a.AvatarMediaAttachment != nil {
|
|
|
|
aviURL = a.AvatarMediaAttachment.URL
|
|
|
|
aviURLStatic = a.AvatarMediaAttachment.Thumbnail.URL
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:12:17 +01:00
|
|
|
// set account header fields if available
|
2021-08-20 12:26:56 +02:00
|
|
|
var headerURL string
|
|
|
|
var headerURLStatic string
|
|
|
|
if a.HeaderMediaAttachmentID != "" {
|
|
|
|
if a.HeaderMediaAttachment == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
avi, err := c.db.GetAttachmentByID(ctx, a.HeaderMediaAttachmentID)
|
2022-01-24 13:12:17 +01:00
|
|
|
if err == nil {
|
|
|
|
a.HeaderMediaAttachment = avi
|
2022-02-08 12:37:58 +01:00
|
|
|
} else {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("AccountToAPIAccountPublic: error getting Header with id %s: %s", a.HeaderMediaAttachmentID, err)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2022-01-25 12:03:25 +01:00
|
|
|
if a.HeaderMediaAttachment != nil {
|
|
|
|
headerURL = a.HeaderMediaAttachment.URL
|
|
|
|
headerURLStatic = a.HeaderMediaAttachment.Thumbnail.URL
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the fields set on this account
|
2021-05-08 14:25:55 +02:00
|
|
|
fields := []model.Field{}
|
2021-04-19 19:42:19 +02:00
|
|
|
for _, f := range a.Fields {
|
2021-05-08 14:25:55 +02:00
|
|
|
mField := model.Field{
|
2021-04-19 19:42:19 +02:00
|
|
|
Name: f.Name,
|
|
|
|
Value: f.Value,
|
|
|
|
}
|
|
|
|
if !f.VerifiedAt.IsZero() {
|
2022-05-24 18:21:27 +02:00
|
|
|
mField.VerifiedAt = util.FormatISO8601(f.VerifiedAt)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
fields = append(fields, mField)
|
|
|
|
}
|
|
|
|
|
2022-09-26 11:56:01 +02:00
|
|
|
// account emojis
|
2021-05-27 16:06:24 +02:00
|
|
|
emojis := []model.Emoji{}
|
2022-09-26 11:56:01 +02:00
|
|
|
gtsEmojis := a.Emojis
|
|
|
|
if len(a.EmojiIDs) > len(gtsEmojis) {
|
|
|
|
gtsEmojis = []*gtsmodel.Emoji{}
|
|
|
|
for _, emojiID := range a.EmojiIDs {
|
|
|
|
emoji, err := c.db.GetEmojiByID(ctx, emojiID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("AccountToAPIAccountPublic: error getting emoji %s from database: %s", emojiID, err)
|
|
|
|
}
|
|
|
|
gtsEmojis = append(gtsEmojis, emoji)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, emoji := range gtsEmojis {
|
|
|
|
if *emoji.Disabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
apiEmoji, err := c.EmojiToAPIEmoji(ctx, emoji)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("AccountToAPIAccountPublic: error converting emoji to api emoji: %s", err)
|
|
|
|
}
|
|
|
|
emojis = append(emojis, apiEmoji)
|
|
|
|
}
|
2021-05-27 16:06:24 +02:00
|
|
|
|
2022-11-15 10:19:32 +01:00
|
|
|
var (
|
|
|
|
acct string
|
|
|
|
role = model.AccountRoleUnknown
|
|
|
|
)
|
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
if a.Domain != "" {
|
|
|
|
// this is a remote user
|
2022-11-15 10:19:32 +01:00
|
|
|
acct = a.Username + "@" + a.Domain
|
2021-04-19 19:42:19 +02:00
|
|
|
} else {
|
|
|
|
// this is a local user
|
|
|
|
acct = a.Username
|
2022-11-15 10:19:32 +01:00
|
|
|
user, err := c.db.GetUserByAccountID(ctx, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("AccountToAPIAccountPublic: error getting user from database for account id %s: %s", a.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case *user.Admin:
|
|
|
|
role = model.AccountRoleAdmin
|
|
|
|
case *user.Moderator:
|
|
|
|
role = model.AccountRoleModerator
|
|
|
|
default:
|
|
|
|
role = model.AccountRoleUser
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 16:22:21 +02:00
|
|
|
var suspended bool
|
|
|
|
if !a.SuspendedAt.IsZero() {
|
|
|
|
suspended = true
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
accountFrontend := &model.Account{
|
2021-04-19 19:42:19 +02:00
|
|
|
ID: a.ID,
|
|
|
|
Username: a.Username,
|
|
|
|
Acct: acct,
|
|
|
|
DisplayName: a.DisplayName,
|
2022-08-15 12:35:05 +02:00
|
|
|
Locked: *a.Locked,
|
|
|
|
Bot: *a.Bot,
|
2022-05-24 18:21:27 +02:00
|
|
|
CreatedAt: util.FormatISO8601(a.CreatedAt),
|
2021-04-19 19:42:19 +02:00
|
|
|
Note: a.Note,
|
|
|
|
URL: a.URL,
|
|
|
|
Avatar: aviURL,
|
|
|
|
AvatarStatic: aviURLStatic,
|
|
|
|
Header: headerURL,
|
|
|
|
HeaderStatic: headerURLStatic,
|
|
|
|
FollowersCount: followersCount,
|
|
|
|
FollowingCount: followingCount,
|
|
|
|
StatusesCount: statusesCount,
|
|
|
|
LastStatusAt: lastStatusAt,
|
2022-09-26 11:56:01 +02:00
|
|
|
Emojis: emojis,
|
2021-04-19 19:42:19 +02:00
|
|
|
Fields: fields,
|
2021-07-11 16:22:21 +02:00
|
|
|
Suspended: suspended,
|
2022-09-12 13:14:29 +02:00
|
|
|
CustomCSS: a.CustomCSS,
|
2022-10-08 14:00:39 +02:00
|
|
|
EnableRSS: *a.EnableRSS,
|
2022-11-15 10:19:32 +01:00
|
|
|
Role: role,
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 14:41:42 +02:00
|
|
|
c.ensureAvatar(accountFrontend)
|
|
|
|
c.ensureHeader(accountFrontend)
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
return accountFrontend, nil
|
2021-07-11 16:22:21 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) AccountToAPIAccountBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
|
2021-07-11 16:22:21 +02:00
|
|
|
var acct string
|
|
|
|
if a.Domain != "" {
|
|
|
|
// this is a remote user
|
|
|
|
acct = fmt.Sprintf("%s@%s", a.Username, a.Domain)
|
|
|
|
} else {
|
|
|
|
// this is a local user
|
|
|
|
acct = a.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
var suspended bool
|
|
|
|
if !a.SuspendedAt.IsZero() {
|
|
|
|
suspended = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Account{
|
|
|
|
ID: a.ID,
|
|
|
|
Username: a.Username,
|
|
|
|
Acct: acct,
|
|
|
|
DisplayName: a.DisplayName,
|
2022-08-15 12:35:05 +02:00
|
|
|
Bot: *a.Bot,
|
2022-05-24 18:21:27 +02:00
|
|
|
CreatedAt: util.FormatISO8601(a.CreatedAt),
|
2021-07-11 16:22:21 +02:00
|
|
|
URL: a.URL,
|
|
|
|
Suspended: suspended,
|
2021-04-19 19:42:19 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) AppToAPIAppSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
|
2021-05-08 14:25:55 +02:00
|
|
|
return &model.Application{
|
2021-04-19 19:42:19 +02:00
|
|
|
ID: a.ID,
|
|
|
|
Name: a.Name,
|
|
|
|
Website: a.Website,
|
|
|
|
RedirectURI: a.RedirectURI,
|
|
|
|
ClientID: a.ClientID,
|
|
|
|
ClientSecret: a.ClientSecret,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) AppToAPIAppPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
|
2021-05-08 14:25:55 +02:00
|
|
|
return &model.Application{
|
2021-04-19 19:42:19 +02:00
|
|
|
Name: a.Name,
|
|
|
|
Website: a.Website,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) AttachmentToAPIAttachment(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) {
|
2022-07-22 12:48:19 +02:00
|
|
|
apiAttachment := model.Attachment{
|
|
|
|
ID: a.ID,
|
|
|
|
Type: strings.ToLower(string(a.Type)),
|
|
|
|
TextURL: a.URL,
|
|
|
|
PreviewURL: a.Thumbnail.URL,
|
2021-05-08 14:25:55 +02:00
|
|
|
Meta: model.MediaMeta{
|
|
|
|
Original: model.MediaDimensions{
|
2021-04-19 19:42:19 +02:00
|
|
|
Width: a.FileMeta.Original.Width,
|
|
|
|
Height: a.FileMeta.Original.Height,
|
|
|
|
Size: fmt.Sprintf("%dx%d", a.FileMeta.Original.Width, a.FileMeta.Original.Height),
|
|
|
|
Aspect: float32(a.FileMeta.Original.Aspect),
|
|
|
|
},
|
2021-05-08 14:25:55 +02:00
|
|
|
Small: model.MediaDimensions{
|
2021-04-19 19:42:19 +02:00
|
|
|
Width: a.FileMeta.Small.Width,
|
|
|
|
Height: a.FileMeta.Small.Height,
|
|
|
|
Size: fmt.Sprintf("%dx%d", a.FileMeta.Small.Width, a.FileMeta.Small.Height),
|
|
|
|
Aspect: float32(a.FileMeta.Small.Aspect),
|
|
|
|
},
|
2021-05-08 14:25:55 +02:00
|
|
|
Focus: model.MediaFocus{
|
2021-04-19 19:42:19 +02:00
|
|
|
X: a.FileMeta.Focus.X,
|
|
|
|
Y: a.FileMeta.Focus.Y,
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 12:48:19 +02:00
|
|
|
Blurhash: a.Blurhash,
|
|
|
|
}
|
|
|
|
|
|
|
|
// nullable fields
|
|
|
|
if a.URL != "" {
|
|
|
|
i := a.URL
|
|
|
|
apiAttachment.URL = &i
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.RemoteURL != "" {
|
|
|
|
i := a.RemoteURL
|
|
|
|
apiAttachment.RemoteURL = &i
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Thumbnail.RemoteURL != "" {
|
|
|
|
i := a.Thumbnail.RemoteURL
|
|
|
|
apiAttachment.PreviewRemoteURL = &i
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Description != "" {
|
|
|
|
i := a.Description
|
|
|
|
apiAttachment.Description = &i
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiAttachment, nil
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) MentionToAPIMention(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) {
|
2021-08-25 15:34:33 +02:00
|
|
|
if m.TargetAccount == nil {
|
|
|
|
targetAccount, err := c.db.GetAccountByID(ctx, m.TargetAccountID)
|
|
|
|
if err != nil {
|
|
|
|
return model.Mention{}, err
|
|
|
|
}
|
|
|
|
m.TargetAccount = targetAccount
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var local bool
|
2021-08-25 15:34:33 +02:00
|
|
|
if m.TargetAccount.Domain == "" {
|
2021-04-19 19:42:19 +02:00
|
|
|
local = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var acct string
|
|
|
|
if local {
|
2021-08-25 15:34:33 +02:00
|
|
|
acct = m.TargetAccount.Username
|
2021-04-19 19:42:19 +02:00
|
|
|
} else {
|
2021-08-25 15:34:33 +02:00
|
|
|
acct = fmt.Sprintf("%s@%s", m.TargetAccount.Username, m.TargetAccount.Domain)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
return model.Mention{
|
2021-08-25 15:34:33 +02:00
|
|
|
ID: m.TargetAccount.ID,
|
|
|
|
Username: m.TargetAccount.Username,
|
|
|
|
URL: m.TargetAccount.URL,
|
2021-04-19 19:42:19 +02:00
|
|
|
Acct: acct,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) EmojiToAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) {
|
2022-11-14 23:47:27 +01:00
|
|
|
var category string
|
|
|
|
if e.CategoryID != "" {
|
|
|
|
if e.Category == nil {
|
|
|
|
var err error
|
|
|
|
e.Category, err = c.db.GetEmojiCategory(ctx, e.CategoryID)
|
|
|
|
if err != nil {
|
|
|
|
return model.Emoji{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
category = e.Category.Name
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
return model.Emoji{
|
2021-04-19 19:42:19 +02:00
|
|
|
Shortcode: e.Shortcode,
|
|
|
|
URL: e.ImageURL,
|
|
|
|
StaticURL: e.ImageStaticURL,
|
2022-08-15 12:35:05 +02:00
|
|
|
VisibleInPicker: *e.VisibleInPicker,
|
2022-11-14 23:47:27 +01:00
|
|
|
Category: category,
|
2021-04-19 19:42:19 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-12 15:01:42 +02:00
|
|
|
func (c *converter) EmojiToAdminAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (*model.AdminEmoji, error) {
|
|
|
|
emoji, err := c.EmojiToAPIEmoji(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.AdminEmoji{
|
|
|
|
Emoji: emoji,
|
|
|
|
ID: e.ID,
|
|
|
|
Disabled: *e.Disabled,
|
|
|
|
Domain: e.Domain,
|
|
|
|
UpdatedAt: util.FormatISO8601(e.UpdatedAt),
|
|
|
|
TotalFileSize: e.ImageFileSize + e.ImageStaticFileSize,
|
|
|
|
ContentType: e.ImageContentType,
|
|
|
|
URI: e.URI,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:47:27 +01:00
|
|
|
func (c *converter) EmojiCategoryToAPIEmojiCategory(ctx context.Context, category *gtsmodel.EmojiCategory) (*model.EmojiCategory, error) {
|
|
|
|
return &model.EmojiCategory{
|
|
|
|
ID: category.ID,
|
|
|
|
Name: category.Name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) TagToAPITag(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) {
|
2021-05-08 14:25:55 +02:00
|
|
|
return model.Tag{
|
2021-04-19 19:42:19 +02:00
|
|
|
Name: t.Name,
|
2021-08-25 15:34:33 +02:00
|
|
|
URL: t.URL,
|
2021-04-19 19:42:19 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
|
2021-08-25 15:34:33 +02:00
|
|
|
repliesCount, err := c.db.CountStatusReplies(ctx, s)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error counting replies: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
reblogsCount, err := c.db.CountStatusReblogs(ctx, s)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error counting reblogs: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
favesCount, err := c.db.CountStatusFaves(ctx, s)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error counting faves: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
var apiRebloggedStatus *model.Status
|
2021-05-08 15:16:24 +02:00
|
|
|
if s.BoostOfID != "" {
|
|
|
|
// the boosted status might have been set on this struct already so check first before doing db calls
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.BoostOf == nil {
|
2021-05-08 15:16:24 +02:00
|
|
|
// it's not set so fetch it from the db
|
2021-08-25 15:34:33 +02:00
|
|
|
bs, err := c.db.GetStatusByID(ctx, s.BoostOfID)
|
|
|
|
if err != nil {
|
2021-05-08 15:16:24 +02:00
|
|
|
return nil, fmt.Errorf("error getting boosted status with id %s: %s", s.BoostOfID, err)
|
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
s.BoostOf = bs
|
2021-05-08 15:16:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the boosted account might have been set on this struct already or passed as a param so check first before doing db calls
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.BoostOfAccount == nil {
|
2021-05-08 15:16:24 +02:00
|
|
|
// it's not set so fetch it from the db
|
2021-08-25 15:34:33 +02:00
|
|
|
ba, err := c.db.GetAccountByID(ctx, s.BoostOf.AccountID)
|
|
|
|
if err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
return nil, fmt.Errorf("error getting boosted account %s from status with id %s: %s", s.BoostOf.AccountID, s.BoostOfID, err)
|
2021-05-08 15:16:24 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
s.BoostOfAccount = ba
|
|
|
|
s.BoostOf.Account = ba
|
2021-05-08 15:16:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiRebloggedStatus, err = c.StatusToAPIStatus(ctx, s.BoostOf, requestingAccount)
|
2021-06-17 18:02:33 +02:00
|
|
|
if err != nil {
|
2021-10-04 15:24:19 +02:00
|
|
|
return nil, fmt.Errorf("error converting boosted status to apitype: %s", err)
|
2021-05-08 15:16:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
var apiApplication *model.Application
|
2021-04-19 19:42:19 +02:00
|
|
|
if s.CreatedWithApplicationID != "" {
|
|
|
|
gtsApplication := >smodel.Application{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := c.db.GetByID(ctx, s.CreatedWithApplicationID, gtsApplication); err != nil {
|
2021-04-19 19:42:19 +02:00
|
|
|
return nil, fmt.Errorf("error fetching application used to create status: %s", err)
|
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiApplication, err = c.AppToAPIAppPublic(ctx, gtsApplication)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing application used to create status: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.Account == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := c.db.GetAccountByID(ctx, s.AccountID)
|
|
|
|
if err != nil {
|
2021-06-17 18:02:33 +02:00
|
|
|
return nil, fmt.Errorf("error getting status author: %s", err)
|
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
s.Account = a
|
2021-06-17 18:02:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAuthorAccount, err := c.AccountToAPIAccountPublic(ctx, s.Account)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing account of status author: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAttachments := []model.Attachment{}
|
2021-04-19 19:42:19 +02:00
|
|
|
// the status might already have some gts attachments on it if it's not been pulled directly from the database
|
2021-10-04 15:24:19 +02:00
|
|
|
// if so, we can directly convert the gts attachments into api ones
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.Attachments != nil {
|
|
|
|
for _, gtsAttachment := range s.Attachments {
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAttachment, err := c.AttachmentToAPIAttachment(ctx, gtsAttachment)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAttachments = append(apiAttachments, apiAttachment)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
// the status doesn't have gts attachments on it, but it does have attachment IDs
|
2021-10-04 15:24:19 +02:00
|
|
|
// in this case, we need to pull the gts attachments from the db to convert them into api ones
|
2021-04-19 19:42:19 +02:00
|
|
|
} else {
|
2021-08-25 15:34:33 +02:00
|
|
|
for _, aID := range s.AttachmentIDs {
|
|
|
|
gtsAttachment, err := c.db.GetAttachmentByID(ctx, aID)
|
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error getting attachment with id %s: %s", aID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAttachment, err := c.AttachmentToAPIAttachment(ctx, gtsAttachment)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting attachment with id %s: %s", aID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAttachments = append(apiAttachments, apiAttachment)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiMentions := []model.Mention{}
|
2021-04-19 19:42:19 +02:00
|
|
|
// the status might already have some gts mentions on it if it's not been pulled directly from the database
|
2021-10-04 15:24:19 +02:00
|
|
|
// if so, we can directly convert the gts mentions into api ones
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.Mentions != nil {
|
|
|
|
for _, gtsMention := range s.Mentions {
|
2021-10-04 15:24:19 +02:00
|
|
|
apiMention, err := c.MentionToAPIMention(ctx, gtsMention)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiMentions = append(apiMentions, apiMention)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
// the status doesn't have gts mentions on it, but it does have mention IDs
|
2021-10-04 15:24:19 +02:00
|
|
|
// in this case, we need to pull the gts mentions from the db to convert them into api ones
|
2021-04-19 19:42:19 +02:00
|
|
|
} else {
|
2021-08-25 15:34:33 +02:00
|
|
|
for _, mID := range s.MentionIDs {
|
|
|
|
gtsMention, err := c.db.GetMention(ctx, mID)
|
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error getting mention with id %s: %s", mID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiMention, err := c.MentionToAPIMention(ctx, gtsMention)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiMentions = append(apiMentions, apiMention)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiTags := []model.Tag{}
|
2021-04-19 19:42:19 +02:00
|
|
|
// the status might already have some gts tags on it if it's not been pulled directly from the database
|
2021-10-04 15:24:19 +02:00
|
|
|
// if so, we can directly convert the gts tags into api ones
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.Tags != nil {
|
|
|
|
for _, gtsTag := range s.Tags {
|
2021-10-04 15:24:19 +02:00
|
|
|
apiTag, err := c.TagToAPITag(ctx, gtsTag)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiTags = append(apiTags, apiTag)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
// the status doesn't have gts tags on it, but it does have tag IDs
|
2021-10-04 15:24:19 +02:00
|
|
|
// in this case, we need to pull the gts tags from the db to convert them into api ones
|
2021-04-19 19:42:19 +02:00
|
|
|
} else {
|
2021-08-20 12:26:56 +02:00
|
|
|
for _, t := range s.TagIDs {
|
2021-04-19 19:42:19 +02:00
|
|
|
gtsTag := >smodel.Tag{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := c.db.GetByID(ctx, t, gtsTag); err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error getting tag with id %s: %s", t, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiTag, err := c.TagToAPITag(ctx, gtsTag)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiTags = append(apiTags, apiTag)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiEmojis := []model.Emoji{}
|
2021-04-19 19:42:19 +02:00
|
|
|
// the status might already have some gts emojis on it if it's not been pulled directly from the database
|
2021-10-04 15:24:19 +02:00
|
|
|
// if so, we can directly convert the gts emojis into api ones
|
2021-08-20 12:26:56 +02:00
|
|
|
if s.Emojis != nil {
|
|
|
|
for _, gtsEmoji := range s.Emojis {
|
2021-10-04 15:24:19 +02:00
|
|
|
apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiEmojis = append(apiEmojis, apiEmoji)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
// the status doesn't have gts emojis on it, but it does have emoji IDs
|
2021-10-04 15:24:19 +02:00
|
|
|
// in this case, we need to pull the gts emojis from the db to convert them into api ones
|
2021-04-19 19:42:19 +02:00
|
|
|
} else {
|
2021-08-20 12:26:56 +02:00
|
|
|
for _, e := range s.EmojiIDs {
|
2021-04-19 19:42:19 +02:00
|
|
|
gtsEmoji := >smodel.Emoji{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := c.db.GetByID(ctx, e, gtsEmoji); err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error getting emoji with id %s: %s", e, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
|
2021-08-29 12:03:08 +02:00
|
|
|
continue
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
apiEmojis = append(apiEmojis, apiEmoji)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:02:33 +02:00
|
|
|
statusInteractions := &statusInteractions{}
|
2021-08-25 15:34:33 +02:00
|
|
|
si, err := c.interactionsWithStatusForAccount(ctx, s, requestingAccount)
|
2021-06-17 18:02:33 +02:00
|
|
|
if err == nil {
|
|
|
|
statusInteractions = si
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:06:44 +02:00
|
|
|
apiStatus := &model.Status{
|
2021-04-19 19:42:19 +02:00
|
|
|
ID: s.ID,
|
2022-05-24 18:21:27 +02:00
|
|
|
CreatedAt: util.FormatISO8601(s.CreatedAt),
|
2022-09-02 17:00:11 +02:00
|
|
|
InReplyToID: nil,
|
|
|
|
InReplyToAccountID: nil,
|
2022-08-15 12:35:05 +02:00
|
|
|
Sensitive: *s.Sensitive,
|
2021-04-19 19:42:19 +02:00
|
|
|
SpoilerText: s.ContentWarning,
|
2021-10-04 15:24:19 +02:00
|
|
|
Visibility: c.VisToAPIVis(ctx, s.Visibility),
|
2021-04-19 19:42:19 +02:00
|
|
|
Language: s.Language,
|
|
|
|
URI: s.URI,
|
|
|
|
URL: s.URL,
|
|
|
|
RepliesCount: repliesCount,
|
|
|
|
ReblogsCount: reblogsCount,
|
|
|
|
FavouritesCount: favesCount,
|
2021-06-17 18:02:33 +02:00
|
|
|
Favourited: statusInteractions.Faved,
|
|
|
|
Bookmarked: statusInteractions.Bookmarked,
|
|
|
|
Muted: statusInteractions.Muted,
|
|
|
|
Reblogged: statusInteractions.Reblogged,
|
2022-08-15 12:35:05 +02:00
|
|
|
Pinned: *s.Pinned,
|
2021-04-19 19:42:19 +02:00
|
|
|
Content: s.Content,
|
2022-09-02 17:00:11 +02:00
|
|
|
Reblog: nil,
|
2021-10-04 15:24:19 +02:00
|
|
|
Application: apiApplication,
|
|
|
|
Account: apiAuthorAccount,
|
|
|
|
MediaAttachments: apiAttachments,
|
|
|
|
Mentions: apiMentions,
|
|
|
|
Tags: apiTags,
|
|
|
|
Emojis: apiEmojis,
|
2022-09-02 17:00:11 +02:00
|
|
|
Card: nil, // TODO: implement cards
|
|
|
|
Poll: nil, // TODO: implement polls
|
2021-04-19 19:42:19 +02:00
|
|
|
Text: s.Text,
|
2021-08-02 19:06:44 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 17:00:11 +02:00
|
|
|
// nullable fields
|
|
|
|
if s.InReplyToID != "" {
|
|
|
|
i := s.InReplyToID
|
|
|
|
apiStatus.InReplyToID = &i
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.InReplyToAccountID != "" {
|
|
|
|
i := s.InReplyToAccountID
|
|
|
|
apiStatus.InReplyToAccountID = &i
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
if apiRebloggedStatus != nil {
|
|
|
|
apiStatus.Reblog = &model.StatusReblogged{Status: apiRebloggedStatus}
|
2021-08-02 19:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiStatus, nil
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
// VisToapi converts a gts visibility into its api equivalent
|
|
|
|
func (c *converter) VisToAPIVis(ctx context.Context, m gtsmodel.Visibility) model.Visibility {
|
2021-05-08 14:25:55 +02:00
|
|
|
switch m {
|
|
|
|
case gtsmodel.VisibilityPublic:
|
|
|
|
return model.VisibilityPublic
|
|
|
|
case gtsmodel.VisibilityUnlocked:
|
|
|
|
return model.VisibilityUnlisted
|
|
|
|
case gtsmodel.VisibilityFollowersOnly, gtsmodel.VisibilityMutualsOnly:
|
|
|
|
return model.VisibilityPrivate
|
|
|
|
case gtsmodel.VisibilityDirect:
|
|
|
|
return model.VisibilityDirect
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-05-09 14:06:06 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) InstanceToAPIInstance(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) {
|
2021-05-09 14:06:06 +02:00
|
|
|
mi := &model.Instance{
|
2021-05-09 20:34:27 +02:00
|
|
|
URI: i.URI,
|
|
|
|
Title: i.Title,
|
|
|
|
Description: i.Description,
|
2021-05-09 14:06:06 +02:00
|
|
|
ShortDescription: i.ShortDescription,
|
2021-05-09 20:34:27 +02:00
|
|
|
Email: i.ContactEmail,
|
2021-05-22 15:51:20 +02:00
|
|
|
Version: i.Version,
|
2021-06-23 16:35:57 +02:00
|
|
|
Stats: make(map[string]int),
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 16:35:57 +02:00
|
|
|
// if the requested instance is *this* instance, we can add some extra information
|
2022-05-30 14:41:24 +02:00
|
|
|
if host := config.GetHost(); i.Domain == host {
|
2022-07-05 14:03:44 +02:00
|
|
|
mi.AccountDomain = config.GetAccountDomain()
|
|
|
|
|
2022-06-26 12:33:11 +02:00
|
|
|
if ia, err := c.db.GetInstanceAccount(ctx, ""); err == nil {
|
2022-11-08 18:11:06 +01:00
|
|
|
// assume default logo
|
|
|
|
mi.Thumbnail = config.GetProtocol() + "://" + host + "/assets/logo.png"
|
|
|
|
|
|
|
|
// take instance account avatar as instance thumbnail if we can
|
|
|
|
if ia.AvatarMediaAttachmentID != "" {
|
|
|
|
if ia.AvatarMediaAttachment == nil {
|
|
|
|
avi, err := c.db.GetAttachmentByID(ctx, ia.AvatarMediaAttachmentID)
|
|
|
|
if err == nil {
|
|
|
|
ia.AvatarMediaAttachment = avi
|
|
|
|
} else if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
log.Errorf("InstanceToAPIInstance: error getting instance avatar attachment with id %s: %s", ia.AvatarMediaAttachmentID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ia.AvatarMediaAttachment != nil {
|
|
|
|
mi.Thumbnail = ia.AvatarMediaAttachment.URL
|
|
|
|
mi.ThumbnailType = ia.AvatarMediaAttachment.File.ContentType
|
|
|
|
mi.ThumbnailDescription = ia.AvatarMediaAttachment.Description
|
|
|
|
}
|
2022-06-26 12:33:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
userCount, err := c.db.CountInstanceUsers(ctx, host)
|
2021-06-23 16:35:57 +02:00
|
|
|
if err == nil {
|
2021-12-07 13:31:39 +01:00
|
|
|
mi.Stats["user_count"] = userCount
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
statusCount, err := c.db.CountInstanceStatuses(ctx, host)
|
2021-06-23 16:35:57 +02:00
|
|
|
if err == nil {
|
2021-12-07 13:31:39 +01:00
|
|
|
mi.Stats["status_count"] = statusCount
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
domainCount, err := c.db.CountInstanceDomains(ctx, host)
|
2021-06-23 16:35:57 +02:00
|
|
|
if err == nil {
|
2021-12-07 13:31:39 +01:00
|
|
|
mi.Stats["domain_count"] = domainCount
|
2021-06-23 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
mi.Registrations = config.GetAccountsRegistrationOpen()
|
|
|
|
mi.ApprovalRequired = config.GetAccountsApprovalRequired()
|
2021-05-09 14:06:06 +02:00
|
|
|
mi.InvitesEnabled = false // TODO
|
2022-05-30 14:41:24 +02:00
|
|
|
mi.MaxTootChars = uint(config.GetStatusesMaxChars())
|
2021-05-22 15:51:20 +02:00
|
|
|
mi.URLS = &model.InstanceURLs{
|
2022-06-26 10:58:45 +02:00
|
|
|
StreamingAPI: "wss://" + host,
|
2021-05-22 15:51:20 +02:00
|
|
|
}
|
2022-05-30 14:41:24 +02:00
|
|
|
mi.Version = config.GetSoftwareVersion()
|
2022-06-26 10:58:45 +02:00
|
|
|
|
|
|
|
// todo: remove hardcoded values and put them in config somewhere
|
|
|
|
mi.Configuration = &model.InstanceConfiguration{
|
|
|
|
Statuses: &model.InstanceConfigurationStatuses{
|
|
|
|
MaxCharacters: config.GetStatusesMaxChars(),
|
|
|
|
MaxMediaAttachments: config.GetStatusesMediaMaxFiles(),
|
2022-09-08 12:36:42 +02:00
|
|
|
CharactersReservedPerURL: instanceStatusesCharactersReservedPerURL,
|
2022-06-26 10:58:45 +02:00
|
|
|
},
|
|
|
|
MediaAttachments: &model.InstanceConfigurationMediaAttachments{
|
|
|
|
SupportedMimeTypes: media.AllSupportedMIMETypes(),
|
2022-10-06 12:00:53 +02:00
|
|
|
ImageSizeLimit: int(config.GetMediaImageMaxSize()), // bytes
|
2022-09-08 12:36:42 +02:00
|
|
|
ImageMatrixLimit: instanceMediaAttachmentsImageMatrixLimit, // height*width
|
2022-10-06 12:00:53 +02:00
|
|
|
VideoSizeLimit: int(config.GetMediaVideoMaxSize()), // bytes
|
2022-09-08 12:36:42 +02:00
|
|
|
VideoFrameRateLimit: instanceMediaAttachmentsVideoFrameRateLimit,
|
|
|
|
VideoMatrixLimit: instanceMediaAttachmentsVideoMatrixLimit, // height*width
|
2022-06-26 10:58:45 +02:00
|
|
|
},
|
|
|
|
Polls: &model.InstanceConfigurationPolls{
|
|
|
|
MaxOptions: config.GetStatusesPollMaxOptions(),
|
|
|
|
MaxCharactersPerOption: config.GetStatusesPollOptionMaxChars(),
|
2022-09-08 12:36:42 +02:00
|
|
|
MinExpiration: instancePollsMinExpiration, // seconds
|
|
|
|
MaxExpiration: instancePollsMaxExpiration, // seconds
|
2022-06-26 10:58:45 +02:00
|
|
|
},
|
2022-09-12 13:14:29 +02:00
|
|
|
Accounts: &model.InstanceConfigurationAccounts{
|
|
|
|
AllowCustomCSS: config.GetAccountsAllowCustomCSS(),
|
|
|
|
},
|
2022-10-06 12:00:53 +02:00
|
|
|
Emojis: &model.InstanceConfigurationEmojis{
|
|
|
|
EmojiSizeLimit: int(config.GetMediaEmojiLocalMaxSize()), // bytes
|
|
|
|
},
|
2022-06-26 10:58:45 +02:00
|
|
|
}
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// contact account is optional but let's try to get it
|
|
|
|
if i.ContactAccountID != "" {
|
2021-08-25 15:34:33 +02:00
|
|
|
if i.ContactAccount == nil {
|
|
|
|
contactAccount, err := c.db.GetAccountByID(ctx, i.ContactAccountID)
|
2021-05-09 14:06:06 +02:00
|
|
|
if err == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
i.ContactAccount = contactAccount
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:24:19 +02:00
|
|
|
ma, err := c.AccountToAPIAccountPublic(ctx, i.ContactAccount)
|
2021-08-25 15:34:33 +02:00
|
|
|
if err == nil {
|
|
|
|
mi.ContactAccount = ma
|
|
|
|
}
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mi, nil
|
|
|
|
}
|
2021-05-21 15:48:26 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) RelationshipToAPIRelationship(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) {
|
2021-05-21 15:48:26 +02:00
|
|
|
return &model.Relationship{
|
2021-05-21 23:04:59 +02:00
|
|
|
ID: r.ID,
|
|
|
|
Following: r.Following,
|
|
|
|
ShowingReblogs: r.ShowingReblogs,
|
|
|
|
Notifying: r.Notifying,
|
|
|
|
FollowedBy: r.FollowedBy,
|
|
|
|
Blocking: r.Blocking,
|
|
|
|
BlockedBy: r.BlockedBy,
|
|
|
|
Muting: r.Muting,
|
2021-05-21 15:48:26 +02:00
|
|
|
MutingNotifications: r.MutingNotifications,
|
2021-05-21 23:04:59 +02:00
|
|
|
Requested: r.Requested,
|
|
|
|
DomainBlocking: r.DomainBlocking,
|
|
|
|
Endorsed: r.Endorsed,
|
|
|
|
Note: r.Note,
|
2021-05-21 15:48:26 +02:00
|
|
|
}, nil
|
|
|
|
}
|
2021-05-27 16:06:24 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) NotificationToAPINotification(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) {
|
2021-08-20 12:26:56 +02:00
|
|
|
if n.TargetAccount == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
tAccount, err := c.db.GetAccountByID(ctx, n.TargetAccountID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-10-04 15:24:19 +02:00
|
|
|
return nil, fmt.Errorf("NotificationToapi: error getting target account with id %s from the db: %s", n.TargetAccountID, err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
n.TargetAccount = tAccount
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
if n.OriginAccount == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
ogAccount, err := c.db.GetAccountByID(ctx, n.OriginAccountID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-10-04 15:24:19 +02:00
|
|
|
return nil, fmt.Errorf("NotificationToapi: error getting origin account with id %s from the db: %s", n.OriginAccountID, err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
n.OriginAccount = ogAccount
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
apiAccount, err := c.AccountToAPIAccountPublic(ctx, n.OriginAccount)
|
2021-05-27 16:06:24 +02:00
|
|
|
if err != nil {
|
2021-10-04 15:24:19 +02:00
|
|
|
return nil, fmt.Errorf("NotificationToapi: error converting account to api: %s", err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
var apiStatus *model.Status
|
2021-05-27 16:06:24 +02:00
|
|
|
if n.StatusID != "" {
|
2021-08-20 12:26:56 +02:00
|
|
|
if n.Status == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
status, err := c.db.GetStatusByID(ctx, n.StatusID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-10-04 15:24:19 +02:00
|
|
|
return nil, fmt.Errorf("NotificationToapi: error getting status with id %s from the db: %s", n.StatusID, err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
n.Status = status
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
if n.Status.Account == nil {
|
|
|
|
if n.Status.AccountID == n.TargetAccount.ID {
|
|
|
|
n.Status.Account = n.TargetAccount
|
|
|
|
} else if n.Status.AccountID == n.OriginAccount.ID {
|
|
|
|
n.Status.Account = n.OriginAccount
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-08-30 11:42:52 +02:00
|
|
|
apiStatus, err = c.StatusToAPIStatus(ctx, n.Status, n.TargetAccount)
|
2021-05-27 16:06:24 +02:00
|
|
|
if err != nil {
|
2021-10-04 15:24:19 +02:00
|
|
|
return nil, fmt.Errorf("NotificationToapi: error converting status to api: %s", err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 11:06:37 +02:00
|
|
|
if apiStatus != nil && apiStatus.Reblog != nil {
|
|
|
|
// use the actual reblog status for the notifications endpoint
|
|
|
|
apiStatus = apiStatus.Reblog.Status
|
|
|
|
}
|
|
|
|
|
2021-05-27 16:06:24 +02:00
|
|
|
return &model.Notification{
|
|
|
|
ID: n.ID,
|
|
|
|
Type: string(n.NotificationType),
|
2022-05-24 18:21:27 +02:00
|
|
|
CreatedAt: util.FormatISO8601(n.CreatedAt),
|
2021-10-04 15:24:19 +02:00
|
|
|
Account: apiAccount,
|
|
|
|
Status: apiStatus,
|
2021-05-27 16:06:24 +02:00
|
|
|
}, nil
|
|
|
|
}
|
2021-07-05 13:23:03 +02:00
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
func (c *converter) DomainBlockToAPIDomainBlock(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) {
|
2021-07-05 13:23:03 +02:00
|
|
|
domainBlock := &model.DomainBlock{
|
2022-06-23 16:54:54 +02:00
|
|
|
Domain: model.Domain{
|
|
|
|
Domain: b.Domain,
|
|
|
|
PublicComment: b.PublicComment,
|
|
|
|
},
|
2021-07-05 13:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we're exporting a domain block, return it with minimal information attached
|
|
|
|
if !export {
|
|
|
|
domainBlock.ID = b.ID
|
2022-08-15 12:35:05 +02:00
|
|
|
domainBlock.Obfuscate = *b.Obfuscate
|
2021-07-05 13:23:03 +02:00
|
|
|
domainBlock.PrivateComment = b.PrivateComment
|
|
|
|
domainBlock.SubscriptionID = b.SubscriptionID
|
|
|
|
domainBlock.CreatedBy = b.CreatedByAccountID
|
2022-05-24 18:21:27 +02:00
|
|
|
domainBlock.CreatedAt = util.FormatISO8601(b.CreatedAt)
|
2021-07-05 13:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return domainBlock, nil
|
|
|
|
}
|