Line data Source code
1 : #include "MessageHandler.h"
2 :
3 :
4 : using std::string;
5 :
6 :
7 177 : MessageHandler::MessageHandler(server_databases_t *databases) :
8 354 : usersDB(new DatabaseManager(databases->usersDB)), chatDB(new ChatDatabaseManager(databases->chatDB)),
9 531 : likesDB(new LikesDatabaseManager(databases->likesDB)) {
10 177 : username = "";
11 176 : ssClient = NULL;
12 176 : this->tokenizer = new Tokenizer(usersDB);
13 177 : }
14 :
15 52 : void MessageHandler::setUser(string name){
16 : //TODO parse username to take @ and .
17 52 : makeUsername(name);
18 52 : LOGG(DEBUG) << "Set user of msgHandler to " << name;
19 52 : username = name;
20 52 : }
21 :
22 177 : void MessageHandler::setSharedLink(std::string linkToShared){
23 177 : if (ssClient == NULL){
24 177 : ssClient = new SharedServerClient(linkToShared);
25 : }
26 : else{
27 0 : delete ssClient;
28 0 : ssClient = new SharedServerClient(linkToShared);
29 : }
30 177 : }
31 :
32 0 : bool MessageHandler::isUserSet(){
33 0 : return (! username.empty());
34 : }
35 :
36 352 : MessageHandler::~MessageHandler() {
37 177 : if ( ssClient != NULL){
38 177 : delete ssClient;
39 : }
40 176 : delete tokenizer;
41 177 : }
42 :
43 8 : void MessageHandler::updateUsersWithSS(){
44 8 : string usersJson;
45 8 : ssClient->getUsers(&usersJson);
46 16 : CsvParser csvParser;
47 16 : JsonParser jsonParser;
48 16 : UserParser userParser;
49 :
50 16 : list<User*> users = userParser.JsonToList(usersJson);
51 8 : LOGG(DEBUG) << "Updating username - id of SS users";
52 672 : for (User* user : users){
53 1328 : string username, id_aux;
54 664 : username = user->getMail();
55 664 : makeUsername(username);//TODO parse email
56 664 : if (! usersDB->getEntry(USER_ID_DB + username, id_aux)){
57 88 : usersDB->addEntry(USER_DB + username, DEFAULT_PASS);
58 88 : usersDB->addEntry(USER_ID_DB + username, to_string(user->getID()));
59 : }
60 664 : }
61 :
62 680 : while (!users.empty()) {
63 664 : delete users.front();
64 664 : users.front() = NULL;
65 664 : users.pop_front();
66 8 : }
67 :
68 8 : }
69 :
70 34 : string MessageHandler::getId(){
71 34 : string id;
72 34 : if (usersDB->getEntry(USER_ID_DB + username, id))
73 34 : return id;
74 0 : return "";
75 : }
76 0 : void MessageHandler::setUsersDB(DatabaseManager * usersDB) {
77 0 : this->usersDB = usersDB;
78 0 : }
79 :
80 2 : bool MessageHandler::getUsers(std::string& resultMsg) {
81 2 : string usersJson;
82 2 : ssClient->getUsers(&usersJson);
83 4 : CsvParser csvParser;
84 4 : JsonParser jsonParser;
85 4 : UserParser userParser;
86 :
87 4 : list<User*> users = userParser.JsonToList(usersJson);
88 :
89 4 : string currentUserData;
90 :
91 2 : if (! this->getUser(username,currentUserData)){
92 0 : LOGG(WARNING) << "User does not exist in SS";
93 0 : return false;
94 : }
95 :
96 4 : User currentUser;
97 2 : csvParser.makeUser(currentUserData,currentUser);
98 :
99 4 : string userMatches;
100 2 : getInteractions(userMatches);
101 :
102 4 : UserMatcher matcher;
103 :
104 4 : list<User*> filtered_users = matcher.filterPossibleMatches(¤tUser, &users, userMatches);
105 :
106 2 : resultMsg = userParser.ListToCsv(filtered_users);
107 :
108 162 : while(!users.empty()){
109 158 : delete users.front();
110 158 : users.front() = NULL;
111 158 : users.pop_front();
112 : }
113 :
114 4 : return true;
115 : }
116 :
117 19 : bool MessageHandler::authenticate(string username, string password) {
118 19 : LOGG(DEBUG) << "Authenticating " + username;
119 19 : makeUsername(username);
120 19 : bool found = usersDB->correctEntry(USER_DB + username, password);
121 19 : if (!found){
122 2 : this->updateUsersWithSS();
123 2 : if (! usersDB->correctEntry(USER_DB + username, password)){
124 2 : LOGG(DEBUG) << "Incorrect Username-Password";
125 2 : return false;
126 : }else{
127 : return true;
128 17 : }
129 : }
130 : return true;
131 12 : }
132 12 :
133 12 : void MessageHandler::createInterests(Interests* interests){
134 27 : map<string,vector<string>> inter = interests->allInterests();
135 15 : map<string,vector<string>>::iterator it_c = inter.begin();
136 15 : for(; it_c != inter.end(); ++it_c){
137 15 : LOGG(DEBUG) << "Creating interests of: " << it_c->first;
138 34 : vector<string> values = it_c->second;
139 19 : vector<string>::iterator it_v = values.begin();
140 19 : for (;it_v != values.end(); ++it_v){
141 38 : LOGG(DEBUG) << "Iterating value: " << (*it_v);
142 38 : Json::Value interest;
143 19 : Json::Value metadata;
144 19 : Json::Value to_post;
145 19 : interest[CATEGORY_KEY] = it_c->first;
146 19 : interest[VALUE_KEY] = (*it_v);
147 19 : to_post[INTEREST_KEY] = interest;
148 19 : metadata[VERSION_KEY] = VERSION_VALUE;
149 38 : metadata[COUNT_KEY] = 1;
150 38 : to_post[META_KEY] = metadata;
151 19 : JsonParser json;
152 19 : string formated_data = json.getAsString(to_post);
153 27 : ssClient->postUsersInterests(&formated_data);
154 : }
155 12 : }
156 8 :
157 8 : }
158 8 : bool MessageHandler::createUser(string user_data, std::string pass) {
159 : string aux_pass;
160 8 : LOGG(DEBUG)<< "Creating user from data: " + user_data;
161 1 :
162 1 : if ( usersDB->getEntry(USER_DB + username, aux_pass)) {
163 : LOGG(DEBUG)<< "User Already Existed";
164 : throw ExistentUserException();
165 14 : }
166 14 :
167 14 : CsvParser csvParser;
168 14 : JsonParser jsonParser;
169 14 : UserParser userParser;
170 7 : User new_user;
171 0 : string user_created;
172 0 : if (! csvParser.makeSignupUser(user_data, new_user)){
173 : LOGG(WARNING) << "Bad data for creating user";
174 7 : return false;
175 14 : }
176 14 : createInterests(new_user.getInterests());
177 7 : Json::Value jsonUser = jsonParser.userToJson(&new_user);
178 7 : Json::Value data_to_post;
179 : data_to_post[META_KEY][VERSION_KEY] = VERSION_VALUE;
180 14 : data_to_post[USER_KEY] = jsonUser;
181 7 :
182 7 : string parsed_user = jsonParser.getAsString(data_to_post);
183 : LOGG(INFO) << "Creating user " + parsed_user;
184 7 : bool posted = ssClient->postUsers(user_created, &parsed_user);
185 14 :
186 7 : if (posted){
187 14 : string id, mail, user_csv;
188 7 : jsonParser.parsing(user_created);
189 : Json::Value json_user =jsonParser.getValue(USER_KEY);
190 7 : id = json_user[ID_KEY].asString();
191 :
192 7 : userParser.JsonToCsvFull(json_user, user_csv, new_user.getDescription());
193 7 :
194 7 : usersDB->addEntry(USER_DB + username, pass);
195 : usersDB->addEntry(USER_LOOKING_DB + username, new_user.getDescription());
196 7 : usersDB->addEntry(USER_ID_DB + username, id);
197 14 :
198 : LOGG(DEBUG) << "User signup New Id: " + id;
199 0 : return true;
200 0 : }else{
201 8 : LOGG(DEBUG) << "User not created";
202 : return false;
203 : }
204 4 : }
205 4 :
206 4 : bool MessageHandler::updateUser(string user_data) {
207 8 : LOGG(DEBUG) << "Updating user with: " << user_data;
208 8 : CsvParser csvParser;
209 8 : JsonParser jsonParser;
210 8 : UserParser userParser;
211 : User new_user;
212 4 : string id = this->getId(), base_user, photo;
213 0 :
214 0 : if ( ! this->ssClient->getUser(id, &base_user) ){
215 : LOGG(WARNING) << "Wanting to update unexistant user " << username;
216 : return false;
217 4 : }
218 4 : //save photo
219 : jsonParser.parsing(base_user);
220 : photo = jsonParser.getValue(USER_KEY)[PHOTO_KEY].asString();
221 4 :
222 : //make csv base User
223 4 : userParser.JsonToCsvFull(jsonParser.getValue(USER_KEY),base_user,"");
224 1 :
225 1 : if (! csvParser.makePutUser(user_data, base_user, new_user)){
226 : LOGG(WARNING) << "Bad format of user data to modify";
227 3 : return false;
228 6 : }
229 6 : this->createInterests(new_user.getInterests());
230 3 : Json::Value jsonUser = jsonParser.userToJson(&new_user, true);
231 3 : Json::Value data_to_post;
232 3 : jsonUser[PHOTO_KEY]= photo;
233 3 : data_to_post[META_KEY][VERSION_KEY] = VERSION_VALUE;
234 : data_to_post[USER_KEY] = jsonUser;
235 3 : LOGG(DEBUG) << "Updating info of " + id ;
236 :
237 7 : bool changed = ssClient->changeUser(id, jsonParser.getAsString(data_to_post).c_str());
238 : //ya no cambia descripcion
239 : return changed;
240 7 : }
241 14 :
242 14 : bool MessageHandler::deleteUser() {
243 : string id = this->getId(), reply;
244 7 : string token;
245 7 :
246 7 : if ( ssClient->deleteUser(id, &reply )){
247 7 : usersDB->deleteEntry(USER_DB + username);
248 7 : usersDB->deleteEntry(USER_ID_DB + username);
249 1 : usersDB->deleteEntry(USER_LOOKING_DB + username);
250 7 : if (usersDB->getEntry(TOKEN_OF_USER_DB + username, token))
251 : usersDB->deleteEntry(USER_OF_TOKEN_DB + token);
252 7 : usersDB->deleteEntry(TOKEN_OF_USER_DB + username);
253 7 : //TODO: Delete matches, friends, y si ambos no estan chat
254 : LOGG(INFO) << "User Deleted " + username;
255 7 : return true;
256 : }
257 : return false;
258 5 : }
259 5 :
260 : bool MessageHandler::getInterestPhoto(std::string& photo_64, std::string id_interest) {
261 : return ssClient->getInterestPhoto(id_interest, photo_64);
262 1 : }
263 1 :
264 : bool MessageHandler::getChat(std::string friend_username, string& chat_history) {
265 1 : LOGG(INFO) << "Fetching chat history from " << username << " and " << friend_username;
266 0 : //TODO DELETE NEW MESSAGES FROM THIS CONVERSATION.
267 0 : return chatDB->getHistory(username, friend_username, chat_history);
268 2 : }
269 2 :
270 2 : bool MessageHandler::postChatMsg(string receiverUserName, string message){
271 2 : LOGG(INFO) << "Posting message " << message << " from " << username << " to " << receiverUserName;
272 0 : chatDB->saveMessage(message,username,receiverUserName);
273 0 : return true;
274 2 : }
275 2 :
276 4 : bool MessageHandler::getNewMessages(string friend_name, string& newMessages){
277 6 : LOGG(INFO) << "Fetching new messages from " << username << " and " << friend_name;
278 2 : chatDB->getNewMsgs(friend_name, username, newMessages);
279 2 : return true;
280 1 : }
281 4 :
282 1 : bool MessageHandler::getPhoto(std::string other_username, string& photo_64) {
283 3 : string user_id, photo;
284 0 : LOGG(DEBUG)<<"Getting user for " << other_username;
285 1 : makeUsername(other_username);
286 :
287 1 : bool userExists = usersDB->getEntry(USER_ID_DB + other_username, user_id);
288 4 :
289 0 : if (! userExists) {
290 : LOGG(WARNING)<<"Wanted to get photo of unexistant: " << other_username;
291 2 : return false;
292 4 : }
293 2 :
294 2 : return ssClient->getUserPhoto(user_id, photo_64);
295 1 : }
296 1 :
297 1 : bool MessageHandler::postPhoto(string photo_64) {
298 1 : LOGG(DEBUG) << "Updating photo of " + this->getId();
299 : if (ssClient->changeUserPhoto(this->getId(), photo_64)){
300 0 : return true;
301 2 : }
302 2 : LOGG(DEBUG) << "Could not update photo";
303 2 : return false;
304 2 : }
305 24 :
306 24 :
307 24 : bool MessageHandler::validateToken(std::string user_token) {
308 22 : bool expired = this->tokenizer->hasExpired(user_token);
309 20 : if (! expired ){
310 19 : string user;
311 19 : if (this->usersDB->getEntry(USER_OF_TOKEN_DB + user_token,user)){
312 19 : LOGG(DEBUG) << "Token corresponds to " << user;
313 0 : this->setUser(user);
314 1 : return true;
315 1 : }
316 : LOGG(DEBUG)<<"Inexistant user for token";
317 2 : return false;
318 2 : }else{
319 2 : LOGG(DEBUG)<<"Token expired";
320 2 : return false;
321 2 : }
322 2 :
323 0 : }
324 4 :
325 4 : void MessageHandler::getInteractions(std::string& interactedUsersData) {
326 4 : LOGG(INFO) << "Devolviendo likes/rechazos de " << username;
327 0 : string interactedUsers;
328 0 : likesDB->getInteractedUsers(username, interactedUsers);
329 0 :
330 2 : istringstream f(interactedUsers);
331 2 : string interactedUserName;
332 0 : while (getline(f, interactedUserName)) {
333 0 : string interactedUserData;
334 0 : getUser(interactedUserName, interactedUserData);
335 0 : interactedUsersData.append(interactedUserData + "\n");
336 0 : }
337 : }
338 3 :
339 5 :
340 3 : void MessageHandler::getMatches(std::string& matches) {
341 3 : LOGG(INFO) << "Fetching matches from " << username;
342 : likesDB->getMatches(username, matches);
343 18 : }
344 32 :
345 16 : string MessageHandler::getToken() {
346 16 : string password, token, pastToken;
347 : LOGG(DEBUG) << "Generating new token for " + username;
348 2 : if (!this->usersDB->getEntry(USER_DB + username, password))
349 1 : {
350 : LOGG(WARNING) << "User has no registered pass";
351 : }
352 2 :
353 16 : //si ya tiene un token asociado lo remuevo antes
354 15 : //de pedir el nuevo por si son iguales (en el mismo segundo)
355 2 : if (this->usersDB->getEntry(TOKEN_OF_USER_DB + username, pastToken)) {
356 2 : this->tokenizer->remove(pastToken);
357 16 : }
358 2 :
359 4 : token = this->tokenizer->newToken(username, password);
360 16 :
361 16 : //renuevo la referencia token-user
362 : this->usersDB->deleteEntry(USER_OF_TOKEN_DB + pastToken);
363 18 : this->usersDB->addEntry(USER_OF_TOKEN_DB + token, username);
364 34 :
365 2 : this->usersDB->addEntry(TOKEN_OF_USER_DB + username, token);
366 4 : return token;
367 2 :
368 17 : }
369 15 :
370 15 : bool MessageHandler::addLocalization(string localization) {
371 30 : LOGG(DEBUG) << "Changing " << localization << " for " << username;
372 15 : string latitude;
373 19 : string longitude;
374 4 : split(localization, latitude, longitude);
375 4 : if ( !isFloat(latitude) || !isFloat(longitude)){
376 4 : LOGG(DEBUG) << "Localization without floats. Latitude: " << latitude << " - longitude: " << longitude;
377 : return false;
378 32 : }
379 30 : //TODO: Refactor!
380 30 : JsonParser jsonParser;
381 30 : UserParser userParser;
382 : User new_user;
383 17 : string id = this->getId(), base_user, photo;
384 2 :
385 0 : if ( ! this->ssClient->getUser(id, &base_user) ) {
386 : LOGG(WARNING) << "Wanting to login unexistant user " << username;
387 2 : return false;
388 15 : }
389 19 : //save photo
390 2 : jsonParser.parsing(base_user);
391 : photo = jsonParser.getValue(USER_KEY)[PHOTO_KEY].asString();
392 17 :
393 0 : //make csv base User
394 30 : userParser.JsonToCsvFull(jsonParser.getValue(USER_KEY),base_user,"");
395 15 :
396 : Json::Value jsonUser;
397 19 : userParser.CsvToJsonFull(base_user,jsonUser,1);
398 2 :
399 2 : if (id.compare(jsonUser[ID_KEY].asString()) != 0){
400 2 : LOGG(WARNING) << "Id in SS does not match with server";
401 2 : return false;
402 32 : }
403 19 :
404 17 : Json::Value new_localization;
405 17 : new_localization[LATITUDE_KEY] = stof(latitude);
406 15 : new_localization[LONGITUDE_KEY] = stof(longitude);
407 17 : jsonUser[LOCATION_KEY] = new_localization;
408 34 : jsonUser[ID_KEY] = stoi(id);
409 15 : jsonUser[PHOTO_KEY] = photo;
410 15 : Json::Value data_to_post;
411 4 : data_to_post[META_KEY][VERSION_KEY] = VERSION_VALUE;
412 15 : data_to_post[USER_KEY] = jsonUser;
413 34 :
414 4 : LOGG(DEBUG) << "Updating info of " + id ;
415 4 : return ssClient->changeUser(id, jsonParser.getAsString(data_to_post));
416 4 : }
417 4 :
418 6 : bool MessageHandler::getUser(string username, string &user_data) {
419 4 : //busca en DB
420 4 : LOGG(DEBUG) << "Getting user " << username;
421 4 : makeUsername(username);
422 6 : string id;
423 3 : //check and update if not found
424 : if ( ! usersDB->getEntry(USER_ID_DB + username, id)) {
425 : this->updateUsersWithSS();
426 4 : }
427 3 :
428 3 : if ( ! usersDB->getEntry(USER_ID_DB + username, id)) {
429 2 : LOGG(DEBUG) << "Does not exist in database";
430 2 : return false;
431 4 : }
432 7 :
433 5 : //busca en SHARED
434 5 : bool ok = ssClient->getUser(id, &user_data);
435 5 : if (ok) {
436 10 : LOGG(DEBUG) << "Got user from shared";
437 6 : UserParser parser;
438 3 : JsonParser json;
439 3 : string copy(user_data), lookingFor = "";
440 5 : json.parsing(copy);
441 6 : usersDB->getEntry(USER_LOOKING_DB + username, lookingFor);
442 : LOGG(DEBUG)<< "Parsing user: " << json.getAsString(json.getValue(USER_KEY)[MAIL_KEY]);
443 2 : parser.JsonToCsvFull(json.getValue(USER_KEY), user_data, lookingFor);
444 2 : } else {
445 5 : LOGG(DEBUG) << "Does not exist in shared";
446 : }
447 2 : return ok;
448 17 : }
449 15 :
450 15 : void MessageHandler::split(string values, string& value1, string& value2){
451 : size_t i = values.find(DB_SEPARATOR);
452 15 : value1 = values.substr(0, i);
453 15 : //TODO Revisar el ultimo indice.
454 : value2 = values.substr(i+1, values.length());
455 2 : }
456 0 :
457 2 : string MessageHandler::getReactionToken(string reaction){
458 0 : //TODO CHECK THE VALUE THAT COMES IN STRING!
459 0 : return (reaction == "true") ? LIKED_TOKEN : DISLIKED_TOKEN;
460 2 : }
461 2 :
462 2 : bool MessageHandler::postInteraction(string friend_name, string reaction){
463 2 : LOGG(INFO) << "Recieved reaction " << reaction << " from " << username << " to " << friend_name;
464 2 : string reactionToken = getReactionToken(reaction);
465 6 : likesDB->saveLike(username, friend_name, reactionToken);
466 : return true;
467 2 : }
468 2 :
469 2 : bool MessageHandler::getNewMatches(string& newMatches){
470 9 : likesDB->getNewMatches(username, newMatches);
471 : return true;
472 : }
473 :
474 :
475 :
476 :
|