| 6310 | 6309 | } |
| 6311 | 6310 | |
| 6312 | 6311 | /** |
| 6312 | + * Syncs swipe data back to the message data at the given message ID (or the last message if no ID is given). |
| 6313 | + * If the swipe ID is not provided, the current swipe ID in the message object is used. |
| 6314 | + * |
| 6315 | + * If the swipe data is invalid in some way, this function will exit out without doing anything. |
| 6316 | + * @param {number?} [messageId=null] - The ID of the message to sync with the swipe data. If no ID is given, the last message is used. |
| 6317 | + * @param {number?} [swipeId=null] - The ID of the swipe to sync. If no ID is given, the current swipe ID in the message object is used. |
| 6318 | + * @returns {boolean} Whether the swipe data was successfully synced to the message |
| 6319 | + */ |
| 6320 | +export function syncSwipeToMes(messageId = null, swipeId = null) { |
| 6321 | + if (!chat.length) { |
| 6322 | + return false; |
| 6323 | + } |
| 6324 | + |
| 6325 | + const targetMessageId = messageId ?? chat.length - 1; |
| 6326 | + if (targetMessageId >= chat.length || targetMessageId < 0) { |
| 6327 | + console.warn(`[syncSwipeToMes] Invalid message ID: ${messageId}`); |
| 6328 | + return false; |
| 6329 | + } |
| 6330 | + |
| 6331 | + const targetMessage = chat[targetMessageId]; |
| 6332 | + if (!targetMessage) { |
| 6333 | + return false; |
| 6334 | + } |
| 6335 | + |
| 6336 | + if (swipeId !== null) { |
| 6337 | + if (isNaN(swipeId) || swipeId < 0) { |
| 6338 | + console.warn(`[syncSwipeToMes] Invalid swipe ID: ${swipeId}`); |
| 6339 | + return false; |
| 6340 | + } |
| 6341 | + targetMessage.swipe_id = swipeId; |
| 6342 | + } |
| 6343 | + |
| 6344 | + // No swipe data there yet, exit out |
| 6345 | + if (typeof targetMessage.swipe_id !== 'number') { |
| 6346 | + return false; |
| 6347 | + } |
| 6348 | + // If swipes structure is invalid, exit out |
| 6349 | + if (!Array.isArray(targetMessage.swipe_info) || !Array.isArray(targetMessage.swipes)) { |
| 6350 | + return false; |
| 6351 | + } |
| 6352 | + |
| 6353 | + const targetSwipeId = targetMessage.swipe_id; |
| 6354 | + if (!targetMessage.swipes[targetSwipeId] || !targetMessage.swipe_info[targetSwipeId]) { |
| 6355 | + console.warn(`[syncSwipeToMes] Invalid swipe ID: ${targetSwipeId}`); |
| 6356 | + return false; |
| 6357 | + } |
| 6358 | + |
| 6359 | + const targetSwipeInfo = targetMessage.swipe_info[targetSwipeId]; |
| 6360 | + if (typeof targetSwipeInfo !== 'object') { |
| 6361 | + return false; |
| 6362 | + } |
| 6363 | + |
| 6364 | + targetMessage.mes = targetMessage.swipes[targetSwipeId]; |
| 6365 | + targetMessage.send_date = targetSwipeInfo.send_date; |
| 6366 | + targetMessage.gen_started = targetSwipeInfo.gen_started; |
| 6367 | + targetMessage.gen_finished = targetSwipeInfo.gen_finished; |
| 6368 | + targetMessage.extra = structuredClone(targetSwipeInfo.extra); |
| 6369 | + |
| 6370 | + return true; |
| 6371 | +} |
| 6372 | + |
| 6373 | +/** |
| 6313 | 6374 | * Saves the image to the message object. |
| 6314 | 6375 | * @param {ParsedImage} img Image object |
| 6315 | 6376 | * @param {object} mes Chat message object |