diff options
Diffstat (limited to 'apps/comments/src/services')
-rw-r--r-- | apps/comments/src/services/CommentsInstance.js | 69 | ||||
-rw-r--r-- | apps/comments/src/services/DavClient.js | 37 | ||||
-rw-r--r-- | apps/comments/src/services/DeleteComment.js | 37 | ||||
-rw-r--r-- | apps/comments/src/services/EditComment.js | 49 | ||||
-rw-r--r-- | apps/comments/src/services/GetComments.js | 80 | ||||
-rw-r--r-- | apps/comments/src/services/NewComment.js | 60 |
6 files changed, 332 insertions, 0 deletions
diff --git a/apps/comments/src/services/CommentsInstance.js b/apps/comments/src/services/CommentsInstance.js new file mode 100644 index 00000000000..9eeea198760 --- /dev/null +++ b/apps/comments/src/services/CommentsInstance.js @@ -0,0 +1,69 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import { getLoggerBuilder } from '@nextcloud/logger' +import { translate as t, translatePlural as n } from '@nextcloud/l10n' +import CommentsApp from '../views/Comments' +import Vue from 'vue' + +const logger = getLoggerBuilder() + .setApp('comments') + .detectUser() + .build() + +// Add translates functions +Vue.mixin({ + data() { + return { + logger, + } + }, + methods: { + t, + n, + }, +}) + +export default class CommentInstance { + + /** + * Initialize a new Comments instance for the desired type + * + * @param {string} commentsType the comments endpoint type + * @param {Object} options the vue options (propsData, parent, el...) + */ + constructor(commentsType = 'files', options) { + // Add comments type as a global mixin + Vue.mixin({ + data() { + return { + commentsType, + } + }, + }) + + // Init Comments component + const View = Vue.extend(CommentsApp) + return new View(options) + } + +} diff --git a/apps/comments/src/services/DavClient.js b/apps/comments/src/services/DavClient.js new file mode 100644 index 00000000000..9fc67b52c98 --- /dev/null +++ b/apps/comments/src/services/DavClient.js @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import webdav from 'webdav' +import axios from '@nextcloud/axios' +import { getRootPath } from '../utils/davUtils' + +// Add this so the server knows it is an request from the browser +axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest' + +// force our axios +const patcher = webdav.getPatcher() +patcher.patch('request', axios) + +// init webdav client +const client = webdav.createClient(getRootPath()) + +export default client diff --git a/apps/comments/src/services/DeleteComment.js b/apps/comments/src/services/DeleteComment.js new file mode 100644 index 00000000000..d9954a5603e --- /dev/null +++ b/apps/comments/src/services/DeleteComment.js @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import client from './DavClient' + +/** + * Delete a comment + * + * @param {string} commentsType the ressource type + * @param {number} ressourceId the ressource ID + * @param {number} commentId the comment iD + */ +export default async function(commentsType, ressourceId, commentId) { + const commentPath = ['', commentsType, ressourceId, commentId].join('/') + + // Fetch newly created comment data + await client.deleteFile(commentPath) +} diff --git a/apps/comments/src/services/EditComment.js b/apps/comments/src/services/EditComment.js new file mode 100644 index 00000000000..fd6624c7da8 --- /dev/null +++ b/apps/comments/src/services/EditComment.js @@ -0,0 +1,49 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import client from './DavClient' + +/** + * Edit an existing comment + * + * @param {string} commentsType the ressource type + * @param {number} ressourceId the ressource ID + * @param {number} commentId the comment iD + * @param {string} message the message content + */ +export default async function(commentsType, ressourceId, commentId, message) { + const commentPath = ['', commentsType, ressourceId, commentId].join('/') + + return await client.customRequest(commentPath, Object.assign({ + method: 'PROPPATCH', + data: `<?xml version="1.0"?> + <d:propertyupdate + xmlns:d="DAV:" + xmlns:oc="http://owncloud.org/ns"> + <d:set> + <d:prop> + <oc:message>${message}</oc:message> + </d:prop> + </d:set> + </d:propertyupdate>`, + })) +} diff --git a/apps/comments/src/services/GetComments.js b/apps/comments/src/services/GetComments.js new file mode 100644 index 00000000000..a1ac89069ee --- /dev/null +++ b/apps/comments/src/services/GetComments.js @@ -0,0 +1,80 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import { parseXML, prepareFileFromProps } from 'webdav/dist/node/interface/dav' +import { processResponsePayload } from 'webdav/dist/node/response' +import client from './DavClient' +import { genFileInfo } from '../utils/fileUtils' + +export const DEFAULT_LIMIT = 5 +/** + * Retrieve the comments list + * + * @param {Object} data destructuring object + * @param {string} data.commentsType the ressource type + * @param {number} data.ressourceId the ressource ID + * @param {Object} [options] optional options for axios + * @returns {Object[]} the comments list + */ +export default async function({ commentsType, ressourceId }, options = {}) { + let response = null + const ressourcePath = ['', commentsType, ressourceId].join('/') + + return await client.customRequest(ressourcePath, Object.assign({ + method: 'REPORT', + data: `<?xml version="1.0"?> + <oc:filter-comments + xmlns:d="DAV:" + xmlns:oc="http://owncloud.org/ns" + xmlns:nc="http://nextcloud.org/ns" + xmlns:ocs="http://open-collaboration-services.org/ns"> + <oc:limit>${DEFAULT_LIMIT}</oc:limit> + <oc:offset>${options.offset || 0}</oc:offset> + </oc:filter-comments>`, + }, options)) + // See example on how it's done normaly + // https://github.com/perry-mitchell/webdav-client/blob/9de2da4a2599e06bd86c2778145b7ade39fe0b3c/source/interface/stat.js#L19 + // Waiting for proper REPORT integration https://github.com/perry-mitchell/webdav-client/issues/207 + .then(res => { + response = res + return res.data + }) + .then(parseXML) + .then(xml => processMultistatus(xml, true)) + .then(comments => processResponsePayload(response, comments, true)) + .then(response => response.data.map(genFileInfo)) +} + +// https://github.com/perry-mitchell/webdav-client/blob/9de2da4a2599e06bd86c2778145b7ade39fe0b3c/source/interface/directoryContents.js#L32 +function processMultistatus(result, isDetailed = false) { + // Extract the response items (directory contents) + const { + multistatus: { response: responseItems }, + } = result + return responseItems.map(item => { + // Each item should contain a stat object + const { + propstat: { prop: props }, + } = item + return prepareFileFromProps(props, props.id.toString(), isDetailed) + }) +} diff --git a/apps/comments/src/services/NewComment.js b/apps/comments/src/services/NewComment.js new file mode 100644 index 00000000000..96aee85e010 --- /dev/null +++ b/apps/comments/src/services/NewComment.js @@ -0,0 +1,60 @@ +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import { genFileInfo } from '../utils/fileUtils' +import { getCurrentUser } from '@nextcloud/auth' +import { getRootPath } from '../utils/davUtils' +import axios from '@nextcloud/axios' +import client from './DavClient' + +/** + * Retrieve the comments list + * + * @param {string} commentsType the ressource type + * @param {number} ressourceId the ressource ID + * @param {string} message the message + * @returns {Object} the new comment + */ +export default async function(commentsType, ressourceId, message) { + const ressourcePath = ['', commentsType, ressourceId].join('/') + + const response = await axios.post(getRootPath() + ressourcePath, { + actorDisplayName: getCurrentUser().displayName, + actorId: getCurrentUser().uid, + actorType: 'users', + creationDateTime: (new Date()).toUTCString(), + message, + objectType: 'files', + verb: 'comment', + }) + + // Retrieve comment id from ressource location + const commentId = parseInt(response.headers['content-location'].split('/').pop()) + const commentPath = ressourcePath + '/' + commentId + + // Fetch newly created comment data + const comment = await client.stat(commentPath, { + details: true, + }) + + return genFileInfo(comment) +} |