/**
  * 更新或创建指定用户对指定消息的阅读状态
  *
  * @param integer $uid     用户ID
  * @param integer $msgId   消息ID
  * @param string  $status  想要获取消息的标记类型
  * @throws ResourceException
  */
 public function putUserMessageStatusAction($uid, $msgId, $status)
 {
     $msgLog = MessageLogs::findFirst(array('rec_UID = ?0 AND msg_id = ?1', 'bind' => array($uid, $msgId)));
     if (!$msgLog) {
         $msgLog = new MessageLogs();
         $msgLog->rec_UID = $uid;
         $msgLog->msg_id = $msgId;
     }
     if (!in_array($status, array(MessageLogs::STATUS_UNREAD, MessageLogs::STATUS_READ, MessageLogs::STATUS_DELETE))) {
         throw new ResourceException('Conflict', 409);
     }
     $msgLog->status = $status;
     if ($msgLog->save()) {
         $this->response(200, 'OK');
     } else {
         throw new ResourceException('Internal Server Error', 500);
     }
 }
Beispiel #2
0
 /**
  * 投递站内信
  *
  * @param integer $send_uid       发送者用户ID
  * @param string  $content        消息内容
  * @param string  $msg_options    消息选项
  * @param integer $uid_or_gid     用户ID或用户组ID
  * @param string  $post_type      投递类型
  * @param string  $post_time      投递时间
  * @param integer $expiry         消息有效期(仅供显示, 不参与最终的失效计算)
  * @param string  $expiry_at_end  消息结束日期
  * @throws ResourceException
  * @throws \Itslove\Passport\Helper\ValidationException
  */
 public function postMessageAction($send_uid, $content, $msg_options, $uid_or_gid, $post_type, $post_time, $expiry, $expiry_at_end)
 {
     //判断发送者发送权限
     /** @var \Itslove\Passport\Models\Users $sendUser */
     if (!($sendUser = Users::findPrimary($send_uid)) || 0 == $sendUser->active) {
         throw new ResourceException('Forbidden', 403);
     }
     $msg = new Messages();
     $msg->send_UID = $this->validation->validate('id', $send_uid);
     $msg->content = $content;
     $msg->msg_options = $msg_options;
     $msg->post_type = $post_type;
     $msg->post_time = $this->validation->validate('datetime', $post_time);
     $msg->send_delete = 0;
     $msg->expiry = $this->validation->validate('uint', $expiry);
     $msg->expiry_at_end = $this->validation->validate('datetime', $expiry_at_end);
     switch ($msg->post_type) {
         case Messages::POST_TYPE_PRIVATE:
             $msg->GID = 0;
             $msgLog = new MessageLogs();
             $msgLog->rec_UID = $uid_or_gid;
             $msgLog->status = MessageLogs::STATUS_UNREAD;
             break;
         case Messages::POST_TYPE_PUBLIC:
             $msg->GID = $uid_or_gid;
             break;
         case Messages::POST_TYPE_GLOBAL:
             $msg->GID = 0;
             break;
         default:
             throw new ResourceException('Conflict', 409);
     }
     $this->db->begin();
     try {
         if (isset($msgLog) && false == Users::findPrimary($msgLog->rec_UID)) {
             throw new ResourceException('Not Found', 404);
         }
         if ($msg->create()) {
             $this->response(201, 'Created', array('msg_id' => $msg->msg_id));
             $this->response->setHeader('Location', 'message/' . $msg->msg_id);
         } else {
             throw new ResourceException('Internal Server Error', 500);
         }
         if (isset($msgLog)) {
             $msgLog->msg_id = $msg->msg_id;
             if (!$msgLog->create()) {
                 throw new ResourceException('Internal Server Error', 500);
             }
         }
         $this->db->commit();
     } catch (ResourceException $e) {
         $this->db->rollback();
         throw $e;
     }
 }