Skip to content

Commit f6dd73d

Browse files
authored
giphy previews on mobile (keybase#16820)
1 parent 44d9081 commit f6dd73d

30 files changed

Lines changed: 334 additions & 111 deletions

File tree

‎go/chat/attachment_httpsrv.go‎

Lines changed: 131 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"net/http"
9+
"net/url"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -34,16 +35,19 @@ type AttachmentHTTPSrv struct {
3435
globals.Contextified
3536
utils.DebugLabeler
3637

37-
endpoint string
38-
pendingEndpoint string
39-
unfurlEndpoint string
40-
giphyEndpoint string
41-
httpSrv *kbhttp.Srv
42-
urlMap *lru.Cache
43-
unfurlMap *lru.Cache
44-
giphyMap *lru.Cache
45-
fetcher types.AttachmentFetcher
46-
ri func() chat1.RemoteInterface
38+
endpoint string
39+
pendingEndpoint string
40+
unfurlEndpoint string
41+
giphyEndpoint string
42+
giphyGalleryEndpoint string
43+
giphySelectEndpoint string
44+
httpSrv *kbhttp.Srv
45+
urlMap *lru.Cache
46+
unfurlMap *lru.Cache
47+
giphyMap *lru.Cache
48+
giphyGalleryMap *lru.Cache
49+
fetcher types.AttachmentFetcher
50+
ri func() chat1.RemoteInterface
4751
}
4852

4953
var _ types.AttachmentURLSrv = (*AttachmentHTTPSrv)(nil)
@@ -61,18 +65,25 @@ func NewAttachmentHTTPSrv(g *globals.Context, fetcher types.AttachmentFetcher, r
6165
if err != nil {
6266
panic(err)
6367
}
68+
ggm, err := lru.New(100)
69+
if err != nil {
70+
panic(err)
71+
}
6472
r := &AttachmentHTTPSrv{
65-
Contextified: globals.NewContextified(g),
66-
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "AttachmentHTTPSrv", false),
67-
endpoint: "at",
68-
pendingEndpoint: "pe",
69-
unfurlEndpoint: "uf",
70-
giphyEndpoint: "gf",
71-
ri: ri,
72-
urlMap: l,
73-
unfurlMap: um,
74-
fetcher: fetcher,
75-
giphyMap: gm,
73+
Contextified: globals.NewContextified(g),
74+
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "AttachmentHTTPSrv", false),
75+
endpoint: "at",
76+
pendingEndpoint: "pe",
77+
unfurlEndpoint: "uf",
78+
giphyEndpoint: "gf",
79+
giphyGalleryEndpoint: "gg",
80+
giphySelectEndpoint: "gs",
81+
ri: ri,
82+
urlMap: l,
83+
unfurlMap: um,
84+
fetcher: fetcher,
85+
giphyMap: gm,
86+
giphyGalleryMap: ggm,
7687
}
7788
r.initHTTPSrv()
7889
r.startHTTPSrv()
@@ -137,6 +148,8 @@ func (r *AttachmentHTTPSrv) startHTTPSrv() {
137148
r.httpSrv.HandleFunc("/"+r.pendingEndpoint, r.servePendingPreview)
138149
r.httpSrv.HandleFunc("/"+r.unfurlEndpoint, r.serveUnfurlAsset)
139150
r.httpSrv.HandleFunc("/"+r.giphyEndpoint, r.serveGiphyLink)
151+
r.httpSrv.HandleFunc("/"+r.giphyGalleryEndpoint, r.serveGiphyGallery)
152+
r.httpSrv.HandleFunc("/"+r.giphySelectEndpoint, r.serveGiphyGallerySelect)
140153
}
141154

142155
func (r *AttachmentHTTPSrv) GetAttachmentFetcher() types.AttachmentFetcher {
@@ -216,6 +229,18 @@ func (r *AttachmentHTTPSrv) GetGiphyURL(ctx context.Context, giphyURL string) st
216229
return url
217230
}
218231

232+
func (r *AttachmentHTTPSrv) GetGiphyGalleryURL(ctx context.Context, convID chat1.ConversationID,
233+
tlfName string, results []chat1.GiphySearchResult) string {
234+
defer r.Trace(ctx, func() error { return nil }, "GetGiphyGalleryURL")()
235+
url := r.getURL(ctx, "gg", r.giphyGalleryEndpoint, r.giphyGalleryMap, giphyGalleryInfo{
236+
results: results,
237+
convID: convID,
238+
tlfName: tlfName,
239+
})
240+
r.Debug(ctx, "GetGiphyGalleryURL: handler URL: %s", url)
241+
return url
242+
}
243+
219244
func (r *AttachmentHTTPSrv) servePendingPreview(w http.ResponseWriter, req *http.Request) {
220245
ctx := globals.ChatCtx(context.Background(), r.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, nil,
221246
NewSimpleIdentifyNotifier(r.G()))
@@ -269,6 +294,91 @@ func (r *AttachmentHTTPSrv) serveUnfurlAsset(w http.ResponseWriter, req *http.Re
269294
}
270295
}
271296

297+
type giphyGalleryInfo struct {
298+
results []chat1.GiphySearchResult
299+
convID chat1.ConversationID
300+
tlfName string
301+
}
302+
303+
func (r *AttachmentHTTPSrv) getGiphyGallerySelectURL(ctx context.Context, convID chat1.ConversationID,
304+
tlfName, targetURL string) string {
305+
addr, err := r.httpSrv.Addr()
306+
if err != nil {
307+
r.Debug(ctx, "getGiphySelectURL: failed to get HTTP server address: %s", err)
308+
return ""
309+
}
310+
return fmt.Sprintf("http://%s/%s?url=%s&convID=%s&tlfName=%s", addr, r.giphySelectEndpoint,
311+
url.QueryEscape(targetURL), convID, tlfName)
312+
}
313+
314+
func (r *AttachmentHTTPSrv) serveGiphyGallerySelect(w http.ResponseWriter, req *http.Request) {
315+
ctx := globals.ChatCtx(context.Background(), r.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, nil,
316+
NewSimpleIdentifyNotifier(r.G()))
317+
defer r.Trace(ctx, func() error { return nil }, "serveGiphyGallerySelect")()
318+
url := req.URL.Query().Get("url")
319+
strConvID := req.URL.Query().Get("convID")
320+
tlfName := req.URL.Query().Get("tlfName")
321+
convID, err := chat1.MakeConvID(strConvID)
322+
if err != nil {
323+
r.makeError(context.TODO(), w, http.StatusInternalServerError, "failed to decode convID: %s",
324+
err)
325+
return
326+
}
327+
if err := r.G().ChatHelper.SendTextByID(ctx, convID, tlfName, url); err != nil {
328+
r.makeError(context.TODO(), w, http.StatusInternalServerError, "failed to send giphy url: %s",
329+
err)
330+
}
331+
ui, err := r.G().UIRouter.GetChatUI()
332+
if err == nil && ui != nil {
333+
ui.ChatGiphyToggleResultWindow(ctx, convID, false, true)
334+
} else {
335+
r.Debug(ctx, "serveGiphyGallerySelect: failed to get chat UI: %s", err)
336+
}
337+
}
338+
339+
func (r *AttachmentHTTPSrv) serveGiphyGallery(w http.ResponseWriter, req *http.Request) {
340+
ctx := globals.ChatCtx(context.Background(), r.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, nil,
341+
NewSimpleIdentifyNotifier(r.G()))
342+
defer r.Trace(ctx, func() error { return nil }, "serveGiphyGallery")()
343+
key := req.URL.Query().Get("key")
344+
infoInt, ok := r.giphyGalleryMap.Get(key)
345+
if !ok {
346+
r.makeError(ctx, w, http.StatusInternalServerError, "invalid key: %s", key)
347+
return
348+
}
349+
galleryInfo := infoInt.(giphyGalleryInfo)
350+
var videoStr string
351+
for _, res := range galleryInfo.results {
352+
videoStr += fmt.Sprintf(`
353+
<video style="height: 100%%" src="%s" onclick="sendMessage('%s')" playsinline webkit-playsinline autoplay muted loop>
354+
</video>
355+
`, res.PreviewUrl, r.getGiphyGallerySelectURL(ctx, galleryInfo.convID, galleryInfo.tlfName,
356+
res.TargetUrl))
357+
}
358+
res := fmt.Sprintf(`
359+
<html>
360+
<head>
361+
<title>Keybase Giphy Gallery</title>
362+
<script>
363+
window.sendMessage = function(url) {
364+
var req = new XMLHttpRequest();
365+
req.open("GET", url);
366+
req.send();
367+
}
368+
</script>
369+
</head>
370+
<body style="margin: 0px;">
371+
<div style="display: flex; flex-direction: row; height: 100%%; overflow-x: auto; overflow-y: hidden; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; border-top: 1px solid rgba(0, 0, 0, 0.20); align-items: flex-end;">
372+
%s
373+
</div>
374+
</body>
375+
</html>`, videoStr)
376+
if _, err := io.WriteString(w, res); err != nil {
377+
r.makeError(context.TODO(), w, http.StatusInternalServerError, "failed to write giphy gallery: %s",
378+
err)
379+
}
380+
}
381+
272382
func (r *AttachmentHTTPSrv) serveGiphyLink(w http.ResponseWriter, req *http.Request) {
273383
ctx := globals.ChatCtx(context.Background(), r.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, nil,
274384
NewSimpleIdentifyNotifier(r.G()))

‎go/chat/commands/base.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (b *baseCommand) HasHelpText() bool {
9696
}
9797

9898
func (b *baseCommand) Preview(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
99-
text string) {
99+
tlfName, text string) {
100100
}
101101

102102
func (b *baseCommand) Export() chat1.ConversationCommand {

‎go/chat/commands/flip.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func (s *Flip) Execute(ctx context.Context, uid gregor1.UID, convID chat1.Conver
3232
return s.G().CoinFlipManager.StartFlip(ctx, uid, convID, tlfName, text, nil)
3333
}
3434

35-
func (s *Flip) Preview(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID, text string) {
35+
func (s *Flip) Preview(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
36+
tlfName, text string) {
3637
s.Lock()
3738
defer s.Unlock()
3839
defer s.Trace(ctx, func() error { return nil }, "Preview")()

‎go/chat/commands/flip_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@ func TestFlipPreview(t *testing.T) {
2626
convID := chat1.ConversationID{1, 2, 3, 4}
2727
timeout := 20 * time.Second
2828

29-
flip.Preview(ctx, uid, convID, "/flip")
29+
flip.Preview(ctx, uid, convID, "", "/flip")
3030
select {
3131
case s := <-ui.CommandMarkdown:
3232
require.NotNil(t, s)
3333
case <-time.After(timeout):
3434
require.Fail(t, "no text")
3535
}
3636

37-
flip.Preview(ctx, uid, convID, "/flip 6")
37+
flip.Preview(ctx, uid, convID, "", "/flip 6")
3838
select {
3939
case s := <-ui.CommandMarkdown:
4040
require.NotNil(t, s)
4141
case <-time.After(timeout):
4242
require.Fail(t, "no text")
4343
}
4444

45-
flip.Preview(ctx, uid, convID, "/flip ")
45+
flip.Preview(ctx, uid, convID, "", "/flip ")
4646
select {
4747
case s := <-ui.CommandMarkdown:
4848
require.NotNil(t, s)
4949
case <-time.After(timeout):
5050
require.Fail(t, "no text")
5151
}
5252

53-
flip.Preview(ctx, uid, convID, "/fli")
53+
flip.Preview(ctx, uid, convID, "", "/fli")
5454
select {
5555
case s := <-ui.CommandMarkdown:
5656
require.Nil(t, s)
5757
case <-time.After(timeout):
5858
require.Fail(t, "no text")
5959
}
6060

61-
flip.Preview(ctx, uid, convID, "/fli")
61+
flip.Preview(ctx, uid, convID, "", "/fli")
6262
select {
6363
case <-ui.CommandMarkdown:
6464
require.Fail(t, "no text expected")

‎go/chat/commands/giphy.go‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import (
1313
)
1414

1515
type giphySearcher interface {
16-
Search(mctx libkb.MetaContext, query *string, urlsrv types.AttachmentURLSrv) ([]chat1.GiphySearchResult, error)
16+
Search(mctx libkb.MetaContext, query *string, limit int, urlsrv types.AttachmentURLSrv) ([]chat1.GiphySearchResult, error)
1717
}
1818

1919
type defaultGiphySearcher struct{}
2020

21-
func (d defaultGiphySearcher) Search(mctx libkb.MetaContext, query *string, urlsrv types.AttachmentURLSrv) ([]chat1.GiphySearchResult, error) {
22-
return giphy.Search(mctx, query, urlsrv)
21+
func (d defaultGiphySearcher) Search(mctx libkb.MetaContext, query *string, limit int,
22+
urlsrv types.AttachmentURLSrv) ([]chat1.GiphySearchResult, error) {
23+
return giphy.Search(mctx, query, limit, urlsrv)
2324
}
2425

2526
type Giphy struct {
@@ -33,9 +34,6 @@ type Giphy struct {
3334

3435
func NewGiphy(g *globals.Context) *Giphy {
3536
usage := "Search for and post GIFs"
36-
if g.GetAppType() == libkb.MobileAppType {
37-
usage = "Post a random GIF"
38-
}
3937
return &Giphy{
4038
baseCommand: newBaseCommand(g, "giphy", "[search terms]", usage, true),
4139
shownResults: make(map[string]*string),
@@ -55,13 +53,21 @@ func (s *Giphy) getQuery(text string) *string {
5553
return query
5654
}
5755

56+
func (s *Giphy) getLimit() int {
57+
limit := 25
58+
if s.G().IsMobileAppType() {
59+
limit = 10
60+
}
61+
return limit
62+
}
63+
5864
func (s *Giphy) Execute(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
5965
tlfName, text string) (err error) {
6066
if !s.Match(ctx, text) {
6167
return ErrInvalidCommand
6268
}
6369
results, err := s.searcher.Search(libkb.NewMetaContext(ctx, s.G().ExternalG()), s.getQuery(text),
64-
s.G().AttachmentURLSrv)
70+
s.getLimit(), s.G().AttachmentURLSrv)
6571
if err != nil {
6672
s.Debug(ctx, "Execute: failed to get Giphy results: %s", err)
6773
return err
@@ -76,20 +82,18 @@ func (s *Giphy) Execute(ctx context.Context, uid gregor1.UID, convID chat1.Conve
7682
}
7783

7884
func (n nullChatUI) ChatGiphySearchResults(ctx context.Context, convID chat1.ConversationID,
79-
results []chat1.GiphySearchResult) error {
85+
results chat1.GiphySearchResults) error {
8086
return nil
8187
}
8288

83-
func (n nullChatUI) ChatGiphyToggleResultWindow(ctx context.Context, convID chat1.ConversationID, show bool) error {
89+
func (n nullChatUI) ChatGiphyToggleResultWindow(ctx context.Context, convID chat1.ConversationID,
90+
show, clearInput bool) error {
8491
return nil
8592
}
8693

87-
func (s *Giphy) Preview(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID, text string) {
94+
func (s *Giphy) Preview(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
95+
tlfName, text string) {
8896
defer s.Trace(ctx, func() error { return nil }, "Preview")()
89-
if s.G().GetAppType() == libkb.MobileAppType {
90-
// no preview on mobile yet
91-
return
92-
}
9397
s.Lock()
9498
if s.currentOpCancelFn != nil {
9599
s.currentOpCancelFn()
@@ -108,7 +112,7 @@ func (s *Giphy) Preview(ctx context.Context, uid gregor1.UID, convID chat1.Conve
108112
if !s.Match(ctx, text) {
109113
if _, ok := s.shownResults[convID.String()]; ok {
110114
// tell UI to clear
111-
s.getChatUI().ChatGiphyToggleResultWindow(ctx, convID, false)
115+
s.getChatUI().ChatGiphyToggleResultWindow(ctx, convID, false, false)
112116
delete(s.shownResults, convID.String())
113117
}
114118
return
@@ -118,13 +122,17 @@ func (s *Giphy) Preview(ctx context.Context, uid gregor1.UID, convID chat1.Conve
118122
s.Debug(ctx, "Preview: same query given, skipping")
119123
return
120124
}
121-
s.getChatUI().ChatGiphyToggleResultWindow(ctx, convID, true)
125+
s.getChatUI().ChatGiphyToggleResultWindow(ctx, convID, true, false)
122126
s.shownResults[convID.String()] = query
123-
results, err := s.searcher.Search(libkb.NewMetaContext(ctx, s.G().ExternalG()), query,
127+
128+
results, err := s.searcher.Search(libkb.NewMetaContext(ctx, s.G().ExternalG()), query, s.getLimit(),
124129
s.G().AttachmentURLSrv)
125130
if err != nil {
126131
s.Debug(ctx, "Preview: failed to get Giphy results: %s", err)
127132
return
128133
}
129-
s.getChatUI().ChatGiphySearchResults(ctx, convID, results)
134+
s.getChatUI().ChatGiphySearchResults(ctx, convID, chat1.GiphySearchResults{
135+
Results: results,
136+
GalleryUrl: s.G().AttachmentURLSrv.GetGiphyGalleryURL(ctx, convID, tlfName, results),
137+
})
130138
}

0 commit comments

Comments
 (0)