summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaksim Sukharev <antreesy.web@gmail.com>2024-03-15 14:47:16 +0100
committerGitHub <noreply@github.com>2024-03-15 14:47:16 +0100
commit7736524344560a378922c573f04485bc84eef4ce (patch)
tree7402b1544be0d209a12eb160f60d9d54df79027b
parent10dfbd3d1b6512c75938c24df45cc0f24e49b1d2 (diff)
parentdda2d2b8e5d5601b7a50a6ab04da79a41a420692 (diff)
Merge pull request #11807 from nextcloud/followup/11802/message-parameters-ts
fix(openapi): use provided from openapi message parameters models
-rw-r--r--src/components/MessagesList/MessagesGroup/Message/MessageButtonsBar/MessageTranslateDialog.vue2
-rw-r--r--src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js4
-rw-r--r--src/deck.js2
-rw-r--r--src/maps.js2
-rw-r--r--src/services/__tests__/messagesService.spec.js (renamed from src/services/messagesService.spec.js)8
-rw-r--r--src/services/avatarService.ts2
-rw-r--r--src/services/conversationsService.js20
-rw-r--r--src/services/messagesService.js228
-rw-r--r--src/services/messagesService.ts238
-rw-r--r--src/services/reactionsService.ts63
-rw-r--r--src/services/translationService.js38
-rw-r--r--src/store/conversationsStore.js6
-rw-r--r--src/store/conversationsStore.spec.js5
-rw-r--r--src/store/messagesStore.js2
-rw-r--r--src/store/messagesStore.spec.js2
-rw-r--r--src/stores/__tests__/reactions.spec.js4
-rw-r--r--src/stores/reactions.js2
-rw-r--r--src/types/index.ts58
-rw-r--r--src/types/vendor/crypto-js.d.ts1
-rw-r--r--src/utils/getItemTypeFromMessage.ts6
20 files changed, 399 insertions, 294 deletions
diff --git a/src/components/MessagesList/MessagesGroup/Message/MessageButtonsBar/MessageTranslateDialog.vue b/src/components/MessagesList/MessagesGroup/Message/MessageButtonsBar/MessageTranslateDialog.vue
index 403b53b3e..234cbc6dd 100644
--- a/src/components/MessagesList/MessagesGroup/Message/MessageButtonsBar/MessageTranslateDialog.vue
+++ b/src/components/MessagesList/MessagesGroup/Message/MessageButtonsBar/MessageTranslateDialog.vue
@@ -91,7 +91,7 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
import NcRichText from '@nextcloud/vue/dist/Components/NcRichText.js'
import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js'
-import { getTranslationLanguages, translateText } from '../../../../../services/messagesService.js'
+import { getTranslationLanguages, translateText } from '../../../../../services/translationService.js'
export default {
name: 'MessageTranslateDialog',
diff --git a/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js b/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js
index 5975bae63..ef4617c22 100644
--- a/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js
+++ b/src/components/MessagesList/MessagesGroup/Message/MessagePart/Reactions.spec.js
@@ -16,13 +16,13 @@ import {
addReactionToMessage,
removeReactionFromMessage,
getReactionsDetails,
-} from '../../../../../services/messagesService.js'
+} from '../../../../../services/reactionsService.ts'
import vuexStore from '../../../../../store/index.js'
import storeConfig from '../../../../../store/storeConfig.js'
import { useReactionsStore } from '../../../../../stores/reactions.js'
import { generateOCSResponse } from '../../../../../test-helpers.js'
-jest.mock('../../../../../services/messagesService', () => ({
+jest.mock('../../../../../services/reactionsService', () => ({
getReactionsDetails: jest.fn(),
addReactionToMessage: jest.fn(),
removeReactionFromMessage: jest.fn(),
diff --git a/src/deck.js b/src/deck.js
index 3b348643c..3d7ad268d 100644
--- a/src/deck.js
+++ b/src/deck.js
@@ -28,7 +28,7 @@ import { showSuccess, showError } from '@nextcloud/dialogs'
import { translate, translatePlural } from '@nextcloud/l10n'
import { generateFilePath, generateUrl } from '@nextcloud/router'
-import { postRichObjectToConversation } from './services/messagesService.js'
+import { postRichObjectToConversation } from './services/messagesService.ts'
import '@nextcloud/dialogs/style.css'
diff --git a/src/maps.js b/src/maps.js
index 6f42865ac..07c475d16 100644
--- a/src/maps.js
+++ b/src/maps.js
@@ -28,7 +28,7 @@ import { showSuccess, showError } from '@nextcloud/dialogs'
import { translate, translatePlural } from '@nextcloud/l10n'
import { generateFilePath, generateUrl } from '@nextcloud/router'
-import { postRichObjectToConversation } from './services/messagesService.js'
+import { postRichObjectToConversation } from './services/messagesService.ts'
import '@nextcloud/dialogs/style.css'
diff --git a/src/services/messagesService.spec.js b/src/services/__tests__/messagesService.spec.js
index 32fc33827..25f2eb4e0 100644
--- a/src/services/messagesService.spec.js
+++ b/src/services/__tests__/messagesService.spec.js
@@ -1,6 +1,7 @@
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
+import { CHAT } from '../../constants.js'
import {
fetchMessages,
getMessageContext,
@@ -10,13 +11,16 @@ import {
editMessage,
postRichObjectToConversation,
updateLastReadMessage,
+} from '../messagesService.ts'
+import {
addReactionToMessage,
removeReactionFromMessage,
getReactionsDetails,
+} from '../reactionsService.ts'
+import {
getTranslationLanguages,
translateText,
-} from './messagesService.js'
-import { CHAT } from '../constants.js'
+} from '../translationService.js'
jest.mock('@nextcloud/axios', () => ({
get: jest.fn(),
diff --git a/src/services/avatarService.ts b/src/services/avatarService.ts
index d8000badc..1c997e447 100644
--- a/src/services/avatarService.ts
+++ b/src/services/avatarService.ts
@@ -46,7 +46,7 @@ const setConversationEmojiAvatar = async function(token: string, emoji: string,
return axios.post(generateOcsUrl('apps/spreed/api/v1/room/{token}/avatar/emoji', { token }), {
emoji,
color,
- } as setEmojiAvatarParams['params'])
+ } as setEmojiAvatarParams)
}
const deleteConversationAvatar = async function(token: string): deleteAvatarResponse {
diff --git a/src/services/conversationsService.js b/src/services/conversationsService.js
index 152aa0c14..f1a93fdaa 100644
--- a/src/services/conversationsService.js
+++ b/src/services/conversationsService.js
@@ -206,24 +206,6 @@ const deleteConversation = async function(token) {
}
/**
- * Clears the conversation history
- *
- * @param {string} token The token of the conversation to be deleted.
- */
-const clearConversationHistory = async function(token) {
- return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }))
-}
-
-/**
- * Set conversation as unread
- *
- * @param {string} token The token of the conversation to be set as unread
- */
-const setConversationUnread = async function(token) {
- return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}/read', { token }))
-}
-
-/**
* Add a conversation to the favorites
*
* @param {string} token The token of the conversation to be favorites
@@ -407,8 +389,6 @@ export {
setConversationPassword,
setConversationName,
setConversationDescription,
- clearConversationHistory,
- setConversationUnread,
setConversationPermissions,
setCallPermissions,
setMessageExpiration,
diff --git a/src/services/messagesService.js b/src/services/messagesService.js
deleted file mode 100644
index c86fadaf7..000000000
--- a/src/services/messagesService.js
+++ /dev/null
@@ -1,228 +0,0 @@
-/**
- * @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@icloud.com>
- *
- * @author Marco Ambrosini <marcoambrosini@icloud.com>
- *
- * @license AGPL-3.0-or-later
- *
- * 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 Hex from 'crypto-js/enc-hex.js'
-import SHA256 from 'crypto-js/sha256.js'
-
-import axios from '@nextcloud/axios'
-import { generateOcsUrl } from '@nextcloud/router'
-
-/**
- * Fetches messages that belong to a particular conversation
- * specified with its token.
- *
- * @param {object} data the wrapping object;
- * @param {string} data.token the conversation token;
- * @param {string} data.lastKnownMessageId last known message id;
- * @param {boolean} data.includeLastKnown whether to include the last known message in the response;
- * @param {number} [data.limit=100] Number of messages to load
- * @param {object} options options;
- */
-const fetchMessages = async function({ token, lastKnownMessageId, includeLastKnown, limit = 100 }, options) {
- return axios.get(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }, options), {
- ...options,
- params: {
- setReadMarker: 0,
- lookIntoFuture: 0,
- lastKnownMessageId,
- limit,
- includeLastKnown: includeLastKnown ? 1 : 0,
- },
- })
-}
-
-/**
- * Fetches newly created messages that belong to a particular conversation
- * specified with its token.
- *
- * @param {object} data the wrapping object;
- * @param {number} data.lastKnownMessageId The id of the last message in the store.
- * @param {string} data.token The conversation token;
- * @param {number} [data.limit=100] Number of messages to load
- * @param {object} options options
- */
-const lookForNewMessages = async ({ token, lastKnownMessageId, limit = 100 }, options) => {
- return axios.get(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }, options), {
- ...options,
- params: {
- setReadMarker: 0,
- lookIntoFuture: 1,
- lastKnownMessageId,
- limit,
- includeLastKnown: 0,
- markNotificationsAsRead: 0,
- },
- })
-}
-
-/**
- * Get the context of a message
- *
- * Loads some messages from before and after the given one.
- *
- * @param {object} data the wrapping object;
- * @param {string} data.token the conversation token;
- * @param {number} data.messageId last known message id;
- * @param {number} [data.limit=50] Number of messages to load
- * @param {object} options options;
- */
-const getMessageContext = async function({ token, messageId, limit = 50 }, options) {
- return axios.get(generateOcsUrl('apps/spreed/api/v1/chat/{token}/{messageId}/context', { token, messageId }, options), {
- ...options,
- params: {
- limit,
- },
- })
-}
-
-/**
- * Posts a new message to the server.
- *
- * @param {object} param0 The message object that is destructured
- * @param {string} param0.token The conversation token
- * @param {string} param0.message The message text
- * @param {string} param0.actorDisplayName The display name of the actor
- * @param {string} param0.referenceId A reference id to identify the message later again
- * @param {object|undefined} param0.parent The message to be replied to
- * @param {object} param1 options object destructured
- * @param {boolean} param1.silent whether the message should trigger a notifications
- */
-const postNewMessage = async function({ token, message, actorDisplayName, referenceId, parent }, { silent, ...options }) {
- return axios.post(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }, options), {
- message,
- actorDisplayName,
- referenceId,
- replyTo: parent?.id,
- silent,
- }, options)
-}
-
-/**
- * Deletes a message from the server.
- *
- * @param {object} param0 The message object that is destructured
- * @param {string} param0.token The conversation token
- * @param {string} param0.id The id of the message to be deleted
- * @param {object} options request options
- */
-const deleteMessage = async function({ token, id }, options) {
- return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}/{id}', { token, id }, options), options)
-}
-
-/**
- * Edit a message text / file share caption.
- *
- * @param {object} param0 The destructured payload
- * @param {string} param0.token The conversation token
- * @param {string} param0.messageId The message id
- * @param {string} param0.updatedMessage The modified text of the message / file share caption
- * @param {object} options request options
- */
-const editMessage = async function({ token, messageId, updatedMessage }, options) {
- return axios.put(generateOcsUrl('apps/spreed/api/v1/chat/{token}/{messageId}', { token, messageId }, options), {
- message: updatedMessage,
- }, options)
-}
-
-/**
- * Post a rich object to a conversation
- *
- * @param {string} token conversation token
- * @param {object} data the wrapping object;
- * @param {string} data.objectType object type
- * @param {string} data.objectId object id
- * @param {string} data.metaData JSON metadata of the rich object encoded as string
- * @param {string} data.referenceId generated reference id, leave empty to generate it based on the other args
- * @param {object} options request options
- */
-const postRichObjectToConversation = async function(token, { objectType, objectId, metaData, referenceId }, options) {
- if (!referenceId) {
- const tempId = 'richobject-' + objectType + '-' + objectId + '-' + token + '-' + (new Date().getTime())
- referenceId = Hex.stringify(SHA256(tempId))
- }
- return axios.post(generateOcsUrl('apps/spreed/api/v1/chat/{token}/share', { token }, options), {
- objectType,
- objectId,
- metaData,
- referenceId,
- }, options)
-}
-
-/**
- * Updates the last read message id
- *
- * @param {string} token The token of the conversation to be removed from favorites
- * @param {number} lastReadMessage id of the last read message to set
- * @param {object} options request options
- */
-const updateLastReadMessage = async function(token, lastReadMessage, options) {
- return axios.post(generateOcsUrl('apps/spreed/api/v1/chat/{token}/read', { token }, options), {
- lastReadMessage,
- }, options)
-}
-
-const addReactionToMessage = async function(token, messageId, selectedEmoji, options) {
- return axios.post(generateOcsUrl('apps/spreed/api/v1/reaction/{token}/{messageId}', { token, messageId }, options), {
- reaction: selectedEmoji,
- }, options)
-}
-
-const removeReactionFromMessage = async function(token, messageId, selectedEmoji, options) {
- return axios.delete(generateOcsUrl('apps/spreed/api/v1/reaction/{token}/{messageId}', { token, messageId }, options), {
- ...options,
- params: {
- reaction: selectedEmoji,
- },
- })
-}
-
-const getReactionsDetails = async function(token, messageId, options) {
- return axios.get(generateOcsUrl('apps/spreed/api/v1/reaction/{token}/{messageId}', { token, messageId }, options), options)
-}
-
-const getTranslationLanguages = async function(options) {
- return axios.get(generateOcsUrl('/translation/languages', undefined, options), options)
-}
-
-const translateText = async function(text, fromLanguage, toLanguage, options) {
- return axios.post(generateOcsUrl('/translation/translate', undefined, options), {
- text,
- fromLanguage,
- toLanguage,
- }, options)
-}
-
-export {
- fetchMessages,
- lookForNewMessages,
- getMessageContext,
- postNewMessage,
- deleteMessage,
- postRichObjectToConversation,
- updateLastReadMessage,
- addReactionToMessage,
- removeReactionFromMessage,
- getReactionsDetails,
- editMessage,
- getTranslationLanguages,
- translateText,
-}
diff --git a/src/services/messagesService.ts b/src/services/messagesService.ts
new file mode 100644
index 000000000..26bd9efec
--- /dev/null
+++ b/src/services/messagesService.ts
@@ -0,0 +1,238 @@
+/**
+ * @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@icloud.com>
+ *
+ * @author Marco Ambrosini <marcoambrosini@icloud.com>
+ *
+ * @license AGPL-3.0-or-later
+ *
+ * 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 Hex from 'crypto-js/enc-hex.js'
+import SHA256 from 'crypto-js/sha256.js'
+
+import axios from '@nextcloud/axios'
+import { generateOcsUrl } from '@nextcloud/router'
+
+import type {
+ ChatMessage,
+ clearHistoryResponse,
+ deleteMessageResponse,
+ editMessageParams,
+ editMessageResponse,
+ getMessageContextParams,
+ getMessageContextResponse,
+ markUnreadResponse,
+ postNewMessageParams,
+ postNewMessageResponse,
+ postRichObjectParams,
+ postRichObjectResponse,
+ receiveMessagesParams,
+ receiveMessagesResponse,
+ setReadMarkerParams,
+ setReadMarkerResponse
+} from '../types'
+
+type ReceiveMessagesPayload = Partial<receiveMessagesParams> & { token: string }
+type GetMessageContextPayload = getMessageContextParams & { token: string, messageId: number }
+type PostNewMessagePayload = Omit<postNewMessageParams, 'replyTo'> & { token: string, parent: ChatMessage }
+type PostNewMessageOptions = Pick<postNewMessageParams, 'silent'> & object
+type DeleteMessagePayload = { token: string, id: number }
+type EditMessagePayload = { token: string, messageId: number, updatedMessage: editMessageParams['message'] }
+
+/**
+ * Fetches messages that belong to a particular conversation
+ * specified with its token.
+ *
+ * @param data the wrapping object;
+ * @param data.token the conversation token;
+ * @param data.lastKnownMessageId last known message id;
+ * @param data.includeLastKnown whether to include the last known message in the response;
+ * @param [data.limit=100] Number of messages to load
+ * @param options options;
+ */
+const fetchMessages = async function({ token, lastKnownMessageId, includeLastKnown, limit = 100 }: ReceiveMessagesPayload, options: object): receiveMessagesResponse {
+ return axios.get(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }, options), {
+ ...options,
+ params: {
+ setReadMarker: 0,
+ lookIntoFuture: 0,
+ lastKnownMessageId,
+ limit,
+ includeLastKnown: includeLastKnown ? 1 : 0,
+ } as receiveMessagesParams,
+ })
+}
+
+/**
+ * Fetches newly created messages that belong to a particular conversation
+ * specified with its token.
+ *
+ * @param data the wrapping object;
+ * @param data.lastKnownMessageId The id of the last message in the store.
+ * @param data.token The conversation token;
+ * @param [data.limit=100] Number of messages to load
+ * @param options options
+ */
+const lookForNewMessages = async ({ token, lastKnownMessageId, limit = 100 }: ReceiveMessagesPayload, options: object): receiveMessagesResponse => {
+ return axios.get(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }, options), {
+ ...options,
+ params: {
+ setReadMarker: 0,
+ lookIntoFuture: 1,
+ lastKnownMessageId,
+ limit,
+ includeLastKnown: 0,
+ markNotificationsAsRead: 0,
+ } as receiveMessagesParams,
+ })
+}
+
+/**
+ * Get the context of a message
+ *
+ * Loads some messages from before and after the given one.
+ *
+ * @param data the wrapping object;
+ * @param data.token the conversation token;
+ * @param data.messageId last known message id;
+ * @param [data.limit=50] Number of messages to load
+ * @param options options;
+ */
+const getMessageContext = async function({ token, messageId, limit = 50 }: GetMessageContextPayload, options: object): getMessageContextResponse {
+ return axios.get(generateOcsUrl('apps/spreed/api/v1/chat/{token}/{messageId}/context', { token, messageId }, options), {
+ ...options,
+ params: {
+ limit,
+ } as getMessageContextParams,
+ })
+}
+
+/**
+ * Posts a new message to the server.
+ *
+ * @param param0 The message object that is destructured
+ * @param param0.token The conversation token
+ * @param param0.message The message text
+ * @param param0.actorDisplayName The display name of the actor
+ * @param param0.referenceId A reference id to identify the message later again
+ * @param param0.parent The message to be replied to
+ * @param param1 options object destructured
+ * @param param1.silent whether the message should trigger a notifications
+ */
+const postNewMessage = async function({ token, message, actorDisplayName, referenceId, parent }: PostNewMessagePayload, { silent, ...options }: PostNewMessageOptions): postNewMessageResponse {
+ return axios.post(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }, options), {
+ message,
+ actorDisplayName,
+ referenceId,
+ replyTo: parent?.id,
+ silent,
+ } as postNewMessageParams, options)
+}
+
+/**
+ * Clears the conversation history
+ *
+ * @param token The token of the conversation to be deleted.
+ */
+const clearConversationHistory = async function(token: string): clearHistoryResponse {
+ return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token }))
+}
+
+/**
+ * Deletes a message from the server.
+ *
+ * @param param0 The message object that is destructured
+ * @param param0.token The conversation token
+ * @param param0.id The id of the message to be deleted
+ * @param options request options
+ */
+const deleteMessage = async function({ token, id }: DeleteMessagePayload, options: object): deleteMessageResponse {
+ return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}/{id}', { token, id }, options), options)
+}
+
+/**
+ * Edit a message text / file share caption.
+ *
+ * @param param0 The destructured payload
+ * @param param0.token The conversation token
+ * @param param0.messageId The message id
+ * @param param0.updatedMessage The modified text of the message / file share caption
+ * @param options request options
+ */
+const editMessage = async function({ token, messageId, updatedMess