Example #1
0
 /**
  * Add message to conversation
  *
  * @param MAILBOX_BOL_Conversation $conversation
  * @param int $senderId
  * @param string $text
  * @throws InvalidArgumentException
  */
 public function addMessage(MAILBOX_BOL_Conversation $conversation, $senderId, $text)
 {
     if (empty($senderId)) {
         throw new InvalidArgumentException("Not numeric params were provided! Numbers are expected!");
     }
     if ($conversation === null) {
         throw new InvalidArgumentException("Conversation doesn't exist!");
     }
     if (empty($conversation->id)) {
         throw new InvalidArgumentException("Conversation with id = " . $conversation->id . " is not exist");
     }
     if (!in_array($senderId, array($conversation->initiatorId, $conversation->interlocutorId))) {
         throw new InvalidArgumentException("Wrong senderId!");
     }
     $senderId = (int) $senderId;
     $recipientId = $senderId == $conversation->initiatorId ? $conversation->interlocutorId : $conversation->initiatorId;
     $text = trim($text);
     if (empty($text)) {
         throw new InvalidArgumentException("Empty string params were provided!");
     }
     // create message
     $message = new MAILBOX_BOL_Message();
     $message->conversationId = $conversation->id;
     $message->senderId = $senderId;
     $message->recipientId = $recipientId;
     $message->text = $text;
     $message->timeStamp = time();
     $this->messageDao->save($message);
     // insert record into LastMessage table
     $lastMessage = $this->lastMessageDao->findByConversationId($conversation->id);
     if ($lastMessage === null) {
         $lastMessage = new MAILBOX_BOL_LastMessage();
         $lastMessage->conversationId = $conversation->id;
         $lastMessage->initiatorMessageId = $message->id;
     } else {
         switch ($senderId) {
             case $conversation->initiatorId:
                 $unReadBy = MAILBOX_BOL_ConversationDao::READ_INTERLOCUTOR;
                 $readBy = MAILBOX_BOL_ConversationDao::READ_INITIATOR;
                 $unDeletedBy = MAILBOX_BOL_ConversationDao::DELETED_INTERLOCUTOR;
                 $consoleViewed = MAILBOX_BOL_ConversationDao::VIEW_INITIATOR;
                 $lastMessage->initiatorMessageId = $message->id;
                 break;
             case $conversation->interlocutorId:
                 $unReadBy = MAILBOX_BOL_ConversationDao::READ_INITIATOR;
                 $readBy = MAILBOX_BOL_ConversationDao::READ_INTERLOCUTOR;
                 $unDeletedBy = MAILBOX_BOL_ConversationDao::DELETED_INITIATOR;
                 $lastMessage->interlocutorMessageId = $message->id;
                 $consoleViewed = MAILBOX_BOL_ConversationDao::VIEW_INTERLOCUTOR;
                 break;
         }
         $conversation->deleted = (int) $conversation->deleted & ~$unDeletedBy;
         $conversation->read = (int) $conversation->read & ~$unReadBy | $readBy;
         $conversation->viewed = $consoleViewed;
         $conversation->notificationSent = 0;
         $this->conversationDao->save($conversation);
     }
     $this->lastMessageDao->save($lastMessage);
     return $message;
 }
Example #2
0
 /**
  * Returns class instance
  *
  * @return MAILBOX_BOL_LastMessageDao
  */
 public static function getInstance()
 {
     if (self::$classInstance === null) {
         self::$classInstance = new self();
     }
     return self::$classInstance;
 }
Example #3
0
 public function getInboxConversationList($userId, $first, $count)
 {
     $sql = " SELECT `conv`.*, `last_m`.*, `mess`.* FROM `" . $this->getTableName() . "` AS `conv`\n\n\t\t\t\t INNER JOIN `" . MAILBOX_BOL_LastMessageDao::getInstance()->getTableName() . "` AS `last_m`\n\t\t\t\t\t ON ( `last_m`.`conversationId` = `conv`.`id` )\n\n\t\t\t\t INNER JOIN `" . MAILBOX_BOL_MessageDao::getInstance()->getTableName() . "` AS `mess`\n\t\t\t\t \tON ( `conv`.`id` = `mess`.conversationId )\n\n\t\t\t\t WHERE ( `conv`.`initiatorId` = :user AND `conv`.`deleted` != " . self::DELETED_INITIATOR . " AND `mess`.`id` = `last_m`.`interlocutorMessageId` )\n\t\t\t\t\t \tOR ( `conv`.`interlocutorId` = :user AND `conv`.`deleted` != " . self::DELETED_INTERLOCUTOR . " AND `mess`.`id` = `last_m`.`initiatorMessageId` )\n\n\t\t\t\t ORDER BY `mess`.`timeStamp` DESC\n\n\t\t\t\t LIMIT :first, :count ";
     return $this->dbo->queryForList($sql, array('user' => $userId, 'first' => $first, 'count' => $count));
 }
Example #4
0
 /**
  *
  * @param array $userId
  * @return array
  */
 public function getNewConversationListForConsoleNotificationMailer($userIdList)
 {
     if (empty($userIdList)) {
         return array();
     }
     $userList = $this->dbo->mergeInClause($userIdList);
     $sql = " SELECT mess.*, `conv`.* FROM `" . $this->getTableName() . "` AS `conv`\n\n\t\t\t\t INNER JOIN `" . MAILBOX_BOL_LastMessageDao::getInstance()->getTableName() . "` AS `last_m`\n\t\t\t\t\t ON (`last_m`.`conversationId` = `conv`.`id`)\n\n            \t INNER JOIN `" . MAILBOX_BOL_MessageDao::getInstance()->getTableName() . "` AS `mess`\n\t\t\t\t \tON ( `last_m`.`initiatorMessageId` = `mess`.id AND ( `last_m`.`initiatorMessageId` > `last_m`.interlocutorMessageId )\n                    OR `last_m`.`interlocutorMessageId` = `mess`.id AND ( `last_m`.`initiatorMessageId` < `last_m`.interlocutorMessageId ) )\n\n\t\t\t     WHERE  `conv`.`notificationSent` = 0 AND ( ( `conv`.`initiatorId` IN ( {$userList} ) AND `last_m`.`interlocutorMessageId` > 0 AND `conv`.`deleted` != " . self::DELETED_INITIATOR . " AND NOT `conv`.`read` & " . self::READ_INITIATOR . "  AND NOT `conv`.`viewed` &  " . self::VIEW_INITIATOR . " )\n\t\t\t\t\t \tOR ( `conv`.`interlocutorId` IN ( {$userList} ) AND `conv`.`deleted` != " . self::DELETED_INTERLOCUTOR . "  AND  NOT `conv`.`read` & " . self::READ_INTERLOCUTOR . " AND NOT `conv`.`viewed` &  " . self::VIEW_INTERLOCUTOR . " ) ) \n        ";
     $conversationList = $this->dbo->queryForList($sql);
     $resultList = array();
     foreach ($conversationList as $conversation) {
         $userId = $conversation['recipientId'];
         $resultList[$userId][] = $conversation;
     }
     return $resultList;
 }