/** * Maintenance actions performed periodically. The method authenticates user if there was * no authentication performed. * * @param WiseChatChannel $channel * * @return null */ public function periodicMaintenance($channel) { // check and authenticate user: if (!$this->authentication->isAuthenticated()) { $user = $this->authentication->authenticateAnonymously(); $this->actions->publishAction('refreshPlainUserName', array('name' => $user->getName()), $user); } // signal presence in the channel: if ($this->userEvents->shouldTriggerEvent('ping', $channel->getName())) { $this->markPresenceInChannel($channel); } $this->refreshChannelUsersData(); }
/** * Endpoint for periodic (every 10-20 seconds) maintenance services like: * - user authentication * - getting the list of actions to execute on the client side * - getting the list of events to listen on the client side * - maintenance actions in messages, bans, users, etc. */ public function maintenanceEndpoint() { $this->verifyCheckSum(); $response = array(); try { $this->checkChatOpen(); $this->checkUserAuthorization(); $this->checkGetParams(array('channelId', 'lastActionId')); $channelId = $this->getGetParam('channelId'); $channel = $this->channelsDAO->get($channelId); $this->checkChannel($channel); $this->checkChannelAuthorization($channel); // periodic maintenance: $this->userService->periodicMaintenance($channel); $this->messagesService->periodicMaintenance($channel); $this->bansService->periodicMaintenance(); // load actions: $lastActionId = intval($this->getGetParam('lastActionId', 0)); $user = $this->authentication->getUser(); $response['actions'] = $this->actions->getJSONReadyActions($lastActionId, $user); // load events: $response['events'] = array(); if ($this->userEvents->shouldTriggerEvent('usersList', $channel->getName())) { if ($this->options->isOptionEnabled('show_users')) { $response['events'][] = array('name' => 'refreshUsersList', 'data' => $this->renderer->getRenderedUsersList($channel)); } if ($this->options->isOptionEnabled('show_users_counter')) { $response['events'][] = array('name' => 'refreshUsersCounter', 'data' => array('total' => $this->channelUsersDAO->getAmountOfUsersInChannel($channel->getId()))); } } } catch (WiseChatUnauthorizedAccessException $exception) { $response['error'] = $exception->getMessage(); $this->sendUnauthorizedStatus(); } catch (Exception $exception) { $response['error'] = $exception->getMessage(); $this->sendBadRequestStatus(); } echo json_encode($response); die; }