@@ -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}
0 commit comments