status.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 10 May 2017 20:12:26 +0200
changeset 179 fbe21b4aabda
parent 162 68df3a01e1a7
child 188 5f4210c4921a
permissions -rw-r--r--
Version 1.5.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
     7
package madon
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     8
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     9
import (
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    10
	"fmt"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    11
	"strconv"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    12
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 161
diff changeset
    13
	"github.com/pkg/errors"
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    14
	"github.com/sendgrid/rest"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    15
)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    16
108
3f21113728f4 Fix typo in comment
Mikael Berthe <mikael@lilotux.net>
parents: 106
diff changeset
    17
// updateStatusOptions contains option fields for POST and DELETE API calls
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    18
type updateStatusOptions struct {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    19
	// The ID is used for most commands
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    20
	ID int64
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    21
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    22
	// The following fields are used for posting a new status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    23
	Status      string
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    24
	InReplyToID int64
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    25
	MediaIDs    []int64
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    26
	Sensitive   bool
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    27
	SpoilerText string
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    28
	Visibility  string // "direct", "private", "unlisted" or "public"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    29
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    30
160
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    31
// getMultipleStatuses returns a list of status entities
161
6786f169b59f Refactor getMultipleAccounts
Mikael Berthe <mikael@lilotux.net>
parents: 160
diff changeset
    32
// If lopt.All is true, several requests will be made until the API server
160
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    33
// has nothing to return.
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    34
func (mc *Client) getMultipleStatuses(endPoint string, params apiCallParams, lopt *LimitParams) ([]Status, error) {
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    35
	var statuses []Status
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    36
	var links apiLinks
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    37
	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &statuses); err != nil {
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    38
		return nil, err
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    39
	}
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    40
	if lopt != nil { // Fetch more pages to reach our limit
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    41
		var statusSlice []Status
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    42
		for (lopt.All || lopt.Limit > len(statuses)) && links.next != nil {
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    43
			newlopt := links.next
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    44
			links = apiLinks{}
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    45
			if err := mc.apiCall(endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil {
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    46
				return nil, err
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    47
			}
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    48
			statuses = append(statuses, statusSlice...)
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    49
			statusSlice = statusSlice[:0] // Clear struct
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    50
		}
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    51
	}
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    52
	return statuses, nil
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    53
}
9f7e683b323f Refactor methods returning a list of statuses
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    54
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    55
// queryStatusData queries the statuses API
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    56
// The operation 'op' can be empty or "status" (the status itself), "context",
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    57
// "card", "reblogged_by", "favourited_by".
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    58
// The data argument will receive the object(s) returned by the API server.
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    59
func (mc *Client) queryStatusData(statusID int64, op string, data interface{}) error {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    60
	if statusID < 1 {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    61
		return ErrInvalidID
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    62
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    63
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    64
	endPoint := "statuses/" + strconv.FormatInt(statusID, 10)
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    65
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    66
	if op != "" && op != "status" {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    67
		switch op {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    68
		case "context", "card", "reblogged_by", "favourited_by":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    69
		default:
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    70
			return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    71
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    72
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    73
		endPoint += "/" + op
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    74
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    75
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    76
	return mc.apiCall(endPoint, rest.Get, nil, nil, nil, data)
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    77
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    78
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    79
// updateStatusData updates the statuses
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    80
// The operation 'op' can be empty or "status" (to post a status), "delete"
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    81
// (for deleting a status), "reblog", "unreblog", "favourite", "unfavourite".
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    82
// The data argument will receive the object(s) returned by the API server.
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
    83
func (mc *Client) updateStatusData(op string, opts updateStatusOptions, data interface{}) error {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    84
	method := rest.Post
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    85
	endPoint := "statuses"
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    86
	params := make(apiCallParams)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    87
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    88
	switch op {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    89
	case "", "status":
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
    90
		op = "status"
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    91
		if opts.Status == "" {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    92
			return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    93
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    94
		switch opts.Visibility {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    95
		case "", "direct", "private", "unlisted", "public":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    96
			// Okay
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    97
		default:
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    98
			return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    99
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   100
		if len(opts.MediaIDs) > 4 {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 161
diff changeset
   101
			return errors.New("too many (>4) media IDs")
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   102
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   103
	case "delete":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   104
		method = rest.Delete
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   105
		if opts.ID < 1 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   106
			return ErrInvalidID
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   107
		}
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   108
		endPoint += "/" + strconv.FormatInt(opts.ID, 10)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   109
	case "reblog", "unreblog", "favourite", "unfavourite":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   110
		if opts.ID < 1 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   111
			return ErrInvalidID
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   112
		}
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   113
		endPoint += "/" + strconv.FormatInt(opts.ID, 10) + "/" + op
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   114
	default:
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   115
		return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   116
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   117
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   118
	// Form items for a new toot
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   119
	if op == "status" {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   120
		params["status"] = opts.Status
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   121
		if opts.InReplyToID > 0 {
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   122
			params["in_reply_to_id"] = strconv.FormatInt(opts.InReplyToID, 10)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   123
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   124
		for i, id := range opts.MediaIDs {
134
588edbc9e14b Add checks on ID list values
Mikael Berthe <mikael@lilotux.net>
parents: 132
diff changeset
   125
			if id < 1 {
588edbc9e14b Add checks on ID list values
Mikael Berthe <mikael@lilotux.net>
parents: 132
diff changeset
   126
				return ErrInvalidID
588edbc9e14b Add checks on ID list values
Mikael Berthe <mikael@lilotux.net>
parents: 132
diff changeset
   127
			}
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   128
			qID := fmt.Sprintf("media_ids[%d]", i+1)
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   129
			params[qID] = strconv.FormatInt(id, 10)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   130
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   131
		if opts.Sensitive {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   132
			params["sensitive"] = "true"
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   133
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   134
		if opts.SpoilerText != "" {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   135
			params["spoiler_text"] = opts.SpoilerText
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   136
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   137
		if opts.Visibility != "" {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
   138
			params["visibility"] = opts.Visibility
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   139
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   140
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   141
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   142
	return mc.apiCall(endPoint, method, params, nil, nil, data)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   143
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   144
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   145
// GetStatus returns a status
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   146
// The returned status can be nil if there is an error or if the
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   147
// requested ID does not exist.
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   148
func (mc *Client) GetStatus(statusID int64) (*Status, error) {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   149
	var status Status
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   150
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   151
	if err := mc.queryStatusData(statusID, "status", &status); err != nil {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   152
		return nil, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   153
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   154
	if status.ID == 0 {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   155
		return nil, ErrEntityNotFound
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   156
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   157
	return &status, nil
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   158
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   159
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   160
// GetStatusContext returns a status context
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   161
func (mc *Client) GetStatusContext(statusID int64) (*Context, error) {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   162
	var context Context
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   163
	if err := mc.queryStatusData(statusID, "context", &context); err != nil {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   164
		return nil, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   165
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   166
	return &context, nil
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   167
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   168
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   169
// GetStatusCard returns a status card
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   170
func (mc *Client) GetStatusCard(statusID int64) (*Card, error) {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   171
	var card Card
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   172
	if err := mc.queryStatusData(statusID, "card", &card); err != nil {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   173
		return nil, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   174
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   175
	return &card, nil
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   176
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   177
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   178
// GetStatusRebloggedBy returns a list of the accounts who reblogged a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   179
func (mc *Client) GetStatusRebloggedBy(statusID int64, lopt *LimitParams) ([]Account, error) {
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   180
	o := &getAccountsOptions{ID: statusID, Limit: lopt}
161
6786f169b59f Refactor getMultipleAccounts
Mikael Berthe <mikael@lilotux.net>
parents: 160
diff changeset
   181
	return mc.getMultipleAccountsHelper("reblogged_by", o)
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   182
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   183
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   184
// GetStatusFavouritedBy returns a list of the accounts who favourited a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   185
func (mc *Client) GetStatusFavouritedBy(statusID int64, lopt *LimitParams) ([]Account, error) {
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
   186
	o := &getAccountsOptions{ID: statusID, Limit: lopt}
161
6786f169b59f Refactor getMultipleAccounts
Mikael Berthe <mikael@lilotux.net>
parents: 160
diff changeset
   187
	return mc.getMultipleAccountsHelper("favourited_by", o)
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   188
}
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   189
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   190
// PostStatus posts a new "toot"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   191
// All parameters but "text" can be empty.
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   192
// Visibility must be empty, or one of "direct", "private", "unlisted" and "public".
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   193
func (mc *Client) PostStatus(text string, inReplyTo int64, mediaIDs []int64, sensitive bool, spoilerText string, visibility string) (*Status, error) {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   194
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   195
	o := updateStatusOptions{
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   196
		Status:      text,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   197
		InReplyToID: inReplyTo,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   198
		MediaIDs:    mediaIDs,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   199
		Sensitive:   sensitive,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   200
		SpoilerText: spoilerText,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   201
		Visibility:  visibility,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   202
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   203
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   204
	err := mc.updateStatusData("status", o, &status)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   205
	if err != nil {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   206
		return nil, err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   207
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   208
	if status.ID == 0 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   209
		return nil, ErrEntityNotFound // TODO Change error message
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   210
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   211
	return &status, err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   212
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   213
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   214
// DeleteStatus deletes a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   215
func (mc *Client) DeleteStatus(statusID int64) error {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   216
	var status Status
132
639bbcddb4fe Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   217
	o := updateStatusOptions{ID: statusID}
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   218
	err := mc.updateStatusData("delete", o, &status)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   219
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   220
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   221
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   222
// ReblogStatus reblogs a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   223
func (mc *Client) ReblogStatus(statusID int64) error {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   224
	var status Status
132
639bbcddb4fe Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   225
	o := updateStatusOptions{ID: statusID}
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   226
	err := mc.updateStatusData("reblog", o, &status)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   227
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   228
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   229
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   230
// UnreblogStatus unreblogs a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   231
func (mc *Client) UnreblogStatus(statusID int64) error {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   232
	var status Status
132
639bbcddb4fe Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   233
	o := updateStatusOptions{ID: statusID}
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   234
	err := mc.updateStatusData("unreblog", o, &status)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   235
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   236
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   237
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   238
// FavouriteStatus favourites a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   239
func (mc *Client) FavouriteStatus(statusID int64) error {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   240
	var status Status
132
639bbcddb4fe Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   241
	o := updateStatusOptions{ID: statusID}
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   242
	err := mc.updateStatusData("favourite", o, &status)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   243
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   244
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   245
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   246
// UnfavouriteStatus unfavourites a status
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
   247
func (mc *Client) UnfavouriteStatus(statusID int64) error {
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   248
	var status Status
132
639bbcddb4fe Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
   249
	o := updateStatusOptions{ID: statusID}
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 134
diff changeset
   250
	err := mc.updateStatusData("unfavourite", o, &status)
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   251
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   252
}