-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationManager.cpp
More file actions
304 lines (269 loc) · 10.1 KB
/
ConversationManager.cpp
File metadata and controls
304 lines (269 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Created by matin on 1/2/26.
//
#include "ConversationManager.h"
#include <sstream>
#include <iostream>
ConversationManager::ConversationManager(UserManager* um, const string& convFile, const string& msgFile)
: nextConversationId(1), conversationsFile(convFile), messagesFile(msgFile), userManager(um) {
loadFromFile();
}
ConversationManager::~ConversationManager() {
clear();
}
void ConversationManager::updateIdCounter() {
nextConversationId = 1;
for (const Conversation* conv : conversations) {
if (conv->getConversationId() >= nextConversationId) {
nextConversationId = conv->getConversationId() + 1;
}
}
}
void ConversationManager::resolveMessageSenderUsernames(Conversation* conv) {
for (Message* msg : const_cast<vector<Message*>&>(conv->getMessages())) {
string username = userManager->getUsernameById(msg->getSenderId());
msg->setSenderUsername(username);
}
}
void ConversationManager::loadFromFile() {
clear();
ifstream file(conversationsFile);
if (file.is_open()) {
string line;
while (getline(file, line)) {
if (line.empty()) continue;
try {
Conversation* conv = Conversation::deserialize(line);
if (conv) {
conversations.push_back(conv);
conversationMap[conv->getConversationId()] = conv;
}
} catch (...) {
// Skip
}
}
file.close();
}
updateIdCounter();
ifstream msgFile(messagesFile);
if (msgFile.is_open()) {
map<int, vector<Message*>> messagesByConv;
string line;
while (getline(msgFile, line)) {
if (line.empty()) continue;
try {
size_t colonPos = line.find(':');
if (colonPos == string::npos) continue;
int convId = stoi(line.substr(0, colonPos));
string msgData = line.substr(colonPos + 1);
Message* msg = Message::deserialize(msgData);
if (msg) {
messagesByConv[convId].push_back(msg);
}
} catch (...) {
// Skip
}
}
msgFile.close();
for (auto& pair : messagesByConv) {
auto it = conversationMap.find(pair.first);
if (it != conversationMap.end()) {
it->second->loadMessages(pair.second);
resolveMessageSenderUsernames(it->second);
} else {
for (Message* msg : pair.second) {
delete msg;
}
}
}
}
}
void ConversationManager::saveToFile() const {
ofstream file(conversationsFile);
if (!file.is_open()) {
throw FileIOException("Cannot open file for writing: " + conversationsFile);
}
for (const Conversation* conv : conversations) {
file << conv->serialize() << "\n";
}
file.close();
}
void ConversationManager::loadMessagesForConversation(Conversation* conv) {
ifstream file(messagesFile);
if (!file.is_open()) return;
vector<Message*> messages;
string line;
int convId = conv->getConversationId();
while (getline(file, line)) {
if (line.empty()) continue;
try {
size_t colonPos = line.find(':');
if (colonPos == string::npos) continue;
int lineConvId = stoi(line.substr(0, colonPos));
if (lineConvId != convId) continue;
string msgData = line.substr(colonPos + 1);
Message* msg = Message::deserialize(msgData);
if (msg) {
messages.push_back(msg);
}
} catch (...) {
// Skip
}
}
file.close();
conv->loadMessages(messages);
resolveMessageSenderUsernames(conv);
}
void ConversationManager::saveMessagesForConversation(const Conversation* conv) const {
saveAllMessages();
}
void ConversationManager::saveAllMessages() const {
ofstream file(messagesFile);
if (!file.is_open()) {
throw FileIOException("Cannot open file for writing: " + messagesFile);
}
for (const Conversation* conv : conversations) {
file << conv->serializeMessages();
}
file.close();
}
PrivateChat* ConversationManager::createPrivateChat(const string& user1, const string& user2) {
if (!userManager->usernameExists(user1)) {
throw InvalidUserException("User '" + user1 + "' does not exist");
}
if (!userManager->usernameExists(user2)) {
throw InvalidUserException("User '" + user2 + "' does not exist");
}
if (user1 == user2) {
throw DuplicateChatException("Cannot create a chat with yourself");
}
int user1Id = userManager->getUserIdByUsername(user1);
int user2Id = userManager->getUserIdByUsername(user2);
return createPrivateChatById(user1Id, user2Id);
}
PrivateChat* ConversationManager::createPrivateChatById(int user1Id, int user2Id) {
if (!userManager->userIdExists(user1Id)) {
throw InvalidUserException("User ID '" + to_string(user1Id) + "' does not exist");
}
if (!userManager->userIdExists(user2Id)) {
throw InvalidUserException("User ID '" + to_string(user2Id) + "' does not exist");
}
if (user1Id == user2Id) {
throw DuplicateChatException("Cannot create a chat with yourself");
}
if (privateChatsExistsById(user1Id, user2Id)) {
string user1 = userManager->getUsernameById(user1Id);
string user2 = userManager->getUsernameById(user2Id);
throw DuplicateChatException("Private chat between '" + user1 + "' and '" + user2 + "' already exists");
}
PrivateChat* chat = new PrivateChat(nextConversationId++, user1Id, user2Id);
conversations.push_back(chat);
conversationMap[chat->getConversationId()] = chat;
saveToFile();
return chat;
}
Conversation* ConversationManager::findById(int id) const {
auto it = conversationMap.find(id);
if (it != conversationMap.end()) {
return it->second;
}
return nullptr;
}
vector<Conversation*> ConversationManager::getConversationsForUser(const string& username) const {
int userId = userManager->getUserIdByUsername(username);
return getConversationsForUserId(userId);
}
vector<Conversation*> ConversationManager::getConversationsForUserId(int userId) const {
vector<Conversation*> result;
for (Conversation* conv : conversations) {
if (conv->hasParticipant(userId)) {
result.push_back(conv);
}
}
return result;
}
bool ConversationManager::privateChatsExists(const string& user1, const string& user2) const {
int user1Id = userManager->getUserIdByUsername(user1);
int user2Id = userManager->getUserIdByUsername(user2);
return privateChatsExistsById(user1Id, user2Id);
}
bool ConversationManager::privateChatsExistsById(int user1Id, int user2Id) const {
return findPrivateChatById(user1Id, user2Id) != nullptr;
}
PrivateChat* ConversationManager::findPrivateChat(const string& user1, const string& user2) const {
int user1Id = userManager->getUserIdByUsername(user1);
int user2Id = userManager->getUserIdByUsername(user2);
return findPrivateChatById(user1Id, user2Id);
}
PrivateChat* ConversationManager::findPrivateChatById(int user1Id, int user2Id) const {
for (Conversation* conv : conversations) {
if (conv->getConversationType() == "PRIVATE") {
PrivateChat* pc = dynamic_cast<PrivateChat*>(conv);
if (pc && pc->hasParticipant(user1Id) && pc->hasParticipant(user2Id)) {
return pc;
}
}
}
return nullptr;
}
string ConversationManager::getOtherParticipantUsername(PrivateChat* pc, int currentUserId) const {
int otherUserId = pc->getOtherParticipantId(currentUserId);
return userManager->getUsernameById(otherUserId);
}
vector<Conversation*> ConversationManager::searchByName(const string& username, const string& searchTerm) const {
int userId = userManager->getUserIdByUsername(username);
vector<Conversation*> result;
string lowerSearch = searchTerm;
transform(lowerSearch.begin(), lowerSearch.end(), lowerSearch.begin(), ::tolower);
for (Conversation* conv : conversations) {
if (!conv->hasParticipant(userId)) continue;
if (conv->getConversationType() == "PRIVATE") {
PrivateChat* pc = dynamic_cast<PrivateChat*>(conv);
if (pc) {
int otherUserId = pc->getOtherParticipantId(userId);
string otherUsername = userManager->getUsernameById(otherUserId);
string lowerOther = otherUsername;
transform(lowerOther.begin(), lowerOther.end(), lowerOther.begin(), ::tolower);
if (lowerOther.find(lowerSearch) != string::npos) {
result.push_back(conv);
}
}
}
}
return result;
}
vector<Conversation*> ConversationManager::sortByName(const string& username) const {
int userId = userManager->getUserIdByUsername(username);
vector<Conversation*> userConvs = getConversationsForUserId(userId);
sort(userConvs.begin(), userConvs.end(),
[this, userId](const Conversation* a, const Conversation* b) {
string nameA, nameB;
if (a->getConversationType() == "PRIVATE") {
const PrivateChat* pcA = dynamic_cast<const PrivateChat*>(a);
if (pcA) {
int otherIdA = pcA->getOtherParticipantId(userId);
nameA = userManager->getUsernameById(otherIdA);
}
}
if (b->getConversationType() == "PRIVATE") {
const PrivateChat* pcB = dynamic_cast<const PrivateChat*>(b);
if (pcB) {
int otherIdB = pcB->getOtherParticipantId(userId);
nameB = userManager->getUsernameById(otherIdB);
}
}
return nameA < nameB;
});
return userConvs;
}
void ConversationManager::addMessage(Conversation* conv, Message* msg) {
conv->addMessage(msg);
saveAllMessages();
}
void ConversationManager::clear() {
for (Conversation* conv : conversations) {
delete conv;
}
conversations.clear();
conversationMap.clear();
}