summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaksim Sukharev <antreesy.web@gmail.com>2024-03-14 18:48:17 +0100
committerMaksim Sukharev <antreesy.web@gmail.com>2024-03-15 10:23:14 +0100
commit0caa8f229be2769e64bbd0145002ccbe858522ae (patch)
treeeb823c91d78d8a1851e5a29651c1d046932bf7c7
parent5c98d3008f976f0e15d53b6eaeef050364123341 (diff)
fix(typescript): migrate messagesService to ts
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
-rw-r--r--src/deck.js2
-rw-r--r--src/maps.js2
-rw-r--r--src/services/avatarService.ts2
-rw-r--r--src/services/messagesService.ts (renamed from src/services/messagesService.js)140
-rw-r--r--src/store/conversationsStore.spec.js2
-rw-r--r--src/store/messagesStore.js2
-rw-r--r--src/store/messagesStore.spec.js2
-rw-r--r--src/types/index.ts15
-rw-r--r--src/types/vendor/crypto-js.d.ts1
9 files changed, 103 insertions, 65 deletions
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/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/messagesService.js b/src/services/messagesService.ts
index c86fadaf7..dd8f38c7d 100644
--- a/src/services/messagesService.js
+++ b/src/services/messagesService.ts
@@ -26,18 +26,42 @@ import SHA256 from 'crypto-js/sha256.js'
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
+import type {
+ ChatMessage,
+ deleteMessageResponse,
+ editMessageParams,
+ editMessageResponse,
+ getMessageContextParams,
+ getMessageContextResponse,
+ 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 {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;
+ * @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 }, 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: {
@@ -46,7 +70,7 @@ const fetchMessages = async function({ token, lastKnownMessageId, includeLastKno
lastKnownMessageId,
limit,
includeLastKnown: includeLastKnown ? 1 : 0,
- },
+ } as receiveMessagesParams,
})
}
@@ -54,13 +78,13 @@ const fetchMessages = async function({ token, lastKnownMessageId, includeLastKno
* 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
+ * @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 }, 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: {
@@ -70,7 +94,7 @@ const lookForNewMessages = async ({ token, lastKnownMessageId, limit = 100 }, op
limit,
includeLastKnown: 0,
markNotificationsAsRead: 0,
- },
+ } as receiveMessagesParams,
})
}
@@ -79,82 +103,82 @@ const lookForNewMessages = async ({ token, lastKnownMessageId, limit = 100 }, op
*
* 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;
+ * @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 }, 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 {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
+ * @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 }, { silent, ...options }) {
+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,
- }, options)
+ } as postNewMessageParams, 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
+ * @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 }, 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 {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
+ * @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, updatedMessage }, options) {
+const editMessage = async function({ token, messageId, updatedMessage }: EditMessagePayload, options: object): editMessageResponse {
return axios.put(generateOcsUrl('apps/spreed/api/v1/chat/{token}/{messageId}', { token, messageId }, options), {
message: updatedMessage,
- }, options)
+ } as editMessageParams, 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
+ * @param token conversation token
+ * @param data the wrapping object;
+ * @param data.objectType object type
+ * @param data.objectId object id
+ * @param data.metaData JSON metadata of the rich object encoded as string
+ * @param data.referenceId generated reference id, leave empty to generate it based on the other args
+ * @param options request options
*/
-const postRichObjectToConversation = async function(token, { objectType, objectId, metaData, referenceId }, options) {
+const postRichObjectToConversation = async function(token: string, { objectType, objectId, metaData, referenceId }: postRichObjectParams, options: object): postRichObjectResponse {
if (!referenceId) {
const tempId = 'richobject-' + objectType + '-' + objectId + '-' + token + '-' + (new Date().getTime())
referenceId = Hex.stringify(SHA256(tempId))
@@ -164,20 +188,20 @@ const postRichObjectToConversation = async function(token, { objectType, objectI
objectId,
metaData,
referenceId,
- }, options)
+ } as postRichObjectParams, 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
+ * @param token The token of the conversation to be removed from favorites
+ * @param lastReadMessage id of the last read message to set
+ * @param options request options
*/
-const updateLastReadMessage = async function(token, lastReadMessage, options) {
+const updateLastReadMessage = async function(token: string, lastReadMessage: setReadMarkerParams, options: object): setReadMarkerResponse {
return axios.post(generateOcsUrl('apps/spreed/api/v1/chat/{token}/read', { token }, options), {
lastReadMessage,
- }, options)
+ } as setReadMarkerParams, options)
}
const addReactionToMessage = async function(token, messageId, selectedEmoji, options) {
diff --git a/src/store/conversationsStore.spec.js b/src/store/conversationsStore.spec.js
index 5e8d29166..02be20ae1 100644
--- a/src/store/conversationsStore.spec.js
+++ b/src/store/conversationsStore.spec.js
@@ -34,7 +34,7 @@ import {
setCallPermissions,
setConversationUnread,
} from '../services/conversationsService.js'
-import { updateLastReadMessage } from '../services/messagesService.js'
+import { updateLastReadMessage } from '../services/messagesService.ts'
import { useTalkHashStore } from '../stores/talkHash.js'
import { generateOCSErrorResponse, generateOCSResponse } from '../test-helpers.js'
diff --git a/src/store/messagesStore.js b/src/store/messagesStore.js
index bf1e62c7e..b9d8b3895 100644
--- a/src/store/messagesStore.js
+++ b/src/store/messagesStore.js
@@ -43,7 +43,7 @@ import {
getMessageContext,
postNewMessage,
postRichObjectToConversation,
-} from '../services/messagesService.js'
+} from '../services/messagesService.ts'
import { useChatExtrasStore } from '../stores/chatExtras.js'
import { useGuestNameStore } from '../stores/guestName.js'
import { useReactionsStore } from '../stores/reactions.js'
diff --git a/src/store/messagesStore.spec.js b/src/store/messagesStore.spec.js
index 75f593f4f..de5347f78 100644
--- a/src/store/messagesStore.spec.js
+++ b/src/store/messagesStore.spec.js
@@ -23,7 +23,7 @@ import {
lookForNewMessages,
postNewMessage,
postRichObjectToConversation,
-} from '../services/messagesService.js'
+} from '../services/messagesService.ts'
import { useChatExtrasStore } from '../stores/chatExtras.js'
import { useGuestNameStore } from '../stores/guestName.js'
import { useReactionsStore } from '../stores/reactions.js'
diff --git a/src/types/index.ts b/src/types/index.ts
index d8edccef2..3dce1e013 100644
--- a/src/types/index.ts
+++ b/src/types/index.ts
@@ -45,10 +45,23 @@ export type File = RichObject<'size'|'path'|'link'|'mimetype'|'preview-available
'height': number,
}
export type ChatMessage = components['schemas']['ChatMessageWithParent']
+export type receiveMessagesParams = ApiOptions<operations['chat-receive-messages']['parameters']['query']>['params']
+export type receiveMessagesResponse = ApiResponse<operations['chat-receive-messages']['responses'][200]['content']['application/json']>
+export type getMessageContextParams = ApiOptions<operations['chat-get-message-context']['parameters']['query']>['params']
+export type getMessageContextResponse = ApiResponse<operations['chat-get-message-context']['responses'][200]['content']['application/json']>
+export type postNewMessageParams = ApiOptions<operations['chat-send-message']['parameters']['query']>['params']
+export type postNewMessageResponse = ApiResponse<operations['chat-send-message']['responses'][201]['content']['application/json']>
+export type deleteMessageResponse = ApiResponse<operations['chat-delete-message']['responses'][200]['content']['application/json']>
+export type editMessageParams = ApiOptions<operations['chat-edit-message']['parameters']['query']>['params']
+export type editMessageResponse = ApiResponse<operations['chat-edit-message']['responses'][200]['content']['application/json']>
+export type postRichObjectParams = ApiOptions<operations['chat-share-object-to-chat']['parameters']['query']>['params']
+export type postRichObjectResponse = ApiResponse<operations['chat-share-object-to-chat']['responses'][201]['content']['application/json']>
+export type setReadMarkerParams = ApiOptions<operations['chat-set-read-marker']['parameters']['query']>['params']
+export type setReadMarkerResponse = ApiResponse<operations['chat-set-read-marker']['responses'][200]['content']['application/json']>
// Avatars
export type setFileAvatarResponse = ApiResponse<operations['avatar-upload-avatar']['responses'][200]['content']['application/json']>
-export type setEmojiAvatarParams = ApiOptions<operations['avatar-emoji-avatar']['parameters']['query']>
+export type setEmojiAvatarParams = ApiOptions<operations['avatar-emoji-avatar']['parameters']['query']>['params']
export type setEmojiAvatarResponse = ApiResponse<operations['avatar-emoji-avatar']['responses'][200]['content']['application/json']>
export type deleteAvatarResponse = ApiResponse<operations['avatar-delete-avatar']['responses'][200]['content']['application/json']>
diff --git a/src/types/vendor/crypto-js.d.ts b/src/types/vendor/crypto-js.d.ts
new file mode 100644
index 000000000..e54b99541
--- /dev/null
+++ b/src/types/vendor/crypto-js.d.ts
@@ -0,0 +1 @@
+declare module 'crypto-js/*.js'