* @brief Writes a chat's snapshot and refreshes its index metadata. */
| 180 | * @brief Writes a chat's snapshot and refreshes its index metadata. |
| 181 | */ |
| 182 | void AI::ChatStore::saveChat(const QString& id, |
| 183 | const QJsonObject& snapshot, |
| 184 | const QString& title, |
| 185 | int count) |
| 186 | { |
| 187 | const int idx = indexOf(id); |
| 188 | if (idx < 0) { |
| 189 | qCWarning(serialStudioAI) << "ChatStore: saveChat for unknown id" << id; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | QDir().mkpath(chatsDir()); |
| 194 | QFile f(chatPath(id)); |
| 195 | if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 196 | qCWarning(serialStudioAI) << "ChatStore: cannot write chat" << id << f.errorString(); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | f.write(QJsonDocument(snapshot).toJson(QJsonDocument::Compact)); |
| 201 | f.close(); |
| 202 | |
| 203 | m_chats[idx].updatedAt = QDateTime::currentMSecsSinceEpoch(); |
| 204 | m_chats[idx].count = count; |
| 205 | if (!title.isEmpty() && m_chats[idx].title.isEmpty()) |
| 206 | m_chats[idx].title = title; |
| 207 | |
| 208 | writeIndex(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @brief Deletes a chat's snapshot file and index entry. |