status.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 13 Apr 2017 19:07:11 +0200
changeset 110 0ee4bfc17cc8
parent 108 3f21113728f4
child 120 579912e9d0ef
permissions -rw-r--r--
Cosmetics - remove some blank lines
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
6d89be3dd966 Placeholders.
Ollivier Robert <roberto@keltia.net>
parents:
diff changeset
     1
package gondole
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     2
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     3
import (
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     4
	"encoding/json"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     5
	"fmt"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     6
	"strconv"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     7
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     8
	"github.com/sendgrid/rest"
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
     9
)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    10
108
3f21113728f4 Fix typo in comment
Mikael Berthe <mikael@lilotux.net>
parents: 106
diff changeset
    11
// 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
    12
type updateStatusOptions struct {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    13
	// The ID is used for most commands
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    14
	ID int
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    15
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    16
	// The following fields are used for posting a new status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    17
	Status      string
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    18
	InReplyToID int
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    19
	MediaIDs    []int
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    20
	Sensitive   bool
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    21
	SpoilerText string
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    22
	Visibility  string // "direct", "private", "unlisted" or "public"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    23
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    24
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    25
// queryStatusData queries the statuses API
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    26
// The subquery can be empty or "status" (the status itself), "context",
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    27
// "card", "reblogged_by", "favourited_by".
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    28
// The data argument will receive the object(s) returned by the API server.
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    29
func (g *Client) queryStatusData(statusID int, subquery string, data interface{}) error {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    30
	endPoint := "statuses/" + strconv.Itoa(statusID)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    31
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    32
	if statusID < 1 {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    33
		return ErrInvalidID
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    34
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    35
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    36
	if subquery != "" && subquery != "status" {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    37
		switch subquery {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    38
		case "context", "card", "reblogged_by", "favourited_by":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    39
		default:
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    40
			return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    41
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    42
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    43
		endPoint += "/" + subquery
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    44
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    45
	req := g.prepareRequest(endPoint)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    46
	r, err := rest.API(req)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    47
	if err != nil {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    48
		return fmt.Errorf("status/%s API query: %s", subquery, err.Error())
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    49
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    50
106
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    51
	// Check for error reply
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    52
	var errorResult Error
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    53
	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    54
		// The empty object is not an error
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    55
		if errorResult.Text != "" {
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    56
			return fmt.Errorf("%s", errorResult.Text)
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    57
		}
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    58
	}
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    59
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
    60
	// Not an error reply; let's unmarshal the data
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    61
	err = json.Unmarshal([]byte(r.Body), &data)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    62
	if err != nil {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    63
		return fmt.Errorf("status/%s API: %s", subquery, err.Error())
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    64
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    65
	return nil
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    66
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
    67
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    68
// updateStatusData updates the statuses
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    69
// The subquery can be empty or "status" (to post a status), "delete" (for
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    70
// deleting a status), "reblog", "unreblog", "favourite", "unfavourite".
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    71
// The data argument will receive the object(s) returned by the API server.
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    72
func (g *Client) updateStatusData(subquery string, opts updateStatusOptions, data interface{}) error {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    73
	method := rest.Post
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    74
	endPoint := "statuses"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    75
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    76
	switch subquery {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    77
	case "", "status":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    78
		subquery = "status"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    79
		if opts.Status == "" {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    80
			return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    81
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    82
		switch opts.Visibility {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    83
		case "", "direct", "private", "unlisted", "public":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    84
			// Okay
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    85
		default:
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    86
			return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    87
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    88
		if len(opts.MediaIDs) > 4 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    89
			return fmt.Errorf("too many (>4) media IDs")
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    90
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    91
	case "delete":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    92
		method = rest.Delete
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    93
		if opts.ID < 1 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    94
			return ErrInvalidID
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    95
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    96
		endPoint += "/" + strconv.Itoa(opts.ID)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    97
	case "reblog", "unreblog", "favourite", "unfavourite":
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    98
		if opts.ID < 1 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
    99
			return ErrInvalidID
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   100
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   101
		endPoint += "/" + strconv.Itoa(opts.ID) + "/" + subquery
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   102
	default:
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   103
		return ErrInvalidParameter
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   104
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   106
	req := g.prepareRequest(endPoint)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   107
	req.Method = method
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   108
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   109
	// Form items for a new toot
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   110
	if subquery == "status" {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   111
		req.QueryParams["status"] = opts.Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   112
		if opts.InReplyToID > 0 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   113
			req.QueryParams["in_reply_to_id"] = strconv.Itoa(opts.InReplyToID)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   114
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   115
		for i, id := range opts.MediaIDs {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   116
			qID := fmt.Sprintf("media_ids[%d]", i+1)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   117
			req.QueryParams[qID] = strconv.Itoa(id)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   118
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   119
		if opts.Sensitive {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   120
			req.QueryParams["sensitive"] = "true"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   121
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   122
		if opts.SpoilerText != "" {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   123
			req.QueryParams["spoiler_text"] = opts.SpoilerText
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   124
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   125
		if opts.Visibility != "" {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   126
			req.QueryParams["visibility"] = opts.Visibility
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   127
		}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   128
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   129
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   130
	r, err := rest.API(req)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   131
	if err != nil {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   132
		return fmt.Errorf("status/%s API query: %s", subquery, err.Error())
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
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   135
	// Check for error reply
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   136
	var errorResult Error
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   137
	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
106
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
   138
		// The empty object is not an error
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
   139
		if errorResult.Text != "" {
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
   140
			return fmt.Errorf("%s", errorResult.Text)
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
   141
		}
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   142
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   143
106
356507eb8db6 Fix the check for API error result
Mikael Berthe <mikael@lilotux.net>
parents: 105
diff changeset
   144
	// Not an error reply; let's unmarshal the data
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   145
	err = json.Unmarshal([]byte(r.Body), &data)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   146
	if err != nil {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   147
		return fmt.Errorf("status/%s API: %s", subquery, err.Error())
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   148
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   149
	return nil
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   150
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   151
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   152
// GetStatus returns a status
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   153
// 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
   154
// requested ID does not exist.
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   155
func (g *Client) GetStatus(id int) (*Status, error) {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   156
	var status Status
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   157
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   158
	if err := g.queryStatusData(id, "status", &status); err != nil {
103
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   159
		return nil, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   160
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   161
	if status.ID == 0 {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   162
		return nil, ErrEntityNotFound
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   163
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   164
	return &status, nil
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
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   167
// GetStatusContext returns a status context
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   168
func (g *Client) GetStatusContext(id int) (*Context, error) {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   169
	var context Context
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   170
	if err := g.queryStatusData(id, "context", &context); err != nil {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   171
		return nil, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   172
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   173
	return &context, nil
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
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   176
// GetStatusCard returns a status card
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   177
func (g *Client) GetStatusCard(id int) (*Card, error) {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   178
	var card Card
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   179
	if err := g.queryStatusData(id, "card", &card); err != nil {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   180
		return nil, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   181
	}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   182
	return &card, nil
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
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   185
// GetStatusRebloggedBy returns a list of the accounts who reblogged a status
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   186
func (g *Client) GetStatusRebloggedBy(id int) ([]Account, error) {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   187
	var accounts []Account
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   188
	err := g.queryStatusData(id, "reblogged_by", &accounts)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   189
	return accounts, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   190
}
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   191
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   192
// GetStatusFavouritedBy returns a list of the accounts who favourited a status
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   193
func (g *Client) GetStatusFavouritedBy(id int) ([]Account, error) {
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   194
	var accounts []Account
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   195
	err := g.queryStatusData(id, "favourited_by", &accounts)
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   196
	return accounts, err
9860bb68a0a6 Add status (GET) methods
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   197
}
105
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   198
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   199
// PostStatus posts a new "toot"
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   200
// All parameters but "text" can be empty.
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   201
// Visibility must be empty, or one of "direct", "private", "unlisted" and "public".
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   202
func (g *Client) PostStatus(text string, inReplyTo int, mediaIDs []int, sensitive bool, spoilerText string, visibility string) (*Status, error) {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   203
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   204
	o := updateStatusOptions{
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   205
		Status:      text,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   206
		InReplyToID: inReplyTo,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   207
		MediaIDs:    mediaIDs,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   208
		Sensitive:   sensitive,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   209
		SpoilerText: spoilerText,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   210
		Visibility:  visibility,
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   211
	}
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
	err := g.updateStatusData("status", o, &status)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   214
	if err != nil {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   215
		return nil, err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   216
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   217
	if status.ID == 0 {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   218
		return nil, ErrEntityNotFound // TODO Change error message
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   219
	}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   220
	return &status, err
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
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   223
// DeleteStatus deletes a status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   224
func (g *Client) DeleteStatus(id int) error {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   225
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   226
	o := updateStatusOptions{ID: id}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   227
	err := g.updateStatusData("delete", o, &status)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   228
	return err
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
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   231
// ReblogStatus reblogs a status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   232
func (g *Client) ReblogStatus(id int) error {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   233
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   234
	o := updateStatusOptions{ID: id}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   235
	err := g.updateStatusData("reblog", o, &status)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   236
	return err
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
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   239
// UnreblogStatus unreblogs a status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   240
func (g *Client) UnreblogStatus(id int) error {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   241
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   242
	o := updateStatusOptions{ID: id}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   243
	err := g.updateStatusData("unreblog", o, &status)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   244
	return err
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
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   247
// FavouriteStatus favourites a status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   248
func (g *Client) FavouriteStatus(id int) error {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   249
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   250
	o := updateStatusOptions{ID: id}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   251
	err := g.updateStatusData("favourite", o, &status)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   252
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   253
}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   254
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   255
// UnfavouriteStatus unfavourites a status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   256
func (g *Client) UnfavouriteStatus(id int) error {
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   257
	var status Status
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   258
	o := updateStatusOptions{ID: id}
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   259
	err := g.updateStatusData("unfavourite", o, &status)
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   260
	return err
e2a16e19eb8b Add missing status commands
Mikael Berthe <mikael@lilotux.net>
parents: 103
diff changeset
   261
}