Skip to content

Commit b0812fb

Browse files
committed
Add batchPartialActivityUpdate method
1 parent cb48558 commit b0812fb

3 files changed

Lines changed: 89 additions & 3 deletions

File tree

lib/GetStream/Stream/ActivityUpdateOperation.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ protected function getHttpRequestHeaders($resource, $action)
3737

3838
public function partiallyUpdateActivity($data){
3939
return $this->makeHttpRequest('activity/', 'POST', $data);
40-
4140
}
4241

4342
public function updateActivities($activities)

lib/GetStream/Stream/Client.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ public function getActivities($ids=null, $foreign_id_times=null)
256256
return $activities->_getActivities($query_params);
257257
}
258258

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+
259269
public function doPartialActivityUpdate($id=null, $foreign_id=null, $time=null, $set=null, $unset=null)
260270
{
261271
$token = $this->signer->jwtScopeToken('*', 'activities', '*');

tests/integration/FeedTest.php

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,83 @@ public function testUpdateActivity()
9191
$this->client->updateActivity($activity);
9292
}
9393

94+
public function testBatchPartialUpdateActivity()
95+
{
96+
$now = new DateTime('now', new DateTimeZone('Pacific/Nauru'));
97+
$activity_data = [
98+
'actor' => 1,
99+
'verb' => 'tweet',
100+
'object' => 1,
101+
'time' => $now->format(DateTime::ISO8601),
102+
'foreign_id' => 'batch1',
103+
'popularity' => 100,
104+
'new' => true,
105+
];
106+
$act1 = $this->user1->addActivity($activity_data);
107+
$activity_data = [
108+
'actor' => 2,
109+
'verb' => 'tweet',
110+
'object' => 2,
111+
'time' => $now->format(DateTime::ISO8601),
112+
'foreign_id' => 'batch2',
113+
'popularity' => 100,
114+
'new' => true,
115+
];
116+
$act2 = $this->user1->addActivity($activity_data);
117+
$payload = [
118+
[
119+
"id" => $act1["id"],
120+
"set" => ["popularity" => 999],
121+
"unset" => ['new'],
122+
],
123+
[
124+
"id" => $act2["id"],
125+
"set" => ["popularity" => 5],
126+
//"unset" => [],
127+
],
128+
];
129+
$response = $this->client->batchPartialActivityUpdate($payload);
130+
$updated = $this->user1->getActivities(0,2)["results"];
131+
if($updated[0]["id"] == $act1["id"]){
132+
$updated1 = $updated[0];
133+
$updated2 = $updated[1];
134+
} else {
135+
$updated1 = $updated[1];
136+
$updated2 = $updated[0];
137+
}
138+
$this->assertEquals($updated1['popularity'], 999);
139+
$this->assertFalse(in_array("new", $updated1));
140+
$this->assertEquals($updated2['popularity'], 5);
141+
$this->assertTrue(in_array("new", $updated2));
142+
143+
$payload = [
144+
[
145+
"foreign_id" => $act1["foreign_id"],
146+
"time" => $act1["time"],
147+
"set" => ["popularity" => 1242],
148+
],
149+
[
150+
"foreign_id" => $act2["foreign_id"],
151+
"time" => $act2["time"],
152+
"set" => ["popularity" => -3],
153+
"unset" => ["new"],
154+
],
155+
];
156+
$response = $this->client->batchPartialActivityUpdate($payload);
157+
$updated = $this->user1->getActivities(0,2)["results"];
158+
if($updated[0]["id"] == $act1["id"]){
159+
$updated1 = $updated[0];
160+
$updated2 = $updated[1];
161+
} else {
162+
$updated1 = $updated[1];
163+
$updated2 = $updated[0];
164+
}
165+
$this->assertEquals($updated1['popularity'], 1242);
166+
$this->assertFalse(in_array("new", $updated1));
167+
$this->assertEquals($updated2['popularity'], -3);
168+
$this->assertFalse(in_array("new", $updated2));
169+
}
170+
94171
public function testPartialUpdateActivity()
95172
{
96173
$now = new DateTime('now', new DateTimeZone('Pacific/Nauru'));
@@ -102,8 +179,8 @@ public function testPartialUpdateActivity()
102179
'foreign_id' => 'batch1',
103180
'product' => ["name"=> "shoes", "price"=> 9.99, "color"=> "blue"],
104181
];
105-
$response = $this->user1->addActivity($activity_data);
106-
$activity = $response;
182+
$activity = $this->user1->addActivity($activity_data);
183+
107184
$set = [
108185
"product.name" => "boots",
109186
"product.price" => 7.99,

0 commit comments

Comments
 (0)