Ejemplo n.º 1
0
 public function addGroup(Group $model)
 {
     if ($resp = $this->checkGroup($model)) {
         return $resp;
     }
     if (!$model->checkType()) {
         return new ApiResponse(Code::FAIL_GROUP_TYPE, null);
     }
     $type = $model->type == Group::TYPE_GROUP ? 'g' : 'r';
     $model->icon = 'group/default/' . $type . rand(0, 9) . '.jpg';
     $model->create_time = time();
     $dbManager = Db::getInstance();
     $dbManager->beginTransaction();
     $statement = $dbManager->prepare(self::ADD_GROUP);
     if (!$statement->execute([$model->uid, $model->type, $model->name, $model->create_time, $model->description, $model->icon])) {
         $dbManager->rollBack();
         return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'create group failed');
     }
     $model->id = $dbManager->lastInsertId();
     $groupUser = new GroupUser();
     $groupUser->uid = $model->uid;
     $groupUser->status = GroupUser::STATUS_AGREE;
     $groupUser->permission = GroupUser::PERMISSION_ROOT;
     if ($resp = GroupUserManager::getInstance()->addGroupUser($model, $groupUser)) {
         $dbManager->rollBack();
         return $resp;
     }
     $dbManager->commit();
     if ($model->type == Group::TYPE_GROUP) {
         JegarnUtil::joinGroup($model->id, $model->uid);
     } else {
         JegarnUtil::joinChatroom($model->id, $model->uid);
     }
     return null;
 }
Ejemplo n.º 2
0
 public function addUser(User $model)
 {
     if (empty($model->username)) {
         return new ApiResponse(Code::FAIL_EMPTY_ACCOUNT, '');
     }
     if ($resp = $this->checkPassword($model->password)) {
         return $resp;
     }
     $gender = rand(0, 10) > 5 ? 'g' : 'b';
     $model->avatar = 'avatar/default/' . $gender . rand(0, 9) . '.jpg';
     $model->token = TextUtil::generateGUID();
     $model->create_time = time();
     $model->reg_ip = ApiRequest::getRemoteIp();
     $password = $this->enCryptPassword($model->password);
     $dbManager = Db::getInstance();
     $statement = $dbManager->prepare(self::ADD_USER);
     if (!$statement->execute([$model->username, $password, $model->create_time, $model->nick, $model->motto, $model->avatar, $model->token, $model->reg_ip])) {
         return new ApiResponse(Code::FAIL_USER_NAME_ALREADY_EXISTS, null);
     }
     $model->id = $dbManager->lastInsertId();
     JegarnUtil::addUser($model->id, $model->username, $model->token);
     JegarnUtil::sendUserSystemTextChatMessage($model->id, 'welcome to jegarn');
     // add login record
     $loginLog = new LoginLog($model->id, LoginLog::STATUS_SUCCESS);
     LoginLogManager::getInstance()->addLog($loginLog);
     // make friends with counter, join group 'Counter Group', join chatroom 'Counter Room'
     $targetId = self::COUNTER_UID;
     $groupId = self::COUNTER_GROUP_ID;
     $chatroomId = self::COUNTER_CHATROOM_ID;
     $sqlList = ["INSERT INTO `m_roster`(uid,target_id,status,create_time,update_time,remark,group_id,rank) VALUES ({$targetId},{$model->id},3,{$model->create_time},0,NULL,0,0)", "INSERT INTO `m_roster`(uid,target_id,status,create_time,update_time,remark,group_id,rank) VALUES ({$model->id},{$targetId},3,{$model->create_time},0,NULL,0,0)", "INSERT into `m_group_user`(gid,uid,permission,create_time,status,remark) values({$groupId},{$model->id},0,{$model->create_time},3,NULL)", "INSERT into `m_group_user`(gid,uid,permission,create_time,status,remark) values({$chatroomId},{$model->id},0,{$model->create_time},3,NULL)"];
     foreach ($sqlList as $sql) {
         $dbManager->exec($sql);
     }
     JegarnUtil::joinGroup($groupId, $model->id);
     JegarnUtil::joinChatroom($chatroomId, $model->id);
     return null;
 }
Ejemplo n.º 3
0
 public function joinGroup(Group $group, GroupUser $model)
 {
     if ($resp = $this->checkGroupWithUser($group, $model)) {
         return $resp;
     }
     if (($result = $this->getGroupUserArray($model)) instanceof ApiResponse) {
         return $result;
     }
     if ($result) {
         if (Group::TYPE_CHATROOM == $group->type) {
             return new ApiResponse(Code::FAIL_GROUP_USER_ALREADY_MEMBER, 'you are one of them');
         } else {
             ConvertUtil::arrayToObject($result, $model, ['id', 'gid', 'uid', 'permission', 'create_time', 'status', 'remark']);
             switch ($model->status) {
                 case GroupUser::STATUS_REQUEST:
                     return new ApiResponse(Code::FAIL_GROUP_USER_ALREADY_REQUEST, 'you have requested');
                     // if user be invited, and want to join this group, make user member
                 // if user be invited, and want to join this group, make user member
                 case GroupUser::STATUS_INVITED:
                     return null;
                 case GroupUser::STATUS_AGREE:
                     return new ApiResponse(Code::FAIL_GROUP_USER_ALREADY_MEMBER, 'you are one of them');
                     // if i am refuse this guy, but he send again, get he the second chance
                 // if i am refuse this guy, but he send again, get he the second chance
                 case GroupUser::STATUS_REFUSED:
                     $model->status = GroupUser::STATUS_REQUEST;
                     if ($resp = $this->updateGroupUserStatus($model)) {
                         return $resp;
                     }
                     JegarnUtil::sendGroupRequestNotification($group->uid, $model->uid, $group->id, $group->name);
                     return null;
                 default:
                     /*case GroupUser::STATUS_UNSUBSCRIBE:
                       case GroupUser::STATUS_BLACK:*/
                     return new ApiResponse(Code::FAIL_GROUP_USER_ALREADY_REFUSED, 'you have requested');
             }
         }
     } else {
         if (Group::TYPE_CHATROOM == $group->type) {
             $model->status = GroupUser::STATUS_AGREE;
             $model->permission = GroupUser::PERMISSION_NORMAL;
             if ($resp = $this->addGroupUser($group, $model)) {
                 return $resp;
             }
             JegarnUtil::joinChatroom($group->id, $model->uid);
         } else {
             $model->status = GroupUser::STATUS_REQUEST;
             $model->permission = GroupUser::PERMISSION_NORMAL;
             if ($resp = $this->addGroupUser($group, $model)) {
                 return $resp;
             }
             JegarnUtil::sendGroupRequestNotification($group->uid, $model->uid, $group->id, $group->name);
         }
     }
     return null;
 }