This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relationship.go
114 lines (93 loc) · 3.41 KB
/
relationship.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gostagram
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
// Relationship struct represent
// relationship status with another user.
type Relationship struct {
OutgoingStatus string `mapstructure:"outgoing_status"`
IncomingStatus string `mapstructure:"incoming_status"`
}
// check if current user follows other
// user
func (r Relationship) Follows() bool {
return r.OutgoingStatus == "follows"
}
// check if current user send a request
// to follow other user.
func (r Relationship) Requested() bool {
return r.OutgoingStatus == "requested"
}
// check if current user was followed by
// other user
func (r Relationship) FollowedBy() bool {
return r.IncomingStatus == "followed_by"
}
// check if current user was requested by
// other user to accept the follow.
func (r Relationship) RequestedBy() bool {
return r.IncomingStatus == "requested_by"
}
// check if current user was blocked by
// other user.
func (r Relationship) BlockedByYou() bool {
return r.IncomingStatus == "blocked_by_you"
}
// Send a post request, with an especific action
// like follow, unfollow, ignore, block, unblock and approve,
// for more information about it, go to
// https://www.instagram.com/developer/endpoints/relationships/#post_relationship
func (c Client) doRelationshipAction(action, uri string) (*Relationship, error) {
tmp, err := c.post(uri, bodyData{
"action": action,
})
if err != nil {
return nil, err
}
tmpRel := (*tmp).(map[string]interface{})
var relationship Relationship
if err = mapstructure.Decode(tmpRel, &relationship); err != nil {
return nil, err
}
return &relationship, nil
}
// Check current user relationship with other user by it's id,
// for more information about it, go to
// https://www.instagram.com/developer/endpoints/relationships/#get_relationship
func (c Client) GetCurrentUserRelationship(user_id string) (*Relationship, error) {
tmp, _, err := c.get(fmt.Sprintf("%susers/%s/relationship?access_token=%s", apiUrl, user_id, c.access_token))
if err != nil {
return nil, err
}
tmpRel := (*tmp).(map[string]interface{})
var relationship Relationship
if err = mapstructure.Decode(tmpRel, &relationship); err != nil {
return nil, err
}
return &relationship, nil
}
func (c Client) FollowUserById(user_id string) (*Relationship, error) {
return c.doRelationshipAction("follow", fmt.Sprintf("%susers/%s/relationship?access_token=%s",
apiUrl, user_id, c.access_token))
}
func (c Client) UnFollowUserById(user_id string) (*Relationship, error) {
return c.doRelationshipAction("unfollow", fmt.Sprintf("%susers/%s/relationship?access_token=%s",
apiUrl, user_id, c.access_token))
}
func (c Client) ApproveUserById(user_id string) (*Relationship, error) {
return c.doRelationshipAction("approve", fmt.Sprintf("%susers/%s/relationship?access_token=%s",
apiUrl, user_id, c.access_token))
}
func (c Client) IgnoreUserById(user_id string) (*Relationship, error) {
return c.doRelationshipAction("ignore", fmt.Sprintf("%susers/%s/relationship?access_token=%s",
apiUrl, user_id, c.access_token))
}
func (c Client) BlockUserById(user_id string) (*Relationship, error) {
return c.doRelationshipAction("block", fmt.Sprintf("%susers/%s/relationship?access_token=%s",
apiUrl, user_id, c.access_token))
}
func (c Client) UnBlockUserById(user_id string) (*Relationship, error) {
return c.doRelationshipAction("unblock", fmt.Sprintf("%susers/%s/relationship?access_token=%s",
apiUrl, user_id, c.access_token))
}