Refactor endpoints/chats.js 1. Move formatBytes utility to util.js 2. Fix type error related to dates sorting 3. Add type to backupFunctions map
| @@ -9,7 +9,14 @@ import { sync as writeFileAtomicSync } from 'write-file-atomic'; | |||
| 9 | import _ from 'lodash'; | 9 | import _ from 'lodash'; |
| 10 | 10 | ||
| 11 | import { jsonParser, urlencodedParser } from '../express-common.js'; | 11 | import { jsonParser, urlencodedParser } from '../express-common.js'; |
| 12 | import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js'; | 12 | import { |
| 13 | getConfigValue, | ||
| 14 | humanizedISO8601DateTime, | ||
| 15 | tryParse, | ||
| 16 | generateTimestamp, | ||
| 17 | removeOldBackups, | ||
| 18 | formatBytes, | ||
| 19 | } from '../util.js'; | ||
| 13 | 20 | ||
| 14 | const isBackupDisabled = getConfigValue('disableChatBackup', false); | 21 | const isBackupDisabled = getConfigValue('disableChatBackup', false); |
| 15 | const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); | 22 | const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); |
| @@ -46,6 +53,9 @@ function backupChat(directory, name, chat) { | |||
| 46 | } | 53 | } |
| 47 | } | 54 | } |
| 48 | 55 | ||
| 56 | /** | ||
| 57 | * @type {Map<string, import('lodash').DebouncedFunc<function(string, string, string): void>>} | ||
| 58 | */ | ||
| 49 | const backupFunctions = new Map(); | 59 | const backupFunctions = new Map(); |
| 50 | 60 | ||
| 51 | /** | 61 | /** |
| @@ -57,20 +67,7 @@ function getBackupFunction(handle) { | |||
| 57 | if (!backupFunctions.has(handle)) { | 67 | if (!backupFunctions.has(handle)) { |
| 58 | backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true })); | 68 | backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true })); |
| 59 | } | 69 | } |
| 60 | return backupFunctions.get(handle); | 70 | return backupFunctions.get(handle) || (() => {}); |
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Formats a byte size into a human-readable string with units | ||
| 65 | * @param {number} bytes - The size in bytes to format | ||
| 66 | * @returns {string} The formatted string (e.g., "1.5 MB") | ||
| 67 | */ | ||
| 68 | function formatBytes(bytes) { | ||
| 69 | if (bytes === 0) return '0 B'; | ||
| 70 | const k = 1024; | ||
| 71 | const sizes = ['B', 'KB', 'MB', 'GB']; | ||
| 72 | const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
| 73 | return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
| 74 | } | 71 | } |
| 75 | 72 | ||
| 76 | /** | 73 | /** |
| @@ -710,7 +707,7 @@ router.post('/search', jsonParser, function (request, response) { | |||
| 710 | } | 707 | } |
| 711 | 708 | ||
| 712 | // Sort by last message date descending | 709 | // Sort by last message date descending |
| 713 | results.sort((a, b) => new Date(b.last_mes) - new Date(a.last_mes)); | 710 | results.sort((a, b) => new Date(b.last_mes).getTime() - new Date(a.last_mes).getTime()); |
| 714 | return response.send(results); | 711 | return response.send(results); |
| 715 | 712 | ||
| 716 | } catch (error) { | 713 | } catch (error) { |
| @@ -143,6 +143,19 @@ export function getHexString(length) { | |||
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | /** | 145 | /** |
| 146 | * Formats a byte size into a human-readable string with units | ||
| 147 | * @param {number} bytes - The size in bytes to format | ||
| 148 | * @returns {string} The formatted string (e.g., "1.5 MB") | ||
| 149 | */ | ||
| 150 | export function formatBytes(bytes) { | ||
| 151 | if (bytes === 0) return '0 B'; | ||
| 152 | const k = 1024; | ||
| 153 | const sizes = ['B', 'KB', 'MB', 'GB']; | ||
| 154 | const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
| 155 | return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
| 156 | } | ||
| 157 | |||
| 158 | /** | ||
| 146 | * Extracts a file with given extension from an ArrayBuffer containing a ZIP archive. | 159 | * Extracts a file with given extension from an ArrayBuffer containing a ZIP archive. |
| 147 | * @param {ArrayBuffer} archiveBuffer Buffer containing a ZIP archive | 160 | * @param {ArrayBuffer} archiveBuffer Buffer containing a ZIP archive |
| 148 | * @param {string} fileExtension File extension to look for | 161 | * @param {string} fileExtension File extension to look for |