Skip to content

Commit 39d91b7

Browse files
committed
Add ability to use downsized images from giphy.
1 parent 79131f1 commit 39d91b7

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

config.sample.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ services:
6767
UserID: "@goneb:localhost" # requires a Syncing client
6868
Config:
6969
api_key: "qwg4672vsuyfsfe"
70+
use_downsized: false
7071

7172
- ID: "guggy_service"
7273
Type: "guggy"

src/github.com/matrix-org/go-neb/services/giphy/giphy.go

+27-13
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ import (
1717
// ServiceType of the Giphy service.
1818
const ServiceType = "giphy"
1919

20+
type image struct {
21+
URL string `json:"url"`
22+
// Giphy returns ints as strings..
23+
Width string `json:"width"`
24+
Height string `json:"height"`
25+
Size string `json:"size"`
26+
}
27+
2028
type result struct {
2129
Slug string `json:"slug"`
2230
Images struct {
23-
Original struct {
24-
URL string `json:"url"`
25-
// Giphy returns ints as strings..
26-
Width string `json:"width"`
27-
Height string `json:"height"`
28-
Size string `json:"size"`
29-
} `json:"original"`
31+
Downsized image `json:"downsized"`
32+
Original image `json:"original"`
3033
} `json:"images"`
3134
}
3235

@@ -38,13 +41,18 @@ type giphySearch struct {
3841
//
3942
// Example request:
4043
// {
41-
// "api_key": "dc6zaTOxFJmzC"
44+
// "api_key": "dc6zaTOxFJmzC",
45+
// "use_downsized": false
4246
// }
4347
type Service struct {
4448
types.DefaultService
4549
// The Giphy API key to use when making HTTP requests to Giphy.
4650
// The public beta API key is "dc6zaTOxFJmzC".
4751
APIKey string `json:"api_key"`
52+
// Whether to use the downsized image from Giphy.
53+
// Uses the original image when set to false.
54+
// Defaults to false.
55+
UseDownsized bool `json:"use_downsized"`
4856
}
4957

5058
// Commands supported:
@@ -68,10 +76,16 @@ func (s *Service) cmdGiphy(client *gomatrix.Client, roomID, userID string, args
6876
if err != nil {
6977
return nil, err
7078
}
71-
if gifResult.Images.Original.URL == "" {
79+
80+
image := gifResult.Images.Original
81+
if s.UseDownsized {
82+
image = gifResult.Images.Downsized
83+
}
84+
85+
if image.URL == "" {
7286
return nil, fmt.Errorf("No results")
7387
}
74-
resUpload, err := client.UploadLink(gifResult.Images.Original.URL)
88+
resUpload, err := client.UploadLink(image.URL)
7589
if err != nil {
7690
return nil, err
7791
}
@@ -81,10 +95,10 @@ func (s *Service) cmdGiphy(client *gomatrix.Client, roomID, userID string, args
8195
Body: gifResult.Slug,
8296
URL: resUpload.ContentURI,
8397
Info: gomatrix.ImageInfo{
84-
Height: asInt(gifResult.Images.Original.Height),
85-
Width: asInt(gifResult.Images.Original.Width),
98+
Height: asInt(image.Height),
99+
Width: asInt(image.Width),
86100
Mimetype: "image/gif",
87-
Size: asInt(gifResult.Images.Original.Size),
101+
Size: asInt(image.Size),
88102
},
89103
}, nil
90104
}

0 commit comments

Comments
 (0)