Skip to content

Commit 0561aee

Browse files
committed
add partial update method
1 parent 786891a commit 0561aee

3 files changed

Lines changed: 84 additions & 3 deletions

File tree

lib/GetStream/Stream/ActivityUpdateOperation.php

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

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

lib/GetStream/Stream/Client.php

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

259+
public function doPartialActivityUpdate($id=null, $foreign_id=null, $time=null, $set=null, $unset=null)
260+
{
261+
$token = $this->signer->jwtScopeToken('*', 'activities', '*');
262+
if($id === null && ($foreign_id === null || $time === null)){
263+
throw new Exception(
264+
"The id or foreign_id+time parameters must be provided and not be None"
265+
);
266+
}
267+
if($id !== null && ($foreign_id !== null || $time !== null)){
268+
throw new Exception(
269+
"Only one of the id or the foreign_id+time parameters can be provided"
270+
);
271+
}
272+
273+
$data = ["set" => $set, "unset" => $unset];
274+
275+
if($id !== null){
276+
$data["id"] = $id;
277+
} else {
278+
$data["foreign_id"] = $foreign_id;
279+
$data["time"] = $time;
280+
}
281+
$activityUpdateOp = new ActivityUpdateOperation($this, $this->api_key, $token);
282+
return $activityUpdateOp->partiallyUpdateActivity($data);
283+
}
284+
259285
public function updateActivities($activities)
260286
{
261287
if (empty($activities)) {

tests/integration/FeedTest.php

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

94+
public function testPartialUpdateActivity()
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+
'product' => ["name"=> "shoes", "price"=> 9.99, "color"=> "blue"],
104+
];
105+
$response = $this->user1->addActivity($activity_data);
106+
$activity = $response;
107+
$set = [
108+
"product.name" => "boots",
109+
"product.price" => 7.99,
110+
"popularity" => 1000,
111+
"foo" => ["bar" => ["baz" => "qux"]],
112+
];
113+
$unset = ["product.color"];
114+
115+
$this->client->doPartialActivityUpdate($activity['id'], null, null, $set, $unset);
116+
117+
$updated = $this->user1->getActivities(0, 1)['results'][0];
118+
119+
$this->assertEquals($updated['id'], $activity['id']);
120+
$this->assertEquals($updated['product']['name'], 'boots');
121+
$this->assertEquals($updated['popularity'], 1000);
122+
$this->assertFalse(in_array('color', $updated['product']));
123+
124+
$set = [
125+
"foo.bar.baz" => 42,
126+
"popularity" => 9000,
127+
];
128+
$unset = ["product.price"];
129+
130+
$this->client->doPartialActivityUpdate(null, $activity['foreign_id'], $activity['time'], $set, $unset);
131+
132+
133+
$updated_again = $this->user1->getActivities(0, 1)['results'][0];
134+
135+
$this->assertEquals($updated_again['id'], $activity['id']);
136+
$this->assertEquals($updated_again['foo']['bar']['baz'], 42);
137+
$this->assertEquals($updated_again['popularity'], 9000);
138+
$this->assertEquals($updated_again['product']['name'], 'boots');
139+
$this->assertFalse(in_array('color', $updated_again['product']));
140+
$this->assertFalse(in_array('price', $updated_again['product']));
141+
142+
}
143+
94144
public function testAddToMany()
95145
{
96146
$id = Uuid::uuid4();
@@ -172,15 +222,15 @@ public function testUnfollowMany()
172222
['source' => $f2->getId(), 'target' => $u2->getId(), 'keep_history' => true]
173223
];
174224
$batcher->unfollowMany($unfollows);
175-
225+
176226
$resp = $f1->following();
177227
$this->assertCount(0, $resp['results']);
178228
$resp = $f2->following();
179229
$this->assertCount(0, $resp['results']);
180230

181231
$this->assertCount(0, $f1->getActivities()['results']);
182232
$this->assertCount(1, $f2->getActivities()['results']);
183-
233+
184234
}
185235

186236
public function testReadonlyToken()
@@ -200,7 +250,7 @@ public function testUserSessionToken()
200250
$payload = JWT::decode($token, getenv('STREAM_API_SECRET'), array('HS256'));
201251
$this->assertSame($payload->client, 'PHP');
202252
}
203-
253+
204254
public function testUserToken()
205255
{
206256
$token = $this->client->createUserToken('user');

0 commit comments

Comments
 (0)