account.go
changeset 159 408aa794d9bb
parent 156 70aadba26338
child 160 9f7e683b323f
equal deleted inserted replaced
158:083d33bb419b 159:408aa794d9bb
    21 )
    21 )
    22 
    22 
    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 int64
    27 
    27 
    28 	// The Q field (query) is used when searching for accounts
    28 	// The Q field (query) is used when searching for accounts
    29 	Q string
    29 	Q string
    30 
    30 
    31 	Limit *LimitParams
    31 	Limit *LimitParams
    34 // getSingleAccount returns an account entity
    34 // getSingleAccount returns an account entity
    35 // The operation 'op' can be "account", "verify_credentials", "follow",
    35 // The operation 'op' can be "account", "verify_credentials", "follow",
    36 // "unfollow", "block", "unblock", "mute", "unmute",
    36 // "unfollow", "block", "unblock", "mute", "unmute",
    37 // "follow_requests/authorize" or // "follow_requests/reject".
    37 // "follow_requests/authorize" or // "follow_requests/reject".
    38 // The id is optional and depends on the operation.
    38 // The id is optional and depends on the operation.
    39 func (mc *Client) getSingleAccount(op string, id int) (*Account, error) {
    39 func (mc *Client) getSingleAccount(op string, id int64) (*Account, error) {
    40 	var endPoint string
    40 	var endPoint string
    41 	method := rest.Get
    41 	method := rest.Get
    42 	strID := strconv.Itoa(id)
    42 	strID := strconv.FormatInt(id, 10)
    43 
    43 
    44 	switch op {
    44 	switch op {
    45 	case "account":
    45 	case "account":
    46 		endPoint = "accounts/" + strID
    46 		endPoint = "accounts/" + strID
    47 	case "verify_credentials":
    47 	case "verify_credentials":
    82 	switch op {
    82 	switch op {
    83 	case "followers", "following":
    83 	case "followers", "following":
    84 		if opts == nil || opts.ID < 1 {
    84 		if opts == nil || opts.ID < 1 {
    85 			return []Account{}, ErrInvalidID
    85 			return []Account{}, ErrInvalidID
    86 		}
    86 		}
    87 		endPoint = "accounts/" + strconv.Itoa(opts.ID) + "/" + op
    87 		endPoint = "accounts/" + strconv.FormatInt(opts.ID, 10) + "/" + op
    88 	case "follow_requests", "blocks", "mutes":
    88 	case "follow_requests", "blocks", "mutes":
    89 		endPoint = op
    89 		endPoint = op
    90 	case "search":
    90 	case "search":
    91 		if opts == nil || opts.Q == "" {
    91 		if opts == nil || opts.Q == "" {
    92 			return []Account{}, ErrInvalidParameter
    92 			return []Account{}, ErrInvalidParameter
    94 		endPoint = "accounts/" + op
    94 		endPoint = "accounts/" + op
    95 	case "reblogged_by", "favourited_by":
    95 	case "reblogged_by", "favourited_by":
    96 		if opts == nil || opts.ID < 1 {
    96 		if opts == nil || opts.ID < 1 {
    97 			return []Account{}, ErrInvalidID
    97 			return []Account{}, ErrInvalidID
    98 		}
    98 		}
    99 		endPoint = "statuses/" + strconv.Itoa(opts.ID) + "/" + op
    99 		endPoint = "statuses/" + strconv.FormatInt(opts.ID, 10) + "/" + op
   100 	default:
   100 	default:
   101 		return nil, ErrInvalidParameter
   101 		return nil, ErrInvalidParameter
   102 	}
   102 	}
   103 
   103 
   104 	// Handle target-specific query parameters
   104 	// Handle target-specific query parameters
   128 }
   128 }
   129 
   129 
   130 // GetAccount returns an account entity
   130 // GetAccount returns an account entity
   131 // The returned value can be nil if there is an error or if the
   131 // The returned value can be nil if there is an error or if the
   132 // requested ID does not exist.
   132 // requested ID does not exist.
   133 func (mc *Client) GetAccount(accountID int) (*Account, error) {
   133 func (mc *Client) GetAccount(accountID int64) (*Account, error) {
   134 	account, err := mc.getSingleAccount("account", accountID)
   134 	account, err := mc.getSingleAccount("account", accountID)
   135 	if err != nil {
   135 	if err != nil {
   136 		return nil, err
   136 		return nil, err
   137 	}
   137 	}
   138 	if account != nil && account.ID == 0 {
   138 	if account != nil && account.ID == 0 {
   152 	}
   152 	}
   153 	return account, nil
   153 	return account, nil
   154 }
   154 }
   155 
   155 
   156 // GetAccountFollowers returns the list of accounts following a given account
   156 // GetAccountFollowers returns the list of accounts following a given account
   157 func (mc *Client) GetAccountFollowers(accountID int, lopt *LimitParams) ([]Account, error) {
   157 func (mc *Client) GetAccountFollowers(accountID int64, lopt *LimitParams) ([]Account, error) {
   158 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   158 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   159 	return mc.getMultipleAccounts("followers", o)
   159 	return mc.getMultipleAccounts("followers", o)
   160 }
   160 }
   161 
   161 
   162 // GetAccountFollowing returns the list of accounts a given account is following
   162 // GetAccountFollowing returns the list of accounts a given account is following
   163 func (mc *Client) GetAccountFollowing(accountID int, lopt *LimitParams) ([]Account, error) {
   163 func (mc *Client) GetAccountFollowing(accountID int64, lopt *LimitParams) ([]Account, error) {
   164 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   164 	o := &getAccountsOptions{ID: accountID, Limit: lopt}
   165 	return mc.getMultipleAccounts("following", o)
   165 	return mc.getMultipleAccounts("following", o)
   166 }
   166 }
   167 
   167 
   168 // FollowAccount follows an account
   168 // FollowAccount follows an account
   169 func (mc *Client) FollowAccount(accountID int) error {
   169 func (mc *Client) FollowAccount(accountID int64) error {
   170 	account, err := mc.getSingleAccount("follow", accountID)
   170 	account, err := mc.getSingleAccount("follow", accountID)
   171 	if err != nil {
   171 	if err != nil {
   172 		return err
   172 		return err
   173 	}
   173 	}
   174 	if account != nil && account.ID != accountID {
   174 	if account != nil && account.ID != accountID {
   176 	}
   176 	}
   177 	return nil
   177 	return nil
   178 }
   178 }
   179 
   179 
   180 // UnfollowAccount unfollows an account
   180 // UnfollowAccount unfollows an account
   181 func (mc *Client) UnfollowAccount(accountID int) error {
   181 func (mc *Client) UnfollowAccount(accountID int64) error {
   182 	account, err := mc.getSingleAccount("unfollow", accountID)
   182 	account, err := mc.getSingleAccount("unfollow", accountID)
   183 	if err != nil {
   183 	if err != nil {
   184 		return err
   184 		return err
   185 	}
   185 	}
   186 	if account != nil && account.ID != accountID {
   186 	if account != nil && account.ID != accountID {
   208 	}
   208 	}
   209 	return &account, nil
   209 	return &account, nil
   210 }
   210 }
   211 
   211 
   212 // BlockAccount blocks an account
   212 // BlockAccount blocks an account
   213 func (mc *Client) BlockAccount(accountID int) error {
   213 func (mc *Client) BlockAccount(accountID int64) error {
   214 	account, err := mc.getSingleAccount("block", accountID)
   214 	account, err := mc.getSingleAccount("block", accountID)
   215 	if err != nil {
   215 	if err != nil {
   216 		return err
   216 		return err
   217 	}
   217 	}
   218 	if account != nil && account.ID != accountID {
   218 	if account != nil && account.ID != accountID {
   220 	}
   220 	}
   221 	return nil
   221 	return nil
   222 }
   222 }
   223 
   223 
   224 // UnblockAccount unblocks an account
   224 // UnblockAccount unblocks an account
   225 func (mc *Client) UnblockAccount(accountID int) error {
   225 func (mc *Client) UnblockAccount(accountID int64) error {
   226 	account, err := mc.getSingleAccount("unblock", accountID)
   226 	account, err := mc.getSingleAccount("unblock", accountID)
   227 	if err != nil {
   227 	if err != nil {
   228 		return err
   228 		return err
   229 	}
   229 	}
   230 	if account != nil && account.ID != accountID {
   230 	if account != nil && account.ID != accountID {
   232 	}
   232 	}
   233 	return nil
   233 	return nil
   234 }
   234 }
   235 
   235 
   236 // MuteAccount mutes an account
   236 // MuteAccount mutes an account
   237 func (mc *Client) MuteAccount(accountID int) error {
   237 func (mc *Client) MuteAccount(accountID int64) error {
   238 	account, err := mc.getSingleAccount("mute", accountID)
   238 	account, err := mc.getSingleAccount("mute", accountID)
   239 	if err != nil {
   239 	if err != nil {
   240 		return err
   240 		return err
   241 	}
   241 	}
   242 	if account != nil && account.ID != accountID {
   242 	if account != nil && account.ID != accountID {
   244 	}
   244 	}
   245 	return nil
   245 	return nil
   246 }
   246 }
   247 
   247 
   248 // UnmuteAccount unmutes an account
   248 // UnmuteAccount unmutes an account
   249 func (mc *Client) UnmuteAccount(accountID int) error {
   249 func (mc *Client) UnmuteAccount(accountID int64) error {
   250 	account, err := mc.getSingleAccount("unmute", accountID)
   250 	account, err := mc.getSingleAccount("unmute", accountID)
   251 	if err != nil {
   251 	if err != nil {
   252 		return err
   252 		return err
   253 	}
   253 	}
   254 	if account != nil && account.ID != accountID {
   254 	if account != nil && account.ID != accountID {
   284 	o := &getAccountsOptions{Limit: lopt}
   284 	o := &getAccountsOptions{Limit: lopt}
   285 	return mc.getMultipleAccounts("follow_requests", o)
   285 	return mc.getMultipleAccounts("follow_requests", o)
   286 }
   286 }
   287 
   287 
   288 // GetAccountRelationships returns a list of relationship entities for the given accounts
   288 // GetAccountRelationships returns a list of relationship entities for the given accounts
   289 func (mc *Client) GetAccountRelationships(accountIDs []int) ([]Relationship, error) {
   289 func (mc *Client) GetAccountRelationships(accountIDs []int64) ([]Relationship, error) {
   290 	if len(accountIDs) < 1 {
   290 	if len(accountIDs) < 1 {
   291 		return nil, ErrInvalidID
   291 		return nil, ErrInvalidID
   292 	}
   292 	}
   293 
   293 
   294 	params := make(apiCallParams)
   294 	params := make(apiCallParams)
   295 	for i, id := range accountIDs {
   295 	for i, id := range accountIDs {
   296 		if id < 1 {
   296 		if id < 1 {
   297 			return nil, ErrInvalidID
   297 			return nil, ErrInvalidID
   298 		}
   298 		}
   299 		qID := fmt.Sprintf("id[%d]", i+1)
   299 		qID := fmt.Sprintf("id[%d]", i+1)
   300 		params[qID] = strconv.Itoa(id)
   300 		params[qID] = strconv.FormatInt(id, 10)
   301 	}
   301 	}
   302 
   302 
   303 	var rl []Relationship
   303 	var rl []Relationship
   304 	if err := mc.apiCall("accounts/relationships", rest.Get, params, nil, nil, &rl); err != nil {
   304 	if err := mc.apiCall("accounts/relationships", rest.Get, params, nil, nil, &rl); err != nil {
   305 		return nil, err
   305 		return nil, err
   312 // If excludeReplies is true, skip statuses that reply to other statuses.
   312 // If excludeReplies is true, skip statuses that reply to other statuses.
   313 // If lopt.All is true, several requests will be made until the API server
   313 // If lopt.All is true, several requests will be made until the API server
   314 // has nothing to return.
   314 // has nothing to return.
   315 // If lopt.Limit is set (and not All), several queries can be made until the
   315 // If lopt.Limit is set (and not All), several queries can be made until the
   316 // limit is reached.
   316 // limit is reached.
   317 func (mc *Client) GetAccountStatuses(accountID int, onlyMedia, excludeReplies bool, lopt *LimitParams) ([]Status, error) {
   317 func (mc *Client) GetAccountStatuses(accountID int64, onlyMedia, excludeReplies bool, lopt *LimitParams) ([]Status, error) {
   318 	if accountID < 1 {
   318 	if accountID < 1 {
   319 		return nil, ErrInvalidID
   319 		return nil, ErrInvalidID
   320 	}
   320 	}
   321 
   321 
   322 	endPoint := "accounts/" + strconv.Itoa(accountID) + "/" + "statuses"
   322 	endPoint := "accounts/" + strconv.FormatInt(accountID, 10) + "/" + "statuses"
   323 	params := make(apiCallParams)
   323 	params := make(apiCallParams)
   324 	if onlyMedia {
   324 	if onlyMedia {
   325 		params["only_media"] = "true"
   325 		params["only_media"] = "true"
   326 	}
   326 	}
   327 	if excludeReplies {
   327 	if excludeReplies {
   347 	}
   347 	}
   348 	return sl, nil
   348 	return sl, nil
   349 }
   349 }
   350 
   350 
   351 // FollowRequestAuthorize authorizes or rejects an account follow-request
   351 // FollowRequestAuthorize authorizes or rejects an account follow-request
   352 func (mc *Client) FollowRequestAuthorize(accountID int, authorize bool) error {
   352 func (mc *Client) FollowRequestAuthorize(accountID int64, authorize bool) error {
   353 	endPoint := "follow_requests/reject"
   353 	endPoint := "follow_requests/reject"
   354 	if authorize {
   354 	if authorize {
   355 		endPoint = "follow_requests/authorize"
   355 		endPoint = "follow_requests/authorize"
   356 	}
   356 	}
   357 	_, err := mc.getSingleAccount(endPoint, accountID)
   357 	_, err := mc.getSingleAccount(endPoint, accountID)