示例#1
0
 public function updateRoster(Roster $model)
 {
     if (!$model->uid || !$model->target_id) {
         return new ApiResponse(Code::FAIL_ROSTER_NOT_EXISTS, 'uid or target_id is empty');
     }
     if ($model->status != Roster::STATUS_UNSUBSCRIBE && $model->status != Roster::STATUS_AGREE && $model->status != Roster::STATUS_REFUSED && $model->status != Roster::STATUS_BLACK) {
         return new ApiResponse(Code::FAIL_ROSTER_STATUS, 'status must be unsubscribe or receive or refused or blacklist');
     }
     if ($resp = $this->checkRoster($model)) {
         return $resp;
     }
     $modelCopy = clone $model;
     if ($resp = $this->getRoster($modelCopy)) {
         return $resp;
     }
     $model->id = $modelCopy->id;
     if ($model->uid != $modelCopy->uid) {
         return new ApiResponse(Code::FAIL_ROSTER_NOT_EXISTS, 'other\'s roster');
     }
     $fullSame = true;
     $compareField = ['status', 'remark', 'group_id', 'rank'];
     foreach ($compareField as $f) {
         if ($model->{$f} != $modelCopy->{$f}) {
             $fullSame = false;
             break;
         }
     }
     if ($fullSame) {
         return null;
     }
     $model->update_time = time();
     $dbManager = Db::getInstance();
     // receive when i am her friend or her ask to make
     $roster = null;
     if ($model->status == Roster::STATUS_AGREE) {
         $roster = new Roster();
         $roster->uid = $model->target_id;
         $roster->target_id = $model->uid;
         if ($resp = $this->getRoster($roster)) {
             return $resp;
         }
         if ($roster->status != Roster::STATUS_REQUEST && $roster->status != Roster::STATUS_AGREE) {
             return new ApiResponse(Code::FAIL_ROSTER_STATUS, 'status must be ask or agree');
         }
     }
     $dbManager->beginTransaction();
     $statement = $dbManager->prepare(self::UPDATE_ROSTER);
     if (!$statement->execute([$model->status, $model->update_time, $model->remark, $model->group_id, $model->rank, $model->id])) {
         $dbManager->rollBack();
         return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'update roster failed');
     }
     if ($roster && $roster->status == Roster::STATUS_REQUEST) {
         $roster->status = Roster::STATUS_AGREE;
         $roster->update_time = time();
         if (!$statement->execute([$roster->status, $roster->update_time, $roster->remark, $roster->group_id, $roster->rank, $roster->id])) {
             $dbManager->rollBack();
             return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'update target roster failed');
         }
         JegarnUtil::sendFriendAgreeNotification($roster->target_id, $roster->uid);
     }
     $dbManager->commit();
     // if status not changed, it may change friend attributes. so don not need notify
     if ($modelCopy->status != Roster::STATUS_AGREE && $model->status == Roster::STATUS_AGREE) {
         JegarnUtil::sendFriendAgreeNotification($model->target_id, $model->uid);
     } else {
         if ($modelCopy->status != Roster::STATUS_REFUSED && $model->status == Roster::STATUS_REFUSED) {
             JegarnUtil::sendFriendRefusedNotification($model->target_id, $model->uid);
         }
     }
     return null;
 }