public function getMembers($chatId) { $members = \Chats\Chat\Member::getList(['where' => ['chat_id', $chatId]]); foreach ($members as $key => $member) { if (strtotime($member->date_last_active) - time() > 30) { $member->delete(); unset($members[$key]); } } return $members; }
public function eventsAction($chatId = 0) { $chatId = (int) $chatId; $result = new Server\Result(); if (!$chatId || !($chat = \Chats\Chat::get($chatId))) { $result->success = false; $result->content = 'Такого чата не существует'; $result->send(); } $where = [['chat_id', $chatId], ['delete', 0]]; if (!empty($_GET['lastEventDate'])) { $where[] = ['date_create', $_GET['lastEventDate'], '>']; } $result->content = ['members' => [], 'messages' => []]; if (Users\User::$cur->id) { $member = \Chats\Chat\Member::get([['chat_id', $chatId], ['user_id', Users\User::$cur->id]]); if (!$member) { $member = new \Chats\Chat\Member(); $member->user_id = Users\User::$cur->id; $member->chat_id = $chatId; } $member->date_last_active = date('Y-m-d H:i:s'); $member->save(); } $messages = \Chats\Chat\Message::getList(['where' => $where, 'limit' => 20, 'order' => ['date_create', 'DESC']]); if ($messages) { $messages = array_reverse($messages); foreach ($messages as $message) { $msg = ['message' => $message->_params, 'fullUserName' => $message->user->name(), 'userFirstName' => $message->user->info->first_name, 'userPhoto' => $message->user->info->photo ? $message->user->info->photo->path : '/static/system/images/no-image.png']; $result->content['messages'][] = $msg; } /* $members = $this->module->getMembers($chatId); foreach ($members as $member) { $result->content['members'][$member->user_id] = [ 'fullUserName' => $member->user->name(), 'userFirstName' => $member->user->info->first_name, 'userPhoto' => $member->user->info->photo ? $member->user->info->photo->path : '/static/system/images/no-image.png' ]; } */ } $result->send(); }