-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathFeedFeaturesTrait.php
More file actions
142 lines (126 loc) · 4.31 KB
/
Copy pathFeedFeaturesTrait.php
File metadata and controls
142 lines (126 loc) · 4.31 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Instagram\SDK\Client\Features;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\PromiseInterface;
use Instagram\SDK\Exceptions\InstagramException;
use Instagram\SDK\Request\Payload\Feed\TimelineRequestPayload;
use Instagram\SDK\Response\Responses\Feed\FeedResponse;
use Instagram\SDK\Response\Responses\Feed\TimelineResponse;
use const Instagram\SDK\TYPE_HASHTAG;
use const Instagram\SDK\TYPE_LOCATION;
use const Instagram\SDK\TYPE_USER;
/**
* Trait FeedFeaturesTrait
*
* @package Instagram\SDK\Client\Features
*/
trait FeedFeaturesTrait
{
use DefaultFeaturesTrait;
/**
* Retrieve feed by hashtag.
*
* @param string $hashTag
* @return PromiseInterface<FeedResponse|InstagramException>
*/
public function feedByHashtag(string $hashTag): PromiseInterface
{
return $this->feed(TYPE_HASHTAG, $hashTag);
}
/**
* Retrieve feed by hashtag.
*
* @param string $locationId
* @return PromiseInterface<FeedResponse|InstagramException>
*/
public function feedByLocation(string $locationId): PromiseInterface
{
return $this->feed(TYPE_LOCATION, $locationId);
}
/**
* Retrieve feed by user id.
*
* @param string $userId
* @return PromiseInterface<FeedResponse|InstagramException>
*/
public function feedByUser(string $userId): PromiseInterface
{
return $this->feed(TYPE_USER, $userId);
}
/**
* Retrieve the feed for a specified hashtag.
*
* @param int $type
* @param string $query
* @param string|null $maxId
* @return PromiseInterface<FeedResponse|InstagramException>
*/
public function feed(int $type, string $query, ?string $maxId = null): PromiseInterface
{
switch ($type) {
case TYPE_HASHTAG:
$result = $this->queryFeed($type, 'feed/tag/%s/', $query, $maxId);
break;
case TYPE_USER:
$result = $this->queryFeed($type, 'feed/user/%s/', $query, $maxId);
break;
case TYPE_LOCATION:
$result = $this->queryFeed($type, 'feed/location/%s/', $query, $maxId);
break;
default:
$result = $this->getInvalidFeedTypeError();
break;
}
return $result;
}
/**
* Retrieve the timeline feed for the current user.
*
* @param TimelineRequestPayload $payload
* @return PromiseInterface<TimelineResponse|InstagramException>
*/
public function timeline(TimelineRequestPayload $payload): PromiseInterface
{
return $this->authenticated(function () use ($payload): PromiseInterface {
// @phan-suppress-next-line PhanThrowTypeAbsentForCall, PhanUndeclaredMethod
$request = $this->buildRequest('feed/timeline/', new TimelineResponse())
->addCSRFToken($this->session)
->addUuid($this->session)
->addPhoneId($this->session)
->addSessionId($this->session)
->addPayloadParam('reason', 'cold_start_fetch')
->addPayloadParams($payload);
return $this->call($request);
});
}
/**
* Query for the feed result.
*
* @param int $type
* @param string $uri
* @param string $query
* @param string|null $maxId
* @return PromiseInterface<FeedResponse|InstagramException>
*/
protected function queryFeed(int $type, string $uri, string $query, ?string $maxId): PromiseInterface
{
return $this->authenticated(function () use ($type, $uri, $query, $maxId): PromiseInterface {
$response = new FeedResponse($tag = rawurlencode($query), $type);
// @phan-suppress-next-line PhanPluginPrintfVariableFormatString
$request = $this->buildRequest(sprintf($uri, $tag), $response)
->addQueryParamIfNotNull('max_id', $maxId);
return $this->call($request);
});
}
/**
* Returns the invalid type error.
*
* @return PromiseInterface
*/
protected function getInvalidFeedTypeError(): PromiseInterface
{
// @phan-suppress-next-line PhanDeprecatedFunction
return Create::rejectionFor('Invalid feed type provided');
}
}