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/>.
|
2023-01-25 10:12:27 +00:00
|
|
|
|
2021-06-17 16:02:33 +00:00
|
|
|
package typeutils
|
|
|
|
|
|
|
|
import (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2023-06-17 15:49:11 +00:00
|
|
|
"errors"
|
2021-06-17 16:02:33 +00:00
|
|
|
"fmt"
|
2023-01-25 10:12:27 +00:00
|
|
|
"net/url"
|
2021-06-17 16:02:33 +00:00
|
|
|
|
2023-06-17 15:49:11 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-06-17 16:02:33 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2023-01-25 10:12:27 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/regexes"
|
2021-06-17 16:02:33 +00:00
|
|
|
)
|
|
|
|
|
2023-01-25 10:12:27 +00:00
|
|
|
type statusInteractions struct {
|
|
|
|
Faved bool
|
|
|
|
Muted bool
|
|
|
|
Bookmarked bool
|
|
|
|
Reblogged bool
|
2023-02-25 12:16:30 +00:00
|
|
|
Pinned bool
|
2023-01-25 10:12:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (c *converter) interactionsWithStatusForAccount(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) {
|
2021-06-17 16:02:33 +00:00
|
|
|
si := &statusInteractions{}
|
|
|
|
|
|
|
|
if requestingAccount != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
faved, err := c.db.IsStatusFavedBy(ctx, s, requestingAccount.ID)
|
2021-06-17 16:02:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error checking if requesting account has faved status: %s", err)
|
|
|
|
}
|
|
|
|
si.Faved = faved
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
reblogged, err := c.db.IsStatusRebloggedBy(ctx, s, requestingAccount.ID)
|
2021-06-17 16:02:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error checking if requesting account has reblogged status: %s", err)
|
|
|
|
}
|
|
|
|
si.Reblogged = reblogged
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
muted, err := c.db.IsStatusMutedBy(ctx, s, requestingAccount.ID)
|
2021-06-17 16:02:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error checking if requesting account has muted status: %s", err)
|
|
|
|
}
|
|
|
|
si.Muted = muted
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
bookmarked, err := c.db.IsStatusBookmarkedBy(ctx, s, requestingAccount.ID)
|
2021-06-17 16:02:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err)
|
|
|
|
}
|
|
|
|
si.Bookmarked = bookmarked
|
2023-02-25 12:16:30 +00:00
|
|
|
|
|
|
|
// The only time 'pinned' should be true is if the
|
|
|
|
// requesting account is looking at its OWN status.
|
|
|
|
if s.AccountID == requestingAccount.ID {
|
|
|
|
si.Pinned = !s.PinnedAt.IsZero()
|
|
|
|
}
|
2021-06-17 16:02:33 +00:00
|
|
|
}
|
|
|
|
return si, nil
|
|
|
|
}
|
|
|
|
|
2023-01-25 10:12:27 +00:00
|
|
|
func misskeyReportInlineURLs(content string) []*url.URL {
|
|
|
|
m := regexes.MisskeyReportNotes.FindAllStringSubmatch(content, -1)
|
|
|
|
urls := make([]*url.URL, 0, len(m))
|
|
|
|
for _, sm := range m {
|
|
|
|
url, err := url.Parse(sm[1])
|
|
|
|
if err == nil && url != nil {
|
|
|
|
urls = append(urls, url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return urls
|
2021-06-17 16:02:33 +00:00
|
|
|
}
|
2023-06-17 15:49:11 +00:00
|
|
|
|
|
|
|
// getURI is a shortcut/util function for extracting
|
|
|
|
// the JSONLDId URI of an Activity or Object.
|
|
|
|
func getURI(withID ap.WithJSONLDId) (*url.URL, string, error) {
|
|
|
|
idProp := withID.GetJSONLDId()
|
|
|
|
if idProp == nil {
|
|
|
|
return nil, "", errors.New("id prop was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !idProp.IsIRI() {
|
|
|
|
return nil, "", errors.New("id prop was not an IRI")
|
|
|
|
}
|
|
|
|
|
|
|
|
id := idProp.Get()
|
|
|
|
return id, id.String(), nil
|
|
|
|
}
|