gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

namestring.go (1940B)


      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 util
     19 
     20 import (
     21 	"fmt"
     22 	"strings"
     23 
     24 	"github.com/superseriousbusiness/gotosocial/internal/regexes"
     25 )
     26 
     27 // ExtractNamestringParts extracts the username test_user and
     28 // the domain example.org from a string like @test_user@example.org.
     29 //
     30 // If nothing is matched, it will return an error.
     31 func ExtractNamestringParts(mention string) (username, host string, err error) {
     32 	matches := regexes.MentionName.FindStringSubmatch(mention)
     33 	switch len(matches) {
     34 	case 2:
     35 		return matches[1], "", nil
     36 	case 3:
     37 		return matches[1], matches[2], nil
     38 	default:
     39 		return "", "", fmt.Errorf("couldn't match mention %s", mention)
     40 	}
     41 }
     42 
     43 // ExtractWebfingerParts returns username test_user and
     44 // domain example.org from a string like acct:test_user@example.org,
     45 // or acct:@test_user@example.org.
     46 //
     47 // If nothing is extracted, it will return an error.
     48 func ExtractWebfingerParts(webfinger string) (username, host string, err error) {
     49 	// remove the acct: prefix if it's present
     50 	webfinger = strings.TrimPrefix(webfinger, "acct:")
     51 
     52 	// prepend an @ if necessary
     53 	if webfinger[0] != '@' {
     54 		webfinger = "@" + webfinger
     55 	}
     56 
     57 	return ExtractNamestringParts(webfinger)
     58 }