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