Cleanup Nextcloud code

This commit is contained in:
the-djmaze 2022-10-21 16:10:07 +02:00
parent c7833d622a
commit 79387262e3
6 changed files with 92 additions and 146 deletions

View file

@ -96,7 +96,7 @@ class Application extends App implements IBootstrap
});
$userSession->listen('\OC\User', 'logout', function($user) {
$oSession = \OC::$server->getSession()['snappymail-password'] = '';
\OC::$server->getSession()['snappymail-password'] = '';
SnappyMailHelper::loadApp();
\RainLoop\Api::LogoutCurrentLogginedUser();
});

View file

@ -1 +0,0 @@
<!-- empty -->

View file

@ -1,3 +0,0 @@
<div style="margin: 20px">
<?php p($l->t('SnappyMail Webmail is not configured yet.')); ?>
</div>

View file

@ -1,8 +0,0 @@
<?php
use OCA\SnappyMail\Settings\PersonalSettings;
$app = new \OCA\SnappyMail\AppInfo\Application();
$controller = $app->getContainer()->query(PersonalSettings::class);
return $controller->getForm()->render();

View file

@ -49,18 +49,6 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
/*
\OC::$server->getCalendarManager();
\OC::$server->getLDAPProvider();
$oFiles = \OCP\Files::getStorage('files');
if ($oFiles and $oFiles->is_dir('/')) {
$dh = $oFiles->opendir('/');
if (\is_resource($dh)) {
while (($file = \readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
// DO THINGS
}
}
}
}
*/
public function NextcloudAttachFile() : array

View file

@ -1,36 +1,25 @@
(rl => {
const
namespace = 'DAV:',
const
namespace = 'DAV:',
propertyRequestBody = `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<d:getlastmodified />
<d:getetag />
<d:getcontenttype />
<d:resourcetype />
<oc:fileid />
<oc:permissions />
<oc:size />
<d:getcontentlength />
<nc:has-preview />
<oc:favorite />
<oc:comments-unread />
<oc:owner-display-name />
<oc:share-types />
</d:prop>
</d:propfind>`,
propfindBody = `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<d:resourcetype />
<oc:size />
<d:getcontentlength />
</d:prop>
</d:propfind>`,
xmlParser = new DOMParser(),
pathRegex = /.*\/remote.php\/dav\/files\/[^/]+/g,
xmlParser = new DOMParser(),
pathRegex = /.*\/remote.php\/dav\/files\/[^/]+/g,
getDavElementsByTagName = (parent, localName) => parent.getElementsByTagNameNS(namespace, localName),
getDavElementByTagName = (parent, localName) => getDavElementsByTagName(parent, localName)?.item(0),
getElementByTagName = (parent, localName) => +parent.getElementsByTagName(localName)?.item(0);
getDavElementsByTagName = (parent, localName) => parent.getElementsByTagNameNS(namespace, localName),
getDavElementByTagName = (parent, localName) => getDavElementsByTagName(parent, localName)?.item(0),
getElementByTagName = (parent, localName) => +parent.getElementsByTagName(localName)?.item(0),
function davFetch(path, options)
{
davFetch = (path, options) => {
if (!parent.OC.requestToken) {
return Promise.reject(new Error('OC.requestToken missing'));
}
@ -44,17 +33,11 @@
}, options);
options.headers.requesttoken = parent.OC.requestToken;
return fetch(cfg.WebDAV + '/files/' + cfg.UID + path, options);
}
},
function createDirectory(path)
{
return davFetch(path, {
method: 'MKCOL'
});
}
createDirectory = path => davFetch(path, { method: 'MKCOL' }),
function fetchFiles(xml, path)
{
fetchFiles = path => {
if (!parent.OC.requestToken) {
return Promise.reject(new Error('OC.requestToken missing'));
}
@ -63,7 +46,7 @@
headers: {
'Content-Type': 'application/xml; charset=utf-8'
},
body: xml
body: propfindBody
})
.then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response })))
.then(text => {
@ -78,11 +61,9 @@
const
e = responseList.item(i),
elem = {
id: parseInt(getElementByTagName(e, 'oc:fileid')?.innerHTML ?? 0),
name: decodeURIComponent(getDavElementByTagName(e, 'href').innerHTML)
.replace(pathRegex, '').replace(/\/$/, ''),
isFile: false,
lastmod: getDavElementByTagName(e, 'getlastmodified').innerHTML
isFile: false
}
if (getDavElementsByTagName(getDavElementByTagName(e, 'resourcetype'), 'collection').length) {
// skip current directory
@ -91,22 +72,16 @@
}
} else {
elem.isFile = true;
elem.mime = getDavElementByTagName(e, 'getcontenttype').innerHTML;
elem.etag = getDavElementByTagName(e, 'getetag').innerHTML;
elem.size = getDavElementByTagName(e, 'getcontentlength')?.innerHTML;
if (!elem.size) {
elem.size = getElementByTagName(e, 'oc:size')?.innerHTML;
}
elem.haspreview = getElementByTagName(e, 'nc:has-preview')?.innerHTML === 'true';
elem.size = getDavElementByTagName(e, 'getcontentlength')?.innerHTML
|| getElementByTagName(e, 'oc:size')?.innerHTML;
}
elemList.push(elem);
}
return Promise.resolve(elemList);
});
}
},
function buildTree(view, parent, items, path)
{
buildTree = (view, parent, items, path) => {
if (items.length) {
items.forEach(item => {
if (!item.isFile) {
@ -116,7 +91,7 @@
ul = document.createElement('ul');
details.addEventListener('toggle', () => {
ul.children.length
|| fetchFiles(propertyRequestBody, item.name).then(items => buildTree(view, ul, items, item.name));
|| fetchFiles(item.name).then(items => buildTree(view, ul, items, item.name));
});
summary.textContent = item.name.replace(/^.*\/([^/]+)$/, '$1');
summary.dataset.icon = '📁';
@ -168,86 +143,81 @@
li.append(btn);
parent.append(li);
}
};
class NextcloudFilesPopupView extends rl.pluginPopupView {
constructor() {
super('NextcloudFiles');
this.addObservables({
files: false
});
}
class NextcloudFilesPopupView extends rl.pluginPopupView {
constructor() {
super('NextcloudFiles');
this.addObservables({
files: false
});
}
onBuild(dom) {
this.tree = dom.querySelector('#sm-nc-files-tree');
this.tree.addEventListener('click', event => {
let el = event.target;
if (el.matches('button')) {
if ('select' == el.name) {
this.select = this.files() ? [el.item] : el.item_name;
this.close();
} else if ('create' == el.name) {
let name = el.input.value.replace(/[|\\?*<":>+[]\/&\s]/g, '');
if (name.length) {
name = el.item_name + '/' + name;
createDirectory(name).then(response => {
if (response.status == 201) {
this.select = name;
this.close();
}
});
}
onBuild(dom) {
this.tree = dom.querySelector('#sm-nc-files-tree');
this.tree.addEventListener('click', event => {
let el = event.target;
if (el.matches('button')) {
if ('select' == el.name) {
this.select = this.files() ? [el.item] : el.item_name;
this.close();
} else if ('create' == el.name) {
let name = el.input.value.replace(/[|\\?*<":>+[]\/&\s]/g, '');
if (name.length) {
name = el.item_name + '/' + name;
createDirectory(name).then(response => {
if (response.status == 201) {
this.select = name;
this.close();
}
});
}
}
});
}
}
});
}
// Happens after showModal()
beforeShow(files, fResolve) {
this.select = '';
this.files(!!files);
this.fResolve = fResolve;
// Happens after showModal()
beforeShow(files, fResolve) {
this.select = '';
this.files(!!files);
this.fResolve = fResolve;
this.tree.innerHTML = '';
fetchFiles(propertyRequestBody, '/').then(items => {
buildTree(this, this.tree, items, '/');
}).catch(err => console.error(err))
}
this.tree.innerHTML = '';
fetchFiles('/').then(items => {
buildTree(this, this.tree, items, '/');
}).catch(err => console.error(err))
}
onHide() {
this.fResolve(this.select);
}
onHide() {
this.fResolve(this.select);
}
/*
onShow() {} // Happens after showModal()
beforeShow() {} // Happens before showModal()
afterShow() {} // Happens after showModal() animation transitionend
onHide() {} // Happens before animation transitionend
afterHide() {} // Happens after animation transitionend
close() {}
beforeShow() {} // Happens before showModal()
onShow() {} // Happens after showModal()
afterShow() {} // Happens after showModal() animation transitionend
onHide() {} // Happens before animation transitionend
afterHide() {} // Happens after animation transitionend
close() {}
*/
}
}
rl.ncFiles = new class {
async getDirectoryContents(path) {
return await fetchFiles(propertyRequestBody, path);
}
rl.ncFiles = {
selectFolder: () =>
new Promise(resolve => {
NextcloudFilesPopupView.showModal([
false,
folder => resolve(folder),
]);
}),
selectFolder() {
return new Promise(resolve => {
NextcloudFilesPopupView.showModal([
false,
folder => resolve(folder),
]);
});
}
selectFiles: () =>
new Promise(resolve => {
NextcloudFilesPopupView.showModal([
true,
files => resolve(files),
]);
})
};
selectFiles() {
return new Promise(resolve => {
NextcloudFilesPopupView.showModal([
true,
files => resolve(files),
]);
});
}
}
})(window.rl);