1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-22 21:40:55 +01:00

Remove "participationStatus" from ConpherenceParticipant

Summary:
Pathway to D17685. This column is a very complicated cache of: is participant.messageCount equal to thread.messageCount?

We can just ask this question with a JOIN instead and simplify things dramatically.

Test Plan:
  - Ran migration.
  - Browsed around.
  - Sent a message, saw unread count go up.
  - Read the message, saw unread count go down.

Reviewers: chad

Reviewed By: chad

Differential Revision: https://secure.phabricator.com/D17730
This commit is contained in:
epriestley 2017-04-19 12:17:37 -07:00
parent c96e17f5ea
commit 0a335f91cd
8 changed files with 41 additions and 71 deletions

View file

@ -0,0 +1,2 @@
ALTER TABLE {$NAMESPACE}_conpherence.conpherence_participant
DROP participationStatus;

View file

@ -305,7 +305,6 @@ phutil_register_library_map(array(
'ConpherenceParticipantCountQuery' => 'applications/conpherence/query/ConpherenceParticipantCountQuery.php', 'ConpherenceParticipantCountQuery' => 'applications/conpherence/query/ConpherenceParticipantCountQuery.php',
'ConpherenceParticipantQuery' => 'applications/conpherence/query/ConpherenceParticipantQuery.php', 'ConpherenceParticipantQuery' => 'applications/conpherence/query/ConpherenceParticipantQuery.php',
'ConpherenceParticipantView' => 'applications/conpherence/view/ConpherenceParticipantView.php', 'ConpherenceParticipantView' => 'applications/conpherence/view/ConpherenceParticipantView.php',
'ConpherenceParticipationStatus' => 'applications/conpherence/constants/ConpherenceParticipationStatus.php',
'ConpherenceQueryThreadConduitAPIMethod' => 'applications/conpherence/conduit/ConpherenceQueryThreadConduitAPIMethod.php', 'ConpherenceQueryThreadConduitAPIMethod' => 'applications/conpherence/conduit/ConpherenceQueryThreadConduitAPIMethod.php',
'ConpherenceQueryTransactionConduitAPIMethod' => 'applications/conpherence/conduit/ConpherenceQueryTransactionConduitAPIMethod.php', 'ConpherenceQueryTransactionConduitAPIMethod' => 'applications/conpherence/conduit/ConpherenceQueryTransactionConduitAPIMethod.php',
'ConpherenceReplyHandler' => 'applications/conpherence/mail/ConpherenceReplyHandler.php', 'ConpherenceReplyHandler' => 'applications/conpherence/mail/ConpherenceReplyHandler.php',
@ -5099,7 +5098,6 @@ phutil_register_library_map(array(
'ConpherenceParticipantCountQuery' => 'PhabricatorOffsetPagedQuery', 'ConpherenceParticipantCountQuery' => 'PhabricatorOffsetPagedQuery',
'ConpherenceParticipantQuery' => 'PhabricatorOffsetPagedQuery', 'ConpherenceParticipantQuery' => 'PhabricatorOffsetPagedQuery',
'ConpherenceParticipantView' => 'AphrontView', 'ConpherenceParticipantView' => 'AphrontView',
'ConpherenceParticipationStatus' => 'ConpherenceConstants',
'ConpherenceQueryThreadConduitAPIMethod' => 'ConpherenceConduitAPIMethod', 'ConpherenceQueryThreadConduitAPIMethod' => 'ConpherenceConduitAPIMethod',
'ConpherenceQueryTransactionConduitAPIMethod' => 'ConpherenceConduitAPIMethod', 'ConpherenceQueryTransactionConduitAPIMethod' => 'ConpherenceConduitAPIMethod',
'ConpherenceReplyHandler' => 'PhabricatorMailReplyHandler', 'ConpherenceReplyHandler' => 'PhabricatorMailReplyHandler',

View file

@ -1,8 +0,0 @@
<?php
final class ConpherenceParticipationStatus extends ConpherenceConstants {
const UP_TO_DATE = 0;
const BEHIND = 1;
}

View file

@ -7,7 +7,6 @@ final class ConpherenceNotificationPanelController
$user = $request->getUser(); $user = $request->getUser();
$conpherences = array(); $conpherences = array();
require_celerity_resource('conpherence-notification-css'); require_celerity_resource('conpherence-notification-css');
$unread_status = ConpherenceParticipationStatus::BEHIND;
$participant_data = id(new ConpherenceParticipantQuery()) $participant_data = id(new ConpherenceParticipantQuery())
->withParticipantPHIDs(array($user->getPHID())) ->withParticipantPHIDs(array($user->getPHID()))
@ -37,7 +36,7 @@ final class ConpherenceNotificationPanelController
'conpherence-notification', 'conpherence-notification',
); );
if ($p_data->getParticipationStatus() == $unread_status) { if (!$p_data->isUpToDate($conpherence)) {
$classes[] = 'phabricator-notification-unread'; $classes[] = 'phabricator-notification-unread';
} }
$uri = $this->getApplicationURI($conpherence->getID().'/'); $uri = $this->getApplicationURI($conpherence->getID().'/');
@ -95,7 +94,7 @@ final class ConpherenceNotificationPanelController
$unread = id(new ConpherenceParticipantCountQuery()) $unread = id(new ConpherenceParticipantCountQuery())
->withParticipantPHIDs(array($user->getPHID())) ->withParticipantPHIDs(array($user->getPHID()))
->withParticipationStatus($unread_status) ->withUnread(true)
->execute(); ->execute();
$unread_count = idx($unread, $user->getPHID(), 0); $unread_count = idx($unread, $user->getPHID(), 0);

View file

@ -173,17 +173,14 @@ final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor {
$phids = $this->getPHIDTransactionNewValue($xaction, array()); $phids = $this->getPHIDTransactionNewValue($xaction, array());
foreach ($phids as $phid) { foreach ($phids as $phid) {
if ($phid == $this->getActor()->getPHID()) { if ($phid == $this->getActor()->getPHID()) {
$status = ConpherenceParticipationStatus::UP_TO_DATE;
$message_count = 1; $message_count = 1;
} else { } else {
$status = ConpherenceParticipationStatus::BEHIND;
$message_count = 0; $message_count = 0;
} }
$participants[$phid] = $participants[$phid] =
id(new ConpherenceParticipant()) id(new ConpherenceParticipant())
->setConpherencePHID($object->getPHID()) ->setConpherencePHID($object->getPHID())
->setParticipantPHID($phid) ->setParticipantPHID($phid)
->setParticipationStatus($status)
->setDateTouched(time()) ->setDateTouched(time())
->setSeenMessageCount($message_count) ->setSeenMessageCount($message_count)
->save(); ->save();
@ -243,17 +240,14 @@ final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor {
$add = array_keys(array_diff_key($new_map, $old_map)); $add = array_keys(array_diff_key($new_map, $old_map));
foreach ($add as $phid) { foreach ($add as $phid) {
if ($phid == $this->getActor()->getPHID()) { if ($phid == $this->getActor()->getPHID()) {
$status = ConpherenceParticipationStatus::UP_TO_DATE;
$message_count = $object->getMessageCount(); $message_count = $object->getMessageCount();
} else { } else {
$status = ConpherenceParticipationStatus::BEHIND;
$message_count = 0; $message_count = 0;
} }
$participants[$phid] = $participants[$phid] =
id(new ConpherenceParticipant()) id(new ConpherenceParticipant())
->setConpherencePHID($object->getPHID()) ->setConpherencePHID($object->getPHID())
->setParticipantPHID($phid) ->setParticipantPHID($phid)
->setParticipationStatus($status)
->setDateTouched(time()) ->setDateTouched(time())
->setSeenMessageCount($message_count) ->setSeenMessageCount($message_count)
->save(); ->save();
@ -279,22 +273,18 @@ final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor {
// update everyone's participation status on a message -only- // update everyone's participation status on a message -only-
$xaction_phid = $xaction->getPHID(); $xaction_phid = $xaction->getPHID();
$behind = ConpherenceParticipationStatus::BEHIND;
$up_to_date = ConpherenceParticipationStatus::UP_TO_DATE;
$participants = $object->getParticipants(); $participants = $object->getParticipants();
$user = $this->getActor(); $user = $this->getActor();
$time = time(); $time = time();
foreach ($participants as $phid => $participant) { foreach ($participants as $phid => $participant) {
if ($phid != $user->getPHID()) { if ($phid != $user->getPHID()) {
if ($participant->getParticipationStatus() != $behind) { if ($participant->isUpToDate($object)) {
$participant->setSeenMessageCount( $participant->setSeenMessageCount(
$object->getMessageCount() - $message_count); $object->getMessageCount() - $message_count);
} }
$participant->setParticipationStatus($behind);
$participant->setDateTouched($time); $participant->setDateTouched($time);
} else { } else {
$participant->setSeenMessageCount($object->getMessageCount()); $participant->setSeenMessageCount($object->getMessageCount());
$participant->setParticipationStatus($up_to_date);
$participant->setDateTouched($time); $participant->setDateTouched($time);
} }
$participant->save(); $participant->save();

View file

@ -1,73 +1,69 @@
<?php <?php
/**
* Query class that answers the question:
*
* - Q: How many unread conpherences am I participating in?
* - A:
* id(new ConpherenceParticipantCountQuery())
* ->withParticipantPHIDs(array($my_phid))
* ->withParticipationStatus(ConpherenceParticipationStatus::BEHIND)
* ->execute();
*/
final class ConpherenceParticipantCountQuery final class ConpherenceParticipantCountQuery
extends PhabricatorOffsetPagedQuery { extends PhabricatorOffsetPagedQuery {
private $participantPHIDs; private $participantPHIDs;
private $participationStatus; private $unread;
public function withParticipantPHIDs(array $phids) { public function withParticipantPHIDs(array $phids) {
$this->participantPHIDs = $phids; $this->participantPHIDs = $phids;
return $this; return $this;
} }
public function withParticipationStatus($participation_status) { public function withUnread($unread) {
$this->participationStatus = $participation_status; $this->unread = $unread;
return $this; return $this;
} }
public function execute() { public function execute() {
$thread = new ConpherenceThread();
$table = new ConpherenceParticipant(); $table = new ConpherenceParticipant();
$conn_r = $table->establishConnection('r'); $conn = $table->establishConnection('r');
$rows = queryfx_all( $rows = queryfx_all(
$conn_r, $conn,
'SELECT COUNT(*) as count, participantPHID '. 'SELECT COUNT(*) as count, participantPHID
'FROM %T participant %Q %Q %Q', FROM %T participant JOIN %T thread
ON participant.conpherencePHID = thread.phid %Q %Q %Q',
$table->getTableName(), $table->getTableName(),
$this->buildWhereClause($conn_r), $thread->getTableName(),
$this->buildGroupByClause($conn_r), $this->buildWhereClause($conn),
$this->buildLimitClause($conn_r)); $this->buildGroupByClause($conn),
$this->buildLimitClause($conn));
return ipull($rows, 'count', 'participantPHID'); return ipull($rows, 'count', 'participantPHID');
} }
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { protected function buildWhereClause(AphrontDatabaseConnection $conn) {
$where = array(); $where = array();
if ($this->participantPHIDs) { if ($this->participantPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'participantPHID IN (%Ls)', 'participant.participantPHID IN (%Ls)',
$this->participantPHIDs); $this->participantPHIDs);
} }
if ($this->participationStatus !== null) { if ($this->unread !== null) {
$where[] = qsprintf( if ($this->unread) {
$conn_r, $where[] = qsprintf(
'participationStatus = %d', $conn,
$this->participationStatus); 'participant.seenMessageCount < thread.messageCount');
} else {
$where[] = qsprintf(
$conn,
'participant.seenMessageCount >= thread.messageCount');
}
} }
return $this->formatWhereClause($where); return $this->formatWhereClause($where);
} }
private function buildGroupByClause(AphrontDatabaseConnection $conn_r) { private function buildGroupByClause(AphrontDatabaseConnection $conn) {
$group_by = qsprintf( return qsprintf(
$conn_r, $conn,
'GROUP BY participantPHID'); 'GROUP BY participantPHID');
return $group_by;
} }
} }

View file

@ -4,7 +4,6 @@ final class ConpherenceParticipant extends ConpherenceDAO {
protected $participantPHID; protected $participantPHID;
protected $conpherencePHID; protected $conpherencePHID;
protected $participationStatus;
protected $seenMessageCount; protected $seenMessageCount;
protected $dateTouched; protected $dateTouched;
protected $settings = array(); protected $settings = array();
@ -15,7 +14,6 @@ final class ConpherenceParticipant extends ConpherenceDAO {
'settings' => self::SERIALIZATION_JSON, 'settings' => self::SERIALIZATION_JSON,
), ),
self::CONFIG_COLUMN_SCHEMA => array( self::CONFIG_COLUMN_SCHEMA => array(
'participationStatus' => 'uint32',
'dateTouched' => 'epoch', 'dateTouched' => 'epoch',
'seenMessageCount' => 'uint64', 'seenMessageCount' => 'uint64',
), ),
@ -24,12 +22,12 @@ final class ConpherenceParticipant extends ConpherenceDAO {
'columns' => array('conpherencePHID', 'participantPHID'), 'columns' => array('conpherencePHID', 'participantPHID'),
'unique' => true, 'unique' => true,
), ),
'unreadCount' => array(
'columns' => array('participantPHID', 'participationStatus'),
),
'participationIndex' => array( 'participationIndex' => array(
'columns' => array('participantPHID', 'dateTouched', 'id'), 'columns' => array('participantPHID', 'dateTouched', 'id'),
), ),
'key_thread' => array(
'columns' => array('participantPHID', 'conpherencePHID'),
),
), ),
) + parent::getConfiguration(); ) + parent::getConfiguration();
} }
@ -41,8 +39,8 @@ final class ConpherenceParticipant extends ConpherenceDAO {
public function markUpToDate( public function markUpToDate(
ConpherenceThread $conpherence, ConpherenceThread $conpherence,
ConpherenceTransaction $xaction) { ConpherenceTransaction $xaction) {
if (!$this->isUpToDate($conpherence)) { if (!$this->isUpToDate($conpherence)) {
$this->setParticipationStatus(ConpherenceParticipationStatus::UP_TO_DATE);
$this->setSeenMessageCount($conpherence->getMessageCount()); $this->setSeenMessageCount($conpherence->getMessageCount());
$this->save(); $this->save();
@ -55,11 +53,7 @@ final class ConpherenceParticipant extends ConpherenceDAO {
} }
public function isUpToDate(ConpherenceThread $conpherence) { public function isUpToDate(ConpherenceThread $conpherence) {
return return ($this->getSeenMessageCount() == $conpherence->getMessageCount());
($this->getSeenMessageCount() == $conpherence->getMessageCount())
&&
($this->getParticipationStatus() ==
ConpherenceParticipationStatus::UP_TO_DATE);
} }
} }

View file

@ -28,10 +28,9 @@ final class PhabricatorUserMessageCountCacheType
$user_phids = mpull($users, 'getPHID'); $user_phids = mpull($users, 'getPHID');
$unread_status = ConpherenceParticipationStatus::BEHIND;
$unread = id(new ConpherenceParticipantCountQuery()) $unread = id(new ConpherenceParticipantCountQuery())
->withParticipantPHIDs($user_phids) ->withParticipantPHIDs($user_phids)
->withParticipationStatus($unread_status) ->withUnread(true)
->execute(); ->execute();
$empty = array_fill_keys($user_phids, 0); $empty = array_fill_keys($user_phids, 0);