Skip to content

Commit ccdf72e

Browse files
committed
Get followers and following. Implemented method chaining
1 parent 9b22456 commit ccdf72e

File tree

4 files changed

+96
-16
lines changed

4 files changed

+96
-16
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP Library for Mastodon REST API
88
* Create and get authorization token, access token, client_id, client_secret and bearer token.
99
* Authenticate users
1010
* Get user information
11+
* Get user followers and following
12+
* Get user status
1113

1214
## Not yet in it
1315

@@ -72,4 +74,10 @@ $bearer_token = $t->authUser("vangelier@hotmail.com", "MySecretP@ssW0rd");
7274
*/
7375
7476
$user_info = $t->getUser("vangelier@hotmail.com", "MySecretP@ssW0rd");
77+
78+
/**
79+
* Get user followers / following
80+
*/
81+
$followers = $t->authenticate("vangelier@hotmail.com", "MySecretP@ssW0rd")
82+
->getFollowers();
7583
```

example.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@
4949
* Get the userinfo by authentication
5050
*/
5151

52-
$user_info = $t->getUser("vangelier@hotmail.com", "MySecretP@ssW0rd");
52+
$user_info = $t->getUser("vangelier@hotmail.com", "MySecretP@ssW0rd");
53+
54+
/**
55+
* Get user followers / following
56+
*/
57+
$followers = $t->authenticate("vangelier@hotmail.com", "MySecretP@ssW0rd")
58+
->getFollowers();

theCodingCompany/Mastodon.php

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ class Mastodon
2020
//Mastodon oAuth
2121
use \theCodingCompany\oAuth;
2222

23+
/**
24+
* Holds our current user_id for :id in API calls
25+
* @var type
26+
*/
27+
private $mastodon_user_id = null;
28+
29+
/**
30+
* Holds our current userinfo
31+
* @var type
32+
*/
33+
private $mastodon_userinfo = null;
34+
2335
/**
2436
* Construct new Mastodon class
2537
*/
@@ -49,27 +61,81 @@ public function createApp($name, $website_url){
4961
}
5062

5163
/**
52-
* Get mastodon user
64+
* Authenticate the user
5365
* @param type $username
5466
* @param type $password
5567
*/
56-
public function getUser($username, $password){
57-
//Authenticate the user
58-
$this->authUser($username, $password);
68+
public function authenticate($username = null, $password = null) {
69+
$this->authUser($username, $password);
70+
71+
//Set current working userid
72+
$this->mastodon_userinfo = $this->getUser();
5973

60-
//Create our object
61-
$http = HttpRequest::Instance($this->getApiURL());
62-
$user_info = $http::Get(
63-
"api/v1/accounts/verify_credentials",
64-
null,
65-
$this->getHeaders()
66-
);
67-
if(is_array($user_info) && isset($user_info["username"])){
68-
return $user_info;
74+
return $this; //For method chaining
75+
}
76+
77+
/**
78+
* Get mastodon user
79+
* @param type $username
80+
* @param type $password
81+
*/
82+
public function getUser(){
83+
if(empty($this->mastodon_userinfo)){
84+
//Create our object
85+
$http = HttpRequest::Instance($this->getApiURL());
86+
$user_info = $http::Get(
87+
"api/v1/accounts/verify_credentials",
88+
null,
89+
$this->getHeaders()
90+
);
91+
if(is_array($user_info) && isset($user_info["username"])){
92+
$this->mastodon_user_id = (int)$user_info["id"];
93+
return $user_info;
94+
}
6995
}
70-
return false;
96+
return $this->mastodon_userinfo;
7197
}
7298

99+
/**
100+
* Get current user's followers
101+
*/
102+
public function getFollowers(){
103+
if($this->mastodon_user_id > 0){
104+
105+
//Create our object
106+
$http = HttpRequest::Instance($this->getApiURL());
107+
$accounts = $http::Get(
108+
"api/v1/accounts/{$this->mastodon_user_id}/followers",
109+
null,
110+
$this->getHeaders()
111+
);
112+
if(is_array($accounts) && count($accounts) > 0){
113+
return $accounts;
114+
}
115+
116+
}
117+
return false;
118+
}
73119

120+
/**
121+
* Get current user's following
122+
*/
123+
public function getFollowing(){
124+
if($this->mastodon_user_id > 0){
125+
126+
//Create our object
127+
$http = HttpRequest::Instance($this->getApiURL());
128+
$accounts = $http::Get(
129+
"api/v1/accounts/{$this->mastodon_user_id}/following",
130+
null,
131+
$this->getHeaders()
132+
);
133+
if(is_array($accounts) && count($accounts) > 0){
134+
return $accounts;
135+
}
136+
137+
}
138+
return false;
139+
}
74140

75141
}

theCodingCompany/oAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function getAccessToken($auth_code = ""){
228228
* @param type $username usernam@domainname.com
229229
* @param type $password The password
230230
*/
231-
public function authUser($username = null, $password = null){
231+
private function authUser($username = null, $password = null){
232232
if(!empty($username) && stristr($username, "@") !== FALSE && !empty($password)){
233233

234234
//Get the API credentials

0 commit comments

Comments
 (0)