github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/unfurl/scrape_map.go (about)

     1  package unfurl
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/url"
     7  	"strconv"
     8  	"time"
     9  
    10  	"github.com/keybase/client/go/chat/maps"
    11  	"github.com/keybase/client/go/chat/types"
    12  	"github.com/keybase/client/go/protocol/chat1"
    13  	"github.com/keybase/client/go/protocol/gregor1"
    14  )
    15  
    16  func (s *Scraper) scrapeMap(ctx context.Context, uri string) (res chat1.UnfurlRaw, err error) {
    17  	defer s.Trace(ctx, &err, "scrapeMap")()
    18  	puri, err := url.Parse(uri)
    19  	if err != nil {
    20  		return res, err
    21  	}
    22  	slat := puri.Query().Get("lat")
    23  	slon := puri.Query().Get("lon")
    24  	sacc := puri.Query().Get("acc")
    25  	sdone := puri.Query().Get("done")
    26  	lat, err := strconv.ParseFloat(slat, 64)
    27  	if err != nil {
    28  		return res, err
    29  	}
    30  	lon, err := strconv.ParseFloat(slon, 64)
    31  	if err != nil {
    32  		return res, err
    33  	}
    34  	acc, err := strconv.ParseFloat(sacc, 64)
    35  	if err != nil {
    36  		return res, err
    37  	}
    38  	liveLocationDone, err := strconv.ParseBool(sdone)
    39  	if err != nil {
    40  		return res, err
    41  	}
    42  	skey := puri.Query().Get("livekey")
    43  	var liveMapURL *string
    44  	var timeStr string
    45  	siteName := "Location Share"
    46  	mapURL, err := maps.GetMapURL(ctx, s.G().ExternalAPIKeySource, lat, lon)
    47  	if err != nil {
    48  		return res, err
    49  	}
    50  	linkURL := maps.GetExternalMapURL(ctx, lat, lon)
    51  	now := time.Now()
    52  	var liveLocationEndTime *gregor1.Time
    53  	if len(skey) > 0 {
    54  		siteName = "Live Location Share"
    55  		if liveLocationDone {
    56  			// if we're done sharing location
    57  			linkURL = "https://google.com/maps"
    58  			return chat1.NewUnfurlRawWithMaps(chat1.UnfurlMapsRaw{
    59  				Title:               "Location share ended",
    60  				Url:                 linkURL,
    61  				SiteName:            siteName,
    62  				ImageUrl:            mapURL,
    63  				LiveLocationDone:    true,
    64  				LiveLocationEndTime: liveLocationEndTime,
    65  				Time:                gregor1.ToTime(now),
    66  				Coord: chat1.Coordinate{
    67  					Lat:      lat,
    68  					Lon:      lon,
    69  					Accuracy: acc,
    70  				},
    71  			}), nil
    72  		}
    73  		key := types.LiveLocationKey(skey)
    74  		coords := s.G().LiveLocationTracker.GetCoordinates(ctx, key)
    75  		endTime := s.G().LiveLocationTracker.GetEndTime(ctx, key)
    76  		if endTime != nil {
    77  			liveLocationEndTime = new(gregor1.Time)
    78  			*liveLocationEndTime = gregor1.ToTime(*endTime)
    79  		}
    80  		liveMapURL = new(string)
    81  		if *liveMapURL, err = maps.GetLiveMapURL(ctx, s.G().ExternalAPIKeySource, coords); err != nil {
    82  			return res, err
    83  		}
    84  		timeStr = fmt.Sprintf("Posted %s.", now.Format("15:04:05 MST"))
    85  	}
    86  
    87  	desc := fmt.Sprintf("Accurate to %dm. %s", int(acc), timeStr)
    88  	return chat1.NewUnfurlRawWithMaps(chat1.UnfurlMapsRaw{
    89  		Title:           "Open this location with Google Maps",
    90  		Url:             linkURL,
    91  		SiteName:        siteName,
    92  		ImageUrl:        mapURL,
    93  		Description:     desc,
    94  		HistoryImageUrl: liveMapURL,
    95  		Coord: chat1.Coordinate{
    96  			Lat:      lat,
    97  			Lon:      lon,
    98  			Accuracy: acc,
    99  		},
   100  		LiveLocationDone:    liveLocationDone,
   101  		LiveLocationEndTime: liveLocationEndTime,
   102  		Time:                gregor1.ToTime(now),
   103  	}), nil
   104  }