account.go
changeset 138 23d3a518d0ad
parent 134 588edbc9e14b
child 143 9ce9b39c851c
equal deleted inserted replaced
137:acaea3179f4d 138:23d3a518d0ad
     2 Copyright 2017 Mikael Berthe
     2 Copyright 2017 Mikael Berthe
     3 
     3 
     4 Licensed under the MIT license.  Please see the LICENSE file is this directory.
     4 Licensed under the MIT license.  Please see the LICENSE file is this directory.
     5 */
     5 */
     6 
     6 
     7 package gondole
     7 package madon
     8 
     8 
     9 import (
     9 import (
    10 	"fmt"
    10 	"fmt"
    11 	"strconv"
    11 	"strconv"
    12 
    12 
    26 // getSingleAccount returns an account entity
    26 // getSingleAccount returns an account entity
    27 // The operation 'op' can be "account", "verify_credentials", "follow",
    27 // The operation 'op' can be "account", "verify_credentials", "follow",
    28 // "unfollow", "block", "unblock", "mute", "unmute",
    28 // "unfollow", "block", "unblock", "mute", "unmute",
    29 // "follow_requests/authorize" or // "follow_requests/reject".
    29 // "follow_requests/authorize" or // "follow_requests/reject".
    30 // The id is optional and depends on the operation.
    30 // The id is optional and depends on the operation.
    31 func (g *Client) getSingleAccount(op string, id int) (*Account, error) {
    31 func (mc *Client) getSingleAccount(op string, id int) (*Account, error) {
    32 	var endPoint string
    32 	var endPoint string
    33 	method := rest.Get
    33 	method := rest.Get
    34 	strID := strconv.Itoa(id)
    34 	strID := strconv.Itoa(id)
    35 
    35 
    36 	switch op {
    36 	switch op {
    49 	default:
    49 	default:
    50 		return nil, ErrInvalidParameter
    50 		return nil, ErrInvalidParameter
    51 	}
    51 	}
    52 
    52 
    53 	var account Account
    53 	var account Account
    54 	if err := g.apiCall(endPoint, method, nil, &account); err != nil {
    54 	if err := mc.apiCall(endPoint, method, nil, &account); err != nil {
    55 		return nil, err
    55 		return nil, err
    56 	}
    56 	}
    57 	return &account, nil
    57 	return &account, nil
    58 }
    58 }
    59 
    59 
    60 // getMultipleAccounts returns a list of account entities
    60 // getMultipleAccounts returns a list of account entities
    61 // The operation 'op' can be "followers", "following", "search", "blocks",
    61 // The operation 'op' can be "followers", "following", "search", "blocks",
    62 // "mutes", "follow_requests".
    62 // "mutes", "follow_requests".
    63 // The id is optional and depends on the operation.
    63 // The id is optional and depends on the operation.
    64 func (g *Client) getMultipleAccounts(op string, opts *getAccountsOptions) ([]Account, error) {
    64 func (mc *Client) getMultipleAccounts(op string, opts *getAccountsOptions) ([]Account, error) {
    65 	var endPoint string
    65 	var endPoint string
    66 
    66 
    67 	switch op {
    67 	switch op {
    68 	case "followers", "following":
    68 	case "followers", "following":
    69 		if opts == nil || opts.ID < 1 {
    69 		if opts == nil || opts.ID < 1 {
    89 			params["limit"] = strconv.Itoa(opts.Limit)
    89 			params["limit"] = strconv.Itoa(opts.Limit)
    90 		}
    90 		}
    91 	}
    91 	}
    92 
    92 
    93 	var accounts []Account
    93 	var accounts []Account
    94 	if err := g.apiCall(endPoint, rest.Get, params, &accounts); err != nil {
    94 	if err := mc.apiCall(endPoint, rest.Get, params, &accounts); err != nil {
    95 		return nil, err
    95 		return nil, err
    96 	}
    96 	}
    97 	return accounts, nil
    97 	return accounts, nil
    98 }
    98 }
    99 
    99 
   100 // GetAccount returns an account entity
   100 // GetAccount returns an account entity
   101 // The returned value can be nil if there is an error or if the
   101 // The returned value can be nil if there is an error or if the
   102 // requested ID does not exist.
   102 // requested ID does not exist.
   103 func (g *Client) GetAccount(accountID int) (*Account, error) {
   103 func (mc *Client) GetAccount(accountID int) (*Account, error) {
   104 	account, err := g.getSingleAccount("account", accountID)
   104 	account, err := mc.getSingleAccount("account", accountID)
   105 	if err != nil {
   105 	if err != nil {
   106 		return nil, err
   106 		return nil, err
   107 	}
   107 	}
   108 	if account != nil && account.ID == 0 {
   108 	if account != nil && account.ID == 0 {
   109 		return nil, ErrEntityNotFound
   109 		return nil, ErrEntityNotFound
   110 	}
   110 	}
   111 	return account, nil
   111 	return account, nil
   112 }
   112 }
   113 
   113 
   114 // GetCurrentAccount returns the current user account
   114 // GetCurrentAccount returns the current user account
   115 func (g *Client) GetCurrentAccount() (*Account, error) {
   115 func (mc *Client) GetCurrentAccount() (*Account, error) {
   116 	account, err := g.getSingleAccount("verify_credentials", 0)
   116 	account, err := mc.getSingleAccount("verify_credentials", 0)
   117 	if err != nil {
   117 	if err != nil {
   118 		return nil, err
   118 		return nil, err
   119 	}
   119 	}
   120 	if account != nil && account.ID == 0 {
   120 	if account != nil && account.ID == 0 {
   121 		return nil, ErrEntityNotFound
   121 		return nil, ErrEntityNotFound
   122 	}
   122 	}
   123 	return account, nil
   123 	return account, nil
   124 }
   124 }
   125 
   125 
   126 // GetAccountFollowers returns the list of accounts following a given account
   126 // GetAccountFollowers returns the list of accounts following a given account
   127 func (g *Client) GetAccountFollowers(accountID int) ([]Account, error) {
   127 func (mc *Client) GetAccountFollowers(accountID int) ([]Account, error) {
   128 	o := &getAccountsOptions{ID: accountID}
   128 	o := &getAccountsOptions{ID: accountID}
   129 	return g.getMultipleAccounts("followers", o)
   129 	return mc.getMultipleAccounts("followers", o)
   130 }
   130 }
   131 
   131 
   132 // GetAccountFollowing returns the list of accounts a given account is following
   132 // GetAccountFollowing returns the list of accounts a given account is following
   133 func (g *Client) GetAccountFollowing(accountID int) ([]Account, error) {
   133 func (mc *Client) GetAccountFollowing(accountID int) ([]Account, error) {
   134 	o := &getAccountsOptions{ID: accountID}
   134 	o := &getAccountsOptions{ID: accountID}
   135 	return g.getMultipleAccounts("following", o)
   135 	return mc.getMultipleAccounts("following", o)
   136 }
   136 }
   137 
   137 
   138 // FollowAccount follows an account
   138 // FollowAccount follows an account
   139 func (g *Client) FollowAccount(accountID int) error {
   139 func (mc *Client) FollowAccount(accountID int) error {
   140 	account, err := g.getSingleAccount("follow", accountID)
   140 	account, err := mc.getSingleAccount("follow", accountID)
   141 	if err != nil {
   141 	if err != nil {
   142 		return err
   142 		return err
   143 	}
   143 	}
   144 	if account != nil && account.ID != accountID {
   144 	if account != nil && account.ID != accountID {
   145 		return ErrEntityNotFound
   145 		return ErrEntityNotFound
   146 	}
   146 	}
   147 	return nil
   147 	return nil
   148 }
   148 }
   149 
   149 
   150 // UnfollowAccount unfollows an account
   150 // UnfollowAccount unfollows an account
   151 func (g *Client) UnfollowAccount(accountID int) error {
   151 func (mc *Client) UnfollowAccount(accountID int) error {
   152 	account, err := g.getSingleAccount("unfollow", accountID)
   152 	account, err := mc.getSingleAccount("unfollow", accountID)
   153 	if err != nil {
   153 	if err != nil {
   154 		return err
   154 		return err
   155 	}
   155 	}
   156 	if account != nil && account.ID != accountID {
   156 	if account != nil && account.ID != accountID {
   157 		return ErrEntityNotFound
   157 		return ErrEntityNotFound
   158 	}
   158 	}
   159 	return nil
   159 	return nil
   160 }
   160 }
   161 
   161 
   162 // FollowRemoteAccount follows a remote account
   162 // FollowRemoteAccount follows a remote account
   163 // The parameter 'uri' is a URI (e.g. "username@domain").
   163 // The parameter 'uri' is a URI (e.mc. "username@domain").
   164 func (g *Client) FollowRemoteAccount(uri string) (*Account, error) {
   164 func (mc *Client) FollowRemoteAccount(uri string) (*Account, error) {
   165 	if uri == "" {
   165 	if uri == "" {
   166 		return nil, ErrInvalidID
   166 		return nil, ErrInvalidID
   167 	}
   167 	}
   168 
   168 
   169 	params := make(apiCallParams)
   169 	params := make(apiCallParams)
   170 	params["uri"] = uri
   170 	params["uri"] = uri
   171 
   171 
   172 	var account Account
   172 	var account Account
   173 	if err := g.apiCall("follows", rest.Post, params, &account); err != nil {
   173 	if err := mc.apiCall("follows", rest.Post, params, &account); err != nil {
   174 		return nil, err
   174 		return nil, err
   175 	}
   175 	}
   176 	if account.ID == 0 {
   176 	if account.ID == 0 {
   177 		return nil, ErrEntityNotFound
   177 		return nil, ErrEntityNotFound
   178 	}
   178 	}
   179 	return &account, nil
   179 	return &account, nil
   180 }
   180 }
   181 
   181 
   182 // BlockAccount blocks an account
   182 // BlockAccount blocks an account
   183 func (g *Client) BlockAccount(accountID int) error {
   183 func (mc *Client) BlockAccount(accountID int) error {
   184 	account, err := g.getSingleAccount("block", accountID)
   184 	account, err := mc.getSingleAccount("block", accountID)
   185 	if err != nil {
   185 	if err != nil {
   186 		return err
   186 		return err
   187 	}
   187 	}
   188 	if account != nil && account.ID != accountID {
   188 	if account != nil && account.ID != accountID {
   189 		return ErrEntityNotFound
   189 		return ErrEntityNotFound
   190 	}
   190 	}
   191 	return nil
   191 	return nil
   192 }
   192 }
   193 
   193 
   194 // UnblockAccount unblocks an account
   194 // UnblockAccount unblocks an account
   195 func (g *Client) UnblockAccount(accountID int) error {
   195 func (mc *Client) UnblockAccount(accountID int) error {
   196 	account, err := g.getSingleAccount("unblock", accountID)
   196 	account, err := mc.getSingleAccount("unblock", accountID)
   197 	if err != nil {
   197 	if err != nil {
   198 		return err
   198 		return err
   199 	}
   199 	}
   200 	if account != nil && account.ID != accountID {
   200 	if account != nil && account.ID != accountID {
   201 		return ErrEntityNotFound
   201 		return ErrEntityNotFound
   202 	}
   202 	}
   203 	return nil
   203 	return nil
   204 }
   204 }
   205 
   205 
   206 // MuteAccount mutes an account
   206 // MuteAccount mutes an account
   207 func (g *Client) MuteAccount(accountID int) error {
   207 func (mc *Client) MuteAccount(accountID int) error {
   208 	account, err := g.getSingleAccount("mute", accountID)
   208 	account, err := mc.getSingleAccount("mute", accountID)
   209 	if err != nil {
   209 	if err != nil {
   210 		return err
   210 		return err
   211 	}
   211 	}
   212 	if account != nil && account.ID != accountID {
   212 	if account != nil && account.ID != accountID {
   213 		return ErrEntityNotFound
   213 		return ErrEntityNotFound
   214 	}
   214 	}
   215 	return nil
   215 	return nil
   216 }
   216 }
   217 
   217 
   218 // UnmuteAccount unmutes an account
   218 // UnmuteAccount unmutes an account
   219 func (g *Client) UnmuteAccount(accountID int) error {
   219 func (mc *Client) UnmuteAccount(accountID int) error {
   220 	account, err := g.getSingleAccount("unmute", accountID)
   220 	account, err := mc.getSingleAccount("unmute", accountID)
   221 	if err != nil {
   221 	if err != nil {
   222 		return err
   222 		return err
   223 	}
   223 	}
   224 	if account != nil && account.ID != accountID {
   224 	if account != nil && account.ID != accountID {
   225 		return ErrEntityNotFound
   225 		return ErrEntityNotFound
   227 	return nil
   227 	return nil
   228 }
   228 }
   229 
   229 
   230 // SearchAccounts returns a list of accounts matching the query string
   230 // SearchAccounts returns a list of accounts matching the query string
   231 // The limit parameter is optional (can be 0).
   231 // The limit parameter is optional (can be 0).
   232 func (g *Client) SearchAccounts(query string, limit int) ([]Account, error) {
   232 func (mc *Client) SearchAccounts(query string, limit int) ([]Account, error) {
   233 	o := &getAccountsOptions{Q: query, Limit: limit}
   233 	o := &getAccountsOptions{Q: query, Limit: limit}
   234 	return g.getMultipleAccounts("search", o)
   234 	return mc.getMultipleAccounts("search", o)
   235 }
   235 }
   236 
   236 
   237 // GetBlockedAccounts returns the list of blocked accounts
   237 // GetBlockedAccounts returns the list of blocked accounts
   238 func (g *Client) GetBlockedAccounts() ([]Account, error) {
   238 func (mc *Client) GetBlockedAccounts() ([]Account, error) {
   239 	return g.getMultipleAccounts("blocks", nil)
   239 	return mc.getMultipleAccounts("blocks", nil)
   240 }
   240 }
   241 
   241 
   242 // GetMutedAccounts returns the list of muted accounts
   242 // GetMutedAccounts returns the list of muted accounts
   243 func (g *Client) GetMutedAccounts() ([]Account, error) {
   243 func (mc *Client) GetMutedAccounts() ([]Account, error) {
   244 	return g.getMultipleAccounts("mutes", nil)
   244 	return mc.getMultipleAccounts("mutes", nil)
   245 }
   245 }
   246 
   246 
   247 // GetAccountFollowRequests returns the list of follow requests accounts
   247 // GetAccountFollowRequests returns the list of follow requests accounts
   248 func (g *Client) GetAccountFollowRequests() ([]Account, error) {
   248 func (mc *Client) GetAccountFollowRequests() ([]Account, error) {
   249 	return g.getMultipleAccounts("follow_requests", nil)
   249 	return mc.getMultipleAccounts("follow_requests", nil)
   250 }
   250 }
   251 
   251 
   252 // GetAccountRelationships returns a list of relationship entities for the given accounts
   252 // GetAccountRelationships returns a list of relationship entities for the given accounts
   253 func (g *Client) GetAccountRelationships(accountIDs []int) ([]Relationship, error) {
   253 func (mc *Client) GetAccountRelationships(accountIDs []int) ([]Relationship, error) {
   254 	if len(accountIDs) < 1 {
   254 	if len(accountIDs) < 1 {
   255 		return nil, ErrInvalidID
   255 		return nil, ErrInvalidID
   256 	}
   256 	}
   257 
   257 
   258 	params := make(apiCallParams)
   258 	params := make(apiCallParams)
   263 		qID := fmt.Sprintf("id[%d]", i+1)
   263 		qID := fmt.Sprintf("id[%d]", i+1)
   264 		params[qID] = strconv.Itoa(id)
   264 		params[qID] = strconv.Itoa(id)
   265 	}
   265 	}
   266 
   266 
   267 	var rl []Relationship
   267 	var rl []Relationship
   268 	if err := g.apiCall("accounts/relationships", rest.Get, params, &rl); err != nil {
   268 	if err := mc.apiCall("accounts/relationships", rest.Get, params, &rl); err != nil {
   269 		return nil, err
   269 		return nil, err
   270 	}
   270 	}
   271 	return rl, nil
   271 	return rl, nil
   272 }
   272 }
   273 
   273 
   274 // GetAccountStatuses returns a list of status entities for the given account
   274 // GetAccountStatuses returns a list of status entities for the given account
   275 // If onlyMedia is true, returns only statuses that have media attachments.
   275 // If onlyMedia is true, returns only statuses that have media attachments.
   276 // If excludeReplies is true, skip statuses that reply to other statuses.
   276 // If excludeReplies is true, skip statuses that reply to other statuses.
   277 func (g *Client) GetAccountStatuses(accountID int, onlyMedia, excludeReplies bool) ([]Status, error) {
   277 func (mc *Client) GetAccountStatuses(accountID int, onlyMedia, excludeReplies bool) ([]Status, error) {
   278 	if accountID < 1 {
   278 	if accountID < 1 {
   279 		return nil, ErrInvalidID
   279 		return nil, ErrInvalidID
   280 	}
   280 	}
   281 
   281 
   282 	endPoint := "accounts/" + strconv.Itoa(accountID) + "/" + "statuses"
   282 	endPoint := "accounts/" + strconv.Itoa(accountID) + "/" + "statuses"
   287 	if excludeReplies {
   287 	if excludeReplies {
   288 		params["exclude_replies"] = "true"
   288 		params["exclude_replies"] = "true"
   289 	}
   289 	}
   290 
   290 
   291 	var sl []Status
   291 	var sl []Status
   292 	if err := g.apiCall(endPoint, rest.Get, params, &sl); err != nil {
   292 	if err := mc.apiCall(endPoint, rest.Get, params, &sl); err != nil {
   293 		return nil, err
   293 		return nil, err
   294 	}
   294 	}
   295 	return sl, nil
   295 	return sl, nil
   296 }
   296 }
   297 
   297 
   298 // FollowRequestAuthorize authorizes or rejects an account follow-request
   298 // FollowRequestAuthorize authorizes or rejects an account follow-request
   299 func (g *Client) FollowRequestAuthorize(accountID int, authorize bool) error {
   299 func (mc *Client) FollowRequestAuthorize(accountID int, authorize bool) error {
   300 	endPoint := "follow_requests/reject"
   300 	endPoint := "follow_requests/reject"
   301 	if authorize {
   301 	if authorize {
   302 		endPoint = "follow_requests/authorize"
   302 		endPoint = "follow_requests/authorize"
   303 	}
   303 	}
   304 	_, err := g.getSingleAccount(endPoint, accountID)
   304 	_, err := mc.getSingleAccount(endPoint, accountID)
   305 	return err
   305 	return err
   306 }
   306 }