Exemple #1
0
 /**
  * 存储一条消息
  * @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');
     $message['isRead'] = 0;
     if ($this->userModel->getTalkId($message['sender']) == $message['receiver']) {
         $message['isRead'] = 1;
     }
     //消息关系数据
     $relationData = array('catId' => $message['catId'], 'sender' => $message['sender'], 'receiver' => $message['receiver'], 'messageId' => $message['messageId'], 'sendTime' => $message['sendTime']);
     //Cli模式下开启长连接
     if (Yaf\Application::app()->getDispatcher()->getRequest()->isCli()) {
         $pdo = PdoFactory::instance('messageMaster', true);
     } else {
         $pdo = PdoFactory::instance('messageMaster');
     }
     try {
         $pdo->beginTransaction();
         $messageDao = new Dao\MessageModel($pdo, Table::message($messageId));
         $relationSenderDao = new Dao\RelationModel($pdo, Table::relation(intval($message['sender'])));
         $relationReceiverDao = new Dao\RelationModel($pdo, Table::relation(intval($message['receiver'])));
         $messageDao->save($message);
         if (!$relationSenderDao->has($message['messageId'])) {
             $relationSenderDao->save($relationData);
         }
         if (!$relationReceiverDao->has($message['messageId'])) {
             $relationReceiverDao->save($relationData);
         }
         $pdo->commit();
     } catch (\PDOException $e) {
         if ($pdo->errorCode() === 2006) {
             $this->setErrCode(Error::MYSQL_GONE_AWAY);
         } else {
             $this->setErrCode(Error::SAVE_MESSAGE_ERROR);
         }
         Log::fatal(self::LOG_DIR . '/save', $e->getMessage());
         $pdo->rollBack();
         return false;
     }
     $this->contactModel->addContact($message);
     $this->cacheModel->cache($message);
     if ($message['isRead'] == 0) {
         $this->userModel->appendUnreadMessage($message);
     }
     return $message;
 }
Exemple #2
0
 /**
  * 存储一条消息
  * @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;
 }