util.go (3149B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package typeutils 19 20 import ( 21 "context" 22 "errors" 23 "fmt" 24 "net/url" 25 26 "github.com/superseriousbusiness/gotosocial/internal/ap" 27 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 28 "github.com/superseriousbusiness/gotosocial/internal/regexes" 29 ) 30 31 type statusInteractions struct { 32 Faved bool 33 Muted bool 34 Bookmarked bool 35 Reblogged bool 36 Pinned bool 37 } 38 39 func (c *converter) interactionsWithStatusForAccount(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) { 40 si := &statusInteractions{} 41 42 if requestingAccount != nil { 43 faved, err := c.db.IsStatusFavedBy(ctx, s, requestingAccount.ID) 44 if err != nil { 45 return nil, fmt.Errorf("error checking if requesting account has faved status: %s", err) 46 } 47 si.Faved = faved 48 49 reblogged, err := c.db.IsStatusRebloggedBy(ctx, s, requestingAccount.ID) 50 if err != nil { 51 return nil, fmt.Errorf("error checking if requesting account has reblogged status: %s", err) 52 } 53 si.Reblogged = reblogged 54 55 muted, err := c.db.IsStatusMutedBy(ctx, s, requestingAccount.ID) 56 if err != nil { 57 return nil, fmt.Errorf("error checking if requesting account has muted status: %s", err) 58 } 59 si.Muted = muted 60 61 bookmarked, err := c.db.IsStatusBookmarkedBy(ctx, s, requestingAccount.ID) 62 if err != nil { 63 return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err) 64 } 65 si.Bookmarked = bookmarked 66 67 // The only time 'pinned' should be true is if the 68 // requesting account is looking at its OWN status. 69 if s.AccountID == requestingAccount.ID { 70 si.Pinned = !s.PinnedAt.IsZero() 71 } 72 } 73 return si, nil 74 } 75 76 func misskeyReportInlineURLs(content string) []*url.URL { 77 m := regexes.MisskeyReportNotes.FindAllStringSubmatch(content, -1) 78 urls := make([]*url.URL, 0, len(m)) 79 for _, sm := range m { 80 url, err := url.Parse(sm[1]) 81 if err == nil && url != nil { 82 urls = append(urls, url) 83 } 84 } 85 return urls 86 } 87 88 // getURI is a shortcut/util function for extracting 89 // the JSONLDId URI of an Activity or Object. 90 func getURI(withID ap.WithJSONLDId) (*url.URL, string, error) { 91 idProp := withID.GetJSONLDId() 92 if idProp == nil { 93 return nil, "", errors.New("id prop was nil") 94 } 95 96 if !idProp.IsIRI() { 97 return nil, "", errors.New("id prop was not an IRI") 98 } 99 100 id := idProp.Get() 101 return id, id.String(), nil 102 }