Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
namespace OCA\Mail\Controller;

use Exception;
use OCA\Mail\AppInfo\Application;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IUserPreferences;
use OCA\Mail\Service\AccountService;
Expand All @@ -36,12 +35,13 @@
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use function json_decode;

class PageController extends Controller {

Expand Down Expand Up @@ -69,7 +69,7 @@ class PageController extends Controller {
/** @var IMailManager */
private $mailManager;

/** @var IInitialStateService */
/** @var IInitialState */
private $initialStateService;

/** @var LoggerInterface */
Expand All @@ -85,7 +85,7 @@ public function __construct(string $appName,
IUserSession $userSession,
IUserPreferences $preferences,
IMailManager $mailManager,
IInitialStateService $initialStateService,
IInitialState $initialStateService,
LoggerInterface $logger) {
parent::__construct($appName, $request);

Expand All @@ -108,8 +108,12 @@ public function __construct(string $appName,
* @return TemplateResponse renders the index page
*/
public function index(): TemplateResponse {
$mailAccounts = $this->accountService->findByUserId($this->currentUserId);
$this->initialStateService->provideInitialState(
'debug',
$this->config->getSystemValue('debug', false)
);

$mailAccounts = $this->accountService->findByUserId($this->currentUserId);
$accountsJson = [];
foreach ($mailAccounts as $mailAccount) {
$json = $mailAccount->jsonSerialize();
Expand All @@ -127,28 +131,29 @@ public function index(): TemplateResponse {
}
$accountsJson[] = $json;
}

$accountSettings = $this->preferences->getPreference('account-settings', json_encode([]));
$this->initialStateService->provideInitialState(
'accounts',
$accountsJson
);
$this->initialStateService->provideInitialState(
'account-settings',
json_decode($this->preferences->getPreference('account-settings'), true, 512, JSON_THROW_ON_ERROR) ?? []
);

$user = $this->userSession->getUser();
$response = new TemplateResponse($this->appName, 'index',
[
'debug' => $this->config->getSystemValue('debug', false),
'attachment-size-limit' => $this->config->getSystemValue('app.mail.attachment-size-limit', 0),
'app-version' => $this->config->getAppValue('mail', 'installed_version'),
'accounts' => base64_encode(json_encode($accountsJson)),
'external-avatars' => $this->preferences->getPreference('external-avatars', 'true'),
'reply-mode' => $this->preferences->getPreference('reply-mode', 'top'),
'collect-data' => $this->preferences->getPreference('collect-data', 'true'),
'account-settings' => base64_encode($accountSettings),
]);
$this->initialStateService->provideInitialState(
Application::APP_ID,
'prefill_displayName',
$user->getDisplayName()
);
$this->initialStateService->provideInitialState(
Application::APP_ID,
'prefill_email',
$this->config->getUserValue($user->getUID(), 'settings', 'email', '')
);
Expand Down
9 changes: 5 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Nextcloud from './mixins/Nextcloud'
import router from './router'
import store from './store'
import { fixAccountId } from './service/AccountService'
import { Base64 } from 'js-base64'
import { loadState } from '@nextcloud/initial-state'

// eslint-disable-next-line camelcase
__webpack_nonce__ = btoa(getRequestToken())
Expand All @@ -58,7 +58,7 @@ const getPreferenceFromPage = (key) => {

store.commit('savePreference', {
key: 'debug',
value: getPreferenceFromPage('debug-mode'),
value: loadState('mail', 'debug', false),
})
store.commit('savePreference', {
key: 'attachment-size-limit',
Expand All @@ -77,8 +77,9 @@ store.commit('savePreference', {
value: getPreferenceFromPage('collect-data'),
})

const accountSettings = JSON.parse(Base64.decode(getPreferenceFromPage('account-settings')))
const accounts = JSON.parse(Base64.decode(getPreferenceFromPage('serialized-accounts')))
const accountSettings = loadState('mail', 'account-settings')
const accounts = loadState('mail', 'accounts', [])

accounts.map(fixAccountId).forEach((account) => {
const settings = accountSettings.find(settings => settings.accountId === account.id)
if (settings) {
Expand Down
27 changes: 11 additions & 16 deletions tests/Unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
Expand Down Expand Up @@ -75,7 +75,7 @@ class PageControllerTest extends TestCase {
/** @var MailManager|MockObject */
private $mailManager;

/** @var IInitialStateService|MockObject */
/** @var IInitialState|MockObject */
private $initialState;

/** @var PageController */
Expand All @@ -97,7 +97,7 @@ protected function setUp(): void {
$this->userSession = $this->createMock(IUserSession::class);
$this->preferences = $this->createMock(IUserPreferences::class);
$this->mailManager = $this->createMock(MailManager::class);
$this->initialState = $this->createMock(IInitialStateService::class);
$this->initialState = $this->createMock(IInitialState::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->controller = new PageController(
Expand Down Expand Up @@ -126,7 +126,7 @@ public function testIndex() {
['external-avatars', 'true', 'true'],
['reply-mode', 'top', 'bottom'],
['collect-data', 'true', 'true'],
['account-settings', json_encode([]), json_encode([])],
['account-settings', null, json_encode([])],
]);
$this->accountService->expects($this->once())
->method('findByUserId')
Expand All @@ -150,9 +150,6 @@ public function testIndex() {
->will($this->returnValue([
'accountId' => 1,
]));
$mailbox->expects($this->once())
->method('jsonSerialize')
->willReturn(['id' => 'inbox']);
$account1->expects($this->once())
->method('getId')
->will($this->returnValue(1));
Expand All @@ -178,9 +175,7 @@ public function testIndex() {
'a12',
],
'mailboxes' => [
[
'id' => 'inbox',
],
$mailbox,
],
],
[
Expand Down Expand Up @@ -218,23 +213,23 @@ public function testIndex() {
->with($this->equalTo('jane'), $this->equalTo('settings'),
$this->equalTo('email'), $this->equalTo(''))
->will($this->returnValue('jane@doe.cz'));
$this->initialState->expects($this->exactly(2))
$this->initialState->expects($this->exactly(5))
->method('provideInitialState')
->withConsecutive(
['mail', 'prefill_displayName', 'Jane Doe'],
['mail', 'prefill_email', 'jane@doe.cz']
['debug', true],
['accounts', $accountsJson],
['account-settings', []],
['prefill_displayName', 'Jane Doe'],
['prefill_email', 'jane@doe.cz']
);

$expected = new TemplateResponse($this->appName, 'index',
[
'debug' => true,
'attachment-size-limit' => 123,
'external-avatars' => 'true',
'reply-mode' => 'bottom',
'app-version' => '1.2.3',
'accounts' => base64_encode(json_encode($accountsJson)),
'collect-data' => 'true',
'account-settings' => base64_encode(json_encode([])),
]);
$csp = new ContentSecurityPolicy();
$csp->addAllowedFrameDomain('\'self\'');
Expand Down