Cleanup Nextcloud code and added mockup for Unified search

https://github.com/the-djmaze/snappymail/issues/96#issuecomment-1277586143
This commit is contained in:
the-djmaze 2022-10-13 16:34:24 +02:00
parent 61770dd5c7
commit d78cf50211
11 changed files with 148 additions and 111 deletions

View file

@ -1,2 +0,0 @@
SnappyMailFormHelper('admin-form', 'admin.php');

View file

@ -1,2 +0,0 @@
SnappyMailFormHelper('personal-form', 'personal.php');

View file

@ -1,18 +0,0 @@
document.addEventListener('readystatechange', () => {
if (document.readyState === 'complete') {
const
buffer = 0, //was 5 but this was creating a white space of 5px at the bottom of the page
ifr = document.getElementById('rliframe'),
pageY = elem => elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop,
resizeIframe = () => {
var height = document.documentElement.clientHeight;
height -= pageY(ifr) + buffer;
height = (height < 0) ? 0 : height;
ifr.style.height = height + 'px';
};
if (ifr) {
ifr.onload = resizeIframe;
window.onresize = resizeIframe;
}
}
});

View file

@ -8,7 +8,11 @@
// Do the following things once the document is fully loaded. // Do the following things once the document is fully loaded.
document.onreadystatechange = () => { document.onreadystatechange = () => {
(document.readyState === 'complete') && watchIFrameTitle(); if (document.readyState === 'complete') {
watchIFrameTitle();
let form = document.querySelector('form.snappymail');
form && SnappyMailFormHelper(form);
}
}; };
// The SnappyMail application is already configured to modify the <title> element // The SnappyMail application is already configured to modify the <title> element
@ -43,12 +47,11 @@ function watchIFrameTitle() {
observer.observe(target, config); observer.observe(target, config);
} }
function SnappyMailFormHelper(sID, sFetchFile, fCallback) function SnappyMailFormHelper(oForm)
{ {
try try
{ {
var var
oForm = document.getElementById('mail-snappymail-' + sID),
oSubmit = document.getElementById('snappymail-save-button'), oSubmit = document.getElementById('snappymail-save-button'),
sSubmitValue = oSubmit.textContent, sSubmitValue = oSubmit.textContent,
oDesc = oForm.querySelector('.snappymail-result-desc') oDesc = oForm.querySelector('.snappymail-result-desc')
@ -64,7 +67,10 @@ function SnappyMailFormHelper(sID, sFetchFile, fCallback)
oDesc.textContent = ''; oDesc.textContent = '';
oSubmit.textContent = '...'; oSubmit.textContent = '...';
fetch(OC.filePath('snappymail', 'fetch', sFetchFile), { let data = new FormData(oForm);
data.set('appname', 'snappymail');
fetch(OC.filePath('snappymail', 'fetch', oForm.getAttribute('action')), {
mode: 'same-origin', mode: 'same-origin',
cache: 'no-cache', cache: 'no-cache',
redirect: 'error', redirect: 'error',
@ -72,7 +78,7 @@ function SnappyMailFormHelper(sID, sFetchFile, fCallback)
credentials: 'same-origin', credentials: 'same-origin',
method: 'POST', method: 'POST',
headers: {}, headers: {},
body: new FormData(oForm) body: data
}) })
.then(response => response.json()) .then(response => response.json())
.then(oData => { .then(oData => {
@ -90,7 +96,6 @@ function SnappyMailFormHelper(sID, sFetchFile, fCallback)
oDesc.textContent = t('snappymail', 'Error'); oDesc.textContent = t('snappymail', 'Error');
} }
} }
fCallback?.(bResult, oData);
}); });
return false; return false;

View file

@ -5,6 +5,7 @@ namespace OCA\SnappyMail\AppInfo;
use OCA\SnappyMail\Util\SnappyMailHelper; use OCA\SnappyMail\Util\SnappyMailHelper;
use OCA\SnappyMail\Controller\FetchController; use OCA\SnappyMail\Controller\FetchController;
use OCA\SnappyMail\Controller\PageController; use OCA\SnappyMail\Controller\PageController;
use OCA\SnappyMail\Search\Provider;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IBootstrap;
@ -15,14 +16,64 @@ use OCP\IUser;
class Application extends App implements IBootstrap class Application extends App implements IBootstrap
{ {
public const APP_ID = 'snappymail';
public function __construct(array $urlParams = [])
{
parent::__construct(self::APP_ID, $urlParams);
}
public function register(IRegistrationContext $context): void public function register(IRegistrationContext $context): void
{ {
/**
* Controllers
*/
$context->registerService(
'PageController', function($c) {
return new PageController(
$c->query('AppName'),
$c->query('Request')
);
}
);
$context->registerService(
'FetchController', function($c) {
return new FetchController(
$c->query('AppName'),
$c->query('Request'),
$c->getServer()->getAppManager(),
$c->query('ServerContainer')->getConfig(),
$c->query(IL10N::class)
);
}
);
/**
* Utils
*/
$context->registerService(
'SnappyMailHelper', function($c) {
return new SnappyMailHelper();
}
);
// $context->registerSearchProvider(Provider::class);
} }
public function boot(IBootContext $context): void public function boot(IBootContext $context): void
{ {
$this->registerNavigation(); $container = $this->getContainer();
$container->query('OCP\INavigationManager')->add(function () use ($container) {
$urlGenerator = $container->query('OCP\IURLGenerator');
return [
'id' => 'snappymail',
'order' => 10,
'href' => $urlGenerator->linkToRoute('snappymail.page.index'),
'icon' => $urlGenerator->imagePath('snappymail', 'logo-64x64.png'),
'name' => \OCP\Util::getL10N('snappymail')->t('Email')
];
});
$userSession = \OC::$server->getUserSession(); $userSession = \OC::$server->getUserSession();
$userSession->listen('\OC\User', 'postLogin', function($user, $loginName, $password, $isTokenLogin) { $userSession->listen('\OC\User', 'postLogin', function($user, $loginName, $password, $isTokenLogin) {
@ -47,65 +98,4 @@ class Application extends App implements IBootstrap
\RainLoop\Api::LogoutCurrentLogginedUser(); \RainLoop\Api::LogoutCurrentLogginedUser();
}); });
} }
// public function __construct(string $appName, array $urlParams = [])
public function __construct()
{
parent::__construct('snappymail');
$container = $this->getContainer();
/**
* Controllers
*/
$container->registerService(
'PageController', function($c) {
return new PageController(
$c->query('AppName'),
$c->query('Request')
);
}
);
$container->registerService(
'FetchController', function($c) {
return new FetchController(
$c->query('AppName'),
$c->query('Request'),
$c->getServer()->getAppManager(),
$c->query('ServerContainer')->getConfig(),
$c->query(IL10N::class)
);
}
);
/**
* Utils
*/
$container->registerService(
'SnappyMailHelper', function($c) {
return new SnappyMailHelper();
}
);
// Add script js/snappymail.js
\OCP\Util::addScript('snappymail', 'snappymail');
}
public function registerNavigation()
{
$container = $this->getContainer();
$container->query('OCP\INavigationManager')->add(function () use ($container) {
$urlGenerator = $container->query('OCP\IURLGenerator');
return [
'id' => 'snappymail',
'order' => 10,
'href' => $urlGenerator->linkToRoute('snappymail.page.index'),
'icon' => $urlGenerator->imagePath('snappymail', 'logo-64x64.png'),
'name' => \OCP\Util::getL10N('snappymail')->t('Email')
];
});
}
} }

View file

@ -24,6 +24,7 @@ class PageController extends Controller
\OC::$server->getNavigationManager()->setActiveEntry('snappymail'); \OC::$server->getNavigationManager()->setActiveEntry('snappymail');
\OCP\Util::addScript('snappymail', 'snappymail');
\OCP\Util::addStyle('snappymail', 'style'); \OCP\Util::addStyle('snappymail', 'style');
$query = ''; $query = '';

View file

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace OCA\SnappyMail\Search;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
/**
* https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/search.html#search-providers
*/
class Provider implements IProvider
{
public function getId(): string
{
return Application::APP_ID;
}
public function getName(): string
{
return $this->l10n->t('Mails');
}
public function getOrder(string $route, array $routeParameters): int
{
if (0 === \strpos($route, Application::APP_ID . '.')) {
// Active app, prefer Mail results
return -1;
}
return 20;
}
public function search(IUser $user, ISearchQuery $query): SearchResult
{
/**
* TODO
* And use \MailSo\Imap\SearchCriterias
*/
return SearchResult::complete(
$this->getName(),
[
/*
new SearchResultEntry(
// thumbnailUrl
'',
// title
\MailSo\Mail\Message->sSubject,
// subline
\MailSo\Mail\Message->oFrom->ToString(),
// resourceUrl
$this->urlGenerator->linkToRouteAbsolute(),
// icon
'icon-mail',
// rounded
false
),
*/
]
);
}
}

View file

@ -6,41 +6,42 @@ use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig; use OCP\IConfig;
use OCP\Settings\ISettings; use OCP\Settings\ISettings;
class AdminSettings implements ISettings { class AdminSettings implements ISettings
{
private $config; private $config;
public function __construct(IConfig $config) { public function __construct(IConfig $config)
{
$this->config = $config; $this->config = $config;
} }
public function getForm() { public function getForm()
{
$keys = [ $keys = [
'snappymail-autologin', 'snappymail-autologin',
'snappymail-autologin-with-email', 'snappymail-autologin-with-email',
'snappymail-embed' 'snappymail-embed'
]; ];
$parameters = []; $parameters = [];
foreach ($keys as $k) { foreach ($keys as $k) {
$v = $this->config->getAppValue('snappymail', $k); $v = $this->config->getAppValue('snappymail', $k);
$parameters[$k] = $v; $parameters[$k] = $v;
} }
$uid = \OC::$server->getUserSession()->getUser()->getUID(); $uid = \OC::$server->getUserSession()->getUser()->getUID();
if (\OC_User::isAdminUser($uid)) { if (\OC_User::isAdminUser($uid)) {
$parameters['snappymail-admin-panel-link'] = SnappyMailHelper::getAppUrl().'?admin'; $parameters['snappymail-admin-panel-link'] = SnappyMailHelper::getAppUrl().'?admin';
} }
\OCP\Util::addScript('snappymail', 'snappymail');
return new TemplateResponse('snappymail', 'admin-local', $parameters); return new TemplateResponse('snappymail', 'admin-local', $parameters);
} }
public function getSection() { public function getSection()
{
return 'additional'; return 'additional';
} }
public function getPriority() { public function getPriority()
{
return 50; return 50;
} }
} }

View file

@ -21,6 +21,7 @@ class PersonalSettings implements ISettings
'snappymail-email' => $this->config->getUserValue($uid, 'snappymail', 'snappymail-email'), 'snappymail-email' => $this->config->getUserValue($uid, 'snappymail', 'snappymail-email'),
'snappymail-password' => $this->config->getUserValue($uid, 'snappymail', 'snappymail-password') ? '******' : '' 'snappymail-password' => $this->config->getUserValue($uid, 'snappymail', 'snappymail-password') ? '******' : ''
]; ];
\OCP\Util::addScript('snappymail', 'snappymail');
return new TemplateResponse('snappymail', 'personal_settings', $parameters, ''); return new TemplateResponse('snappymail', 'personal_settings', $parameters, '');
} }
@ -33,5 +34,4 @@ class PersonalSettings implements ISettings
{ {
return 50; return 50;
} }
} }

View file

@ -1,10 +1,6 @@
<?php script('snappymail', 'admin') ?>
<div class="section"> <div class="section">
<form id="mail-snappymail-admin-form" action="#" method="post"> <form class="snappymail" action="admin.php" method="post">
<input type="hidden" name="requesttoken" value="<?php echo $_['requesttoken'] ?>" id="requesttoken"> <input type="hidden" name="requesttoken" value="<?php echo $_['requesttoken'] ?>" id="requesttoken">
<input type="hidden" name="appname" value="snappymail">
<fieldset class="personalblock"> <fieldset class="personalblock">
<h2><?php echo($l->t('SnappyMail Webmail')); ?></h2> <h2><?php echo($l->t('SnappyMail Webmail')); ?></h2>
<br /> <br />

View file

@ -1,5 +1,4 @@
<?php script('snappymail', 'personal'); <?php
$textLink1 = $l->t('may have some'); $textLink1 = $l->t('may have some');
$textLink2 = $l->t('security considerations'); $textLink2 = $l->t('security considerations');
$links = [ $links = [
@ -8,10 +7,8 @@ $links = [
]; ?> ]; ?>
<div class="section"> <div class="section">
<form id="mail-snappymail-personal-form" action="#" method="post"> <form class="snappymail" action="personal.php" method="post">
<input type="hidden" name="requesttoken" value="<?php echo $_['requesttoken'] ?>" id="requesttoken"> <input type="hidden" name="requesttoken" value="<?php echo $_['requesttoken'] ?>" id="requesttoken">
<input type="hidden" name="appname" value="snappymail">
<fieldset class="personalblock"> <fieldset class="personalblock">
<h2><?php echo($l->t('SnappyMail Webmail')); ?></h2> <h2><?php echo($l->t('SnappyMail Webmail')); ?></h2>
<p> <p>