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

     1  package display
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/keybase/client/go/chat/types"
     8  	"github.com/keybase/client/go/protocol/chat1"
     9  )
    10  
    11  func assetToImageDisplay(ctx context.Context, convID chat1.ConversationID, asset chat1.Asset,
    12  	srv types.AttachmentURLSrv) (res chat1.UnfurlImageDisplay, err error) {
    13  	var height, width int
    14  	typ, err := asset.Metadata.AssetType()
    15  	if err != nil {
    16  		return res, err
    17  	}
    18  	isVideo := false
    19  	switch typ {
    20  	case chat1.AssetMetadataType_IMAGE:
    21  		height = asset.Metadata.Image().Height
    22  		width = asset.Metadata.Image().Width
    23  	case chat1.AssetMetadataType_VIDEO:
    24  		isVideo = asset.MimeType != "image/gif"
    25  		height = asset.Metadata.Video().Height
    26  		width = asset.Metadata.Video().Width
    27  	default:
    28  		return res, errors.New("unknown asset type")
    29  	}
    30  	return chat1.UnfurlImageDisplay{
    31  		IsVideo: isVideo,
    32  		Height:  height,
    33  		Width:   width,
    34  		Url:     srv.GetUnfurlAssetURL(ctx, convID, asset),
    35  	}, nil
    36  }
    37  
    38  func displayUnfurlGeneric(ctx context.Context, srv types.AttachmentURLSrv, convID chat1.ConversationID,
    39  	unfurl chat1.UnfurlGeneric) (res chat1.UnfurlGenericDisplay) {
    40  	res.Title = unfurl.Title
    41  	res.Url = unfurl.Url
    42  	res.SiteName = unfurl.SiteName
    43  	res.PublishTime = unfurl.PublishTime
    44  	res.Description = unfurl.Description
    45  	res.MapInfo = unfurl.MapInfo
    46  	if unfurl.Image != nil {
    47  		if media, err := assetToImageDisplay(ctx, convID, *unfurl.Image, srv); err == nil {
    48  			res.Media = &media
    49  		}
    50  	}
    51  	if unfurl.Favicon != nil {
    52  		res.Favicon = new(chat1.UnfurlImageDisplay)
    53  		if fav, err := assetToImageDisplay(ctx, convID, *unfurl.Favicon, srv); err == nil {
    54  			res.Favicon = &fav
    55  		}
    56  	}
    57  	return res
    58  }
    59  
    60  func displayUnfurlGiphy(ctx context.Context, srv types.AttachmentURLSrv, convID chat1.ConversationID,
    61  	unfurl chat1.UnfurlGiphy) (res chat1.UnfurlGiphyDisplay, err error) {
    62  	if unfurl.Image != nil {
    63  		if img, err := assetToImageDisplay(ctx, convID, *unfurl.Image, srv); err == nil {
    64  			res.Image = &img
    65  		}
    66  	}
    67  	if unfurl.Video != nil {
    68  		if vid, err := assetToImageDisplay(ctx, convID, *unfurl.Video, srv); err == nil {
    69  			res.Video = &vid
    70  		}
    71  	}
    72  	if unfurl.Favicon != nil {
    73  		res.Favicon = new(chat1.UnfurlImageDisplay)
    74  		if fav, err := assetToImageDisplay(ctx, convID, *unfurl.Favicon, srv); err == nil {
    75  			res.Favicon = &fav
    76  		}
    77  	}
    78  	if res.Image == nil && res.Video == nil {
    79  		return res, errors.New("no image for video for giphy")
    80  	}
    81  	return res, nil
    82  }
    83  
    84  func DisplayUnfurl(ctx context.Context, srv types.AttachmentURLSrv, convID chat1.ConversationID,
    85  	unfurl chat1.Unfurl) (res chat1.UnfurlDisplay, err error) {
    86  	typ, err := unfurl.UnfurlType()
    87  	if err != nil {
    88  		return res, err
    89  	}
    90  	switch typ {
    91  	case chat1.UnfurlType_GENERIC:
    92  		return chat1.NewUnfurlDisplayWithGeneric(displayUnfurlGeneric(ctx, srv, convID, unfurl.Generic())),
    93  			nil
    94  	case chat1.UnfurlType_GIPHY:
    95  		giphy, err := displayUnfurlGiphy(ctx, srv, convID, unfurl.Giphy())
    96  		if err != nil {
    97  			return res, err
    98  		}
    99  		return chat1.NewUnfurlDisplayWithGiphy(giphy), nil
   100  	case chat1.UnfurlType_YOUTUBE:
   101  		return chat1.NewUnfurlDisplayWithYoutube(chat1.UnfurlYoutubeDisplay{}), nil
   102  	default:
   103  		return res, errors.New("unknown unfurl type")
   104  	}
   105  }