2021-08-25 15:34:33 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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-06-13 18:42:28 +02:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2021-06-13 18:42:28 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2021-07-28 11:42:26 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-06-13 18:42:28 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
|
|
|
)
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessVisibility(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountDefaultVis gtsmodel.Visibility, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
// by default all flags are set to true
|
|
|
|
gtsAdvancedVis := >smodel.VisibilityAdvanced{
|
|
|
|
Federated: true,
|
|
|
|
Boostable: true,
|
|
|
|
Replyable: true,
|
|
|
|
Likeable: true,
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:06:44 +02:00
|
|
|
var vis gtsmodel.Visibility
|
|
|
|
// If visibility isn't set on the form, then just take the account default.
|
2021-06-13 18:42:28 +02:00
|
|
|
// If that's also not set, take the default for the whole instance.
|
2021-08-02 19:06:44 +02:00
|
|
|
if form.Visibility != "" {
|
|
|
|
vis = p.tc.MastoVisToVis(form.Visibility)
|
2021-06-13 18:42:28 +02:00
|
|
|
} else if accountDefaultVis != "" {
|
2021-08-02 19:06:44 +02:00
|
|
|
vis = accountDefaultVis
|
2021-06-13 18:42:28 +02:00
|
|
|
} else {
|
2021-08-02 19:06:44 +02:00
|
|
|
vis = gtsmodel.VisibilityDefault
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 19:06:44 +02:00
|
|
|
switch vis {
|
2021-06-13 18:42:28 +02:00
|
|
|
case gtsmodel.VisibilityPublic:
|
|
|
|
// for public, there's no need to change any of the advanced flags from true regardless of what the user filled out
|
|
|
|
break
|
|
|
|
case gtsmodel.VisibilityUnlocked:
|
|
|
|
// for unlocked the user can set any combination of flags they like so look at them all to see if they're set and then apply them
|
|
|
|
if form.Federated != nil {
|
|
|
|
gtsAdvancedVis.Federated = *form.Federated
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Boostable != nil {
|
|
|
|
gtsAdvancedVis.Boostable = *form.Boostable
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Replyable != nil {
|
|
|
|
gtsAdvancedVis.Replyable = *form.Replyable
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Likeable != nil {
|
|
|
|
gtsAdvancedVis.Likeable = *form.Likeable
|
|
|
|
}
|
|
|
|
|
|
|
|
case gtsmodel.VisibilityFollowersOnly, gtsmodel.VisibilityMutualsOnly:
|
|
|
|
// for followers or mutuals only, boostable will *always* be false, but the other fields can be set so check and apply them
|
|
|
|
gtsAdvancedVis.Boostable = false
|
|
|
|
|
|
|
|
if form.Federated != nil {
|
|
|
|
gtsAdvancedVis.Federated = *form.Federated
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Replyable != nil {
|
|
|
|
gtsAdvancedVis.Replyable = *form.Replyable
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Likeable != nil {
|
|
|
|
gtsAdvancedVis.Likeable = *form.Likeable
|
|
|
|
}
|
|
|
|
|
|
|
|
case gtsmodel.VisibilityDirect:
|
|
|
|
// direct is pretty easy: there's only one possible setting so return it
|
|
|
|
gtsAdvancedVis.Federated = true
|
|
|
|
gtsAdvancedVis.Boostable = false
|
|
|
|
gtsAdvancedVis.Federated = true
|
|
|
|
gtsAdvancedVis.Likeable = true
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:06:44 +02:00
|
|
|
status.Visibility = vis
|
2021-06-13 18:42:28 +02:00
|
|
|
status.VisibilityAdvanced = gtsAdvancedVis
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessReplyToID(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
if form.InReplyToID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this status is a reply to another status, we need to do a bit of work to establish whether or not this status can be posted:
|
|
|
|
//
|
|
|
|
// 1. Does the replied status exist in the database?
|
|
|
|
// 2. Is the replied status marked as replyable?
|
|
|
|
// 3. Does a block exist between either the current account or the account that posted the status it's replying to?
|
|
|
|
//
|
|
|
|
// If this is all OK, then we fetch the repliedStatus and the repliedAccount for later processing.
|
|
|
|
repliedStatus := >smodel.Status{}
|
|
|
|
repliedAccount := >smodel.Account{}
|
|
|
|
// check replied status exists + is replyable
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := p.db.GetByID(ctx, form.InReplyToID, repliedStatus); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
if err == db.ErrNoEntries {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("status with id %s not replyable because it doesn't exist", form.InReplyToID)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("status with id %s not replyable: %s", form.InReplyToID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if repliedStatus.VisibilityAdvanced != nil {
|
|
|
|
if !repliedStatus.VisibilityAdvanced.Replyable {
|
|
|
|
return fmt.Errorf("status with id %s is marked as not replyable", form.InReplyToID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check replied account is known to us
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := p.db.GetByID(ctx, repliedStatus.AccountID, repliedAccount); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
if err == db.ErrNoEntries {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("status with id %s not replyable because account id %s is not known", form.InReplyToID, repliedStatus.AccountID)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("status with id %s not replyable: %s", form.InReplyToID, err)
|
|
|
|
}
|
|
|
|
// check if a block exists
|
2021-08-25 15:34:33 +02:00
|
|
|
if blocked, err := p.db.IsBlocked(ctx, thisAccountID, repliedAccount.ID, true); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != db.ErrNoEntries {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("status with id %s not replyable: %s", form.InReplyToID, err)
|
|
|
|
}
|
|
|
|
} else if blocked {
|
|
|
|
return fmt.Errorf("status with id %s not replyable", form.InReplyToID)
|
|
|
|
}
|
|
|
|
status.InReplyToID = repliedStatus.ID
|
|
|
|
status.InReplyToAccountID = repliedAccount.ID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
if form.MediaIDs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gtsMediaAttachments := []*gtsmodel.MediaAttachment{}
|
|
|
|
attachments := []string{}
|
|
|
|
for _, mediaID := range form.MediaIDs {
|
|
|
|
// check these attachments exist
|
|
|
|
a := >smodel.MediaAttachment{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := p.db.GetByID(ctx, mediaID, a); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("invalid media type or media not found for media id %s", mediaID)
|
|
|
|
}
|
|
|
|
// check they belong to the requesting account id
|
|
|
|
if a.AccountID != thisAccountID {
|
|
|
|
return fmt.Errorf("media with id %s does not belong to account %s", mediaID, thisAccountID)
|
|
|
|
}
|
|
|
|
// check they're not already used in a status
|
|
|
|
if a.StatusID != "" || a.ScheduledStatusID != "" {
|
|
|
|
return fmt.Errorf("media with id %s is already attached to a status", mediaID)
|
|
|
|
}
|
|
|
|
gtsMediaAttachments = append(gtsMediaAttachments, a)
|
|
|
|
attachments = append(attachments, a.ID)
|
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Attachments = gtsMediaAttachments
|
|
|
|
status.AttachmentIDs = attachments
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessLanguage(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountDefaultLanguage string, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
if form.Language != "" {
|
|
|
|
status.Language = form.Language
|
|
|
|
} else {
|
|
|
|
status.Language = accountDefaultLanguage
|
|
|
|
}
|
|
|
|
if status.Language == "" {
|
|
|
|
return errors.New("no language given either in status create form or account default")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessMentions(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
menchies := []string{}
|
2021-08-25 15:34:33 +02:00
|
|
|
gtsMenchies, err := p.db.MentionStringsToMentions(ctx, util.DeriveMentionsFromStatus(form.Status), accountID, status.ID)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating mentions from status: %s", err)
|
|
|
|
}
|
|
|
|
for _, menchie := range gtsMenchies {
|
|
|
|
menchieID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
menchie.ID = menchieID
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := p.db.Put(ctx, menchie); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("error putting mentions in db: %s", err)
|
|
|
|
}
|
|
|
|
menchies = append(menchies, menchie.ID)
|
|
|
|
}
|
|
|
|
// add full populated gts menchies to the status for passing them around conveniently
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Mentions = gtsMenchies
|
2021-06-13 18:42:28 +02:00
|
|
|
// add just the ids of the mentioned accounts to the status for putting in the db
|
2021-08-20 12:26:56 +02:00
|
|
|
status.MentionIDs = menchies
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessTags(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
tags := []string{}
|
2021-08-25 15:34:33 +02:00
|
|
|
gtsTags, err := p.db.TagStringsToTags(ctx, util.DeriveHashtagsFromStatus(form.Status), accountID, status.ID)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating hashtags from status: %s", err)
|
|
|
|
}
|
|
|
|
for _, tag := range gtsTags {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := p.db.Put(ctx, tag); err != nil && err != db.ErrAlreadyExists {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("error putting tags in db: %s", err)
|
|
|
|
}
|
|
|
|
tags = append(tags, tag.ID)
|
|
|
|
}
|
|
|
|
// add full populated gts tags to the status for passing them around conveniently
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Tags = gtsTags
|
2021-06-13 18:42:28 +02:00
|
|
|
// add just the ids of the used tags to the status for putting in the db
|
2021-08-20 12:26:56 +02:00
|
|
|
status.TagIDs = tags
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessEmojis(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error {
|
2021-06-13 18:42:28 +02:00
|
|
|
emojis := []string{}
|
2021-08-25 15:34:33 +02:00
|
|
|
gtsEmojis, err := p.db.EmojiStringsToEmojis(ctx, util.DeriveEmojisFromStatus(form.Status), accountID, status.ID)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating emojis from status: %s", err)
|
|
|
|
}
|
|
|
|
for _, e := range gtsEmojis {
|
|
|
|
emojis = append(emojis, e.ID)
|
|
|
|
}
|
|
|
|
// add full populated gts emojis to the status for passing them around conveniently
|
2021-08-20 12:26:56 +02:00
|
|
|
status.Emojis = gtsEmojis
|
2021-06-13 18:42:28 +02:00
|
|
|
// add just the ids of the used emojis to the status for putting in the db
|
2021-08-20 12:26:56 +02:00
|
|
|
status.EmojiIDs = emojis
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (p *processor) ProcessContent(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error {
|
2021-07-26 20:25:54 +02:00
|
|
|
// if there's nothing in the status at all we can just return early
|
2021-06-13 18:42:28 +02:00
|
|
|
if form.Status == "" {
|
|
|
|
status.Content = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-26 20:25:54 +02:00
|
|
|
// if format wasn't specified we should set the default
|
|
|
|
if form.Format == "" {
|
|
|
|
form.Format = apimodel.StatusFormatDefault
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 11:42:26 +02:00
|
|
|
// remove any existing html from the status
|
|
|
|
content := text.RemoveHTML(form.Status)
|
|
|
|
|
2021-07-26 20:25:54 +02:00
|
|
|
// parse content out of the status depending on what format has been submitted
|
2021-08-11 16:54:54 +02:00
|
|
|
var formatted string
|
2021-07-26 20:25:54 +02:00
|
|
|
switch form.Format {
|
|
|
|
case apimodel.StatusFormatPlain:
|
2021-08-25 15:34:33 +02:00
|
|
|
formatted = p.formatter.FromPlain(ctx, content, status.Mentions, status.Tags)
|
2021-07-26 20:25:54 +02:00
|
|
|
case apimodel.StatusFormatMarkdown:
|
2021-08-25 15:34:33 +02:00
|
|
|
formatted = p.formatter.FromMarkdown(ctx, content, status.Mentions, status.Tags)
|
2021-07-26 20:25:54 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("format %s not recognised as a valid status format", form.Format)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 16:54:54 +02:00
|
|
|
status.Content = formatted
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|