search.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 06 Sep 2018 01:07:40 +0200
changeset 238 1c0042e76902
parent 236 5b87cc73ed97
child 240 80c81e9b77b4
permissions -rw-r--r--
Do not use a global API version Mastodon uses an API version per endpoint (cf. search v2) so we need to be able to specify the version for any API call.

/*
Copyright 2017-2018 Mikael Berthe

Licensed under the MIT license.  Please see the LICENSE file is this directory.
*/

package madon

import (
	"github.com/sendgrid/rest"
)

// Search search for contents (accounts or statuses) and returns a Results
func (mc *Client) Search(query string, resolve bool) (*Results, error) {
	if query == "" {
		return nil, ErrInvalidParameter
	}

	params := make(apiCallParams)
	params["q"] = query
	if resolve {
		params["resolve"] = "true"
	}

	var resultsV1 struct {
		Results
		Hashtags []string `json:"hashtags"`
	}
	if err := mc.apiCall("v1/"+"search", rest.Get, params, nil, nil, &resultsV1); err != nil {
		return nil, err
	}

	var results Results
	results.Accounts = resultsV1.Accounts
	results.Statuses = resultsV1.Statuses
	for _, t := range resultsV1.Hashtags {
		results.Hashtags = append(results.Hashtags, Tag{Name: t})
	}

	return &results, nil
}