Skip to content

Commit 75b806a

Browse files
authored
Merge pull request #75 from GetStream/task/prepare-release
Add endpoints, commit tests
2 parents d6c52de + b0812fb commit 75b806a

5 files changed

Lines changed: 465 additions & 4 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace GetStream\Stream;
4+
5+
class Activities extends Feed
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $token;
11+
12+
/**
13+
* @param Client $client
14+
* @param string $api_key
15+
* @param string $token
16+
*/
17+
public function __construct($client, $api_key, $token)
18+
{
19+
$this->client = $client;
20+
$this->api_key = $api_key;
21+
$this->token = $token;
22+
}
23+
24+
/**
25+
* @param string $resource
26+
* @param string $action
27+
*
28+
* @return array
29+
*/
30+
protected function getHttpRequestHeaders($resource, $action)
31+
{
32+
$headers = parent::getHttpRequestHeaders($resource, $action);
33+
$headers['Authorization'] = $this->token;
34+
35+
return $headers;
36+
}
37+
38+
public function _getActivities($query_params)
39+
{
40+
if (empty($query_params)) {
41+
return;
42+
}
43+
44+
return $this->makeHttpRequest('activities/', 'GET', null, $query_params);
45+
}
46+
}

lib/GetStream/Stream/ActivityUpdateOperation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ protected function getHttpRequestHeaders($resource, $action)
3535
return $headers;
3636
}
3737

38+
public function partiallyUpdateActivity($data){
39+
return $this->makeHttpRequest('activity/', 'POST', $data);
40+
}
41+
3842
public function updateActivities($activities)
3943
{
4044
if (empty($activities)) {

lib/GetStream/Stream/Client.php

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace GetStream\Stream;
33

4+
use DateTime;
45
use Exception;
56

67
const VERSION = '2.6.0';
@@ -109,6 +110,19 @@ public function setLocation($location)
109110
* @return string
110111
*/
111112
public function createUserSessionToken($user_id, array $extra_data=null)
113+
{
114+
if(is_null($extra_data)){
115+
$extra_data = array();
116+
}
117+
return $this->createUserToken($user_id, $extra_data);
118+
}
119+
120+
/**
121+
* @param string $user_id
122+
* @param array $extra_data
123+
* @return string
124+
*/
125+
public function createUserToken($user_id, array $extra_data=null)
112126
{
113127
if(is_null($extra_data)){
114128
$extra_data = array();
@@ -215,12 +229,74 @@ public function buildRequestUrl($uri)
215229
return "{$baseUrl}/{$this->api_version}/{$uri}";
216230
}
217231

232+
public function getActivities($ids=null, $foreign_id_times=null)
233+
{
234+
if($ids!==null){
235+
$query_params = ["ids" => join(',', $ids)];
236+
} else {
237+
$fids = [];
238+
$times = [];
239+
foreach($foreign_id_times as $fit){
240+
$fids[] = $fit[0];
241+
try {
242+
$times[] = $fit[1]->format(DateTime::ISO8601);
243+
} catch(Exception $e) {
244+
// assume it's in the right format already
245+
$times[] = $fit[1];
246+
}
247+
}
248+
$query_params = [
249+
"foreign_ids" => join(',', $fids),
250+
"timestamps" => join(',', $times)
251+
];
252+
253+
}
254+
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
255+
$activities = new Activities($this, $this->api_key, $token);
256+
return $activities->_getActivities($query_params);
257+
}
258+
259+
public function batchPartialActivityUpdate($data)
260+
{
261+
if(count($data) > 100){
262+
throw new Exception("Max 100 activities allowed in batch update");
263+
}
264+
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
265+
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
266+
return $activityUpdateOp->partiallyUpdateActivity(["changes" => $data]);
267+
}
268+
269+
public function doPartialActivityUpdate($id=null, $foreign_id=null, $time=null, $set=null, $unset=null)
270+
{
271+
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
272+
if($id === null && ($foreign_id === null || $time === null)){
273+
throw new Exception(
274+
"The id or foreign_id+time parameters must be provided and not be None"
275+
);
276+
}
277+
if($id !== null && ($foreign_id !== null || $time !== null)){
278+
throw new Exception(
279+
"Only one of the id or the foreign_id+time parameters can be provided"
280+
);
281+
}
282+
283+
$data = ["set" => $set, "unset" => $unset];
284+
285+
if($id !== null){
286+
$data["id"] = $id;
287+
} else {
288+
$data["foreign_id"] = $foreign_id;
289+
$data["time"] = $time;
290+
}
291+
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
292+
return $activityUpdateOp->partiallyUpdateActivity($data);
293+
}
294+
218295
public function updateActivities($activities)
219296
{
220297
if (empty($activities)) {
221298
return;
222299
}
223-
224300
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
225301
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
226302
return $activityUpdateOp->updateActivities($activities);
@@ -231,7 +307,6 @@ public function updateActivity($activity)
231307
return $this->updateActivities([$activity]);
232308
}
233309

234-
235310
/**
236311
* Creates a redirect url for tracking the given events in the context of
237312
* getstream.io/personalization
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace GetStream\Integration;
4+
5+
use DateTime;
6+
use DateTimeZone;
7+
use Firebase\JWT\JWT;
8+
use GetStream\Stream\Client;
9+
use GetStream\Stream\Feed;
10+
use PHPUnit\Framework\TestCase;
11+
use Ramsey\Uuid\Uuid;
12+
13+
class CollectionTest extends TestCase
14+
{
15+
/**
16+
* @var Client
17+
*/
18+
protected $client;
19+
20+
/**
21+
* @var Feed
22+
*/
23+
protected $user1;
24+
25+
/**
26+
* @var Feed
27+
*/
28+
protected $user2;
29+
30+
/**
31+
* @var Feed
32+
*/
33+
protected $aggregated2;
34+
35+
/**
36+
* @var Feed
37+
*/
38+
protected $aggregated3;
39+
40+
/**
41+
* @var Feed
42+
*/
43+
protected $flat3;
44+
45+
/**
46+
* @var string
47+
*/
48+
protected $activity_id;
49+
50+
/**
51+
* @var Collections
52+
*/
53+
protected $collections;
54+
55+
protected function setUp()
56+
{
57+
$this->client = new Client(
58+
getenv('STREAM_API_KEY'),
59+
getenv('STREAM_API_SECRET'),
60+
'v1.0',
61+
getenv('STREAM_REGION')
62+
);
63+
$this->client->setLocation('qa');
64+
$this->client->timeout = 10000;
65+
$this->user1 = $this->client->feed('user', Uuid::uuid4());
66+
$this->user2 = $this->client->feed('user', Uuid::uuid4());
67+
// $this->aggregated2 = $this->client->feed('aggregated', Uuid::uuid4());
68+
// $this->aggregated3 = $this->client->feed('aggregated', Uuid::uuid4());
69+
// $this->flat3 = $this->client->feed('flat', Uuid::uuid4());
70+
// $activity_data = ['actor' => 1, 'verb' => 'tweet', 'object' => 1];
71+
// $response = $this->user1->addActivity($activity_data);
72+
// $this->activity_id = $response['id'];
73+
$this->collections = $this->client->collections();
74+
}
75+
76+
public function testUpsert()
77+
{
78+
$collection = $this->collections->upsert('animals', ['id' => '1', 'name' => 'bear', 'color' => 'blue']);
79+
$collection = $this->collections->upsert('items', ['id' => '42', 'name' => 'towel']);
80+
81+
}
82+
83+
public function testAddDataCollection()
84+
{
85+
$data = array('client' => 'php');
86+
$collection = $this->collections->add('like', $this->activity_id, 'bob', $data);
87+
$this->assertSame($collection['user_id'], 'bob');
88+
$this->assertSame($collection['kind'], 'like');
89+
$this->assertSame($collection['activity_id'], $this->activity_id);
90+
$this->assertSame($collection['data'], $data);
91+
}
92+
93+
public function testCreateReference()
94+
{
95+
$data = array('client' => 'php');
96+
$collection = $this->collections->add('like', $this->activity_id, 'bob', $data);
97+
$collectionId = $collection['id'];
98+
$refId = $this->collections->createReference($collection['id']);
99+
$this->assertSame($refId, 'SR:' . $collectionId);
100+
$refObj = $this->collections->createReference($collection);
101+
$this->assertSame($refObj, 'SR:' . $collectionId);
102+
}
103+
104+
public function testAddChildCollection()
105+
{
106+
$initial_collection = $this->collections->add('like', $this->activity_id, 'bob');
107+
$child_collection = $this->collections->addChild('like', $initial_collection['id'], 'alice');
108+
$this->assertSame($child_collection['user_id'], 'alice');
109+
$this->assertSame($initial_collection['user_id'], 'bob');
110+
$this->assertSame($child_collection['kind'], 'like');
111+
$this->assertSame($child_collection['activity_id'], $this->activity_id);
112+
$this->assertSame($child_collection['parent'], $initial_collection['id']);
113+
}
114+
115+
public function testAddTargetFeedsCollection()
116+
{
117+
$target_feeds = array($this->aggregated2->getId(), $this->aggregated3->getId());
118+
$collection = $this->collections->add('like', $this->activity_id, 'bob', null, $target_feeds);
119+
$this->assertSame($collection['user_id'], 'bob');
120+
$this->assertSame($collection['kind'], 'like');
121+
$this->assertSame($collection['activity_id'], $this->activity_id);
122+
$response = $this->aggregated2->getActivities($offset=0, $limit=3);
123+
// check a targeted feed
124+
$latest_activity = $response["results"][0]['activities'][0];
125+
$this->assertSame(
126+
$latest_activity["collection"],
127+
$this->collections->createReference($collection)
128+
);
129+
$this->assertSame($latest_activity["verb"], "like");
130+
}
131+
132+
public function testGetCollection(){
133+
$created_collection = $this->collections->add('like', $this->activity_id, 'bob');
134+
$retrieved_collection = $this->collections->get($created_collection['id']);
135+
$this->assertSame($created_collection['id'], $retrieved_collection['id']);
136+
$this->assertSame($created_collection['user_id'], $retrieved_collection['user_id']);
137+
$this->assertSame($created_collection['kind'], $retrieved_collection['kind']);
138+
$this->assertSame($created_collection['created_at'], $retrieved_collection['created_at']);
139+
}
140+
141+
/**
142+
* @expectedException \GetStream\Stream\StreamFeedException
143+
*/
144+
public function testDeleteCollection(){
145+
$created_collection = $this->collections->add('like', $this->activity_id, 'bob');
146+
$retrieved_collection = $this->collections->get($created_collection['id']);
147+
$this->collections->delete($created_collection['id']);
148+
$retrieved_collection = $this->collections->get($created_collection['id']);
149+
}
150+
151+
public function testUpdateCollection(){
152+
$data = array('client' => 'php');
153+
$created_collection = $this->collections->add('unlike', $this->activity_id, 'bob', $data);
154+
$retrieved_collection = $this->collections->get($created_collection['id']);
155+
$updated_data = array('client' => 'updated-php', 'more' => 'kets');
156+
$updated_collection = $this->collections->update($created_collection['id'], $updated_data);
157+
$this->assertSame($retrieved_collection['data'], $data);
158+
$this->assertSame($updated_collection['data'], $updated_data);
159+
}
160+
161+
public function testFilterCollection(){
162+
$collections = $this->collections->filter('user_id', 'bob', 'like');
163+
foreach($collections['results'] as $collection){
164+
$this->assertSame($collection['kind'], 'like');
165+
$this->assertSame($collection['user_id'], 'bob');
166+
}
167+
$collections = $this->collections->filter('user_id', 'bob', 'unlike');
168+
foreach($collections['results'] as $collection){
169+
$this->assertSame($collection['kind'], 'unlike');
170+
$this->assertSame($collection['user_id'], 'bob');
171+
}
172+
}
173+
174+
}

0 commit comments

Comments
 (0)