account.go
changeset 161 6786f169b59f
parent 160 9f7e683b323f
child 162 68df3a01e1a7
equal deleted inserted replaced
160:9f7e683b323f 161:6786f169b59f
    64 	}
    64 	}
    65 	return &account, nil
    65 	return &account, nil
    66 }
    66 }
    67 
    67 
    68 // getMultipleAccounts returns a list of account entities
    68 // getMultipleAccounts returns a list of account entities
    69 // The operation 'op' can be "followers", "following", "search", "blocks",
    69 // If lopt.All is true, several requests will be made until the API server
    70 // "mutes", "follow_requests".
       
    71 // The id is optional and depends on the operation.
       
    72 // If opts.All is true, several requests will be made until the API server
       
    73 // has nothing to return.
    70 // has nothing to return.
    74 func (mc *Client) getMultipleAccounts(op string, opts *getAccountsOptions) ([]Account, error) {
    71 func (mc *Client) getMultipleAccounts(endPoint string, params apiCallParams, lopt *LimitParams) ([]Account, error) {
    75 	var endPoint string
       
    76 	var lopt *LimitParams
       
    77 
       
    78 	if opts != nil {
       
    79 		lopt = opts.Limit
       
    80 	}
       
    81 
       
    82 	switch op {
       
    83 	case "followers", "following":
       
    84 		if opts == nil || opts.ID < 1 {
       
    85 			return []Account{}, ErrInvalidID
       
    86 		}
       
    87 		endPoint = "accounts/" + strconv.FormatInt(opts.ID, 10) + "/" + op
       
    88 	case "follow_requests", "blocks", "mutes":
       
    89 		endPoint = op
       
    90 	case "search":
       
    91 		if opts == nil || opts.Q == "" {
       
    92 			return []Account{}, ErrInvalidParameter
       
    93 		}
       
    94 		endPoint = "accounts/" + op
       
    95 	case "reblogged_by", "favourited_by":
       
    96 		if opts == nil || opts.ID < 1 {
       
    97 			return []Account{}, ErrInvalidID
       
    98 		}
       
    99 		endPoint = "statuses/" + strconv.FormatInt(opts.ID, 10) + "/" + op
       
   100 	default:
       
   101 		return nil, ErrInvalidParameter
       
   102 	}
       
   103 
       
   104 	// Handle target-specific query parameters
       
   105 	params := make(apiCallParams)
       
   106 	if op == "search" {
       
   107 		params["q"] = opts.Q
       
   108 	}
       
   109 
       
   110 	var accounts []Account
    72 	var accounts []Account
   111 	var links apiLinks
    73 	var links apiLinks
   112 	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &accounts); err != nil {
    74 	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &accounts); err != nil {
   113 		return nil, err
    75 		return nil, err
   114 	}
    76 	}
   125 		}
    87 		}
   126 	}
    88 	}
   127 	return accounts, nil
    89 	return accounts, nil
   128 }
    90 }
   129 
    91 
       
    92 // getMultipleAccountsHelper returns a list of account entities
       
    93 // The operation 'op' can be "followers", "following", "search", "blocks",
       
    94 // "mutes", "follow_requests".
       
    95 // The id is optional and depends on the operation.
       
    96 // If opts.All is true, several requests will be made until the API server
       
    97 // has nothing to return.
       
    98 func (mc *Client) getMultipleAccountsHelper(op string, opts *getAccountsOptions) ([]Account, error) {
       
    99 	var endPoint string
       
   100 	var lopt *LimitParams
       
   101 
       
   102 	if opts != nil {
       
   103 		lopt = opts.Limit
       
   104 	}
       
   105 
       
   106 	switch op {
       
   107 	case "followers", "following":
       
   108 		if opts == nil || opts.ID < 1 {
       
   109 			return []Account{}, ErrInvalidID
       
   110 		}
       
   111 		endPoint = "accounts/" + strconv.FormatInt(opts.ID, 10) + "/" + op
       
   112 	case "follow_requests", "blocks", "mutes":
       
   113 		endPoint = op
       
   114 	case "search":
       
   115 		if opts == nil || opts.Q == "" {
       
   116 			return []Account{}, ErrInvalidParameter
       
   117 		}
       
   118 		endPoint = "accounts/" + op
       
   119 	case "reblogged_by", "favourited_by":
       
   120 		if opts == nil || opts.ID < 1 {
       
   121 			return []Account{}, ErrInvalidID
       
   122 		}
       
   123 		endPoint = "statuses/" + strconv.FormatInt(opts.ID, 10) + "/" + op
       
   124 	default:
       
   125 		return nil, ErrInvalidParameter
       
   126 	}
       
   127 
       
   128 	// Handle target-specific query parameters
       
   129 	params := make(apiCallParams)
       
   130 	if op == "search" {
       
   131 		params["q"] = opts.Q
       
   132 	}
       
   133 
       
   134 	return mc.getMultipleAccounts(endPoint, params, lopt)
       
   135 }
       
   136 
   130 // GetAccount returns an account entity
   137 // GetAccount returns an account entity
   131 // The returned value can be nil if there is an error or if the
   138 // The returned value can be nil if there is an error or if the
   132 // requested ID does not exist.
   139 // requested ID does not exist.
   133 func (mc *Client) GetAccount(accountID int64) (*Account, error) {
   140 func (mc *Client) GetAccount(accountID int64) (*Account, error) {
   134 	account, err := mc.getSingleAccount("account", accountID)
   141 	account, err := mc.getSingleAccount("account", accountID)
   154 }
   161 }
   155 
   162 
   156 // GetAccountFollowers returns the list of accounts following a given account
   163 // GetAccountFollowers returns the list of accounts following a given account
   157 func (mc *Client) GetAccountFollowers(accountID int64, lopt *LimitParams) ([]Account, error) {
   164 func (mc *Client) GetAccountFollowers(accountID int64, lopt *LimitParams) ([]Account, error) {
   158 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   165 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   159 	return mc.getMultipleAccounts("followers", o)
   166 	return mc.getMultipleAccountsHelper("followers", o)
   160 }
   167 }
   161 
   168 
   162 // GetAccountFollowing returns the list of accounts a given account is following
   169 // GetAccountFollowing returns the list of accounts a given account is following
   163 func (mc *Client) GetAccountFollowing(accountID int64, lopt *LimitParams) ([]Account, error) {
   170 func (mc *Client) GetAccountFollowing(accountID int64, lopt *LimitParams) ([]Account, error) {
   164 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   171 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   165 	return mc.getMultipleAccounts("following", o)
   172 	return mc.getMultipleAccountsHelper("following", o)
   166 }
   173 }
   167 
   174 
   168 // FollowAccount follows an account
   175 // FollowAccount follows an account
   169 func (mc *Client) FollowAccount(accountID int64) error {
   176 func (mc *Client) FollowAccount(accountID int64) error {
   170 	account, err := mc.getSingleAccount("follow", accountID)
   177 	account, err := mc.getSingleAccount("follow", accountID)
   259 
   266 
   260 // SearchAccounts returns a list of accounts matching the query string
   267 // SearchAccounts returns a list of accounts matching the query string
   261 // The lopt parameter is optional (can be nil) or can be used to set a limit.
   268 // The lopt parameter is optional (can be nil) or can be used to set a limit.
   262 func (mc *Client) SearchAccounts(query string, lopt *LimitParams) ([]Account, error) {
   269 func (mc *Client) SearchAccounts(query string, lopt *LimitParams) ([]Account, error) {
   263 	o := &getAccountsOptions{Q: query, Limit: lopt}
   270 	o := &getAccountsOptions{Q: query, Limit: lopt}
   264 	return mc.getMultipleAccounts("search", o)
   271 	return mc.getMultipleAccountsHelper("search", o)
   265 }
   272 }
   266 
   273 
   267 // GetBlockedAccounts returns the list of blocked accounts
   274 // GetBlockedAccounts returns the list of blocked accounts
   268 // The lopt parameter is optional (can be nil).
   275 // The lopt parameter is optional (can be nil).
   269 func (mc *Client) GetBlockedAccounts(lopt *LimitParams) ([]Account, error) {
   276 func (mc *Client) GetBlockedAccounts(lopt *LimitParams) ([]Account, error) {
   270 	o := &getAccountsOptions{Limit: lopt}
   277 	o := &getAccountsOptions{Limit: lopt}
   271 	return mc.getMultipleAccounts("blocks", o)
   278 	return mc.getMultipleAccountsHelper("blocks", o)
   272 }
   279 }
   273 
   280 
   274 // GetMutedAccounts returns the list of muted accounts
   281 // GetMutedAccounts returns the list of muted accounts
   275 // The lopt parameter is optional (can be nil).
   282 // The lopt parameter is optional (can be nil).
   276 func (mc *Client) GetMutedAccounts(lopt *LimitParams) ([]Account, error) {
   283 func (mc *Client) GetMutedAccounts(lopt *LimitParams) ([]Account, error) {
   277 	o := &getAccountsOptions{Limit: lopt}
   284 	o := &getAccountsOptions{Limit: lopt}
   278 	return mc.getMultipleAccounts("mutes", o)
   285 	return mc.getMultipleAccountsHelper("mutes", o)
   279 }
   286 }
   280 
   287 
   281 // GetAccountFollowRequests returns the list of follow requests accounts
   288 // GetAccountFollowRequests returns the list of follow requests accounts
   282 // The lopt parameter is optional (can be nil).
   289 // The lopt parameter is optional (can be nil).
   283 func (mc *Client) GetAccountFollowRequests(lopt *LimitParams) ([]Account, error) {
   290 func (mc *Client) GetAccountFollowRequests(lopt *LimitParams) ([]Account, error) {
   284 	o := &getAccountsOptions{Limit: lopt}
   291 	o := &getAccountsOptions{Limit: lopt}
   285 	return mc.getMultipleAccounts("follow_requests", o)
   292 	return mc.getMultipleAccountsHelper("follow_requests", o)
   286 }
   293 }
   287 
   294 
   288 // GetAccountRelationships returns a list of relationship entities for the given accounts
   295 // GetAccountRelationships returns a list of relationship entities for the given accounts
   289 func (mc *Client) GetAccountRelationships(accountIDs []int64) ([]Relationship, error) {
   296 func (mc *Client) GetAccountRelationships(accountIDs []int64) ([]Relationship, error) {
   290 	if len(accountIDs) < 1 {
   297 	if len(accountIDs) < 1 {