github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/unfurl/utils.go (about)

     1  package unfurl
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/keybase/client/go/chat/types"
     9  	"github.com/keybase/client/go/protocol/chat1"
    10  	"golang.org/x/net/publicsuffix"
    11  )
    12  
    13  func GetDefaultFaviconURL(uri string) (string, error) {
    14  	parsed, err := url.Parse(uri)
    15  	if err != nil {
    16  		return "", err
    17  	}
    18  	parsed.Path = "favicon.ico"
    19  	parsed.RawQuery = ""
    20  	return parsed.String(), nil
    21  }
    22  
    23  func GetDefaultAppleTouchURL(uri string) (string, error) {
    24  	parsed, err := url.Parse(uri)
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  	parsed.Path = "apple-touch-icon.png"
    29  	parsed.RawQuery = ""
    30  	return parsed.String(), nil
    31  }
    32  
    33  func GetHostname(uri string) (string, error) {
    34  	parsed, err := url.Parse(uri)
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  	return parsed.Hostname(), nil
    39  }
    40  
    41  func GetDomain(uri string) (res string, err error) {
    42  	hostname, err := GetHostname(uri)
    43  	if err != nil {
    44  		return res, err
    45  	}
    46  	if len(hostname) == 0 {
    47  		return res, errors.New("no hostname")
    48  	}
    49  	if hostname == types.MapsDomain {
    50  		return hostname, nil
    51  	}
    52  	return publicsuffix.EffectiveTLDPlusOne(hostname)
    53  }
    54  
    55  func IsDomain(domain, target string) bool {
    56  	return strings.Contains(domain, target+".")
    57  }
    58  
    59  func ClassifyDomain(domain string) chat1.UnfurlType {
    60  	switch {
    61  	case domain == "gph.is":
    62  		fallthrough
    63  	case IsDomain(domain, "giphy"):
    64  		return chat1.UnfurlType_GIPHY
    65  	case domain == types.MapsDomain:
    66  		return chat1.UnfurlType_MAPS
    67  	default:
    68  		return chat1.UnfurlType_GENERIC
    69  	}
    70  }