1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 18:22:41 +01:00

Consolidate PhabricatorFeedStory construction code

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
This commit is contained in:
epriestley 2012-07-02 10:37:22 -07:00
parent f5f88d8698
commit d49b06c322
3 changed files with 57 additions and 39 deletions

View file

@ -107,26 +107,7 @@ final class PhabricatorFeedQuery {
$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);
}
}

View file

@ -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;
@ -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;

View file

@ -85,24 +85,10 @@ final class PhabricatorNotificationQuery extends PhabricatorOffsetPagedQuery {
$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;