From 51b7f1ec5f38d477a9bd03ddbb8ee143a2ed9136 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 13 Feb 2017 17:02:03 +0000 Subject: [PATCH 01/25] Imgur data model updates -- tests broken --- .../matrix-org/go-neb/services/imgur/imgur.go | 250 ++++++++++++++++++ .../go-neb/services/imgur/imgur_test.go | 111 ++++++++ 2 files changed, 361 insertions(+) create mode 100644 src/github.com/matrix-org/go-neb/services/imgur/imgur.go create mode 100644 src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go new file mode 100644 index 0000000..d367b20 --- /dev/null +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -0,0 +1,250 @@ +// Package imgur implements a Service which adds !commands for imgur image search +package imgur + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "math" + "net/http" + "net/url" + "strings" + + log "github.com/Sirupsen/logrus" + "github.com/matrix-org/go-neb/types" +) + +// ServiceType of the Imgur service +const ServiceType = "imgur" + +var httpClient = &http.Client{} + +type imgurGalleryImage struct { + ID string `json:"id"` // id string The ID for the image + Title string `json:"title"` // title string The title of the image. + Description string `json:"description"` // description string Description of the image. + DateTime int64 `json:"datetime"` // datetime integer Time inserted into the gallery, epoch time + Type string `json:"type"` // type string Image MIME type. + Animated bool `json:"animated"` // animated boolean is the image animated + Width int `json:"width"` // width integer The width of the image in pixels + Height int `json:"height"` // height integer The height of the image in pixels + Size int64 `json:"size"` // size integer The size of the image in bytes + Views int64 `json:"views"` // views integer The number of image views + Link string `json:"link"` // link string The direct link to the the image. (Note: if fetching an animated GIF that was over 20MB in original size, a .gif thumbnail will be returned) + Gifv string `json:"gifv"` // gifv string OPTIONAL, The .gifv link. Only available if the image is animated and type is 'image/gif'. + MP4 string `json:"mp4"` // mp4 string OPTIONAL, The direct link to the .mp4. Only available if the image is animated and type is 'image/gif'. + MP4Size int64 `json:"mp4_size"` // mp4_size integer OPTIONAL, The Content-Length of the .mp4. Only available if the image is animated and type is 'image/gif'. Note that a zero value (0) is possible if the video has not yet been generated + Looping bool `json:"looping"` // looping boolean OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'. + NSFW bool `json:"nsfw"` // nsfw boolean Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available. + Topic string `json:"topic"` // topic string Topic of the gallery image. + Section string `json:"section"` // section string If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc) + IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not + // ** Uninplemented fields ** + // bandwidth integer Bandwidth consumed by the image in bytes + // deletehash string OPTIONAL, the deletehash, if you're logged in as the image owner + // comment_count int Number of comments on the gallery image. + // topic_id integer Topic ID of the gallery image. + // vote string The current user's vote on the album. null if not signed in or if the user hasn't voted on it. + // favorite boolean Indicates if the current user favorited the image. Defaults to false if not signed in. + // account_url string The username of the account that uploaded it, or null. + // account_id integer The account ID of the account that uploaded it, or null. + // ups integer Upvotes for the image + // downs integer Number of downvotes for the image + // points integer Upvotes minus downvotes + // score integer Imgur popularity score +} + +type imgurGalleryAlbum struct { + ID string `json:"id"` // id string The ID for the album + Title string `json:"title"` // title string The title of the album. + Description string `json:"description"` // description string Description of the album. + DateTime int64 `json:"datetime"` // datetime integer Time inserted into the gallery, epoch time + Views int64 `json:"views"` // views integer The number of album views + Link string `json:"link"` // link string The URL link to the album + NSFW bool `json:"nsfw"` // nsfw boolean Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available. + Topic string `json:"topic"` // topic string Topic of the gallery album. + IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not + Cover string `json:"cover"` // cover string The ID of the album cover image + CoverWidth int `json:"cover_width"` // cover_width integer The width, in pixels, of the album cover image + CoverHeight int `json:"cover_height"` // cover_height integer The height, in pixels, of the album cover image + ImagesCount int `json:"images_count"` // images_count integer The total number of images in the album + Images []imgurGalleryImage `json:"images"` // images Array of Images An array of all the images in the album (only available when requesting the direct album) + + // ** Unimplemented fields ** + // account_url string The account username or null if it's anonymous. + // account_id integer The account ID of the account that uploaded it, or null. + // privacy string The privacy level of the album, you can only view public if not logged in as album owner + // layout string The view layout of the album. + // views integer The number of image views + // ups integer Upvotes for the image + // downs integer Number of downvotes for the image + // points integer Upvotes minus downvotes + // score integer Imgur popularity score + // vote string The current user's vote on the album. null if not signed in or if the user hasn't voted on it. + // favorite boolean Indicates if the current user favorited the album. Defaults to false if not signed in. + // comment_count int Number of comments on the gallery album. + // topic_id integer Topic ID of the gallery album. +} + +type imgurSearchResponse struct { + Data json.RawMessage `json:"data"` + Success bool `json:"success"` // Request completed successfully + Status int `json:"status"` // HTTP response code +} + +// Service contains the Config fields for the Imgur service. +// +// Example request: +// { +// "client_id": "AIzaSyA4FD39..." +// "client_secret": "ASdsaijwdfASD..." +// } +type Service struct { + types.DefaultService + // The API key to use when making HTTP requests to Imgur. + ClientSecret string `json:"client_secret"` + // The Imgur client ID + ClientID string `json:"client_id"` +} + +// Commands supported: +// !imgur image some_search_query_without_quotes +// Responds with a suitable image into the same room as the command. +func (s *Service) Commands(client *gomatrix.Client) []types.Command { + return []types.Command{ + types.Command{ + Path: []string{"imgur", "image"}, + Command: func(roomID, userID string, args []string) (interface{}, error) { + return s.cmdImgurImgSearch(client, roomID, userID, args) + }, + }, + types.Command{ + Path: []string{"imgur", "help"}, + Command: func(roomID, userID string, args []string) (interface{}, error) { + return usageMessage(), nil + }, + }, + } +} + +// usageMessage returns a matrix TextMessage representation of the service usage +func usageMessage() *gomatrix.TextMessage { + return &gomatrix.TextMessage{"m.notice", + `Usage: !imgur image image_search_text`} +} + +func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { + + if len(args) < 1 { + return usageMessage(), nil + } + + // Get the query text to search for. + querySentence := strings.Join(args, " ") + + searchResult, err := s.text2imgImgur(querySentence) + + if err != nil { + return nil, err + } + + var imgURL = searchResult.Link + if imgURL == "" { + return gomatrix.TextMessage{ + MsgType: "m.notice", + Body: "No image found!", + }, nil + } + + // FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400" + resUpload, err := client.UploadLink(imgURL) + if err != nil { + return nil, fmt.Errorf("Failed to upload Imgur image to matrix: %s", err.Error()) + } + + return gomatrix.ImageMessage{ + MsgType: "m.image", + Body: querySentence, + URL: resUpload.ContentURI, + Info: gomatrix.ImageInfo{ + Height: uint(math.Floor(searchResult.Height)), + Width: uint(math.Floor(searchResult.Width)), + Mimetype: searchResult.Type, + }, + }, nil +} + +// text2imgImgur returns info about an image or an album +func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) { + log.Info("Searching Imgur for an image of a ", query) + + var base = "https://api.imgur.com/3/gallery/search/" + var sort = "time" // time | viral | top + var window = all // day | week | month | year | all + var page = 1 + var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page) + + u, err := url.Parse(urlString) + + if err != nil { + return nil, nil, err + } + + res, err := http.Get(u.String()) + if res != nil { + defer res.Body.Close() + } + if err != nil { + return nil, nil, err + } + if res.StatusCode > 200 { + return nil, nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) + } + + var searchResults imgurSearchResults + if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || !searchResults.data { + return nil, nil, fmt.Errorf("No images found - %s", err.Error()) + } + + // Check if we have an image or a gallery + var dataInt map[string]interface{} + if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || !searchResults.data { + return nil, nil, fmt.Errorf("Failed to parse response data - %s", err.Error()) + } + + // Return an album + if dataInt["is_album"].(bool) { + var album imgurGalleryAlbum + if err := json.Unmarshal(searchResults.Data, &album); err != nill { + return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) + } + + return nil, album, nil + } + + // Return an image + var image imgurGalleryImage + if err := json.Unmarshal(searchResults.Data, &image); err != nill { + return nil, nil, fmt.Errorf("Failed to parse image data - %s", err.Error()) + } + return image, nil, nil +} + +// response2String returns a string representation of an HTTP response body +func response2String(res *http.Response) string { + bs, err := ioutil.ReadAll(res.Body) + if err != nil { + return "Failed to decode response body" + } + str := string(bs) + return str +} + +// Initialise the service +func init() { + types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { + return &Service{ + DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), + } + }) +} diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go new file mode 100644 index 0000000..c4bccb5 --- /dev/null +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -0,0 +1,111 @@ +package imgur + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" + + "github.com/matrix-org/go-neb/database" + "github.com/matrix-org/go-neb/testutils" + "github.com/matrix-org/go-neb/types" + "github.com/matrix-org/gomatrix" +) + +// TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make +// sure all cases are handled, rather than just the general case as is here. +func TestCommand(t *testing.T) { + database.SetServiceDB(&database.NopStorage{}) + apiKey := "secret" + googleImageURL := "https://www.googleapis.com/customsearch/v1" + + // Mock the response from Google + googleTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { + googleURL := "https://www.googleapis.com/customsearch/v1" + query := req.URL.Query() + + // Check the base API URL + if !strings.HasPrefix(req.URL.String(), googleURL) { + t.Fatalf("Bad URL: got %s want prefix %s", req.URL.String(), googleURL) + } + // Check the request method + if req.Method != "GET" { + t.Fatalf("Bad method: got %s want GET", req.Method) + } + // Check the API key + if query.Get("key") != apiKey { + t.Fatalf("Bad apiKey: got %s want %s", query.Get("key"), apiKey) + } + // Check the search query + var searchString = query.Get("q") + var searchStringLength = len(searchString) + if searchStringLength > 0 && !strings.HasPrefix(searchString, "image") { + t.Fatalf("Bad search string: got \"%s\" (%d characters) ", searchString, searchStringLength) + } + + resImage := googleImage{ + Width: 64, + Height: 64, + } + + res := googleSearchResult{ + Title: "A Cat", + Link: "http://cat.com/cat.jpg", + Mime: "image/jpeg", + Image: resImage, + } + + b, err := json.Marshal(res) + if err != nil { + t.Fatalf("Failed to marshal Google response - %s", err) + } + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBuffer(b)), + }, nil + }) + // clobber the Google service http client instance + httpClient = &http.Client{Transport: googleTrans} + + // Create the Google service + srv, err := types.CreateService("id", ServiceType, "@googlebot:hyrule", []byte( + `{"api_key":"`+apiKey+`"}`, + )) + if err != nil { + t.Fatal("Failed to create Google service: ", err) + } + google := srv.(*Service) + + // Mock the response from Matrix + matrixTrans := struct{ testutils.MockTransport }{} + matrixTrans.RT = func(req *http.Request) (*http.Response, error) { + if req.URL.String() == googleImageURL { // getting the Google image + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString("some image data")), + }, nil + } else if strings.Contains(req.URL.String(), "_matrix/media/r0/upload") { // uploading the image to matrix + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`{"content_uri":"mxc://foo/bar"}`)), + }, nil + } + return nil, fmt.Errorf("Unknown URL: %s", req.URL.String()) + } + matrixCli, _ := gomatrix.NewClient("https://hyrule", "@googlebot:hyrule", "its_a_secret") + matrixCli.Client = &http.Client{Transport: matrixTrans} + + // Execute the matrix !command + cmds := google.Commands(matrixCli) + if len(cmds) != 2 { + t.Fatalf("Unexpected number of commands: %d", len(cmds)) + } + // cmd := cmds[0] + // _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) + // if err != nil { + // t.Fatalf("Failed to process command: %s", err.Error()) + // } +} From 823d3277c7eb660bc70555bfd817d9de041747a6 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 00:12:02 +0000 Subject: [PATCH 02/25] Handle image search results --- config.sample.yaml | 6 ++ src/github.com/matrix-org/go-neb/goneb.go | 1 + .../matrix-org/go-neb/services/imgur/imgur.go | 79 +++++++++++-------- .../go-neb/services/imgur/imgur_test.go | 36 ++++----- 4 files changed, 71 insertions(+), 51 deletions(-) diff --git a/config.sample.yaml b/config.sample.yaml index 04579ac..91186e4 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -81,6 +81,12 @@ services: api_key: "AIzaSyA4FD39m9" cx: "AIASDFWSRRtrtr" + - ID: "imgur_service" + Type: "imgur" + UserID: "@imgur:localhost" # requires a Syncing client + Config: + api_key: "AIzaSyA4FD39m9" + - ID: "rss_service" Type: "rssbot" UserID: "@another_goneb:localhost" diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index 8fa3599..ee7f2cc 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -24,6 +24,7 @@ import ( _ "github.com/matrix-org/go-neb/services/github" _ "github.com/matrix-org/go-neb/services/google" _ "github.com/matrix-org/go-neb/services/guggy" + _ "github.com/matrix-org/go-neb/services/imgur" _ "github.com/matrix-org/go-neb/services/jira" _ "github.com/matrix-org/go-neb/services/rssbot" _ "github.com/matrix-org/go-neb/services/slackapi" diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index d367b20..a9ce597 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -5,13 +5,13 @@ import ( "encoding/json" "fmt" "io/ioutil" - "math" "net/http" "net/url" "strings" log "github.com/Sirupsen/logrus" "github.com/matrix-org/go-neb/types" + "github.com/matrix-org/gomatrix" ) // ServiceType of the Imgur service @@ -108,12 +108,12 @@ type Service struct { } // Commands supported: -// !imgur image some_search_query_without_quotes +// !imgur some_search_query_without_quotes // Responds with a suitable image into the same room as the command. func (s *Service) Commands(client *gomatrix.Client) []types.Command { return []types.Command{ types.Command{ - Path: []string{"imgur", "image"}, + Path: []string{"imgur"}, Command: func(roomID, userID string, args []string) (interface{}, error) { return s.cmdImgurImgSearch(client, roomID, userID, args) }, @@ -130,7 +130,7 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command { // usageMessage returns a matrix TextMessage representation of the service usage func usageMessage() *gomatrix.TextMessage { return &gomatrix.TextMessage{"m.notice", - `Usage: !imgur image image_search_text`} + `Usage: !imgur image_search_text`} } func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { @@ -142,36 +142,49 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri // Get the query text to search for. querySentence := strings.Join(args, " ") - searchResult, err := s.text2imgImgur(querySentence) + searchResultImage, searchResultAlbum, err := s.text2imgImgur(querySentence) if err != nil { return nil, err } - var imgURL = searchResult.Link - if imgURL == "" { + // Image returned + if searchResultImage != nil { + var imgURL = searchResultImage.Link + if imgURL == "" { + return gomatrix.TextMessage{ + MsgType: "m.notice", + Body: "No image found!", + }, nil + } + + // FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400" + resUpload, err := client.UploadLink(imgURL) + if err != nil { + return nil, fmt.Errorf("Failed to upload Imgur image to matrix: %s", err.Error()) + } + + return gomatrix.ImageMessage{ + MsgType: "m.image", + Body: querySentence, + URL: resUpload.ContentURI, + Info: gomatrix.ImageInfo{ + Height: uint(searchResultImage.Height), + Width: uint(searchResultImage.Width), + Mimetype: searchResultImage.Type, + }, + }, nil + } else if searchResultAlbum != nil { + return gomatrix.TextMessage{ + MsgType: "m.notice", + Body: "Search returned an album - Not currently supported", + }, nil + } else { return gomatrix.TextMessage{ MsgType: "m.notice", Body: "No image found!", }, nil } - - // FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400" - resUpload, err := client.UploadLink(imgURL) - if err != nil { - return nil, fmt.Errorf("Failed to upload Imgur image to matrix: %s", err.Error()) - } - - return gomatrix.ImageMessage{ - MsgType: "m.image", - Body: querySentence, - URL: resUpload.ContentURI, - Info: gomatrix.ImageInfo{ - Height: uint(math.Floor(searchResult.Height)), - Width: uint(math.Floor(searchResult.Width)), - Mimetype: searchResult.Type, - }, - }, nil } // text2imgImgur returns info about an image or an album @@ -179,8 +192,8 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery log.Info("Searching Imgur for an image of a ", query) var base = "https://api.imgur.com/3/gallery/search/" - var sort = "time" // time | viral | top - var window = all // day | week | month | year | all + var sort = "time" // time | viral | top + var window = "all" // day | week | month | year | all var page = 1 var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page) @@ -201,33 +214,33 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery return nil, nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) } - var searchResults imgurSearchResults - if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || !searchResults.data { + var searchResults imgurSearchResponse + if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || !searchResults.Data { return nil, nil, fmt.Errorf("No images found - %s", err.Error()) } // Check if we have an image or a gallery var dataInt map[string]interface{} - if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || !searchResults.data { + if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || !searchResults.Data { return nil, nil, fmt.Errorf("Failed to parse response data - %s", err.Error()) } // Return an album if dataInt["is_album"].(bool) { var album imgurGalleryAlbum - if err := json.Unmarshal(searchResults.Data, &album); err != nill { + if err := json.Unmarshal(searchResults.Data, &album); err != nil { return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) } - return nil, album, nil + return nil, &album, nil } // Return an image var image imgurGalleryImage - if err := json.Unmarshal(searchResults.Data, &image); err != nill { + if err := json.Unmarshal(searchResults.Data, &image); err != nil { return nil, nil, fmt.Errorf("Failed to parse image data - %s", err.Error()) } - return image, nil, nil + return &image, nil, nil } // response2String returns a string representation of an HTTP response body diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index c4bccb5..53e86ab 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -20,16 +20,16 @@ import ( func TestCommand(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) apiKey := "secret" - googleImageURL := "https://www.googleapis.com/customsearch/v1" + imgurImageURL := "https://www.imgurapis.com/customsearch/v1" - // Mock the response from Google - googleTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { - googleURL := "https://www.googleapis.com/customsearch/v1" + // Mock the response from imgur + imgurTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { + imgurURL := "https://www.imgurapis.com/customsearch/v1" query := req.URL.Query() // Check the base API URL - if !strings.HasPrefix(req.URL.String(), googleURL) { - t.Fatalf("Bad URL: got %s want prefix %s", req.URL.String(), googleURL) + if !strings.HasPrefix(req.URL.String(), imgurURL) { + t.Fatalf("Bad URL: got %s want prefix %s", req.URL.String(), imgurURL) } // Check the request method if req.Method != "GET" { @@ -46,12 +46,12 @@ func TestCommand(t *testing.T) { t.Fatalf("Bad search string: got \"%s\" (%d characters) ", searchString, searchStringLength) } - resImage := googleImage{ + resImage := imgurImage{ Width: 64, Height: 64, } - res := googleSearchResult{ + res := imgurSearchResult{ Title: "A Cat", Link: "http://cat.com/cat.jpg", Mime: "image/jpeg", @@ -60,29 +60,29 @@ func TestCommand(t *testing.T) { b, err := json.Marshal(res) if err != nil { - t.Fatalf("Failed to marshal Google response - %s", err) + t.Fatalf("Failed to marshal imgur response - %s", err) } return &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBuffer(b)), }, nil }) - // clobber the Google service http client instance - httpClient = &http.Client{Transport: googleTrans} + // clobber the imgur service http client instance + httpClient = &http.Client{Transport: imgurTrans} - // Create the Google service - srv, err := types.CreateService("id", ServiceType, "@googlebot:hyrule", []byte( + // Create the imgur service + srv, err := types.CreateService("id", ServiceType, "@imgurbot:hyrule", []byte( `{"api_key":"`+apiKey+`"}`, )) if err != nil { - t.Fatal("Failed to create Google service: ", err) + t.Fatal("Failed to create imgur service: ", err) } - google := srv.(*Service) + imgur := srv.(*Service) // Mock the response from Matrix matrixTrans := struct{ testutils.MockTransport }{} matrixTrans.RT = func(req *http.Request) (*http.Response, error) { - if req.URL.String() == googleImageURL { // getting the Google image + if req.URL.String() == imgurImageURL { // getting the imgur image return &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString("some image data")), @@ -95,11 +95,11 @@ func TestCommand(t *testing.T) { } return nil, fmt.Errorf("Unknown URL: %s", req.URL.String()) } - matrixCli, _ := gomatrix.NewClient("https://hyrule", "@googlebot:hyrule", "its_a_secret") + matrixCli, _ := gomatrix.NewClient("https://hyrule", "@imgurbot:hyrule", "its_a_secret") matrixCli.Client = &http.Client{Transport: matrixTrans} // Execute the matrix !command - cmds := google.Commands(matrixCli) + cmds := imgur.Commands(matrixCli) if len(cmds) != 2 { t.Fatalf("Unexpected number of commands: %d", len(cmds)) } From f8e4bce075e09121d46e2c294c75332c9f056f33 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 10:01:59 +0000 Subject: [PATCH 03/25] Fix data object check --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index a9ce597..5d0aaa7 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -215,13 +215,13 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } var searchResults imgurSearchResponse - if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || !searchResults.Data { + if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || searchResults.Data == nil { return nil, nil, fmt.Errorf("No images found - %s", err.Error()) } // Check if we have an image or a gallery var dataInt map[string]interface{} - if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || !searchResults.Data { + if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || searchResults.Data == nil { return nil, nil, fmt.Errorf("Failed to parse response data - %s", err.Error()) } From 33c75423e0d1174dcd8bccbb490f570aa6d420a9 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 10:36:08 +0000 Subject: [PATCH 04/25] Add auth header --- .../matrix-org/go-neb/services/imgur/imgur.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 5d0aaa7..4c8cb60 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -101,10 +101,10 @@ type imgurSearchResponse struct { // } type Service struct { types.DefaultService - // The API key to use when making HTTP requests to Imgur. - ClientSecret string `json:"client_secret"` // The Imgur client ID ClientID string `json:"client_id"` + // The API key to use when making HTTP requests to Imgur. + ClientSecret string `json:"client_secret"` } // Commands supported: @@ -198,12 +198,18 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page) u, err := url.Parse(urlString) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return nil, nil, err } - res, err := http.Get(u.String()) + log.Printf("Client ID: %s", s.ClientID) + req.Header.Add("Authorization", "Client-ID "+s.ClientID) + res, err := httpClient.Do(req) if res != nil { defer res.Body.Close() } From 024a7844129242d8977240852ff046c1552d33f4 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 12:19:59 +0000 Subject: [PATCH 05/25] Fix data structure --- .../matrix-org/go-neb/services/imgur/imgur.go | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 4c8cb60..cbff5aa 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -87,9 +87,9 @@ type imgurGalleryAlbum struct { } type imgurSearchResponse struct { - Data json.RawMessage `json:"data"` - Success bool `json:"success"` // Request completed successfully - Status int `json:"status"` // HTTP response code + Data []json.RawMessage `json:"data"` + Success bool `json:"success"` // Request completed successfully + Status int `json:"status"` // HTTP response code } // Service contains the Config fields for the Imgur service. @@ -159,9 +159,10 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri } // FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400" + log.Printf("Uploading image at: %s", imgURL) resUpload, err := client.UploadLink(imgURL) if err != nil { - return nil, fmt.Errorf("Failed to upload Imgur image to matrix: %s", err.Error()) + return nil, fmt.Errorf("Failed to upload Imgur image (%s) to matrix: %s", imgURL, err.Error()) } return gomatrix.ImageMessage{ @@ -191,11 +192,12 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) { log.Info("Searching Imgur for an image of a ", query) - var base = "https://api.imgur.com/3/gallery/search/" + query = url.QueryEscape(query) + var base = "https://api.imgur.com/3/gallery/search" var sort = "time" // time | viral | top var window = "all" // day | week | month | year | all var page = 1 - var urlString = fmt.Sprintf("%s/%s/%s/%d", base, sort, window, page) + var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", base, sort, window, page, query) u, err := url.Parse(urlString) if err != nil { @@ -207,7 +209,6 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery return nil, nil, err } - log.Printf("Client ID: %s", s.ClientID) req.Header.Add("Authorization", "Client-ID "+s.ClientID) res, err := httpClient.Do(req) if res != nil { @@ -225,28 +226,26 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery return nil, nil, fmt.Errorf("No images found - %s", err.Error()) } - // Check if we have an image or a gallery - var dataInt map[string]interface{} - if err := json.Unmarshal(searchResults.Data, &dataInt); err != nil || searchResults.Data == nil { - return nil, nil, fmt.Errorf("Failed to parse response data - %s", err.Error()) + log.Printf("%d results were returned from Imgur", len(searchResults.Data)) + for i := 0; i < len(searchResults.Data); i++ { + // Return the first image result + var image imgurGalleryImage + if err := json.Unmarshal(searchResults.Data[i], &image); err == nil { + return &image, nil, nil + } } // Return an album - if dataInt["is_album"].(bool) { - var album imgurGalleryAlbum - if err := json.Unmarshal(searchResults.Data, &album); err != nil { - return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) - } + // if dataInt["is_album"].(bool) { + // var album imgurGalleryAlbum + // if err := json.Unmarshal(searchResults.Data, &album); err != nil { + // return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) + // } + // + // return nil, &album, nil + // } - return nil, &album, nil - } - - // Return an image - var image imgurGalleryImage - if err := json.Unmarshal(searchResults.Data, &image); err != nil { - return nil, nil, fmt.Errorf("Failed to parse image data - %s", err.Error()) - } - return &image, nil, nil + return nil, nil, fmt.Errorf("No images found") } // response2String returns a string representation of an HTTP response body From 8479782cad83cf697473b9f7fe848943f93ab1e6 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 15:33:36 +0000 Subject: [PATCH 06/25] Check that response is actually an image --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index cbff5aa..763cd2b 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -159,7 +159,7 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri } // FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400" - log.Printf("Uploading image at: %s", imgURL) + // log.Printf("Uploading image at: %s", imgURL) resUpload, err := client.UploadLink(imgURL) if err != nil { return nil, fmt.Errorf("Failed to upload Imgur image (%s) to matrix: %s", imgURL, err.Error()) @@ -198,6 +198,7 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery var window = "all" // day | week | month | year | all var page = 1 var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", base, sort, window, page, query) + // var urlString = fmt.Sprintf("%s?q=%s", base, query) u, err := url.Parse(urlString) if err != nil { @@ -230,7 +231,7 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery for i := 0; i < len(searchResults.Data); i++ { // Return the first image result var image imgurGalleryImage - if err := json.Unmarshal(searchResults.Data[i], &image); err == nil { + if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !image.IsAlbum { return &image, nil, nil } } From 77e5f692a279eb1b914d5e60b728928c006afad2 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 15:53:07 +0000 Subject: [PATCH 07/25] Return a random image result. Clean up some commented code --- .../matrix-org/go-neb/services/imgur/imgur.go | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 763cd2b..c8a4bfd 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/rand" "net/http" "net/url" "strings" @@ -228,23 +229,18 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } log.Printf("%d results were returned from Imgur", len(searchResults.Data)) + // Return a random image result + var images []imgurGalleryImage for i := 0; i < len(searchResults.Data); i++ { - // Return the first image result var image imgurGalleryImage if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !image.IsAlbum { - return &image, nil, nil + images = append(images, image) } } - - // Return an album - // if dataInt["is_album"].(bool) { - // var album imgurGalleryAlbum - // if err := json.Unmarshal(searchResults.Data, &album); err != nil { - // return nil, nil, fmt.Errorf("Failed to parse album data - %s", err.Error()) - // } - // - // return nil, &album, nil - // } + if len(images) > 0 { + r := rand.Intn(len(images) - 1) + return &images[r], nil, nil + } return nil, nil, fmt.Errorf("No images found") } From 0d2593b15228e6dcc93d1ad3860803992fbf46a8 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 16:10:04 +0000 Subject: [PATCH 08/25] Fix imgur bot tests --- .../go-neb/services/imgur/imgur_test.go | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index 53e86ab..ca0442f 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -15,16 +15,14 @@ import ( "github.com/matrix-org/gomatrix" ) -// TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make -// sure all cases are handled, rather than just the general case as is here. func TestCommand(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) - apiKey := "secret" - imgurImageURL := "https://www.imgurapis.com/customsearch/v1" + clientID := "My ID" + imgurImageURL := "https://api.imgur.com/3/gallery/search" // Mock the response from imgur imgurTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { - imgurURL := "https://www.imgurapis.com/customsearch/v1" + imgurURL := "https://api.imgur.com/3/gallery/search" query := req.URL.Query() // Check the base API URL @@ -35,26 +33,22 @@ func TestCommand(t *testing.T) { if req.Method != "GET" { t.Fatalf("Bad method: got %s want GET", req.Method) } - // Check the API key - if query.Get("key") != apiKey { - t.Fatalf("Bad apiKey: got %s want %s", query.Get("key"), apiKey) + // Check the Client ID + authHeader := req.Header.Get("Authorization") + if authHeader != "Client-ID "+clientID { + t.Fatalf("Bad client ID - Expected: %s, got %s", "Client-ID "+clientID, authHeader) } + // Check the search query var searchString = query.Get("q") - var searchStringLength = len(searchString) - if searchStringLength > 0 && !strings.HasPrefix(searchString, "image") { - t.Fatalf("Bad search string: got \"%s\" (%d characters) ", searchString, searchStringLength) - } - - resImage := imgurImage{ - Width: 64, - Height: 64, + if !strings.HasPrefix(searchString, "image") { + t.Fatalf("Bad search string: got \"%s\"", searchString) } - res := imgurSearchResult{ + res := imgurGalleryImage{ Title: "A Cat", - Link: "http://cat.com/cat.jpg", - Mime: "image/jpeg", + Link: "http://i.imgur.com/cat.jpg", + Type: "image/jpeg", Image: resImage, } @@ -72,7 +66,9 @@ func TestCommand(t *testing.T) { // Create the imgur service srv, err := types.CreateService("id", ServiceType, "@imgurbot:hyrule", []byte( - `{"api_key":"`+apiKey+`"}`, + `{ + "client_id":"`+clientID+`" + }`, )) if err != nil { t.Fatal("Failed to create imgur service: ", err) @@ -103,9 +99,9 @@ func TestCommand(t *testing.T) { if len(cmds) != 2 { t.Fatalf("Unexpected number of commands: %d", len(cmds)) } - // cmd := cmds[0] - // _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) - // if err != nil { - // t.Fatalf("Failed to process command: %s", err.Error()) - // } + cmd := cmds[0] + _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) + if err != nil { + t.Fatalf("Failed to process command: %s", err.Error()) + } } From 80e1637238b84e29faba4bc1f402731387fff93a Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 16:20:24 +0000 Subject: [PATCH 09/25] Fix struct initialisation --- src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index ca0442f..6fefae2 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -49,7 +49,6 @@ func TestCommand(t *testing.T) { Title: "A Cat", Link: "http://i.imgur.com/cat.jpg", Type: "image/jpeg", - Image: resImage, } b, err := json.Marshal(res) From d51c28cf8bae7bdb5dc6c61c6eac3d6f9f40f497 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 16:57:50 +0000 Subject: [PATCH 10/25] Fix tests and underlying bugs --- .../matrix-org/go-neb/services/imgur/imgur.go | 17 +++++++---- .../go-neb/services/imgur/imgur_test.go | 29 ++++++++++++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index c8a4bfd..913718f 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -114,15 +114,15 @@ type Service struct { func (s *Service) Commands(client *gomatrix.Client) []types.Command { return []types.Command{ types.Command{ - Path: []string{"imgur"}, + Path: []string{"imgur", "help"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - return s.cmdImgurImgSearch(client, roomID, userID, args) + return usageMessage(), nil }, }, types.Command{ - Path: []string{"imgur", "help"}, + Path: []string{"imgur"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - return usageMessage(), nil + return s.cmdImgurImgSearch(client, roomID, userID, args) }, }, } @@ -224,8 +224,10 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } var searchResults imgurSearchResponse - if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || searchResults.Data == nil { + if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil { return nil, nil, fmt.Errorf("No images found - %s", err.Error()) + } else if len(searchResults.Data) < 1 { + return nil, nil, fmt.Errorf("No images found") } log.Printf("%d results were returned from Imgur", len(searchResults.Data)) @@ -238,7 +240,10 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } } if len(images) > 0 { - r := rand.Intn(len(images) - 1) + var r = 0 + if len(images) > 1 { + r = rand.Intn(len(images) - 1) + } return &images[r], nil, nil } diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index 6fefae2..14a1f4a 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -18,7 +18,8 @@ import ( func TestCommand(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) clientID := "My ID" - imgurImageURL := "https://api.imgur.com/3/gallery/search" + imgurImageURL := "http://i.imgur.com/cat.jpg" + testSearchString := "Czechoslovakian bananna" // Mock the response from imgur imgurTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { @@ -41,16 +42,30 @@ func TestCommand(t *testing.T) { // Check the search query var searchString = query.Get("q") - if !strings.HasPrefix(searchString, "image") { - t.Fatalf("Bad search string: got \"%s\"", searchString) + if searchString != testSearchString { + t.Fatalf("Bad search string - got: \"%s\", expected: \"%s\"", testSearchString, searchString) } - res := imgurGalleryImage{ + img := imgurGalleryImage{ Title: "A Cat", - Link: "http://i.imgur.com/cat.jpg", + Link: imgurImageURL, Type: "image/jpeg", } + imgJSON, err := json.Marshal(img) + if err != nil { + t.Fatalf("Failed to Marshal test image data - %s", err) + } + rawImageJSON := json.RawMessage(imgJSON) + + res := imgurSearchResponse{ + Data: []json.RawMessage{ + rawImageJSON, + }, + Success: true, + Status: 200, + } + b, err := json.Marshal(res) if err != nil { t.Fatalf("Failed to marshal imgur response - %s", err) @@ -98,8 +113,8 @@ func TestCommand(t *testing.T) { if len(cmds) != 2 { t.Fatalf("Unexpected number of commands: %d", len(cmds)) } - cmd := cmds[0] - _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) + cmd := cmds[1] + _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{testSearchString}) if err != nil { t.Fatalf("Failed to process command: %s", err.Error()) } From f1da4265459d3408ce66b4a84fb9ca644b30bd49 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 17:28:32 +0000 Subject: [PATCH 11/25] Fix google bot tests --- .../go-neb/services/google/google_test.go | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/google/google_test.go b/src/github.com/matrix-org/go-neb/services/google/google_test.go index c069cc2..d3a5cf7 100644 --- a/src/github.com/matrix-org/go-neb/services/google/google_test.go +++ b/src/github.com/matrix-org/go-neb/services/google/google_test.go @@ -20,7 +20,7 @@ import ( func TestCommand(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) apiKey := "secret" - googleImageURL := "https://www.googleapis.com/customsearch/v1" + googleImageURL := "http://cat.com/cat.jpg" // Mock the response from Google googleTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { @@ -51,13 +51,19 @@ func TestCommand(t *testing.T) { Height: 64, } - res := googleSearchResult{ + image := googleSearchResult{ Title: "A Cat", - Link: "http://cat.com/cat.jpg", + Link: googleImageURL, Mime: "image/jpeg", Image: resImage, } + res := googleSearchResults{ + Items: []googleSearchResult{ + image, + }, + } + b, err := json.Marshal(res) if err != nil { t.Fatalf("Failed to marshal Google response - %s", err) @@ -103,9 +109,9 @@ func TestCommand(t *testing.T) { if len(cmds) != 3 { t.Fatalf("Unexpected number of commands: %d", len(cmds)) } - // cmd := cmds[0] - // _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) - // if err != nil { - // t.Fatalf("Failed to process command: %s", err.Error()) - // } + cmd := cmds[0] + _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) + if err != nil { + t.Fatalf("Failed to process command: %s", err.Error()) + } } From c30a9f87b3c6f35fb81b5e29ee551296c872937f Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 18:11:42 +0000 Subject: [PATCH 12/25] Fix response code check --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 913718f..1da41d3 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -219,7 +219,7 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery if err != nil { return nil, nil, err } - if res.StatusCode > 200 { + if res.StatusCode < 200 || res.StatusCode >= 300 { return nil, nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) } From aa3454f2c86fdcfb371cca75794ed6c54042b5c8 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 18:12:46 +0000 Subject: [PATCH 13/25] Inline redundant variable --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 1da41d3..4019aca 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -194,11 +194,11 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery log.Info("Searching Imgur for an image of a ", query) query = url.QueryEscape(query) - var base = "https://api.imgur.com/3/gallery/search" + var sort = "time" // time | viral | top var window = "all" // day | week | month | year | all var page = 1 - var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", base, sort, window, page, query) + var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", "https://api.imgur.com/3/gallery/search", sort, window, page, query) // var urlString = fmt.Sprintf("%s?q=%s", base, query) u, err := url.Parse(urlString) From c211f4e4c1a14f2aa782c9094d40440d15f34925 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 18:14:24 +0000 Subject: [PATCH 14/25] Removed old comment -- error was due to non image file type (HTML) --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 4019aca..886e116 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -159,8 +159,6 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri }, nil } - // FIXME -- Sometimes upload fails with a cryptic error - "msg=Upload request failed code=400" - // log.Printf("Uploading image at: %s", imgURL) resUpload, err := client.UploadLink(imgURL) if err != nil { return nil, fmt.Errorf("Failed to upload Imgur image (%s) to matrix: %s", imgURL, err.Error()) From a71efc96f0bd239ca85c4d3afa4b66a4696a7a6b Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 18:18:33 +0000 Subject: [PATCH 15/25] Improve function names --- .../matrix-org/go-neb/services/imgur/imgur.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 886e116..c71b30e 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -122,7 +122,7 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command { types.Command{ Path: []string{"imgur"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - return s.cmdImgurImgSearch(client, roomID, userID, args) + return s.cmdImgSearch(client, roomID, userID, args) }, }, } @@ -134,7 +134,7 @@ func usageMessage() *gomatrix.TextMessage { `Usage: !imgur image_search_text`} } -func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { +func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { if len(args) < 1 { return usageMessage(), nil @@ -143,7 +143,7 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri // Get the query text to search for. querySentence := strings.Join(args, " ") - searchResultImage, searchResultAlbum, err := s.text2imgImgur(querySentence) + searchResultImage, searchResultAlbum, err := s.text2img(querySentence) if err != nil { return nil, err @@ -187,8 +187,8 @@ func (s *Service) cmdImgurImgSearch(client *gomatrix.Client, roomID, userID stri } } -// text2imgImgur returns info about an image or an album -func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) { +// text2img returns info about an image or an album +func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) { log.Info("Searching Imgur for an image of a ", query) query = url.QueryEscape(query) From 4e461c16e70250eda31a5ba881f15abe34fea250 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 18:20:17 +0000 Subject: [PATCH 16/25] Fix typo --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index c71b30e..b6af10e 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -40,7 +40,7 @@ type imgurGalleryImage struct { Topic string `json:"topic"` // topic string Topic of the gallery image. Section string `json:"section"` // section string If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc) IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not - // ** Uninplemented fields ** + // ** Unimplemented fields ** // bandwidth integer Bandwidth consumed by the image in bytes // deletehash string OPTIONAL, the deletehash, if you're logged in as the image owner // comment_count int Number of comments on the gallery image. From 3f813040763dc1fde26bed24a79c57b241394f20 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 18:24:17 +0000 Subject: [PATCH 17/25] Removed redundant field name and type from implemented field comments --- .../matrix-org/go-neb/services/imgur/imgur.go | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index b6af10e..2d5da05 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -21,25 +21,25 @@ const ServiceType = "imgur" var httpClient = &http.Client{} type imgurGalleryImage struct { - ID string `json:"id"` // id string The ID for the image - Title string `json:"title"` // title string The title of the image. - Description string `json:"description"` // description string Description of the image. - DateTime int64 `json:"datetime"` // datetime integer Time inserted into the gallery, epoch time - Type string `json:"type"` // type string Image MIME type. - Animated bool `json:"animated"` // animated boolean is the image animated - Width int `json:"width"` // width integer The width of the image in pixels - Height int `json:"height"` // height integer The height of the image in pixels - Size int64 `json:"size"` // size integer The size of the image in bytes - Views int64 `json:"views"` // views integer The number of image views - Link string `json:"link"` // link string The direct link to the the image. (Note: if fetching an animated GIF that was over 20MB in original size, a .gif thumbnail will be returned) - Gifv string `json:"gifv"` // gifv string OPTIONAL, The .gifv link. Only available if the image is animated and type is 'image/gif'. - MP4 string `json:"mp4"` // mp4 string OPTIONAL, The direct link to the .mp4. Only available if the image is animated and type is 'image/gif'. - MP4Size int64 `json:"mp4_size"` // mp4_size integer OPTIONAL, The Content-Length of the .mp4. Only available if the image is animated and type is 'image/gif'. Note that a zero value (0) is possible if the video has not yet been generated - Looping bool `json:"looping"` // looping boolean OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'. - NSFW bool `json:"nsfw"` // nsfw boolean Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available. - Topic string `json:"topic"` // topic string Topic of the gallery image. - Section string `json:"section"` // section string If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc) - IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not + ID string `json:"id"` // The ID for the image + Title string `json:"title"` // The title of the image. + Description string `json:"description"` // Description of the image. + DateTime int64 `json:"datetime"` // Time inserted into the gallery, epoch time + Type string `json:"type"` // Image MIME type. + Animated bool `json:"animated"` // Is the image animated + Width int `json:"width"` // The width of the image in pixels + Height int `json:"height"` // The height of the image in pixels + Size int64 `json:"size"` // The size of the image in bytes + Views int64 `json:"views"` // The number of image views + Link string `json:"link"` // The direct link to the the image. (Note: if fetching an animated GIF that was over 20MB in original size, a .gif thumbnail will be returned) + Gifv string `json:"gifv"` // OPTIONAL, The .gifv link. Only available if the image is animated and type is 'image/gif'. + MP4 string `json:"mp4"` // OPTIONAL, The direct link to the .mp4. Only available if the image is animated and type is 'image/gif'. + MP4Size int64 `json:"mp4_size"` // OPTIONAL, The Content-Length of the .mp4. Only available if the image is animated and type is 'image/gif'. Note that a zero value (0) is possible if the video has not yet been generated + Looping bool `json:"looping"` // OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'. + NSFW bool `json:"nsfw"` // Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available. + Topic string `json:"topic"` // Topic of the gallery image. + Section string `json:"section"` // If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc) + IsAlbum bool `json:"is_album"` // If it's an album or not // ** Unimplemented fields ** // bandwidth integer Bandwidth consumed by the image in bytes // deletehash string OPTIONAL, the deletehash, if you're logged in as the image owner @@ -56,20 +56,20 @@ type imgurGalleryImage struct { } type imgurGalleryAlbum struct { - ID string `json:"id"` // id string The ID for the album - Title string `json:"title"` // title string The title of the album. - Description string `json:"description"` // description string Description of the album. - DateTime int64 `json:"datetime"` // datetime integer Time inserted into the gallery, epoch time - Views int64 `json:"views"` // views integer The number of album views - Link string `json:"link"` // link string The URL link to the album - NSFW bool `json:"nsfw"` // nsfw boolean Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available. - Topic string `json:"topic"` // topic string Topic of the gallery album. - IsAlbum bool `json:"is_album"` // is_album boolean If it's an album or not - Cover string `json:"cover"` // cover string The ID of the album cover image - CoverWidth int `json:"cover_width"` // cover_width integer The width, in pixels, of the album cover image - CoverHeight int `json:"cover_height"` // cover_height integer The height, in pixels, of the album cover image - ImagesCount int `json:"images_count"` // images_count integer The total number of images in the album - Images []imgurGalleryImage `json:"images"` // images Array of Images An array of all the images in the album (only available when requesting the direct album) + ID string `json:"id"` // The ID for the album + Title string `json:"title"` // The title of the album. + Description string `json:"description"` // Description of the album. + DateTime int64 `json:"datetime"` // Time inserted into the gallery, epoch time + Views int64 `json:"views"` // The number of album views + Link string `json:"link"` // The URL link to the album + NSFW bool `json:"nsfw"` // Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available. + Topic string `json:"topic"` // Topic of the gallery album. + IsAlbum bool `json:"is_album"` // If it's an album or not + Cover string `json:"cover"` // The ID of the album cover image + CoverWidth int `json:"cover_width"` // The width, in pixels, of the album cover image + CoverHeight int `json:"cover_height"` // The height, in pixels, of the album cover image + ImagesCount int `json:"images_count"` // The total number of images in the album + Images []imgurGalleryImage `json:"images"` // An array of all the images in the album (only available when requesting the direct album) // ** Unimplemented fields ** // account_url string The account username or null if it's anonymous. From 592b7830e3e0c824abcfd9dcd81ff077e4e2a73f Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 10:15:13 +0000 Subject: [PATCH 18/25] Refactor to reduce cyclomatic complexity --- .../matrix-org/go-neb/services/imgur/imgur.go | 70 +++++++++++-------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 2d5da05..04a075c 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -190,39 +190,14 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a // text2img returns info about an image or an album func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) { log.Info("Searching Imgur for an image of a ", query) - - query = url.QueryEscape(query) - - var sort = "time" // time | viral | top - var window = "all" // day | week | month | year | all - var page = 1 - var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", "https://api.imgur.com/3/gallery/search", sort, window, page, query) - // var urlString = fmt.Sprintf("%s?q=%s", base, query) - - u, err := url.Parse(urlString) - if err != nil { - return nil, nil, err - } - - req, err := http.NewRequest("GET", u.String(), nil) + bytes, err := queryImgur(query, s) if err != nil { return nil, nil, err } - req.Header.Add("Authorization", "Client-ID "+s.ClientID) - res, err := httpClient.Do(req) - if res != nil { - defer res.Body.Close() - } - if err != nil { - return nil, nil, err - } - if res.StatusCode < 200 || res.StatusCode >= 300 { - return nil, nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) - } - var searchResults imgurSearchResponse - if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil { + // if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil { + if err := json.Unmarshal(bytes, &searchResults); err != nil { return nil, nil, fmt.Errorf("No images found - %s", err.Error()) } else if len(searchResults.Data) < 1 { return nil, nil, fmt.Errorf("No images found") @@ -248,6 +223,45 @@ func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum return nil, nil, fmt.Errorf("No images found") } +// Query imgur and return HTTP response or error +func queryImgur(query string, s *Service) ([]byte, error) { + query = url.QueryEscape(query) + + var sort = "time" // time | viral | top + var window = "all" // day | week | month | year | all + var page = 1 + var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", "https://api.imgur.com/3/gallery/search", sort, window, page, query) + // var urlString = fmt.Sprintf("%s?q=%s", base, query) + + u, err := url.Parse(urlString) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return nil, err + } + + req.Header.Add("Authorization", "Client-ID "+s.ClientID) + res, err := httpClient.Do(req) + if res != nil { + defer res.Body.Close() + } + if err != nil { + return nil, err + } + if res.StatusCode < 200 || res.StatusCode >= 300 { + return nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) + } + + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + return bytes, nil +} + // response2String returns a string representation of an HTTP response body func response2String(res *http.Response) string { bs, err := ioutil.ReadAll(res.Body) From 84235d07942f59e71b9791ac4d379a6a738f8c8d Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 11:30:53 +0000 Subject: [PATCH 19/25] Inline base URL --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 04a075c..ff1fd2c 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -230,7 +230,7 @@ func queryImgur(query string, s *Service) ([]byte, error) { var sort = "time" // time | viral | top var window = "all" // day | week | month | year | all var page = 1 - var urlString = fmt.Sprintf("%s/%s/%s/%d?q=%s", "https://api.imgur.com/3/gallery/search", sort, window, page, query) + var urlString = fmt.Sprintf("https://api.imgur.com/3/gallery/search/%s/%s/%d?q=%s", sort, window, page, query) // var urlString = fmt.Sprintf("%s?q=%s", base, query) u, err := url.Parse(urlString) From d28156c99aa0584cdfddbd51cca8e9b61cb81030 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 11:44:21 +0000 Subject: [PATCH 20/25] Pass clientID directly --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index ff1fd2c..5732dba 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -190,7 +190,7 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a // text2img returns info about an image or an album func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum, error) { log.Info("Searching Imgur for an image of a ", query) - bytes, err := queryImgur(query, s) + bytes, err := queryImgur(query, s.ClientID) if err != nil { return nil, nil, err } @@ -224,7 +224,7 @@ func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum } // Query imgur and return HTTP response or error -func queryImgur(query string, s *Service) ([]byte, error) { +func queryImgur(query, clientID string) ([]byte, error) { query = url.QueryEscape(query) var sort = "time" // time | viral | top @@ -243,7 +243,7 @@ func queryImgur(query string, s *Service) ([]byte, error) { return nil, err } - req.Header.Add("Authorization", "Client-ID "+s.ClientID) + req.Header.Add("Authorization", "Client-ID "+clientID) res, err := httpClient.Do(req) if res != nil { defer res.Body.Close() From 790e6586b4a67900ae26096d9c9f0556ccd8e907 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 11:57:25 +0000 Subject: [PATCH 21/25] Improve string formatting --- .../matrix-org/go-neb/services/imgur/imgur_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index 14a1f4a..9103199 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -80,9 +80,9 @@ func TestCommand(t *testing.T) { // Create the imgur service srv, err := types.CreateService("id", ServiceType, "@imgurbot:hyrule", []byte( - `{ - "client_id":"`+clientID+`" - }`, + fmt.Sprintf(`{ + "client_id":"%s" + }`, clientID), )) if err != nil { t.Fatal("Failed to create imgur service: ", err) From 1ced308b56bb580d7115043f84c7815323e5505a Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 12:55:03 +0000 Subject: [PATCH 22/25] Use boolean references --- .../matrix-org/go-neb/services/imgur/imgur.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 5732dba..009c26c 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -26,7 +26,7 @@ type imgurGalleryImage struct { Description string `json:"description"` // Description of the image. DateTime int64 `json:"datetime"` // Time inserted into the gallery, epoch time Type string `json:"type"` // Image MIME type. - Animated bool `json:"animated"` // Is the image animated + Animated *bool `json:"animated"` // Is the image animated Width int `json:"width"` // The width of the image in pixels Height int `json:"height"` // The height of the image in pixels Size int64 `json:"size"` // The size of the image in bytes @@ -35,11 +35,11 @@ type imgurGalleryImage struct { Gifv string `json:"gifv"` // OPTIONAL, The .gifv link. Only available if the image is animated and type is 'image/gif'. MP4 string `json:"mp4"` // OPTIONAL, The direct link to the .mp4. Only available if the image is animated and type is 'image/gif'. MP4Size int64 `json:"mp4_size"` // OPTIONAL, The Content-Length of the .mp4. Only available if the image is animated and type is 'image/gif'. Note that a zero value (0) is possible if the video has not yet been generated - Looping bool `json:"looping"` // OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'. - NSFW bool `json:"nsfw"` // Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available. + Looping *bool `json:"looping"` // OPTIONAL, Whether the image has a looping animation. Only available if the image is animated and type is 'image/gif'. + NSFW *bool `json:"nsfw"` // Indicates if the image has been marked as nsfw or not. Defaults to null if information is not available. Topic string `json:"topic"` // Topic of the gallery image. Section string `json:"section"` // If the image has been categorized by our backend then this will contain the section the image belongs in. (funny, cats, adviceanimals, wtf, etc) - IsAlbum bool `json:"is_album"` // If it's an album or not + IsAlbum *bool `json:"is_album"` // If it's an album or not // ** Unimplemented fields ** // bandwidth integer Bandwidth consumed by the image in bytes // deletehash string OPTIONAL, the deletehash, if you're logged in as the image owner @@ -62,9 +62,9 @@ type imgurGalleryAlbum struct { DateTime int64 `json:"datetime"` // Time inserted into the gallery, epoch time Views int64 `json:"views"` // The number of album views Link string `json:"link"` // The URL link to the album - NSFW bool `json:"nsfw"` // Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available. + NSFW *bool `json:"nsfw"` // Indicates if the album has been marked as nsfw or not. Defaults to null if information is not available. Topic string `json:"topic"` // Topic of the gallery album. - IsAlbum bool `json:"is_album"` // If it's an album or not + IsAlbum *bool `json:"is_album"` // If it's an album or not Cover string `json:"cover"` // The ID of the album cover image CoverWidth int `json:"cover_width"` // The width, in pixels, of the album cover image CoverHeight int `json:"cover_height"` // The height, in pixels, of the album cover image @@ -89,7 +89,7 @@ type imgurGalleryAlbum struct { type imgurSearchResponse struct { Data []json.RawMessage `json:"data"` - Success bool `json:"success"` // Request completed successfully + Success *bool `json:"success"` // Request completed successfully Status int `json:"status"` // HTTP response code } @@ -208,7 +208,7 @@ func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum var images []imgurGalleryImage for i := 0; i < len(searchResults.Data); i++ { var image imgurGalleryImage - if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !image.IsAlbum { + if err := json.Unmarshal(searchResults.Data[i], &image); err == nil && !*(image.IsAlbum) { images = append(images, image) } } From 808bc2f3adcad17e4f3bf505868bc412f6a6b52c Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 12:59:23 +0000 Subject: [PATCH 23/25] Add a comment to explain temporary storage as RawMessage objects --- src/github.com/matrix-org/go-neb/services/imgur/imgur.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 009c26c..352538b 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -88,7 +88,7 @@ type imgurGalleryAlbum struct { } type imgurSearchResponse struct { - Data []json.RawMessage `json:"data"` + Data []json.RawMessage `json:"data"` // Data temporarily stored as RawMessage objects, as it can contain a mix of imgurGalleryImage and imgurGalleryAlbum objects Success *bool `json:"success"` // Request completed successfully Status int `json:"status"` // HTTP response code } From fef5fb5debb50b06ae4891eee8c1a9f52311542a Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 13:13:02 +0000 Subject: [PATCH 24/25] Clean up code style --- .../matrix-org/go-neb/services/imgur/imgur.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index 352538b..860b10a 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -20,6 +20,7 @@ const ServiceType = "imgur" var httpClient = &http.Client{} +// Represents an Imgur Gallery Image type imgurGalleryImage struct { ID string `json:"id"` // The ID for the image Title string `json:"title"` // The title of the image. @@ -55,6 +56,7 @@ type imgurGalleryImage struct { // score integer Imgur popularity score } +// Represents an Imgur gallery album type imgurGalleryAlbum struct { ID string `json:"id"` // The ID for the album Title string `json:"title"` // The title of the album. @@ -87,6 +89,7 @@ type imgurGalleryAlbum struct { // topic_id integer Topic ID of the gallery album. } +// Imgur gallery search response type imgurSearchResponse struct { Data []json.RawMessage `json:"data"` // Data temporarily stored as RawMessage objects, as it can contain a mix of imgurGalleryImage and imgurGalleryAlbum objects Success *bool `json:"success"` // Request completed successfully @@ -134,17 +137,16 @@ func usageMessage() *gomatrix.TextMessage { `Usage: !imgur image_search_text`} } +// Search Imgur for a relevant image and upload it to matrix func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { - + // Check for query text if len(args) < 1 { return usageMessage(), nil } - // Get the query text to search for. + // Perform search querySentence := strings.Join(args, " ") - searchResultImage, searchResultAlbum, err := s.text2img(querySentence) - if err != nil { return nil, err } @@ -159,11 +161,13 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a }, nil } + // Upload image resUpload, err := client.UploadLink(imgURL) if err != nil { return nil, fmt.Errorf("Failed to upload Imgur image (%s) to matrix: %s", imgURL, err.Error()) } + // Return image message return gomatrix.ImageMessage{ MsgType: "m.image", Body: querySentence, @@ -227,11 +231,11 @@ func (s *Service) text2img(query string) (*imgurGalleryImage, *imgurGalleryAlbum func queryImgur(query, clientID string) ([]byte, error) { query = url.QueryEscape(query) + // Build the query URL var sort = "time" // time | viral | top var window = "all" // day | week | month | year | all var page = 1 var urlString = fmt.Sprintf("https://api.imgur.com/3/gallery/search/%s/%s/%d?q=%s", sort, window, page, query) - // var urlString = fmt.Sprintf("%s?q=%s", base, query) u, err := url.Parse(urlString) if err != nil { @@ -243,6 +247,7 @@ func queryImgur(query, clientID string) ([]byte, error) { return nil, err } + // Add authorisation header req.Header.Add("Authorization", "Client-ID "+clientID) res, err := httpClient.Do(req) if res != nil { @@ -255,6 +260,7 @@ func queryImgur(query, clientID string) ([]byte, error) { return nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) } + // Read and return response body bytes, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err From 74751cb2194bebacdc4feb646e2dac0bf997305f Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 21 Feb 2017 19:50:09 +0000 Subject: [PATCH 25/25] Initialise struct literals with boolean pointers, using anonymous functions --- .../matrix-org/go-neb/services/imgur/imgur_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index 9103199..2ebe548 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -47,9 +47,10 @@ func TestCommand(t *testing.T) { } img := imgurGalleryImage{ - Title: "A Cat", - Link: imgurImageURL, - Type: "image/jpeg", + Title: "A Cat", + Link: imgurImageURL, + Type: "image/jpeg", + IsAlbum: func() *bool { b := false; return &b }(), } imgJSON, err := json.Marshal(img) @@ -62,7 +63,7 @@ func TestCommand(t *testing.T) { Data: []json.RawMessage{ rawImageJSON, }, - Success: true, + Success: func() *bool { b := true; return &b }(), Status: 200, }