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
4953var _ 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
142155func (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+
219244func (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+
272382func (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 ()))
0 commit comments