Skip to content

Commit

Permalink
94: Hook for user deletion and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Janis Koehr committed Jul 23, 2017
1 parent 790a9f4 commit 0a6d1ed
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 35 deletions.
3 changes: 2 additions & 1 deletion appinfo/app.php
Expand Up @@ -11,4 +11,5 @@
namespace OCA\Ocr\AppInfo;

$app = new \OCA\Ocr\AppInfo\Application();
$app->registerPersonal();
$app->registerPersonal();
$app->registerHooks();
4 changes: 2 additions & 2 deletions js/dist/ocradmin.js
Expand Up @@ -192,10 +192,10 @@ return /******/ (function(modules) { // webpackBootstrap
};
Controller.prototype.displayMessage = function (message, error) {
if (error) {
this.notification.showHtml("<div>" + message + "</div>", { timeout: 10, type: 'error' });
this.notification.showHtml("<div>" + t('ocr', 'OCR') + ": " + message + "</div>", { timeout: 10, type: 'error' });
}
else {
this.notification.showHtml("<div>" + message + "</div>", { timeout: 5 });
this.notification.showHtml("<div>" + t('ocr', 'OCR') + ": " + message + "</div>", { timeout: 5 });
}
};
Controller.prototype.getLanguages = function () {
Expand Down
2 changes: 1 addition & 1 deletion js/dist/ocrapp.js
Expand Up @@ -509,7 +509,7 @@ return /******/ (function(modules) { // webpackBootstrap
this._notificationRow = undefined;
}
View.prototype.displayError = function (message) {
this.notification.showHtml("<div>" + message + "</div>", { timeout: 10, type: 'error' });
this.notification.showHtml("<div>" + t('ocr', 'OCR') + ": " + message + "</div>", { timeout: 10, type: 'error' });
};
View.prototype.renderDropdown = function (languages) {
this.destroyDropdown();
Expand Down
4 changes: 2 additions & 2 deletions js/dist/ocrpersonal.js
Expand Up @@ -132,10 +132,10 @@ return /******/ (function(modules) { // webpackBootstrap
}
View.prototype.displayMessage = function (message, error) {
if (error) {
this.notification.showHtml("<div>" + message + "</div>", { timeout: 10, type: 'error' });
this.notification.showHtml("<div>" + t('ocr', 'OCR') + ": " + message + "</div>", { timeout: 10, type: 'error' });
}
else {
this.notification.showHtml("<div>" + message + "</div>", { timeout: 10 });
this.notification.showHtml("<div>" + t('ocr', 'OCR') + ": " + message + "</div>", { timeout: 10 });
}
};
View.prototype.render = function (jobs) {
Expand Down
4 changes: 2 additions & 2 deletions js/src/admin/controller/controller.spec.ts
Expand Up @@ -52,15 +52,15 @@ describe('For the controller', () => {

cut.displayMessage(message, true);

expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>${message}</div>`, { timeout: 10, type: 'error' });
expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>OCR: ${message}</div>`, { timeout: 10, type: 'error' });
});

it('should display a normal message.', () => {
const message = 'test';

cut.displayMessage(message, false);

expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>${message}</div>`, { timeout: 5 });
expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>OCR: ${message}</div>`, { timeout: 5 });
});
});

Expand Down
4 changes: 2 additions & 2 deletions js/src/admin/controller/controller.ts
Expand Up @@ -89,9 +89,9 @@ export class Controller {
*/
public displayMessage(message: string, error: boolean): void {
if (error) {
this.notification.showHtml(`<div>${message}</div>`, { timeout: 10, type: 'error' });
this.notification.showHtml(`<div>${this.t('ocr', 'OCR')}: ${message}</div>`, { timeout: 10, type: 'error' });
} else {
this.notification.showHtml(`<div>${message}</div>`, { timeout: 5 });
this.notification.showHtml(`<div>${this.t('ocr', 'OCR')}: ${message}</div>`, { timeout: 5 });
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/src/app/view/view.spec.ts
Expand Up @@ -24,7 +24,7 @@ describe('For the view', () => {

cut.displayError(message);

expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>${message}</div>`, { timeout: 10, type: 'error' });
expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>OCR: ${message}</div>`, { timeout: 10, type: 'error' });
});
});

Expand Down
2 changes: 1 addition & 1 deletion js/src/app/view/view.ts
Expand Up @@ -32,7 +32,7 @@ export class View {
* @param message The message to display.
*/
public displayError(message: string): void {
this.notification.showHtml(`<div>${message}</div>`, { timeout: 10, type: 'error' });
this.notification.showHtml(`<div>${this.t('ocr', 'OCR')}: ${message}</div>`, { timeout: 10, type: 'error' });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions js/src/personal/view/view.spec.ts
Expand Up @@ -25,15 +25,15 @@ describe('For the view', () => {

cut.displayMessage(message, false);

expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>${message}</div>`, { timeout: 10 });
expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>OCR: ${message}</div>`, { timeout: 10 });
});

it('should display an error message.', () => {
const message = 'test';

cut.displayMessage(message, true);

expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>${message}</div>`, { timeout: 10, type: 'error' });
expect(notificationMock.showHtml).toHaveBeenCalledWith(`<div>OCR: ${message}</div>`, { timeout: 10, type: 'error' });
});
});

Expand Down
4 changes: 2 additions & 2 deletions js/src/personal/view/view.ts
Expand Up @@ -25,9 +25,9 @@ export class View {
*/
public displayMessage(message: string, error: boolean): void {
if (error) {
this.notification.showHtml(`<div>${message}</div>`, { timeout: 10, type: 'error' });
this.notification.showHtml(`<div>${this.t('ocr', 'OCR')}: ${message}</div>`, { timeout: 10, type: 'error' });
} else {
this.notification.showHtml(`<div>${message}</div>`, { timeout: 10 });
this.notification.showHtml(`<div>${this.t('ocr', 'OCR')}: ${message}</div>`, { timeout: 10 });
}
}

Expand Down
34 changes: 28 additions & 6 deletions lib/AppInfo/Application.php
Expand Up @@ -29,6 +29,7 @@
use OCA\Ocr\Util\PHPUtil;
use OCA\Ocr\Util\FileUtil;
use OCA\Ocr\Util\RedisUtil;
use OCA\Ocr\Hooks\UserHooks;


/**
Expand Down Expand Up @@ -85,7 +86,7 @@ function (IContainer $c) {
/**
* Register the RedisUtil
*/
$container->registerService('RedisUtil',
$container->registerService('RedisUtil',
function (IContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
Expand Down Expand Up @@ -132,7 +133,8 @@ function (IContainer $c) {
function (IAppContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
return new AppConfigService($server->getConfig(), $server->getL10N('ocr'), $c->query('RedisUtil'), $server->getLogger());
return new AppConfigService($server->getConfig(), $server->getL10N('ocr'), $c->query('RedisUtil'),
$server->getLogger());
});
/**
* Register the Job Service
Expand Down Expand Up @@ -162,9 +164,9 @@ function (IAppContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
return new JobService($server->getL10N('ocr'), $server->getLogger(), $c->query('CurrentUID'),
new View('/' . $c->query('CurrentUID') . '/files'), $server->getTempManager(), $c->query('RedisService'),
$c->query('OcrJobMapper'), $c->query('FileService'), $c->query('AppConfigService'),
$c->query('PHPUtil'), $c->query('FileUtil'));
new View('/' . $c->query('CurrentUID') . '/files'), $server->getTempManager(),
$c->query('RedisService'), $c->query('OcrJobMapper'), $c->query('FileService'),
$c->query('AppConfigService'), $c->query('PHPUtil'), $c->query('FileUtil'));
});
/**
* Register the Status Service
Expand Down Expand Up @@ -198,6 +200,16 @@ function (IAppContainer $c) {
/**
* Controller
*/
$container->registerService('UserHooks',
function (IAppContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
return new UserHooks($c->query('ServerContainer')
->getUserManager(), $c->query('OcrJobMapper'), $server->getLogger());
});
/**
* Controller
*/
$container->registerAlias('PersonalSettingsController', PersonalSettingsController::class);
}

Expand All @@ -209,4 +221,14 @@ public function registerPersonal() {
\OCP\App::registerPersonal($this->getContainer()
->getAppName(), 'personal');
}
}

/**
* Registers the User Hooks.
* @codeCoverageIgnore
*/
public function registerHooks() {
$this->getContainer()
->query('UserHooks')
->register();
}
}
42 changes: 30 additions & 12 deletions lib/Db/OcrJobMapper.php
Expand Up @@ -3,7 +3,8 @@
/**
* Nextcloud - OCR
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
* later.
* See the COPYING file.
*
* @author Janis Koehr <janiskoehr@icloud.com>
* @copyright Janis Koehr 2017
Expand Down Expand Up @@ -68,10 +69,11 @@ public function findAll($userId) {
*/
public function findAllProcessed($userId) {
$sql = 'SELECT * FROM *PREFIX*ocr_jobs WHERE user_id = ? AND status = ?';
return $this->findEntities($sql, [
$userId,
OcrConstants::STATUS_PROCESSED
]);
return $this->findEntities($sql,
[
$userId,
OcrConstants::STATUS_PROCESSED
]);
}

/**
Expand All @@ -83,10 +85,11 @@ public function findAllProcessed($userId) {
*/
public function findAllPending($userId) {
$sql = 'SELECT * FROM *PREFIX*ocr_jobs WHERE user_id = ? AND status = ?';
return $this->findEntities($sql, [
$userId,
OcrConstants::STATUS_PENDING
]);
return $this->findEntities($sql,
[
$userId,
OcrConstants::STATUS_PENDING
]);
}

/**
Expand All @@ -98,10 +101,25 @@ public function findAllPending($userId) {
*/
public function findAllFailed($userId) {
$sql = 'SELECT * FROM *PREFIX*ocr_jobs WHERE user_id = ? AND status = ? AND error_displayed = ?';
return $this->findEntities($sql,
[
$userId,
OcrConstants::STATUS_FAILED,
false
]);
}

/**
* Deletes all jobs for a given userid.
* (For example after user deletion)
*
* @param unknown $userId
* @return OcrJob[]
*/
public function deleteAllForUser($userId) {
$sql = 'DELETE FROM *PREFIX*ocr_jobs WHERE user_id = ?';
return $this->findEntities($sql, [
$userId,
OcrConstants::STATUS_FAILED,
false
$userId
]);
}
}
62 changes: 62 additions & 0 deletions lib/Hooks/UserHooks.php
@@ -0,0 +1,62 @@
<?php

/**
* Nextcloud - OCR
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING file.
*
* @author Janis Koehr <janiskoehr@icloud.com>
* @copyright Janis Koehr 2017
*/
namespace OCA\Ocr\Hooks;

use OCP\IUserManager;
use OCA\Ocr\Db\OcrJobMapper;
use OCP\ILogger;


/**
* Class UserHooks
*
* @package OCA\Ocr\Hooks
*/
class UserHooks {

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

/**
*
* @var OcrJobMapper
*/
private $ocrJobMapper;

/**
*
* @var ILogger
*/
private $logger;

public function __construct(IUserManager $userManager, OcrJobMapper $ocrJobMapper, ILogger $logger) {
$this->userManager = $userManager;
$this->ocrJobMapper = $ocrJobMapper;
$this->logger = $logger;
}

/**
* Registers the user hooks.
*/
public function register() {
$postDelete = function ($user) {
$this->logger->debug('Deleting all jobs for user "{user}" after user deletion (Hook).', [
'user' => $user->getUID()
]);
$this->ocrJobMapper->deleteAllForUser($user->getUID());
};
$this->userManager->listen('\OC\User', 'postDelete', $postDelete);
}
}
4 changes: 4 additions & 0 deletions tests/Unit/AppInfo/ApplicationTest.php
Expand Up @@ -104,6 +104,10 @@ public function queryData() {
array(
'PersonalSettingsController',
'OCP\AppFramework\Controller'
),
array(
'UserHooks',
'OCA\Ocr\Hooks\UserHooks'
)
);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Db/OcrJobMapperTest.php
Expand Up @@ -127,4 +127,12 @@ public function testFindAll() {
$result = $this->cut->findAll($this->userId);
$this->assertEquals($this->jobs, $result);
}

public function testDeleteAllForUser() {
$rows = $this->twoRows;
$sql = 'DELETE FROM *PREFIX*ocr_jobs WHERE user_id = ?';
$this->setMapperResult($sql, [$this->userId], $rows);
$result = $this->cut->deleteAllForUser($this->userId);
$this->assertEquals($this->jobs, $result);
}
}

0 comments on commit 0a6d1ed

Please sign in to comment.