util.go (3168B)
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 processing 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/superseriousbusiness/gotosocial/internal/config" 25 "github.com/superseriousbusiness/gotosocial/internal/db" 26 "github.com/superseriousbusiness/gotosocial/internal/federation" 27 "github.com/superseriousbusiness/gotosocial/internal/gtscontext" 28 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 29 "github.com/superseriousbusiness/gotosocial/internal/id" 30 "github.com/superseriousbusiness/gotosocial/internal/util" 31 ) 32 33 func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.ParseMentionFunc { 34 return func(ctx context.Context, targetAccount string, originAccountID string, statusID string) (*gtsmodel.Mention, error) { 35 // get the origin account first since we'll need it to create the mention 36 originAccount, err := dbConn.GetAccountByID(ctx, originAccountID) 37 if err != nil { 38 return nil, fmt.Errorf("couldn't get mention origin account with id %s", originAccountID) 39 } 40 41 username, domain, err := util.ExtractNamestringParts(targetAccount) 42 if err != nil { 43 return nil, fmt.Errorf("couldn't extract namestring parts from %s: %s", targetAccount, err) 44 } 45 46 var mentionedAccount *gtsmodel.Account 47 if domain == "" || domain == config.GetHost() || domain == config.GetAccountDomain() { 48 localAccount, err := dbConn.GetAccountByUsernameDomain(ctx, username, "") 49 if err != nil { 50 return nil, err 51 } 52 mentionedAccount = localAccount 53 } else { 54 var requestingUsername string 55 56 if originAccount.Domain == "" { 57 requestingUsername = originAccount.Username 58 } 59 60 remoteAccount, _, err := federator.GetAccountByUsernameDomain( 61 gtscontext.SetFastFail(ctx), 62 requestingUsername, 63 username, 64 domain, 65 ) 66 if err != nil { 67 return nil, fmt.Errorf("parseMentionFunc: error fetching account: %s", err) 68 } 69 70 // we were able to resolve it! 71 mentionedAccount = remoteAccount 72 } 73 74 mentionID, err := id.NewRandomULID() 75 if err != nil { 76 return nil, err 77 } 78 79 return >smodel.Mention{ 80 ID: mentionID, 81 StatusID: statusID, 82 OriginAccountID: originAccount.ID, 83 OriginAccountURI: originAccount.URI, 84 TargetAccountID: mentionedAccount.ID, 85 NameString: targetAccount, 86 TargetAccountURI: mentionedAccount.URI, 87 TargetAccountURL: mentionedAccount.URL, 88 OriginAccount: mentionedAccount, 89 }, nil 90 } 91 }