/** * 存储一条消息 * @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; }
/** * 存储一条消息 * @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 $isGroup 是否进行分组 * return array */ public function getUnreadMessageList($isGroup = false) { $hKey = self::UNREAD_KEY . '_' . $this->userId; try { $data = $this->getUserRedis()->hgetall($hKey); } catch (\RedisException $e) { Log::error(self::LOG_DIR . '/unread', __METHOD__ . ':' . $e->getMessage()); return array(); } if (!$isGroup) { return empty($data) ? array() : $data; } $tmpArr = array(); foreach ($data as $messageId => $sender) { $tmpArr[$sender][] = $messageId; } return $tmpArr; }
/** * 取得联系人列表 * @param Int $page 页码 * @param Int $pageSize 分页数 * @return Array */ public function getContactList($page, $pageSize) { $page = intval($page); $pageSize = intval($pageSize); $start = ($page - 1) * $pageSize; $end = $start + $pageSize; $listKey = self::CONTACT_LIST_KEY . $this->userId; $result = array(); $userModel = new UserModel($this->userId); try { $contactList = $this->getContactRedis()->ZREVRANGE($listKey, $start, $end); if (is_array($contactList) && !empty($contactList)) { $unreadMessageList = $userModel->getUnreadMessageList(true); foreach ($contactList as $contactKey) { $contact = $this->getContactRedis()->hGetAll($contactKey); if (empty($contact['contactId'])) { continue; } //规避垃圾数据 $contact['unreadNum'] = isset($unreadMessageList[$contact['contactId']]) ? count($unreadMessageList[$contact['contactId']]) : 0; $contact['onlinestate'] = $userModel->getOnlineState($contact['contactId']); $contact['photo'] = $userModel->getPhoto($contact['contactId']); $result[] = $contact; } } } catch (\Exception $e) { $this->setErrCode(Error::GET_CONTACT_LIST_FAIL); Log::fatal(self::LOG_FILE . '/getList', $e->getMessage()); return $result; } return $result; }
/** * 缓存count或者更新count * @param int $sender * @param int $receiver * @param array $updateData * @return boolean */ private function cacheCount($sender, $receiver, $updateData = array()) { try { //缓存往来消息总数 $contactKey_1 = Cache::CONTACT_MESSAGE_COUNT . '_' . $sender . '_' . $receiver; if ($total = $this->getCacheRedis()->get($contactKey_1)) { if (isset($updateData['senderDeleted']) && $updateData['senderDeleted'] == 1) { $this->getCacheRedis()->set($contactKey_1, $total - 1); } else { $this->getCacheRedis()->incr($contactKey_1); } } $contactKey_2 = Cache::CONTACT_MESSAGE_COUNT . '_' . $receiver . '_' . $sender; if ($total = $this->getCacheRedis()->get($contactKey_2)) { if (isset($updateData['receiverDeleted']) && $updateData['receiverDeleted'] == 1) { $this->getCacheRedis()->set($contactKey_2, $total - 1); } else { $this->getCacheRedis()->incr($contactKey_2); } } //缓存收件箱 $inBoxKey = Cache::INBOX_COUNT . '_' . $receiver; if ($total = $this->getCacheRedis()->get($inBoxKey)) { if (isset($updateData['receiverDeleted']) && $updateData['receiverDeleted'] == 1) { $this->getCacheRedis()->set($inBoxKey, $total - 1); } else { $this->getCacheRedis()->incr($inBoxKey); } } //缓存发件箱 $outBoxKey = Cache::OUTBOX_COUNT . '_' . $sender; if ($total = $this->getCacheRedis()->get($outBoxKey)) { if (isset($updateData['senderDeleted']) && $updateData['senderDeleted'] == 1) { $this->getCacheRedis()->set($outBoxKey, $total - 1); } else { $this->getCacheRedis()->incr($outBoxKey); } } } catch (\RedisException $e) { Log::error(self::LOG_DIR . '/cache', $e->getMessage()); return false; } return true; }