timelines.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 16 Apr 2017 02:12:38 +0200
changeset 127 96a7f2432d27
parent 120 579912e9d0ef
child 130 c450bb73f59a
permissions -rw-r--r--
GetTimelines: Allow '#' as hashtag prefix

package gondole

import (
	"fmt"
	"strings"

	"github.com/sendgrid/rest"
)

// GetTimelines returns a timeline (a list of statuses
// timeline can be "home", "public", or a hashtag (use ":hashtag" or "#hashtag")
// For the public timelines, you can set 'local' to true to get only the
// local instance.
func (g *Client) GetTimelines(timeline string, local bool) ([]Status, error) {
	var endPoint string

	switch {
	case timeline == "home", timeline == "public":
		endPoint = "timelines/" + timeline
	case strings.HasPrefix(timeline, ":"), strings.HasPrefix(timeline, "#"):
		hashtag := timeline[1:]
		if hashtag == "" {
			return nil, fmt.Errorf("timelines API: empty hashtag")
		}
		endPoint = "timelines/tag/" + hashtag
	default:
		return nil, fmt.Errorf("GetTimelines: bad timelines argument")
	}

	params := make(apiCallParams)
	if timeline == "public" && local {
		params["local"] = "true"
	}

	var tl []Status
	if err := g.apiCall(endPoint, rest.Get, params, &tl); err != nil {
		return nil, err
	}
	return tl, nil
}