Beispiel #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');
     $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;
 }