Skip to content

Commit

Permalink
Cleanup duplicate API methods
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jan 26, 2021
1 parent b3e4d57 commit cca0a12
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 611 deletions.
5 changes: 0 additions & 5 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
return [
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#get', 'url' => '/announcement', 'verb' => 'GET'],
['name' => 'page#add', 'url' => '/announcement', 'verb' => 'POST'],
['name' => 'page#delete', 'url' => '/announcement/{id}', 'verb' => 'DELETE'],
['name' => 'page#removeNotifications', 'url' => '/announcement/{id}/notifications', 'verb' => 'DELETE'],
['name' => 'page#searchGroups', 'url' => '/groups', 'verb' => 'GET'],
],
'ocs' => [
['name' => 'API#get', 'url' => '/api/v1/announcements', 'verb' => 'GET'],
Expand Down
26 changes: 4 additions & 22 deletions lib/Controller/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@

namespace OCA\AnnouncementCenter\Controller;

use InvalidArgumentException;
use OCA\AnnouncementCenter\Manager;
use OCA\AnnouncementCenter\Model\Announcement;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
Expand All @@ -50,9 +47,6 @@ class APIController extends OCSController {
/** @var IJobList */
protected $jobList;

/** @var IDBConnection */
protected $connection;

/** @var IGroupManager */
protected $groupManager;

Expand All @@ -65,42 +59,30 @@ class APIController extends OCSController {
/** @var Manager */
protected $manager;

/** @var IConfig */
protected $config;

/** @var ITimeFactory */
protected $timeFactory;

/** @var IUserSession */
protected $userSession;

/** @var IInitialStateService */
protected $initialState;

public function __construct(string $appName,
IRequest $request,
IDBConnection $connection,
IGroupManager $groupManager,
IUserManager $userManager,
IJobList $jobList,
IL10N $l,
Manager $manager,
IConfig $config,
ITimeFactory $timeFactory,
IUserSession $userSession,
IInitialStateService $initialState) {
IUserSession $userSession) {
parent::__construct($appName, $request);

$this->connection = $connection;
$this->groupManager = $groupManager;
$this->userManager = $userManager;
$this->jobList = $jobList;
$this->l = $l;
$this->manager = $manager;
$this->config = $config;
$this->timeFactory = $timeFactory;
$this->userSession = $userSession;
$this->initialState = $initialState;
}

/**
Expand Down Expand Up @@ -139,7 +121,7 @@ public function add(string $subject, string $message, array $groups, bool $activ

try {
$announcement = $this->manager->announce($subject, $message, $userId, $this->timeFactory->getTime(), $groups, $comments);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
return new DataResponse(
['error' => $this->l->t('The subject is too long or empty')],
Http::STATUS_BAD_REQUEST
Expand Down Expand Up @@ -226,7 +208,7 @@ public function delete(int $id): DataResponse {
* @NoAdminRequired
*
* @param int $id
* @return Response
* @return DataResponse
*/
public function removeNotifications(int $id): DataResponse {
if (!$this->manager->checkIsAdmin()) {
Expand Down
206 changes: 7 additions & 199 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,201 +27,36 @@

use OCA\AnnouncementCenter\AppInfo\Application;
use OCA\AnnouncementCenter\Manager;
use OCA\AnnouncementCenter\Model\Announcement;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCA\AnnouncementCenter\BackgroundJob;

class PageController extends Controller {

/** @var IJobList */
protected $jobList;

/** @var IDBConnection */
protected $connection;

/** @var IGroupManager */
protected $groupManager;

/** @var IUserManager */
protected $userManager;

/** @var IL10N */
protected $l;

/** @var Manager */
protected $manager;

/** @var IConfig */
protected $config;

/** @var ITimeFactory */
protected $timeFactory;

/** @var IUserSession */
protected $userSession;

/** @var IInitialStateService */
/** @var IInitialState */
protected $initialState;

public function __construct(string $AppName,
IRequest $request,
IDBConnection $connection,
IGroupManager $groupManager,
IUserManager $userManager,
IJobList $jobList,
IL10N $l,
Manager $manager,
IConfig $config,
ITimeFactory $timeFactory,
IUserSession $userSession,
IInitialStateService $initialState) {
IInitialState $initialState) {
parent::__construct($AppName, $request);

$this->connection = $connection;
$this->groupManager = $groupManager;
$this->userManager = $userManager;
$this->jobList = $jobList;
$this->l = $l;
$this->manager = $manager;
$this->config = $config;
$this->timeFactory = $timeFactory;
$this->userSession = $userSession;
$this->initialState = $initialState;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $offset
* @return JSONResponse
*/
public function get($offset = 0): JSONResponse {
$announcements = $this->manager->getAnnouncements($offset);
$data = array_map([$this, 'renderAnnouncement'], $announcements);
return new JSONResponse($data);
}

/**
* @NoAdminRequired
*
* @param string $subject
* @param string $message
* @param string[] $groups,
* @param bool $activities
* @param bool $notifications
* @param bool $comments
* @return JSONResponse
*/
public function add($subject, $message, array $groups, $activities, $notifications, $comments): JSONResponse {
if (!$this->manager->checkIsAdmin()) {
return new JSONResponse(
['message' => 'Logged in user must be an admin'],
Http::STATUS_FORBIDDEN
);
}
$user = $this->userSession->getUser();
$userId = $user instanceof IUser ? $user->getUID() : '';

try {
$announcement = $this->manager->announce($subject, $message, $userId, $this->timeFactory->getTime(), $groups, $comments);
} catch (\InvalidArgumentException $e) {
return new JSONResponse(
['error' => $this->l->t('The subject is too long or empty')],
Http::STATUS_BAD_REQUEST
);
}

if ($activities || $notifications) {
$this->jobList->add(BackgroundJob::class, [
'id' => $announcement->getId(),
'activities' => $activities,
'notifications' => $notifications,
]);
}

return new JSONResponse($this->renderAnnouncement($announcement));
}

protected function renderAnnouncement(Announcement $announcement): array {
$displayName = $announcement->getUser();
$user = $this->userManager->get($announcement->getUser());
if ($user instanceof IUser) {
$displayName = $user->getDisplayName();
}

$result = [
'id' => $announcement->getId(),
'author_id' => $announcement->getUser(),
'author' => $displayName,
'time' => $announcement->getTime(),
'subject' => $announcement->getSubject(),
'message' => $announcement->getParsedMessage(),
'groups' => null,
'comments' => $announcement->getAllowComments() ? $this->manager->getNumberOfComments($announcement) : false,
];

if ($this->manager->checkIsAdmin()) {
$result['groups'] = $this->manager->getGroups($announcement);
$result['notifications'] = $this->manager->hasNotifications($announcement);
}

return $result;
}

/**
* @NoAdminRequired
*
* @param int $id
* @return Response
*/
public function delete($id): Response {
if (!$this->manager->checkIsAdmin()) {
return new JSONResponse(
['message' => 'Logged in user must be an admin'],
Http::STATUS_FORBIDDEN
);
}

$this->manager->delete($id);

return new Response();
}

/**
* @NoAdminRequired
*
* @param int $id
* @return Response
*/
public function removeNotifications($id): Response {
if (!$this->manager->checkIsAdmin()) {
return new JSONResponse(
['message' => 'Logged in user must be an admin'],
Http::STATUS_FORBIDDEN
);
}

$this->manager->removeNotifications($id);

return new Response();
}

/**
* @NoAdminRequired
* @NoCSRFRequired
Expand All @@ -235,49 +70,22 @@ public function index($announcement = 0): TemplateResponse {
}

$this->initialState->provideInitialState(
Application::APP_ID,
'isAdmin',
$this->manager->checkIsAdmin()
);
$this->initialState->provideInitialState(
Application::APP_ID,
'createActivities',
$this->config->getAppValue('announcementcenter', 'create_activities', 'yes') === 'yes'
$this->config->getAppValue(Application::APP_ID, 'create_activities', 'yes') === 'yes'
);
$this->initialState->provideInitialState(
Application::APP_ID,
'createNotifications',
$this->config->getAppValue('announcementcenter', 'create_notifications', 'yes') === 'yes'
$this->config->getAppValue(Application::APP_ID, 'create_notifications', 'yes') === 'yes'
);
$this->initialState->provideInitialState(
Application::APP_ID,
'allowComments',
$this->config->getAppValue('announcementcenter', 'allow_comments', 'yes') === 'yes'
$this->config->getAppValue(Application::APP_ID, 'allow_comments', 'yes') === 'yes'
);

return new TemplateResponse('announcementcenter', 'main');
}

/**
* @NoAdminRequired
*
* @param string $pattern
* @return JSONResponse
*/
public function searchGroups($pattern): JSONResponse {
if (!$this->manager->checkIsAdmin()) {
return new JSONResponse(
['message' => 'Logged in user must be an admin'],
Http::STATUS_FORBIDDEN
);
}

$groups = $this->groupManager->search($pattern, 10);
$gids = [];
foreach ($groups as $group) {
$gids[] = $group->getGID();
}

return new JSONResponse($gids);
return new TemplateResponse(Application::APP_ID, 'main');
}
}
Loading

0 comments on commit cca0a12

Please sign in to comment.