Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Consolidate PhabricatorFeedStory construction code
Browse files Browse the repository at this point in the history
Summary: This code is duplicated in two places; share it.

Test Plan: Looked at feed and notifications.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D2901
  • Loading branch information
epriestley committed Jul 2, 2012
1 parent f5f88d8 commit d49b06c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
23 changes: 2 additions & 21 deletions src/applications/feed/PhabricatorFeedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,7 @@ public function execute() {
$data = array_reverse($data);
}

$data = $story_table->loadAllFromArray($data);

$stories = array();
foreach ($data as $story_data) {
$class = $story_data->getStoryType();

try {
if (!class_exists($class) ||
!is_subclass_of($class, 'PhabricatorFeedStory')) {
$class = 'PhabricatorFeedStoryUnknown';
}
} catch (PhutilMissingSymbolException $ex) {
// If the class can't be loaded, libphutil will throw an exception.
// Render the story using the unknown story view.
$class = 'PhabricatorFeedStoryUnknown';
}

$stories[] = newv($class, array($story_data));
}

return $stories;
return PhabricatorFeedStory::loadAllFromRows($data);
}

}
51 changes: 51 additions & 0 deletions src/applications/feed/story/PhabricatorFeedStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
* limitations under the License.
*/

/**
* Manages rendering and aggregation of a story. A story is an event (like a
* user adding a comment) which may be represented in different forms on
* different channels (like feed, notifications and realtime alerts).
*
* @task load Loading Stories
*/
abstract class PhabricatorFeedStory {

private $data;
Expand All @@ -24,6 +31,50 @@ abstract class PhabricatorFeedStory {
private $framed;
private $primaryObjectPHID;


/* -( Loading Stories )---------------------------------------------------- */


/**
* Given @{class:PhabricatorFeedStoryData} rows, load them into objects and
* construct appropriate @{class:PhabricatorFeedStory} wrappers for each
* data row.
*
* @param list<dict> List of @{class:PhabricatorFeedStoryData} rows from the
* database.
* @return list<PhabricatorFeedStory> List of @{class:PhabricatorFeedStory}
* objects.
* @task load
*/
public static function loadAllFromRows(array $rows) {
$stories = array();

$data = id(new PhabricatorFeedStoryData())->loadAllFromArray($rows);
foreach ($data as $story_data) {
$class = $story_data->getStoryType();

$ok = false;
try {
$ok = is_subclass_of($class, 'PhabricatorFeedStory');
} catch (PhutilMissingSymbolException $ex) {
$ok = false;
}

// If the story type isn't a valid class or isn't a subclass of
// PhabricatorFeedStory, load it as PhabricatorFeedStoryUnknown.

if (!$ok) {
$class = 'PhabricatorFeedStoryUnknown';
}

$key = $story_data->getChronologicalKey();
$stories[$key] = newv($class, array($story_data));
}

return $stories;
}


public function setPrimaryObjectPHID($primary_object_phid) {
$this->primaryObjectPHID = $primary_object_phid;
return $this;
Expand Down
22 changes: 4 additions & 18 deletions src/applications/notification/PhabricatorNotificationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,10 @@ public function execute() {
$viewed_map = ipull($data, 'hasViewed', 'chronologicalKey');
$primary_map = ipull($data, 'primaryObjectPHID', 'chronologicalKey');

$data = $story_table->loadAllFromArray($data);

$stories = array();

foreach ($data as $story_data) {
$class = $story_data->getStoryType();
try {
if (!class_exists($class) ||
!is_subclass_of($class, 'PhabricatorFeedStory')) {
$class = 'PhabricatorFeedStoryUnknown';
}
} catch (PhutilMissingSymbolException $ex) {
$class = 'PhabricatorFeedStoryUnknown';
}
$story = newv($class, array($story_data));
$story->setHasViewed($viewed_map[$story->getChronologicalKey()]);
$story->setPrimaryObjectPHID($primary_map[$story->getChronologicalKey()]);
$stories[] = $story;
$stories = PhabricatorFeedStory::loadAllFromRows($data);
foreach ($stories as $key => $story) {
$story->setHasViewed($viewed_map[$key]);
$story->setPrimaryObjectPHID($primary_map[$key]);
}

return $stories;
Expand Down

0 comments on commit d49b06c

Please sign in to comment.