| 7942 | /** | 7942 | /** |
| 7943 | * Deletes a swipe from the chat. | 7943 | * Deletes a swipe from the chat. |
| 7944 | * | 7944 | * |
| 7945 | * @param {number?} swipeId - The ID of the swipe to delete. If not provided, the current swipe will be deleted. | 7945 | * @param {number?} [swipeId = null] - The ID of the swipe to delete. If not provided, the current swipe will be deleted. |
| | 7946 | * @param {number?} [messageId = chat.length - 1] - The ID of the message to delete from. If not provided, the last message will be targeted. |
| 7946 | * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion. | 7947 | * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion. |
| 7947 | */ | 7948 | */ |
| 7948 | export async function deleteSwipe(swipeId = null) { | 7949 | export async function deleteSwipe(swipeId = null, messageId = chat.length - 1) { |
| 7949 | if (swipeId && (isNaN(swipeId) || swipeId < 0)) { | 7950 | if (swipeId && (isNaN(swipeId) || swipeId < 0)) { |
| 7950 | toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`); | 7951 | toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`); |
| 7951 | return; | 7952 | return; |
| 7952 | } | 7953 | } |
| 7953 | | 7954 | |
| 7954 | const lastMessage = chat[chat.length - 1]; | 7955 | const message = chat[messageId]; |
| 7955 | if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) { | 7956 | if (!message || !Array.isArray(message.swipes) || !message.swipes.length) { |
| 7956 | toastr.warning(t`No messages to delete swipes from.`); | 7957 | toastr.warning(t`No messages to delete swipes from.`); |
| 7957 | return; | 7958 | return; |
| 7958 | } | 7959 | } |
| 7959 | | 7960 | |
| 7960 | if (lastMessage.swipes.length <= 1) { | 7961 | if (message.swipes.length <= 1) { |
| 7961 | toastr.warning(t`Can't delete the last swipe.`); | 7962 | toastr.warning(t`Can't delete the last swipe.`); |
| 7962 | return; | 7963 | return; |
| 7963 | } | 7964 | } |
| 7964 | | 7965 | |
| 7965 | swipeId = swipeId ?? lastMessage.swipe_id; | 7966 | swipeId = swipeId ?? message.swipe_id; |
| 7966 | | 7967 | |
| 7967 | if (swipeId < 0 || swipeId >= lastMessage.swipes.length) { | 7968 | if (swipeId < 0 || swipeId >= message.swipes.length) { |
| 7968 | toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`); | 7969 | toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`); |
| 7969 | return; | 7970 | return; |
| 7970 | } | 7971 | } |
| 7971 | | 7972 | |
| 7972 | lastMessage.swipes.splice(swipeId, 1); | 7973 | message.swipes.splice(swipeId, 1); |
| 7973 | | 7974 | |
| 7974 | if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) { | 7975 | if (Array.isArray(message.swipe_info) && message.swipe_info.length) { |
| 7975 | lastMessage.swipe_info.splice(swipeId, 1); | 7976 | message.swipe_info.splice(swipeId, 1); |
| 7976 | } | 7977 | } |
| 7977 | | 7978 | |
| 7978 | // Select the next swipe, or the one before if it was the last one | 7979 | // Select the next swipe, or the one before if it was the last one |
| 7979 | const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1); | 7980 | const newSwipeId = Math.min(swipeId, message.swipes.length - 1); |
| 7980 | syncSwipeToMes(null, newSwipeId); | 7981 | syncSwipeToMes(messageId, newSwipeId); |
| 7981 | | 7982 | |
| 7982 | await eventSource.emit(event_types.MESSAGE_SWIPE_DELETED, { swipeId, newSwipeId }); | 7983 | await eventSource.emit(event_types.MESSAGE_SWIPE_DELETED, { messageId, swipeId, newSwipeId }); |
| 7983 | | 7984 | |
| 7984 | await saveChatConditional(); | 7985 | await saveChatConditional(); |
| 7985 | await reloadCurrentChat(); | 7986 | await reloadCurrentChat(); |