public function processRequest()
 {
     $request = $this->getRequest();
     $chrono_key = $request->getStr('chronoKey');
     $user = $request->getUser();
     if ($request->isDialogFormPost()) {
         $table = new PhabricatorFeedStoryNotification();
         queryfx($table->establishConnection('w'), 'UPDATE %T SET hasViewed = 1 ' . 'WHERE userPHID = %s AND hasViewed = 0 and chronologicalKey <= %s', $table->getTableName(), $user->getPHID(), $chrono_key);
         return id(new AphrontReloadResponse())->setURI('/notification/');
     }
     $dialog = new AphrontDialogView();
     $dialog->setUser($user);
     $dialog->addCancelButton('/notification/');
     if ($chrono_key) {
         $dialog->setTitle(pht('Really mark all notifications as read?'));
         $dialog->addHiddenInput('chronoKey', $chrono_key);
         $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
         if ($is_serious) {
             $dialog->appendChild(pht('All unread notifications will be marked as read. You can not ' . 'undo this action.'));
         } else {
             $dialog->appendChild(pht("You can't ignore your problems forever, you know."));
         }
         $dialog->addSubmitButton(pht('Mark All Read'));
     } else {
         $dialog->setTitle(pht('No notifications to mark as read.'));
         $dialog->appendChild(pht('You have no unread notifications.'));
     }
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 protected function collectGarbage()
 {
     $table = new PhabricatorFeedStoryNotification();
     $conn_w = $table->establishConnection('w');
     queryfx($conn_w, 'DELETE FROM %T WHERE chronologicalKey < (%d << 32)
     ORDER BY chronologicalKey ASC LIMIT 100', $table->getTableName(), $this->getGarbageEpoch());
     return $conn_w->getAffectedRows() == 100;
 }
 public static function updateObjectNotificationViews(PhabricatorUser $user, $object_phid)
 {
     if (PhabricatorEnv::getEnvConfig('notification.enabled')) {
         $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
         $notification_table = new PhabricatorFeedStoryNotification();
         $conn = $notification_table->establishConnection('w');
         queryfx($conn, "UPDATE %T\n         SET hasViewed = 1\n         WHERE userPHID = %s\n           AND primaryObjectPHID = %s\n           AND hasViewed = 0", $notification_table->getTableName(), $user->getPHID(), $object_phid);
         unset($unguarded);
     }
 }
 public function newValueForUsers($key, array $users)
 {
     if (!$users) {
         return array();
     }
     $user_phids = mpull($users, 'getPHID');
     $table = new PhabricatorFeedStoryNotification();
     $conn_r = $table->establishConnection('r');
     $rows = queryfx_all($conn_r, 'SELECT userPHID, COUNT(*) N FROM %T
     WHERE userPHID IN (%Ls) AND hasViewed = 0
     GROUP BY userPHID', $table->getTableName(), $user_phids);
     $empty = array_fill_keys($user_phids, 0);
     return ipull($rows, 'N', 'userPHID') + $empty;
 }
 protected function loadPage()
 {
     $story_table = new PhabricatorFeedStoryData();
     $notification_table = new PhabricatorFeedStoryNotification();
     $conn = $story_table->establishConnection('r');
     $data = queryfx_all($conn, 'SELECT story.*, notif.hasViewed FROM %T notif
      JOIN %T story ON notif.chronologicalKey = story.chronologicalKey
      %Q
      ORDER BY notif.chronologicalKey DESC
      %Q', $notification_table->getTableName(), $story_table->getTableName(), $this->buildWhereClause($conn), $this->buildLimitClause($conn));
     $viewed_map = ipull($data, 'hasViewed', 'chronologicalKey');
     $stories = PhabricatorFeedStory::loadAllFromRows($data, $this->getViewer());
     foreach ($stories as $key => $story) {
         $story->setHasViewed($viewed_map[$key]);
     }
     return $stories;
 }
 public function execute()
 {
     if (!$this->userPHID) {
         throw new Exception("Call setUser() before executing the query");
     }
     $story_table = new PhabricatorFeedStoryData();
     $notification_table = new PhabricatorFeedStoryNotification();
     $conn = $story_table->establishConnection('r');
     $data = queryfx_all($conn, "SELECT story.*, notif.primaryObjectPHID, notif.hasViewed FROM %T notif\n         JOIN %T story ON notif.chronologicalKey = story.chronologicalKey\n         %Q\n         ORDER BY notif.chronologicalKey DESC\n         %Q", $notification_table->getTableName(), $story_table->getTableName(), $this->buildWhereClause($conn), $this->buildLimitClause($conn));
     $viewed_map = ipull($data, 'hasViewed', 'chronologicalKey');
     $primary_map = ipull($data, 'primaryObjectPHID', 'chronologicalKey');
     $stories = PhabricatorFeedStory::loadAllFromRows($data);
     foreach ($stories as $key => $story) {
         $story->setHasViewed($viewed_map[$key]);
         $story->setPrimaryObjectPHID($primary_map[$key]);
     }
     return $stories;
 }
 public static function updateObjectNotificationViews(PhabricatorUser $user, $object_phid)
 {
     if (PhabricatorEnv::isReadOnly()) {
         return;
     }
     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
     $notification_table = new PhabricatorFeedStoryNotification();
     $conn = $notification_table->establishConnection('w');
     queryfx($conn, 'UPDATE %T
    SET hasViewed = 1
    WHERE userPHID = %s
      AND primaryObjectPHID = %s
      AND hasViewed = 0', $notification_table->getTableName(), $user->getPHID(), $object_phid);
     unset($unguarded);
     $count_key = PhabricatorUserNotificationCountCacheType::KEY_COUNT;
     PhabricatorUserCache::clearCache($count_key, $user->getPHID());
     $user->clearCacheData($count_key);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($request->isDialogFormPost()) {
         $table = new PhabricatorFeedStoryNotification();
         queryfx($table->establishConnection('w'), 'UPDATE %T SET hasViewed = 1 WHERE
       userPHID = %s AND hasViewed = 0', $table->getTableName(), $user->getPHID());
         return id(new AphrontReloadResponse())->setURI('/notification/');
     }
     $dialog = new AphrontDialogView();
     $dialog->setUser($user);
     $dialog->setTitle('Really mark all notifications as read?');
     $dialog->appendChild("You can't ignore your problems forever, you know.");
     $dialog->addCancelButton('/notification/');
     $dialog->addSubmitButton('Mark All Read');
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 private function destroyNotifications($object_phid)
 {
     $table = new PhabricatorFeedStoryNotification();
     $conn_w = $table->establishConnection('w');
     queryfx($conn_w, 'DELETE FROM %T WHERE primaryObjectPHID = %s', $table->getTableName(), $object_phid);
 }
 private function insertNotifications($chrono_key, array $subscribed_phids)
 {
     if (!$this->primaryObjectPHID) {
         throw new Exception(pht('You must call %s if you %s!', 'setPrimaryObjectPHID()', 'setSubscribedPHIDs()'));
     }
     $notif = new PhabricatorFeedStoryNotification();
     $sql = array();
     $conn = $notif->establishConnection('w');
     $will_receive_mail = array_fill_keys($this->mailRecipientPHIDs, true);
     foreach (array_unique($subscribed_phids) as $user_phid) {
         if (isset($will_receive_mail[$user_phid])) {
             $mark_read = 1;
         } else {
             $mark_read = 0;
         }
         $sql[] = qsprintf($conn, '(%s, %s, %s, %d)', $this->primaryObjectPHID, $user_phid, $chrono_key, $mark_read);
     }
     if ($sql) {
         queryfx($conn, 'INSERT INTO %T ' . '(primaryObjectPHID, userPHID, chronologicalKey, hasViewed) ' . 'VALUES %Q', $notif->getTableName(), implode(', ', $sql));
     }
 }
 private function insertNotifications($chrono_key)
 {
     if (!$this->subscribedPHIDs) {
         return;
     }
     if (!$this->primaryObjectPHID) {
         throw new Exception("You must call setPrimaryObjectPHID() if you setSubscribedPHIDs()!");
     }
     $notif = new PhabricatorFeedStoryNotification();
     $sql = array();
     $conn = $notif->establishConnection('w');
     foreach (array_unique($this->subscribedPHIDs) as $user_phid) {
         $sql[] = qsprintf($conn, '(%s, %s, %s, %d)', $this->primaryObjectPHID, $user_phid, $chrono_key, 0);
     }
     queryfx($conn, 'INSERT INTO %T
  (primaryObjectPHID, userPHID, chronologicalKey, hasViewed)
  VALUES %Q', $notif->getTableName(), implode(', ', $sql));
 }
 public function destroyObject(PhabricatorDestructionEngine $engine, $object)
 {
     $table = new PhabricatorFeedStoryNotification();
     $conn_w = $table->establishConnection('w');
     queryfx($conn_w, 'DELETE FROM %T WHERE primaryObjectPHID = %s', $table->getTableName(), $object->getPHID());
 }