/** * 存储一条消息 * @param Array $message */ public function save($data) { $message = $this->filter($data); if (!$this->check($message)) { $this->setErrCode(Error::PARAMS_ERROR); return false; } $messageId = Dao\MessageIdModel::getId(); $message['messageId'] = $messageId; $message['sendTime'] = date('Y-m-d H:i:s'); $userModel = new UserModel($this->userId); $message['isRead'] = 0; if ($userModel->getTalkId($message['sender']) == $message['receiver']) { $message['isRead'] = 1; } $messageDao = new Dao\MessageModel(Table::message($messageId)); $relationSenderDao = new Dao\RelationModel(Table::relation($message['sender'])); $relationReceiverDao = new Dao\RelationModel(Table::relation($message['sender'])); $relationData = array('sender' => $message['sender'], 'receiver' => $message['receiver'], 'senderNickname' => $message['senderNickname'], 'receiverNickname' => $message['receiverNickname'], 'messageId' => $messageId, 'isRead' => $message['isRead']); $pdo = PdoFactory::instance(Dao\MessageModel::DB_MASTER); $pdo->beginTransaction(); try { if (!$messageDao->save($message)) { throw new \Exception('save message fail!'); } if (!$relationSenderDao->has($messageId) && $relationSenderDao->save($relationData)) { throw new \Exception('save sender relation fail!'); } if (!$relationReceiverDao->has($messageId) && $relationReceiverDao->save($relationData)) { throw new \Exception('save receiver relation fail!'); } $pdo->commit(); } catch (\Exception $e) { $pdo->rollBack(); $this->setErrCode(Error::SAVE_MESSAGE_ERROR); Log::fatal(self::LOG_DIR, $e->getMessage()); return false; } $messageContentModel = new MessageContentModel(); //处理消息关系 $message['summary'] = $messageContentModel->getMessageSummary($message['content']); $contactModel = new ContactModel($this->userId); $contactModel->addContact($message); //追加未读消息 if ($message['isRead'] == 0) { $userModel->appendUnreadMessage($message['sender'], $messageId); } return true; }
/** * 取得消息数据 * @param $messageId */ private function getMessage($messageId) { $message = $this->cacheModel->getCacheMessage($messageId); $messageDao = null; if (empty($message) || !is_array($message)) { $pdo = PdoFactory::instance('messageSlave'); $messageDao = new Dao\MessageModel($pdo, Table::message($messageId)); $message = $messageDao->get($messageId); $this->cacheModel->cacheMessage($message); } //设置消息其他信息 $message['senderPhoto'] = $this->userModel->getPhoto($message['sender']); $message['receiverPhoto'] = $this->userModel->getPhoto($message['receiver']); //消息富文本 $message['richContent'] = $this->messageContentModel->getRichContent($message['content']); return $message; }
/** * 设置消息已读 * @param $messageId */ public function setIsRead($messageId) { $pdo = PdoFactory::instance('messageMaster'); $messageDao = new Dao\MessageModel($pdo, Table::message($messageId)); $message = $this->cacheModel->getCacheMessage($messageId); if (empty($message) || !is_array($message)) { $pdo = PdoFactory::instance('messageSlave'); $messageDao = new Dao\MessageModel($pdo, Table::message($messageId)); $message = $messageDao->get($messageId); } if (empty($message) || !is_array($message)) { return false; } $messageDao->update($messageId, array('isRead' => 1)); $message['isRead'] = 1; $this->cacheModel->cacheMessage($message); $this->userModel->removeUnreadMessage($this->userId, $messageId); return true; }