(un)follow/mute/block return a Relationship entity
authorMikael Berthe <mikael@lilotux.net>
Fri, 26 May 2017 11:50:32 +0200
changeset 183 cd5aa242c01f
parent 182 d360e5a41070
child 184 546db8bb5867
(un)follow/mute/block return a Relationship entity
account.go
--- a/account.go	Fri May 26 11:20:29 2017 +0200
+++ b/account.go	Fri May 26 11:50:32 2017 +0200
@@ -32,9 +32,31 @@
 	Limit *LimitParams
 }
 
+// updateRelationship returns a Relationship entity
+// The operation 'op' can be "follow", "unfollow", "block", "unblock",
+// "mute", "unmute".
+// The id is optional and depends on the operation.
+func (mc *Client) updateRelationship(op string, id int64) (*Relationship, error) {
+	var endPoint string
+	method := rest.Post
+	strID := strconv.FormatInt(id, 10)
+
+	switch op {
+	case "follow", "unfollow", "block", "unblock", "mute", "unmute":
+		endPoint = "accounts/" + strID + "/" + op
+	default:
+		return nil, ErrInvalidParameter
+	}
+
+	var rel Relationship
+	if err := mc.apiCall(endPoint, method, nil, nil, nil, &rel); err != nil {
+		return nil, err
+	}
+	return &rel, nil
+}
+
 // getSingleAccount returns an account entity
-// The operation 'op' can be "account", "verify_credentials", "follow",
-// "unfollow", "block", "unblock", "mute", "unmute",
+// The operation 'op' can be "account", "verify_credentials",
 // "follow_requests/authorize" or // "follow_requests/reject".
 // The id is optional and depends on the operation.
 func (mc *Client) getSingleAccount(op string, id int64) (*Account, error) {
@@ -47,9 +69,6 @@
 		endPoint = "accounts/" + strID
 	case "verify_credentials":
 		endPoint = "accounts/verify_credentials"
-	case "follow", "unfollow", "block", "unblock", "mute", "unmute":
-		endPoint = "accounts/" + strID + "/" + op
-		method = rest.Post
 	case "follow_requests/authorize", "follow_requests/reject":
 		// The documentation is incorrect, the endpoint actually
 		// is "follow_requests/:id/{authorize|reject}"
@@ -175,11 +194,11 @@
 
 // FollowAccount follows an account
 func (mc *Client) FollowAccount(accountID int64) error {
-	account, err := mc.getSingleAccount("follow", accountID)
+	rel, err := mc.updateRelationship("follow", accountID)
 	if err != nil {
 		return err
 	}
-	if account != nil && account.ID != accountID {
+	if rel == nil {
 		return ErrEntityNotFound
 	}
 	return nil
@@ -187,11 +206,11 @@
 
 // UnfollowAccount unfollows an account
 func (mc *Client) UnfollowAccount(accountID int64) error {
-	account, err := mc.getSingleAccount("unfollow", accountID)
+	rel, err := mc.updateRelationship("unfollow", accountID)
 	if err != nil {
 		return err
 	}
-	if account != nil && account.ID != accountID {
+	if rel == nil {
 		return ErrEntityNotFound
 	}
 	return nil
@@ -219,11 +238,11 @@
 
 // BlockAccount blocks an account
 func (mc *Client) BlockAccount(accountID int64) error {
-	account, err := mc.getSingleAccount("block", accountID)
+	rel, err := mc.updateRelationship("block", accountID)
 	if err != nil {
 		return err
 	}
-	if account != nil && account.ID != accountID {
+	if rel == nil {
 		return ErrEntityNotFound
 	}
 	return nil
@@ -231,11 +250,11 @@
 
 // UnblockAccount unblocks an account
 func (mc *Client) UnblockAccount(accountID int64) error {
-	account, err := mc.getSingleAccount("unblock", accountID)
+	rel, err := mc.updateRelationship("unblock", accountID)
 	if err != nil {
 		return err
 	}
-	if account != nil && account.ID != accountID {
+	if rel == nil {
 		return ErrEntityNotFound
 	}
 	return nil
@@ -243,11 +262,11 @@
 
 // MuteAccount mutes an account
 func (mc *Client) MuteAccount(accountID int64) error {
-	account, err := mc.getSingleAccount("mute", accountID)
+	rel, err := mc.updateRelationship("mute", accountID)
 	if err != nil {
 		return err
 	}
-	if account != nil && account.ID != accountID {
+	if rel == nil {
 		return ErrEntityNotFound
 	}
 	return nil
@@ -255,11 +274,11 @@
 
 // UnmuteAccount unmutes an account
 func (mc *Client) UnmuteAccount(accountID int64) error {
-	account, err := mc.getSingleAccount("unmute", accountID)
+	rel, err := mc.updateRelationship("unmute", accountID)
 	if err != nil {
 		return err
 	}
-	if account != nil && account.ID != accountID {
+	if rel == nil {
 		return ErrEntityNotFound
 	}
 	return nil