Nextcloud remove deprecated jQuery code and get things working properly #96

This commit is contained in:
the-djmaze 2022-10-11 10:18:43 +02:00
parent 4583616ea6
commit 8b842d920d
11 changed files with 169 additions and 180 deletions

0
integrations/nextcloud/snappymail/css/style.css Executable file → Normal file
View file

12
integrations/nextcloud/snappymail/js/admin.js Executable file → Normal file
View file

@ -1,12 +1,2 @@
/**
* Nextcloud - SnappyMail mail plugin
*
* @author SnappyMail Team, Nextgen-Networks (@nextgen-networks), Tab Fitts (@tabp0le), Pierre-Alain Bandinelli (@pierre-alain-b)
*
* Based initially on https://github.com/SnappyMail/snappymail-webmail/tree/master/build/owncloud
*/
$(function() {
SnappyMailFormHelper('#mail-snappymail-admin-form', 'admin.php');
});
SnappyMailFormHelper('admin-form', 'admin.php');

12
integrations/nextcloud/snappymail/js/personal.js Executable file → Normal file
View file

@ -1,12 +1,2 @@
/**
* Nextcloud - SnappyMail mail plugin
*
* @author SnappyMail Team, Nextgen-Networks (@nextgen-networks), Tab Fitts (@tabp0le), Pierre-Alain Bandinelli (@pierre-alain-b)
*
* Based initially on https://github.com/SnappyMail/snappymail-webmail/tree/master/build/owncloud
*/
$(function() {
SnappyMailFormHelper('#mail-snappymail-personal-form', 'personal.php');
});
SnappyMailFormHelper('personal-form', 'personal.php');

View file

@ -1,114 +0,0 @@
// Do the following things once the document is fully loaded.
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
watchIFrameTitle();
}
}
// The SnappyMail application is already configured to modify the <title> element
// of its root document with the number of unread messages in the inbox.
// However, its document is the SnappyMail iframe. This function sets up a
// Mutation Observer to watch the <title> element of the iframe for changes in
// the unread message count and propagates that to the parent <title> element,
// allowing the unread message count to be displayed in the NC tab's text when
// the SnappyMail app is selected.
function watchIFrameTitle() {
iframe = document.getElementById('rliframe');
if (!iframe) {
return;
}
target = iframe.contentDocument.getElementsByTagName('title')[0];
config = {
characterData: true,
childList: true,
subtree: true
}
observer = new MutationObserver(function(mutations) {
title = mutations[0].target.innerText;
if (title) {
matches = title.match(/\(([0-9]+)\)/);
if (matches) {
document.title = '('+ matches[1] + ') ' + t('snappymail', 'Email') + ' - Nextcloud';
} else {
document.title = t('snappymail', 'Email') + ' - Nextcloud';
}
}
});
observer.observe(target, config);
}
function SnappyMailFormHelper(sID, sAjaxFile, fCallback)
{
try
{
var
oForm = $(sID),
oSubmit = $('#snappymail-save-button', oForm),
sSubmitValue = oSubmit.val(),
oDesc = oForm.find('.snappymail-result-desc')
;
oSubmit.click(function (oEvent) {
var oDefAjax = null;
oEvent.preventDefault();
oForm
.addClass('snappymail-ajax')
.removeClass('snappymail-error')
.removeClass('snappymail-success')
;
oDesc.text('');
oSubmit.val('...');
oDefAjax = $.ajax({
'type': 'POST',
'async': true,
'url': OC.filePath('snappymail', 'ajax', sAjaxFile),
'data': oForm.serialize(),
'dataType': 'json',
'global': true
});
oDefAjax.always(function (oData) {
var bResult = false;
oForm.removeClass('snappymail-ajax');
oSubmit.val(sSubmitValue);
if (oData)
{
bResult = 'success' === oData['status'];
if (oData['Message'])
{
oDesc.text(t('snappymail', oData['Message']));
}
}
if (bResult)
{
oForm.addClass('snappymail-success');
}
else
{
oForm.addClass('snappymail-error');
if ('' === oDesc.text())
{
oDesc.text(t('snappymail', 'Error'));
}
}
if (fCallback)
{
fCallback(bResult, oData);
}
});
return false;
});
}
catch(e) {}
}

32
integrations/nextcloud/snappymail/js/resize.js Executable file → Normal file
View file

@ -1,26 +1,18 @@
$(function (window, document) {
(()=>{
var
const
buffer = 0, //was 5 but this was creating a white space of 5px at the bottom of the page
ifr = document.getElementById('rliframe')
;
function pageY(elem) {
return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
}
function resizeIframe() {
var height = document.documentElement.clientHeight;
height -= pageY(ifr) + buffer;
height = (height < 0) ? 0 : height;
ifr.style.height = height + 'px';
}
if (ifr)
{
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;
}
}(window, document));
})();

View file

@ -0,0 +1,122 @@
/**
* Nextcloud - SnappyMail mail plugin
*
* @author RainLoop Team, Nextgen-Networks (@nextgen-networks), Tab Fitts (@tabp0le), Pierre-Alain Bandinelli (@pierre-alain-b), SnappyMail
*
* Based initially on https://github.com/RainLoop/rainloop-webmail/tree/master/build/owncloud/rainloop-app
*/
// Do the following things once the document is fully loaded.
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
watchIFrameTitle();
}
}
// The SnappyMail application is already configured to modify the <title> element
// of its root document with the number of unread messages in the inbox.
// However, its document is the SnappyMail iframe. This function sets up a
// Mutation Observer to watch the <title> element of the iframe for changes in
// the unread message count and propagates that to the parent <title> element,
// allowing the unread message count to be displayed in the NC tab's text when
// the SnappyMail app is selected.
function watchIFrameTitle() {
let iframe = document.getElementById('rliframe');
if (!iframe) {
return;
}
let target = iframe.contentDocument.getElementsByTagName('title')[0];
let config = {
characterData: true,
childList: true,
subtree: true
};
let observer = new MutationObserver(function(mutations) {
let title = mutations[0].target.innerText;
if (title) {
let matches = title.match(/\(([0-9]+)\)/);
if (matches) {
document.title = '('+ matches[1] + ') ' + t('snappymail', 'Email') + ' - Nextcloud';
} else {
document.title = t('snappymail', 'Email') + ' - Nextcloud';
}
}
});
observer.observe(target, config);
}
function SnappyMailFormHelper(sID, sAjaxFile, fCallback)
{
try
{
var
oForm = document.getElementById('mail-snappymail-' + sID),
oSubmit = document.getElementById('snappymail-save-button'),
sSubmitValue = oSubmit.textContent,
oDesc = oForm.querySelector('.snappymail-result-desc')
;
oForm.addEventListener('submit', (...args) => {
console.dir({args:args});
return false
});
oSubmit.onclick = oEvent => {
oEvent.preventDefault();
oForm.classList.add('snappymail-ajax')
oForm.classList.remove('snappymail-error')
oForm.classList.remove('snappymail-success')
oDesc.textContent = '';
oSubmit.textContent = '...';
fetch(OC.filePath('snappymail', 'ajax', sAjaxFile), {
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin',
method: 'POST',
/*
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
'snappymail-email': document.getElementById('snappymail-email').value,
'snappymail-password': document.getElementById('snappymail-password').value
})
*/
headers: {},
body: new FormData(oForm)
})
.then(response => response.json())
.then(oData => {
let bResult = false;
oForm.classList.remove('snappymail-ajax');
oSubmit.textContent = sSubmitValue;
if (oData) {
bResult = 'success' === oData.status;
if (oData.Message) {
oDesc.textContent = t('snappymail', oData.Message);
}
}
if (bResult) {
oForm.classList.add('snappymail-success');
} else {
oForm.classList.add('snappymail-error');
if ('' === oDesc.textContent) {
oDesc.textContent = t('snappymail', 'Error');
}
}
if (fCallback) {
fCallback(bResult, oData);
}
});
return false;
};
}
catch(e) {
console.error(e);
}
}

View file

@ -53,9 +53,9 @@ class AjaxController extends Controller {
}
}
/**
* @NoAdminRequired
*/
/**
* @NoAdminRequired
*/
public function setPersonal(): JSONResponse {
try {

View file

@ -20,10 +20,30 @@ class PageController extends Controller
\OCP\Util::addStyle('snappymail', 'style');
$session = \OC::$server->getSession();
$query = '';
// If the user has set credentials for SnappyMail in their personal
// settings, override everything before and use those instead.
$config = \OC::$server->getConfig();
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
$sEmail = $config->getUserValue($sUID, 'snappymail', 'snappymail-email', '');
if ($sEmail) {
$password = SnappyMailHelper::decodePassword(
$config->getUserValue($sUID, 'snappymail', 'snappymail-password', ''),
\md5($sEmail)
);
if ($password) {
$query = '?sso&hash=' . \RainLoop\Api::CreateUserSsoHash($sEmail, $password);
}
}
if (!$query) {
$session = \OC::$server->getSession();
if (!empty($session['snappymail-sso-hash'])) {
$query = '?sso&hash=' . $session['snappymail-sso-hash'];
}
}
$params = [
'snappymail-iframe-url' => SnappyMailHelper::normalizeUrl(SnappyMailHelper::getAppUrl())
. (empty($session['snappymail-sso-hash']) ? '' : '?sso&hash=' . $session['snappymail-sso-hash'])
'snappymail-iframe-url' => SnappyMailHelper::normalizeUrl(SnappyMailHelper::getAppUrl()) . $query
];
$response = new TemplateResponse('snappymail', 'index', $params);

View file

@ -7,36 +7,25 @@ class SnappyMailHelper
public function registerHooks()
{
$config = \OC::$server->getConfig();
$session = \OC::$server->getSession();
$userSession = \OC::$server->getUserSession();
$userSession->listen('\OC\User', 'postLogin', function($user, $loginName, $password, $isTokenLogin) {
$config = \OC::$server->getConfig();
$sEmail = '';
// If the user has set credentials for SnappyMail in their personal
// settings, override everything before and use those instead.
$sIndividualEmail = $config->getUserValue($user->getUID(), 'snappymail', 'snappymail-email', '');
if ($sIndividualEmail) {
$sEmail = $sIndividualEmail;
$password = SnappyMailHelper::decodePassword(
$this->config->getUserValue($sUser, 'snappymail', 'snappymail-password', ''),
\md5($sEmail)
);
}
// Only store the user's password in the current session if they have
// enabled auto-login using Nextcloud username or email address.
else if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
$sEmail = $user->getUID();
} else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sEmail = $config->getUserValue($user->getUID(), 'settings', 'email', '');
}
if ($sEmail) {
static::startApp(true);
$session['snappymail-sso-hash'] = \RainLoop\Api::CreateUserSsoHash($sEmail, $password/*, array $aAdditionalOptions = array(), bool $bUseTimeout = true*/);
\OC::$server->getSession()['snappymail-sso-hash'] = \RainLoop\Api::CreateUserSsoHash($sEmail, $password/*, array $aAdditionalOptions = array(), bool $bUseTimeout = true*/);
}
});
$userSession->listen('\OC\User', 'logout', function($user) {
$session['snappymail-sso-hash'] = '';
\OC::$server->getSession()['snappymail-sso-hash'] = '';
static::startApp(true);
\RainLoop\Api::LogoutCurrentLogginedUser();
});
@ -51,7 +40,7 @@ class SnappyMailHelper
$_SERVER['SCRIPT_NAME'] = \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/index.php';
}
$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true;
$sData = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/snappymail/';
$sData = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/appdata_snappymail/';
if (\is_dir($sData)) {
\define('APP_DATA_FOLDER_PATH', $sData);
}

View file

@ -37,7 +37,7 @@
</div>
<br />
<br />
<input type="button" id="snappymail-save-button" name="snappymail-save-button" value="<?php echo($l->t('Save')); ?>" />
<button id="snappymail-save-button" name="snappymail-save-button"><?php echo($l->t('Save')); ?></button>
&nbsp;&nbsp;<span class="snappymail-result-desc"></span>
</p>
</fieldset>

View file

@ -24,7 +24,7 @@ $links = [
<input type="password" id="snappymail-password" name="snappymail-password"
value="<?php echo $_['snappymail-password']; ?>" placeholder="<?php echo($l->t('Password')); ?>" />
<input type="button" id="snappymail-save-button" name="snappymail-save-button" value="<?php echo($l->t('Save')); ?>" />
<button id="snappymail-save-button" name="snappymail-save-button"><?php echo($l->t('Save')); ?></button>
&nbsp;&nbsp;<span class="snappymail-result-desc"></span>
</p>
</fieldset>