diff --git a/app/Builders/ImGroupUserList.php b/app/Builders/ImGroupUserList.php index 0f3e1846..909adbed 100644 --- a/app/Builders/ImGroupUserList.php +++ b/app/Builders/ImGroupUserList.php @@ -36,7 +36,7 @@ class ImGroupUserList extends Builder $userRepo = new UserRepo(); - $columns = ['id', 'name', 'avatar', 'about', 'vip']; + $columns = ['id', 'name', 'avatar', 'title', 'about', 'vip']; $users = $userRepo->findByIds($ids, $columns); diff --git a/app/Caches/ImGroupActiveUserList.php b/app/Caches/ImGroupActiveUserList.php new file mode 100644 index 00000000..82c24f68 --- /dev/null +++ b/app/Caches/ImGroupActiveUserList.php @@ -0,0 +1,82 @@ +lifetime; + } + + public function getKey($id = null) + { + return "im_group_active_user_list:{$id}"; + } + + public function getContent($id = null) + { + $users = $this->findUsers($id); + + if (!$users) return []; + + $result = []; + + foreach ($users as $user) { + $result[] = [ + 'id' => $user->id, + 'name' => $user->name, + 'avatar' => $user->avatar, + 'title' => $user->title, + 'about' => $user->about, + 'vip' => $user->vip, + ]; + } + + return $result; + } + + /** + * @param int $groupId + * @param int $days + * @param int $limit + * @return ResultsetInterface|Resultset|UserModel[] + */ + protected function findUsers($groupId, $days = 7, $limit = 5) + { + $result = []; + + $startTime = strtotime("-{$days} days"); + $endTime = time(); + + $rows = ImGroupMessageModel::query() + ->columns(['sender_id', 'total_count' => 'count(sender_id)']) + ->groupBy('sender_id') + ->orderBy('total_count DESC') + ->where('group_id = :group_id:', ['group_id' => $groupId]) + ->betweenWhere('create_time', $startTime, $endTime) + ->limit($limit) + ->execute(); + + if ($rows->count() > 0) { + + $ids = kg_array_column($rows->toArray(), 'sender_id'); + + $userRepo = new UserRepo(); + + $result = $userRepo->findByIds($ids); + } + + return $result; + } + +} diff --git a/app/Http/Web/Controllers/ImGroupController.php b/app/Http/Web/Controllers/ImGroupController.php index 61c452c5..cc808e77 100644 --- a/app/Http/Web/Controllers/ImGroupController.php +++ b/app/Http/Web/Controllers/ImGroupController.php @@ -54,57 +54,26 @@ class ImGroupController extends Controller { $service = new ImGroupService(); - $group = $service->getGroup($id); - $pager = $service->getGroupUsers($id); - $pager->items = kg_array_object($pager->items); + $pager->target = 'user-list'; - $this->view->setVar('group', $group); + $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW); $this->view->setVar('pager', $pager); } /** - * @Get("/{id:[0-9]+}/edit", name="web.im_group.edit") + * @Get("/{id:[0-9]+}/users/active", name="web.im_group.active_users") */ - public function editAction($id) + public function activeUsersAction($id) { $service = new ImGroupService(); - $group = $service->getGroup($id); + $users = $service->getActiveGroupUsers($id); - $this->view->setVar('group', $group); - } - - /** - * @Post("/{id:[0-9]+}/update", name="web.im_group.update") - */ - public function updateAction($id) - { - $service = new ImGroupService(); - - $service->updateGroup($id); - - $content = ['msg' => '更新群组成功']; - - return $this->jsonSuccess($content); - } - - /** - * @Post("/{gid:[0-9]+}/user/{uid:[0-9]+}/delete", name="web.im_group.delete_user") - */ - public function deleteGroupUserAction($gid, $uid) - { - $service = new ImGroupService(); - - $service->deleteGroupUser($gid, $uid); - - $content = [ - 'location' => $this->request->getHTTPReferer(), - 'msg' => '移除用户成功', - ]; - - return $this->jsonSuccess($content); + $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW); + $this->view->pick('im_group/active_users'); + $this->view->setVar('users', $users); } } diff --git a/app/Http/Web/Controllers/ImGroupManageController.php b/app/Http/Web/Controllers/ImGroupManageController.php new file mode 100644 index 00000000..12be7023 --- /dev/null +++ b/app/Http/Web/Controllers/ImGroupManageController.php @@ -0,0 +1,73 @@ +getGroup($id); + + $pager = $service->getGroupUsers($id); + + $pager->items = kg_array_object($pager->items); + + $this->view->setVar('group', $group); + $this->view->setVar('pager', $pager); + } + + /** + * @Get("/{id:[0-9]+}/edit", name="web.igm.edit") + */ + public function editAction($id) + { + $service = new ImGroupService(); + + $group = $service->getGroup($id); + + $this->view->setVar('group', $group); + } + + /** + * @Post("/{id:[0-9]+}/update", name="web.igm.update") + */ + public function updateAction($id) + { + $service = new ImGroupService(); + + $service->updateGroup($id); + + $content = ['msg' => '更新群组成功']; + + return $this->jsonSuccess($content); + } + + /** + * @Post("/{gid:[0-9]+}/user/{uid:[0-9]+}/delete", name="web.igm.delete_user") + */ + public function deleteGroupUserAction($gid, $uid) + { + $service = new ImGroupService(); + + $service->deleteGroupUser($gid, $uid); + + $content = [ + 'location' => $this->request->getHTTPReferer(), + 'msg' => '移除用户成功', + ]; + + return $this->jsonSuccess($content); + } + +} diff --git a/app/Http/Web/Services/Im.php b/app/Http/Web/Services/Im.php index 121868b9..bfdfaa33 100644 --- a/app/Http/Web/Services/Im.php +++ b/app/Http/Web/Services/Im.php @@ -371,15 +371,18 @@ class Im extends Service if ($online) { Gateway::sendToUid($to['id'], $content); } else { - $msgCount = $relation->msg_count + 1; - $relation->update(['msg_count' => $msgCount]); + $this->incrFriendUserMsgCount($relation); } } elseif ($to['type'] == 'group') { + $validator = new ImGroupValidator(); + + $group = $validator->checkGroup($to['id']); + $validator = new ImGroupUserValidator(); - $validator->checkGroupUser($to['id'], $user->id); + $validator->checkGroupUser($group->id, $user->id); $messageModel = new ImGroupMessageModel(); @@ -389,6 +392,8 @@ class Im extends Service 'content' => $from['content'], ]); + $this->incrGroupMessageCount($group); + $excludeClientId = null; /** diff --git a/app/Http/Web/Services/ImFriendTrait.php b/app/Http/Web/Services/ImFriendTrait.php index 73f6eb7f..a210b721 100644 --- a/app/Http/Web/Services/ImFriendTrait.php +++ b/app/Http/Web/Services/ImFriendTrait.php @@ -279,4 +279,10 @@ Trait ImFriendTrait } } + protected function incrFriendUserMsgCount(ImFriendUserModel $friendUser) + { + $friendUser->msg_count += 1; + $friendUser->update(); + } + } diff --git a/app/Http/Web/Services/ImGroup.php b/app/Http/Web/Services/ImGroup.php index 0c4aac57..cb55705f 100644 --- a/app/Http/Web/Services/ImGroup.php +++ b/app/Http/Web/Services/ImGroup.php @@ -4,6 +4,7 @@ namespace App\Http\Web\Services; use App\Builders\ImGroupList as ImGroupListBuilder; use App\Builders\ImGroupUserList as ImGroupUserListBuilder; +use App\Caches\ImGroupActiveUserList as ImGroupActiveUserListCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\ImGroup as ImGroupModel; use App\Repos\ImGroup as ImGroupRepo; @@ -34,6 +35,67 @@ class ImGroup extends Service return $this->handleGroups($pager); } + public function getGroup($id) + { + $validator = new ImGroupValidator(); + + $group = $validator->checkGroup($id); + + $userRepo = new UserRepo(); + + $owner = $userRepo->findById($group->owner_id); + + return [ + 'id' => $group->id, + 'type' => $group->type, + 'name' => $group->name, + 'avatar' => $group->avatar, + 'about' => $group->about, + 'user_count' => $group->user_count, + 'msg_count' => $group->msg_count, + 'owner' => [ + 'id' => $owner->id, + 'name' => $owner->name, + 'avatar' => $owner->avatar, + 'title' => $owner->title, + 'about' => $owner->about, + 'vip' => $owner->vip, + ], + ]; + } + + public function getGroupUsers($id) + { + $validator = new ImGroupValidator(); + + $group = $validator->checkGroup($id); + + $pagerQuery = new PagerQuery(); + + $params = $pagerQuery->getParams(); + + $params['group_id'] = $group->id; + + $sort = $pagerQuery->getSort(); + $page = $pagerQuery->getPage(); + $limit = $pagerQuery->getLimit(); + + $repo = new ImGroupUserRepo(); + + $pager = $repo->paginate($params, $sort, $page, $limit); + + return $this->handleGroupUsers($pager); + } + + public function getActiveGroupUsers($id) + { + $cache = new ImGroupActiveUserListCache(); + + $result = $cache->get($id); + + return $result ?: []; + } + public function updateGroup($id) { $post = $this->request->getPost(); @@ -68,54 +130,6 @@ class ImGroup extends Service return $group; } - public function getGroup($id) - { - $validator = new ImGroupValidator(); - - $group = $validator->checkGroup($id); - - $userRepo = new UserRepo(); - - $owner = $userRepo->findById($group->owner_id); - - return [ - 'id' => $group->id, - 'name' => $group->name, - 'about' => $group->about, - 'user_count' => $group->user_count, - 'owner' => [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, - 'title' => $owner->title, - 'about' => $owner->about, - ], - ]; - } - - public function getGroupUsers($id) - { - $validator = new ImGroupValidator(); - - $group = $validator->checkGroup($id); - - $pagerQuery = new PagerQuery(); - - $params = $pagerQuery->getParams(); - - $params['group_id'] = $group->id; - - $sort = $pagerQuery->getSort(); - $page = $pagerQuery->getPage(); - $limit = $pagerQuery->getLimit(); - - $repo = new ImGroupUserRepo(); - - $pager = $repo->paginate($params, $sort, $page, $limit); - - return $this->handleGroupUsers($pager); - } - public function deleteGroupUser($groupId, $userId) { $loginUser = $this->getLoginUser(); @@ -183,6 +197,7 @@ class ImGroup extends Service 'avatar' => $group['avatar'], 'about' => $group['about'], 'user_count' => $group['user_count'], + 'msg_count' => $group['msg_count'], 'owner' => $group['owner'], ]; } diff --git a/app/Http/Web/Services/ImGroupTrait.php b/app/Http/Web/Services/ImGroupTrait.php index 549bbfe2..2ace2d61 100644 --- a/app/Http/Web/Services/ImGroupTrait.php +++ b/app/Http/Web/Services/ImGroupTrait.php @@ -148,7 +148,7 @@ Trait ImGroupTrait { $userRepo = new ImUserRepo(); - $receiver = $userRepo->findById($group->user_id); + $receiver = $userRepo->findById($group->owner_id); $itemType = ImSystemMessageModel::TYPE_GROUP_REQUEST; @@ -314,4 +314,10 @@ Trait ImGroupTrait } } + protected function incrGroupMessageCount(ImGroupModel $group) + { + $group->msg_count += 1; + $group->update(); + } + } diff --git a/app/Http/Web/Views/course/show.volt b/app/Http/Web/Views/course/show.volt index 703081d4..a355589c 100644 --- a/app/Http/Web/Views/course/show.volt +++ b/app/Http/Web/Views/course/show.volt @@ -84,8 +84,12 @@ {% set show_sidebar_related = 1 %}
diff --git a/app/Http/Web/Views/course/topics.volt b/app/Http/Web/Views/course/topics.volt index 4839d2fc..e2072626 100644 --- a/app/Http/Web/Views/course/topics.volt +++ b/app/Http/Web/Views/course/topics.volt @@ -3,7 +3,8 @@{{ group.about }}-