account.go
changeset 183 cd5aa242c01f
parent 162 68df3a01e1a7
child 184 546db8bb5867
equal deleted inserted replaced
182:d360e5a41070 183:cd5aa242c01f
    30 	Q string
    30 	Q string
    31 
    31 
    32 	Limit *LimitParams
    32 	Limit *LimitParams
    33 }
    33 }
    34 
    34 
       
    35 // updateRelationship returns a Relationship entity
       
    36 // The operation 'op' can be "follow", "unfollow", "block", "unblock",
       
    37 // "mute", "unmute".
       
    38 // The id is optional and depends on the operation.
       
    39 func (mc *Client) updateRelationship(op string, id int64) (*Relationship, error) {
       
    40 	var endPoint string
       
    41 	method := rest.Post
       
    42 	strID := strconv.FormatInt(id, 10)
       
    43 
       
    44 	switch op {
       
    45 	case "follow", "unfollow", "block", "unblock", "mute", "unmute":
       
    46 		endPoint = "accounts/" + strID + "/" + op
       
    47 	default:
       
    48 		return nil, ErrInvalidParameter
       
    49 	}
       
    50 
       
    51 	var rel Relationship
       
    52 	if err := mc.apiCall(endPoint, method, nil, nil, nil, &rel); err != nil {
       
    53 		return nil, err
       
    54 	}
       
    55 	return &rel, nil
       
    56 }
       
    57 
    35 // getSingleAccount returns an account entity
    58 // getSingleAccount returns an account entity
    36 // The operation 'op' can be "account", "verify_credentials", "follow",
    59 // The operation 'op' can be "account", "verify_credentials",
    37 // "unfollow", "block", "unblock", "mute", "unmute",
       
    38 // "follow_requests/authorize" or // "follow_requests/reject".
    60 // "follow_requests/authorize" or // "follow_requests/reject".
    39 // The id is optional and depends on the operation.
    61 // The id is optional and depends on the operation.
    40 func (mc *Client) getSingleAccount(op string, id int64) (*Account, error) {
    62 func (mc *Client) getSingleAccount(op string, id int64) (*Account, error) {
    41 	var endPoint string
    63 	var endPoint string
    42 	method := rest.Get
    64 	method := rest.Get
    45 	switch op {
    67 	switch op {
    46 	case "account":
    68 	case "account":
    47 		endPoint = "accounts/" + strID
    69 		endPoint = "accounts/" + strID
    48 	case "verify_credentials":
    70 	case "verify_credentials":
    49 		endPoint = "accounts/verify_credentials"
    71 		endPoint = "accounts/verify_credentials"
    50 	case "follow", "unfollow", "block", "unblock", "mute", "unmute":
       
    51 		endPoint = "accounts/" + strID + "/" + op
       
    52 		method = rest.Post
       
    53 	case "follow_requests/authorize", "follow_requests/reject":
    72 	case "follow_requests/authorize", "follow_requests/reject":
    54 		// The documentation is incorrect, the endpoint actually
    73 		// The documentation is incorrect, the endpoint actually
    55 		// is "follow_requests/:id/{authorize|reject}"
    74 		// is "follow_requests/:id/{authorize|reject}"
    56 		endPoint = op[:16] + strID + "/" + op[16:]
    75 		endPoint = op[:16] + strID + "/" + op[16:]
    57 		method = rest.Post
    76 		method = rest.Post
   173 	return mc.getMultipleAccountsHelper("following", o)
   192 	return mc.getMultipleAccountsHelper("following", o)
   174 }
   193 }
   175 
   194 
   176 // FollowAccount follows an account
   195 // FollowAccount follows an account
   177 func (mc *Client) FollowAccount(accountID int64) error {
   196 func (mc *Client) FollowAccount(accountID int64) error {
   178 	account, err := mc.getSingleAccount("follow", accountID)
   197 	rel, err := mc.updateRelationship("follow", accountID)
   179 	if err != nil {
   198 	if err != nil {
   180 		return err
   199 		return err
   181 	}
   200 	}
   182 	if account != nil && account.ID != accountID {
   201 	if rel == nil {
   183 		return ErrEntityNotFound
   202 		return ErrEntityNotFound
   184 	}
   203 	}
   185 	return nil
   204 	return nil
   186 }
   205 }
   187 
   206 
   188 // UnfollowAccount unfollows an account
   207 // UnfollowAccount unfollows an account
   189 func (mc *Client) UnfollowAccount(accountID int64) error {
   208 func (mc *Client) UnfollowAccount(accountID int64) error {
   190 	account, err := mc.getSingleAccount("unfollow", accountID)
   209 	rel, err := mc.updateRelationship("unfollow", accountID)
   191 	if err != nil {
   210 	if err != nil {
   192 		return err
   211 		return err
   193 	}
   212 	}
   194 	if account != nil && account.ID != accountID {
   213 	if rel == nil {
   195 		return ErrEntityNotFound
   214 		return ErrEntityNotFound
   196 	}
   215 	}
   197 	return nil
   216 	return nil
   198 }
   217 }
   199 
   218 
   217 	return &account, nil
   236 	return &account, nil
   218 }
   237 }
   219 
   238 
   220 // BlockAccount blocks an account
   239 // BlockAccount blocks an account
   221 func (mc *Client) BlockAccount(accountID int64) error {
   240 func (mc *Client) BlockAccount(accountID int64) error {
   222 	account, err := mc.getSingleAccount("block", accountID)
   241 	rel, err := mc.updateRelationship("block", accountID)
   223 	if err != nil {
   242 	if err != nil {
   224 		return err
   243 		return err
   225 	}
   244 	}
   226 	if account != nil && account.ID != accountID {
   245 	if rel == nil {
   227 		return ErrEntityNotFound
   246 		return ErrEntityNotFound
   228 	}
   247 	}
   229 	return nil
   248 	return nil
   230 }
   249 }
   231 
   250 
   232 // UnblockAccount unblocks an account
   251 // UnblockAccount unblocks an account
   233 func (mc *Client) UnblockAccount(accountID int64) error {
   252 func (mc *Client) UnblockAccount(accountID int64) error {
   234 	account, err := mc.getSingleAccount("unblock", accountID)
   253 	rel, err := mc.updateRelationship("unblock", accountID)
   235 	if err != nil {
   254 	if err != nil {
   236 		return err
   255 		return err
   237 	}
   256 	}
   238 	if account != nil && account.ID != accountID {
   257 	if rel == nil {
   239 		return ErrEntityNotFound
   258 		return ErrEntityNotFound
   240 	}
   259 	}
   241 	return nil
   260 	return nil
   242 }
   261 }
   243 
   262 
   244 // MuteAccount mutes an account
   263 // MuteAccount mutes an account
   245 func (mc *Client) MuteAccount(accountID int64) error {
   264 func (mc *Client) MuteAccount(accountID int64) error {
   246 	account, err := mc.getSingleAccount("mute", accountID)
   265 	rel, err := mc.updateRelationship("mute", accountID)
   247 	if err != nil {
   266 	if err != nil {
   248 		return err
   267 		return err
   249 	}
   268 	}
   250 	if account != nil && account.ID != accountID {
   269 	if rel == nil {
   251 		return ErrEntityNotFound
   270 		return ErrEntityNotFound
   252 	}
   271 	}
   253 	return nil
   272 	return nil
   254 }
   273 }
   255 
   274 
   256 // UnmuteAccount unmutes an account
   275 // UnmuteAccount unmutes an account
   257 func (mc *Client) UnmuteAccount(accountID int64) error {
   276 func (mc *Client) UnmuteAccount(accountID int64) error {
   258 	account, err := mc.getSingleAccount("unmute", accountID)
   277 	rel, err := mc.updateRelationship("unmute", accountID)
   259 	if err != nil {
   278 	if err != nil {
   260 		return err
   279 		return err
   261 	}
   280 	}
   262 	if account != nil && account.ID != accountID {
   281 	if rel == nil {
   263 		return ErrEntityNotFound
   282 		return ErrEntityNotFound
   264 	}
   283 	}
   265 	return nil
   284 	return nil
   266 }
   285 }
   267 
   286