* @brief Writes m_chats / m_lastActive back to the index file; best effort. */
| 284 | * @brief Writes m_chats / m_lastActive back to the index file; best effort. |
| 285 | */ |
| 286 | void AI::ChatStore::writeIndex() const |
| 287 | { |
| 288 | QJsonArray arr; |
| 289 | for (const auto& meta : m_chats) { |
| 290 | QJsonObject obj; |
| 291 | obj[QStringLiteral("id")] = meta.id; |
| 292 | obj[QStringLiteral("title")] = meta.title; |
| 293 | obj[QStringLiteral("createdAt")] = static_cast<double>(meta.createdAt); |
| 294 | obj[QStringLiteral("updatedAt")] = static_cast<double>(meta.updatedAt); |
| 295 | obj[QStringLiteral("count")] = meta.count; |
| 296 | arr.append(obj); |
| 297 | } |
| 298 | |
| 299 | QJsonObject root; |
| 300 | root[QStringLiteral("schema")] = 1; |
| 301 | root[QStringLiteral("lastActive")] = m_lastActive; |
| 302 | root[QStringLiteral("chats")] = arr; |
| 303 | |
| 304 | QDir().mkpath(chatsDir()); |
| 305 | QFile f(indexPath()); |
| 306 | if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 307 | qCWarning(serialStudioAI) << "ChatStore: cannot write index" << f.errorString(); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | f.write(QJsonDocument(root).toJson(QJsonDocument::Compact)); |
| 312 | f.close(); |
| 313 | } |