/** * Deletes old messages according to the plugin's settings. * Images connected to the messages (WordPress Media Library attachments) are also deleted. * * @param WiseChatChannel $channel * * @throws Exception */ private function deleteOldMessages($channel) { $minutesThreshold = $this->options->getIntegerOption('auto_clean_after', 0); if ($minutesThreshold > 0) { $criteria = new WiseChatMessagesCriteria(); $criteria->setChannelName($channel->getName()); $criteria->setIncludeAdminMessages(true); $criteria->setMaximumTime(time() - $minutesThreshold * 60); $messages = $this->messagesDAO->getAllByCriteria($criteria); $messagesIds = array(); foreach ($messages as $message) { $messagesIds[] = $message->getId(); } if (count($messagesIds) > 0) { $this->attachmentsService->deleteAttachmentsByMessageIds($messagesIds); $this->actions->publishAction('deleteMessages', array('ids' => $messagesIds)); $this->messagesDAO->deleteAllByCriteria($criteria); } } }
/** * 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; }
/** * Refreshes username after setting a new one. * * @param WiseChatUser $user * * @return null */ private function refreshNewUserName($user) { WiseChatContainer::load('dao/criteria/WiseChatMessagesCriteria'); $this->refreshUserName($user); $this->messagesDAO->updateUserNameByCriteria($user->getName(), WiseChatMessagesCriteria::build()->setUserId($user->getId())); /** @var WiseChatRenderer $renderer */ $renderer = WiseChatContainer::get('rendering/WiseChatRenderer'); $criteria = new WiseChatMessagesCriteria(); $criteria->setUserId($user->getId()); $messages = $this->messagesDAO->getAllByCriteria($criteria); if (count($messages) > 0) { $messagesIds = array(); $renderedUserName = null; foreach ($messages as $message) { $messagesIds[] = $message->getId(); if ($renderedUserName === null) { $renderedUserName = $renderer->getRenderedUserName($message); } } $this->actions->publishAction('replaceUserNameInMessages', array('renderedUserName' => $renderedUserName, 'messagesIds' => $messagesIds)); } }