Skip to content

Commit

Permalink
add update to target to feed
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbugli committed Aug 31, 2017
1 parent e9eb8ce commit b827e98
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Change history
================

2.4.0
=====
:release-date: 2017-08-31
:by: Tommaso Barbugli

* Add support for update to target

2.3.0
=====
:release-date: 2017-03-20
Expand Down
29 changes: 29 additions & 0 deletions lib/GetStream/Stream/BaseFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,33 @@ public function delete()
{
return $this->makeHttpRequest("{$this->base_feed_url}/", 'DELETE', null, null, 'feed', 'delete');
}

/**
* @param string $foreign_id
* @param string $time
* @param array $new_targets
* @param array $added_targets
* @param array $removed_targets
* @return mixed
*/
public function updateActivityToTargets($foreign_id, $time, $new_targets = [], $added_targets = [], $removed_targets = [])
{
$data = [
'foreign_id' => $foreign_id,
'time' => $time,
];

if ($new_targets) {
$data['new_targets'] = $new_targets;
}

if ($added_targets) {
$data['added_targets'] = $added_targets;
}

if ($removed_targets) {
$data['removed_targets'] = $removed_targets;
}
return $this->makeHttpRequest("feed_targets/{$this->slug}/{$this->user_id}/activity_to_targets/", 'POST', $data, null, 'feed_targets', 'write');
}
}
122 changes: 122 additions & 0 deletions test/IntegrationFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@
use DateTime;
use DateTimeZone;

function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),

// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,

// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,

// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}

class IntegrationTest extends \PHPUnit_Framework_TestCase
{
protected $client;
Expand Down Expand Up @@ -469,4 +491,104 @@ public function testAddActivitiesTo()
$response = $this->client->feed('flat', 'remotefeed2')->getActivities(0, 2);
$this->assertSame($response['results'][0]['actor'], 'many2');
}

public function testUpdateActivitiesToRemoveTarget()
{
$feed = $this->client->feed('user', gen_uuid());
$target = gen_uuid();
$now = new DateTime('now', new DateTimeZone('Pacific/Nauru'));
$time = $now->format(DateTime::ISO8601);
$activities = [
[
'actor' => 'actor', 'verb' => 'tweet', 'object' => 1,
'to' => ["flat:${target}"], 'time' => $time,
'foreign_id' => 'fid1',
],
];
$feed->addActivities($activities);
$response = $this->client->feed('flat', $target)->getActivities();
$this->assertCount(1, $response['results']);

$feed->updateActivityToTargets('fid1', $time, [], [], ["flat:${target}"]);

$response = $this->client->feed('flat', $target)->getActivities();
$this->assertCount(0, $response['results']);
}

public function testUpdateActivitiesToAddTarget()
{
$feed = $this->client->feed('user', gen_uuid());
$target = gen_uuid();
$now = new DateTime('now', new DateTimeZone('Pacific/Nauru'));
$time = $now->format(DateTime::ISO8601);
$activities = [
[
'actor' => 'actor', 'verb' => 'tweet', 'object' => 1,
'time' => $time, 'foreign_id' => 'fid1',
],
];
$feed->addActivities($activities);
$response = $this->client->feed('flat', $target)->getActivities();
$this->assertCount(0, $response['results']);

$feed->updateActivityToTargets('fid1', $time, [], ["flat:${target}"], []);

$response = $this->client->feed('flat', $target)->getActivities();
$this->assertCount(1, $response['results']);
}

public function testUpdateActivitiesToAddRemoveTarget()
{
$feed = $this->client->feed('user', gen_uuid());
$target1 = gen_uuid();
$target2 = gen_uuid();
$now = new DateTime('now', new DateTimeZone('Pacific/Nauru'));
$time = $now->format(DateTime::ISO8601);
$activities = [
[
'actor' => 'actor', 'verb' => 'tweet', 'object' => 1,
'to' => ["flat:${target1}"], 'time' => $time,
'foreign_id' => 'fid1',
],
];
$feed->addActivities($activities);
$response = $this->client->feed('flat', $target1)->getActivities();
$this->assertCount(1, $response['results']);

$feed->updateActivityToTargets('fid1', $time, [], ["flat:${target2}"], ["flat:${target1}"]);

$response = $this->client->feed('flat', $target1)->getActivities();
$this->assertCount(0, $response['results']);

$response = $this->client->feed('flat', $target2)->getActivities();
$this->assertCount(1, $response['results']);
}

public function testUpdateActivitiesToReplaceTargets()
{
$feed = $this->client->feed('user', gen_uuid());
$target1 = gen_uuid();
$target2 = gen_uuid();
$now = new DateTime('now', new DateTimeZone('Pacific/Nauru'));
$time = $now->format(DateTime::ISO8601);
$activities = [
[
'actor' => 'actor', 'verb' => 'tweet', 'object' => 1,
'to' => ["flat:${target1}"], 'time' => $time,
'foreign_id' => 'fid1',
],
];
$feed->addActivities($activities);
$response = $this->client->feed('flat', $target1)->getActivities();
$this->assertCount(1, $response['results']);

$feed->updateActivityToTargets('fid1', $time, ["flat:${target2}"]);

$response = $this->client->feed('flat', $target1)->getActivities();
$this->assertCount(0, $response['results']);

$response = $this->client->feed('flat', $target2)->getActivities();
$this->assertCount(1, $response['results']);
}

}
31 changes: 0 additions & 31 deletions test/IntegrationHerokuFeedTest.php

This file was deleted.

0 comments on commit b827e98

Please sign in to comment.