account.go
changeset 149 5f922977d7c7
parent 143 9ce9b39c851c
child 152 d9e73e9df9c6
equal deleted inserted replaced
148:ae2cbcf18b55 149:5f922977d7c7
    23 // getAccountsOptions contains option fields for POST and DELETE API calls
    23 // getAccountsOptions contains option fields for POST and DELETE API calls
    24 type getAccountsOptions struct {
    24 type getAccountsOptions struct {
    25 	// The ID is used for most commands
    25 	// The ID is used for most commands
    26 	ID int
    26 	ID int
    27 
    27 
    28 	// The following fields are used when searching for accounts
    28 	// The Q field (query) is used when searching for accounts
    29 	Q     string
    29 	Q string
    30 	Limit int
    30 
       
    31 	Limit *LimitParams
    31 }
    32 }
    32 
    33 
    33 // getSingleAccount returns an account entity
    34 // getSingleAccount returns an account entity
    34 // The operation 'op' can be "account", "verify_credentials", "follow",
    35 // The operation 'op' can be "account", "verify_credentials", "follow",
    35 // "unfollow", "block", "unblock", "mute", "unmute",
    36 // "unfollow", "block", "unblock", "mute", "unmute",
    56 	default:
    57 	default:
    57 		return nil, ErrInvalidParameter
    58 		return nil, ErrInvalidParameter
    58 	}
    59 	}
    59 
    60 
    60 	var account Account
    61 	var account Account
    61 	if err := mc.apiCall(endPoint, method, nil, &account); err != nil {
    62 	if err := mc.apiCall(endPoint, method, nil, nil, &account); err != nil {
    62 		return nil, err
    63 		return nil, err
    63 	}
    64 	}
    64 	return &account, nil
    65 	return &account, nil
    65 }
    66 }
    66 
    67 
    68 // The operation 'op' can be "followers", "following", "search", "blocks",
    69 // The operation 'op' can be "followers", "following", "search", "blocks",
    69 // "mutes", "follow_requests".
    70 // "mutes", "follow_requests".
    70 // The id is optional and depends on the operation.
    71 // The id is optional and depends on the operation.
    71 func (mc *Client) getMultipleAccounts(op string, opts *getAccountsOptions) ([]Account, error) {
    72 func (mc *Client) getMultipleAccounts(op string, opts *getAccountsOptions) ([]Account, error) {
    72 	var endPoint string
    73 	var endPoint string
       
    74 	var lopt *LimitParams
       
    75 
       
    76 	if opts != nil {
       
    77 		lopt = opts.Limit
       
    78 	}
    73 
    79 
    74 	switch op {
    80 	switch op {
    75 	case "followers", "following":
    81 	case "followers", "following":
    76 		if opts == nil || opts.ID < 1 {
    82 		if opts == nil || opts.ID < 1 {
    77 			return []Account{}, ErrInvalidID
    83 			return []Account{}, ErrInvalidID
    90 
    96 
    91 	// Handle target-specific query parameters
    97 	// Handle target-specific query parameters
    92 	params := make(apiCallParams)
    98 	params := make(apiCallParams)
    93 	if op == "search" {
    99 	if op == "search" {
    94 		params["q"] = opts.Q
   100 		params["q"] = opts.Q
    95 		if opts.Limit > 0 {
       
    96 			params["limit"] = strconv.Itoa(opts.Limit)
       
    97 		}
       
    98 	}
   101 	}
    99 
   102 
   100 	var accounts []Account
   103 	var accounts []Account
   101 	if err := mc.apiCall(endPoint, rest.Get, params, &accounts); err != nil {
   104 	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &accounts); err != nil {
   102 		return nil, err
   105 		return nil, err
   103 	}
   106 	}
   104 	return accounts, nil
   107 	return accounts, nil
   105 }
   108 }
   106 
   109 
   129 	}
   132 	}
   130 	return account, nil
   133 	return account, nil
   131 }
   134 }
   132 
   135 
   133 // GetAccountFollowers returns the list of accounts following a given account
   136 // GetAccountFollowers returns the list of accounts following a given account
   134 func (mc *Client) GetAccountFollowers(accountID int) ([]Account, error) {
   137 func (mc *Client) GetAccountFollowers(accountID int, lopt *LimitParams) ([]Account, error) {
   135 	o := &getAccountsOptions{ID: accountID}
   138 	o := &getAccountsOptions{ID: accountID}
   136 	return mc.getMultipleAccounts("followers", o)
   139 	return mc.getMultipleAccounts("followers", o)
   137 }
   140 }
   138 
   141 
   139 // GetAccountFollowing returns the list of accounts a given account is following
   142 // GetAccountFollowing returns the list of accounts a given account is following
   140 func (mc *Client) GetAccountFollowing(accountID int) ([]Account, error) {
   143 func (mc *Client) GetAccountFollowing(accountID int, lopt *LimitParams) ([]Account, error) {
   141 	o := &getAccountsOptions{ID: accountID}
   144 	o := &getAccountsOptions{ID: accountID}
   142 	return mc.getMultipleAccounts("following", o)
   145 	return mc.getMultipleAccounts("following", o)
   143 }
   146 }
   144 
   147 
   145 // FollowAccount follows an account
   148 // FollowAccount follows an account
   175 
   178 
   176 	params := make(apiCallParams)
   179 	params := make(apiCallParams)
   177 	params["uri"] = uri
   180 	params["uri"] = uri
   178 
   181 
   179 	var account Account
   182 	var account Account
   180 	if err := mc.apiCall("follows", rest.Post, params, &account); err != nil {
   183 	if err := mc.apiCall("follows", rest.Post, params, nil, &account); err != nil {
   181 		return nil, err
   184 		return nil, err
   182 	}
   185 	}
   183 	if account.ID == 0 {
   186 	if account.ID == 0 {
   184 		return nil, ErrEntityNotFound
   187 		return nil, ErrEntityNotFound
   185 	}
   188 	}
   233 	}
   236 	}
   234 	return nil
   237 	return nil
   235 }
   238 }
   236 
   239 
   237 // SearchAccounts returns a list of accounts matching the query string
   240 // SearchAccounts returns a list of accounts matching the query string
   238 // The limit parameter is optional (can be 0).
   241 // The lopt parameter is optional (can be nil) or can be used to set a limit.
   239 func (mc *Client) SearchAccounts(query string, limit int) ([]Account, error) {
   242 func (mc *Client) SearchAccounts(query string, lopt *LimitParams) ([]Account, error) {
   240 	o := &getAccountsOptions{Q: query, Limit: limit}
   243 	o := &getAccountsOptions{Q: query, Limit: lopt}
   241 	return mc.getMultipleAccounts("search", o)
   244 	return mc.getMultipleAccounts("search", o)
   242 }
   245 }
   243 
   246 
   244 // GetBlockedAccounts returns the list of blocked accounts
   247 // GetBlockedAccounts returns the list of blocked accounts
   245 func (mc *Client) GetBlockedAccounts() ([]Account, error) {
   248 func (mc *Client) GetBlockedAccounts(lopt *LimitParams) ([]Account, error) {
   246 	return mc.getMultipleAccounts("blocks", nil)
   249 	return mc.getMultipleAccounts("blocks", nil)
   247 }
   250 }
   248 
   251 
   249 // GetMutedAccounts returns the list of muted accounts
   252 // GetMutedAccounts returns the list of muted accounts
   250 func (mc *Client) GetMutedAccounts() ([]Account, error) {
   253 func (mc *Client) GetMutedAccounts(lopt *LimitParams) ([]Account, error) {
   251 	return mc.getMultipleAccounts("mutes", nil)
   254 	return mc.getMultipleAccounts("mutes", nil)
   252 }
   255 }
   253 
   256 
   254 // GetAccountFollowRequests returns the list of follow requests accounts
   257 // GetAccountFollowRequests returns the list of follow requests accounts
   255 func (mc *Client) GetAccountFollowRequests() ([]Account, error) {
   258 func (mc *Client) GetAccountFollowRequests(lopt *LimitParams) ([]Account, error) {
   256 	return mc.getMultipleAccounts("follow_requests", nil)
   259 	return mc.getMultipleAccounts("follow_requests", nil)
   257 }
   260 }
   258 
   261 
   259 // GetAccountRelationships returns a list of relationship entities for the given accounts
   262 // GetAccountRelationships returns a list of relationship entities for the given accounts
   260 func (mc *Client) GetAccountRelationships(accountIDs []int) ([]Relationship, error) {
   263 func (mc *Client) GetAccountRelationships(accountIDs []int) ([]Relationship, error) {
   270 		qID := fmt.Sprintf("id[%d]", i+1)
   273 		qID := fmt.Sprintf("id[%d]", i+1)
   271 		params[qID] = strconv.Itoa(id)
   274 		params[qID] = strconv.Itoa(id)
   272 	}
   275 	}
   273 
   276 
   274 	var rl []Relationship
   277 	var rl []Relationship
   275 	if err := mc.apiCall("accounts/relationships", rest.Get, params, &rl); err != nil {
   278 	if err := mc.apiCall("accounts/relationships", rest.Get, params, nil, &rl); err != nil {
   276 		return nil, err
   279 		return nil, err
   277 	}
   280 	}
   278 	return rl, nil
   281 	return rl, nil
   279 }
   282 }
   280 
   283 
   281 // GetAccountStatuses returns a list of status entities for the given account
   284 // GetAccountStatuses returns a list of status entities for the given account
   282 // If onlyMedia is true, returns only statuses that have media attachments.
   285 // If onlyMedia is true, returns only statuses that have media attachments.
   283 // If excludeReplies is true, skip statuses that reply to other statuses.
   286 // If excludeReplies is true, skip statuses that reply to other statuses.
   284 func (mc *Client) GetAccountStatuses(accountID int, onlyMedia, excludeReplies bool) ([]Status, error) {
   287 func (mc *Client) GetAccountStatuses(accountID int, onlyMedia, excludeReplies bool, lopt *LimitParams) ([]Status, error) {
   285 	if accountID < 1 {
   288 	if accountID < 1 {
   286 		return nil, ErrInvalidID
   289 		return nil, ErrInvalidID
   287 	}
   290 	}
   288 
   291 
   289 	endPoint := "accounts/" + strconv.Itoa(accountID) + "/" + "statuses"
   292 	endPoint := "accounts/" + strconv.Itoa(accountID) + "/" + "statuses"
   294 	if excludeReplies {
   297 	if excludeReplies {
   295 		params["exclude_replies"] = "true"
   298 		params["exclude_replies"] = "true"
   296 	}
   299 	}
   297 
   300 
   298 	var sl []Status
   301 	var sl []Status
   299 	if err := mc.apiCall(endPoint, rest.Get, params, &sl); err != nil {
   302 	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &sl); err != nil {
   300 		return nil, err
   303 		return nil, err
   301 	}
   304 	}
   302 	return sl, nil
   305 	return sl, nil
   303 }
   306 }
   304 
   307