mirror of
https://we.phorge.it/source/phorge.git
synced 2025-03-03 07:59:15 +01:00
Summary: Depends on D19919. Ref T11351. This method appeared in D8802 (note that "get...Object" was renamed to "get...Transaction" there, so this method was actually "new" even though a method of the same name had existed before). The goal at the time was to let Harbormaster post build results to Diffs and have them end up on Revisions, but this eventually got a better implementation (see below) where the Harbormaster-specific code can just specify a "publishable object" where build results should go. The new `get...Object` semantics ultimately broke some stuff, and the actual implementation in Differential was removed in D10911, so this method hasn't really served a purpose since December 2014. I think that broke the Harbormaster thing by accident and we just lived with it for a bit, then Harbormaster got some more work and D17139 introduced "publishable" objects which was a better approach. This was later refined by D19281. So: the original problem (sending build results to the right place) has a good solution now, this method hasn't done anything for 4 years, and it was probably a bad idea in the first place since it's pretty weird/surprising/fragile. Note that `Comment` objects still have an unrelated method with the same name. In that case, the method ties the `Comment` storage object to the related `Transaction` storage object. Test Plan: Grepped for `getApplicationTransactionObject`, verified that all remaining callsites are related to `Comment` objects. Reviewers: amckinley Reviewed By: amckinley Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam Maniphest Tasks: T11351 Differential Revision: https://secure.phabricator.com/D19920
348 lines
9.2 KiB
PHP
348 lines
9.2 KiB
PHP
<?php
|
|
|
|
final class ConpherenceThread extends ConpherenceDAO
|
|
implements
|
|
PhabricatorPolicyInterface,
|
|
PhabricatorApplicationTransactionInterface,
|
|
PhabricatorMentionableInterface,
|
|
PhabricatorDestructibleInterface,
|
|
PhabricatorNgramsInterface {
|
|
|
|
protected $title;
|
|
protected $topic;
|
|
protected $profileImagePHID;
|
|
protected $messageCount;
|
|
protected $mailKey;
|
|
protected $viewPolicy;
|
|
protected $editPolicy;
|
|
protected $joinPolicy;
|
|
|
|
private $participants = self::ATTACHABLE;
|
|
private $transactions = self::ATTACHABLE;
|
|
private $profileImageFile = self::ATTACHABLE;
|
|
private $handles = self::ATTACHABLE;
|
|
|
|
public static function initializeNewRoom(PhabricatorUser $sender) {
|
|
$default_policy = id(new ConpherenceThreadMembersPolicyRule())
|
|
->getObjectPolicyFullKey();
|
|
return id(new ConpherenceThread())
|
|
->setMessageCount(0)
|
|
->setTitle('')
|
|
->setTopic('')
|
|
->attachParticipants(array())
|
|
->setViewPolicy($default_policy)
|
|
->setEditPolicy($default_policy)
|
|
->setJoinPolicy('');
|
|
}
|
|
|
|
protected function getConfiguration() {
|
|
return array(
|
|
self::CONFIG_AUX_PHID => true,
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
|
'title' => 'text255?',
|
|
'topic' => 'text255',
|
|
'messageCount' => 'uint64',
|
|
'mailKey' => 'text20',
|
|
'joinPolicy' => 'policy',
|
|
'profileImagePHID' => 'phid?',
|
|
),
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
'key_phid' => null,
|
|
'phid' => array(
|
|
'columns' => array('phid'),
|
|
'unique' => true,
|
|
),
|
|
),
|
|
) + parent::getConfiguration();
|
|
}
|
|
|
|
public function generatePHID() {
|
|
return PhabricatorPHID::generateNewPHID(
|
|
PhabricatorConpherenceThreadPHIDType::TYPECONST);
|
|
}
|
|
|
|
public function save() {
|
|
if (!$this->getMailKey()) {
|
|
$this->setMailKey(Filesystem::readRandomCharacters(20));
|
|
}
|
|
return parent::save();
|
|
}
|
|
|
|
public function getMonogram() {
|
|
return 'Z'.$this->getID();
|
|
}
|
|
|
|
public function getURI() {
|
|
return '/'.$this->getMonogram();
|
|
}
|
|
|
|
public function attachParticipants(array $participants) {
|
|
assert_instances_of($participants, 'ConpherenceParticipant');
|
|
$this->participants = $participants;
|
|
return $this;
|
|
}
|
|
|
|
public function getParticipants() {
|
|
return $this->assertAttached($this->participants);
|
|
}
|
|
|
|
public function getParticipant($phid) {
|
|
$participants = $this->getParticipants();
|
|
return $participants[$phid];
|
|
}
|
|
|
|
public function getParticipantIfExists($phid, $default = null) {
|
|
$participants = $this->getParticipants();
|
|
return idx($participants, $phid, $default);
|
|
}
|
|
|
|
public function getParticipantPHIDs() {
|
|
$participants = $this->getParticipants();
|
|
return array_keys($participants);
|
|
}
|
|
|
|
public function attachHandles(array $handles) {
|
|
assert_instances_of($handles, 'PhabricatorObjectHandle');
|
|
$this->handles = $handles;
|
|
return $this;
|
|
}
|
|
|
|
public function getHandles() {
|
|
return $this->assertAttached($this->handles);
|
|
}
|
|
|
|
public function attachTransactions(array $transactions) {
|
|
assert_instances_of($transactions, 'ConpherenceTransaction');
|
|
$this->transactions = $transactions;
|
|
return $this;
|
|
}
|
|
|
|
public function getTransactions($assert_attached = true) {
|
|
return $this->assertAttached($this->transactions);
|
|
}
|
|
|
|
public function hasAttachedTransactions() {
|
|
return $this->transactions !== self::ATTACHABLE;
|
|
}
|
|
|
|
public function getTransactionsFrom($begin = 0, $amount = null) {
|
|
$length = count($this->transactions);
|
|
|
|
return array_slice(
|
|
$this->getTransactions(),
|
|
$length - $begin - $amount,
|
|
$amount);
|
|
}
|
|
|
|
public function getProfileImageURI() {
|
|
return $this->getProfileImageFile()->getBestURI();
|
|
}
|
|
|
|
public function attachProfileImageFile(PhabricatorFile $file) {
|
|
$this->profileImageFile = $file;
|
|
return $this;
|
|
}
|
|
|
|
public function getProfileImageFile() {
|
|
return $this->assertAttached($this->profileImageFile);
|
|
}
|
|
|
|
/**
|
|
* Get a thread title which doesn't require handles to be attached.
|
|
*
|
|
* This is a less rich title than @{method:getDisplayTitle}, but does not
|
|
* require handles to be attached. We use it to build thread handles without
|
|
* risking cycles or recursion while querying.
|
|
*
|
|
* @return string Lower quality human-readable title.
|
|
*/
|
|
public function getStaticTitle() {
|
|
$title = $this->getTitle();
|
|
if (strlen($title)) {
|
|
return $title;
|
|
}
|
|
|
|
return pht('Private Room');
|
|
}
|
|
|
|
public function getDisplayData(PhabricatorUser $viewer) {
|
|
$handles = $this->getHandles();
|
|
|
|
if ($this->hasAttachedTransactions()) {
|
|
$transactions = $this->getTransactions();
|
|
} else {
|
|
$transactions = array();
|
|
}
|
|
|
|
$img_src = $this->getProfileImageURI();
|
|
|
|
$message_transaction = null;
|
|
foreach ($transactions as $transaction) {
|
|
if ($message_transaction) {
|
|
break;
|
|
}
|
|
switch ($transaction->getTransactionType()) {
|
|
case PhabricatorTransactions::TYPE_COMMENT:
|
|
$message_transaction = $transaction;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
if ($message_transaction) {
|
|
$message_handle = $handles[$message_transaction->getAuthorPHID()];
|
|
$subtitle = sprintf(
|
|
'%s: %s',
|
|
$message_handle->getName(),
|
|
id(new PhutilUTF8StringTruncator())
|
|
->setMaximumGlyphs(60)
|
|
->truncateString(
|
|
$message_transaction->getComment()->getContent()));
|
|
} else {
|
|
// Kinda lame, but maybe add last message to cache?
|
|
$subtitle = pht('No recent messages');
|
|
}
|
|
|
|
$user_participation = $this->getParticipantIfExists($viewer->getPHID());
|
|
$theme = ConpherenceRoomSettings::COLOR_LIGHT;
|
|
if ($user_participation) {
|
|
$user_seen_count = $user_participation->getSeenMessageCount();
|
|
$participant = $this->getParticipant($viewer->getPHID());
|
|
$settings = $participant->getSettings();
|
|
$theme = idx($settings, 'theme', $theme);
|
|
} else {
|
|
$user_seen_count = 0;
|
|
}
|
|
|
|
$unread_count = $this->getMessageCount() - $user_seen_count;
|
|
$theme_class = ConpherenceRoomSettings::getThemeClass($theme);
|
|
|
|
$title = $this->getTitle();
|
|
$topic = $this->getTopic();
|
|
|
|
return array(
|
|
'title' => $title,
|
|
'topic' => $topic,
|
|
'subtitle' => $subtitle,
|
|
'unread_count' => $unread_count,
|
|
'epoch' => $this->getDateModified(),
|
|
'image' => $img_src,
|
|
'theme' => $theme_class,
|
|
);
|
|
}
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface Implementation )-------------------------- */
|
|
|
|
|
|
public function getCapabilities() {
|
|
return array(
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
);
|
|
}
|
|
|
|
public function getPolicy($capability) {
|
|
switch ($capability) {
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
return $this->getViewPolicy();
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
return $this->getEditPolicy();
|
|
}
|
|
return PhabricatorPolicies::POLICY_NOONE;
|
|
}
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
|
|
// this bad boy isn't even created yet so go nuts $user
|
|
if (!$this->getID()) {
|
|
return true;
|
|
}
|
|
|
|
switch ($capability) {
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
return false;
|
|
}
|
|
|
|
$participants = $this->getParticipants();
|
|
return isset($participants[$user->getPHID()]);
|
|
}
|
|
|
|
public function describeAutomaticCapability($capability) {
|
|
switch ($capability) {
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
return pht('Participants in a room can always view it.');
|
|
break;
|
|
}
|
|
}
|
|
|
|
public static function loadViewPolicyObjects(
|
|
PhabricatorUser $viewer,
|
|
array $conpherences) {
|
|
|
|
assert_instances_of($conpherences, __CLASS__);
|
|
|
|
$policies = array();
|
|
foreach ($conpherences as $room) {
|
|
$policies[$room->getViewPolicy()] = 1;
|
|
}
|
|
$policy_objects = array();
|
|
if ($policies) {
|
|
$policy_objects = id(new PhabricatorPolicyQuery())
|
|
->setViewer($viewer)
|
|
->withPHIDs(array_keys($policies))
|
|
->execute();
|
|
}
|
|
|
|
return $policy_objects;
|
|
}
|
|
|
|
public function getPolicyIconName(array $policy_objects) {
|
|
assert_instances_of($policy_objects, 'PhabricatorPolicy');
|
|
|
|
$icon = $policy_objects[$this->getViewPolicy()]->getIcon();
|
|
return $icon;
|
|
}
|
|
|
|
|
|
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
|
|
|
|
|
|
public function getApplicationTransactionEditor() {
|
|
return new ConpherenceEditor();
|
|
}
|
|
|
|
public function getApplicationTransactionTemplate() {
|
|
return new ConpherenceTransaction();
|
|
}
|
|
|
|
|
|
/* -( PhabricatorNgramInterface )------------------------------------------ */
|
|
|
|
|
|
public function newNgrams() {
|
|
return array(
|
|
id(new ConpherenceThreadTitleNgrams())
|
|
->setValue($this->getTitle()),
|
|
);
|
|
}
|
|
|
|
|
|
/* -( PhabricatorDestructibleInterface )----------------------------------- */
|
|
|
|
|
|
public function destroyObjectPermanently(
|
|
PhabricatorDestructionEngine $engine) {
|
|
|
|
$this->openTransaction();
|
|
$this->delete();
|
|
|
|
$participants = id(new ConpherenceParticipant())
|
|
->loadAllWhere('conpherencePHID = %s', $this->getPHID());
|
|
foreach ($participants as $participant) {
|
|
$participant->delete();
|
|
}
|
|
|
|
$this->saveTransaction();
|
|
|
|
}
|
|
}
|