1919from __future__ import annotations
2020
2121import datetime
22- import hashlib
2322import logging
2423from typing import Any
2524from typing import Dict
25+ from typing import List
2626from typing import Optional
2727
2828import Geohash
2929from PyMatcha .models .like import Like
3030from PyMatcha .models .match import Match
31+ from PyMatcha .models .message import Message
3132from PyMatcha .models .report import Report
3233from PyMatcha .models .tag import Tag
3334from PyMatcha .models .view import View
@@ -62,11 +63,6 @@ class User(Model):
6263 confirmed_on = Field (datetime .datetime , fmt = "%Y-%m-%d %H:%M:%S" )
6364 previous_reset_token = Field (str )
6465
65- def check_password (self , password : str ) -> bool :
66- logging .debug ("Checking password again {} hashed password" .format (self .id ))
67- _hash , salt = self .password .split (":" )
68- return _hash == hashlib .sha256 (salt .encode () + password .encode ()).hexdigest ()
69-
7066 @staticmethod
7167 def create (
7268 first_name : str ,
@@ -94,7 +90,7 @@ def create(
9490 pass
9591 else :
9692 logging .error ("Email {} taken" .format (email ))
97- raise ConflictError ("Email {} taken" .format (email ), "Use another email" )
93+ raise ConflictError ("Email {} taken. " .format (email ), "Use another email. " )
9894
9995 # Check username availability
10096 try :
@@ -103,21 +99,20 @@ def create(
10399 pass
104100 else :
105101 logging .error ("Username {} taken" .format (username ))
106- raise ConflictError ("Username {} taken" .format (username ), "Try another username" )
102+ raise ConflictError ("Username {} taken. " .format (username ), "Try another username. " )
107103
108104 # Check correct gender
109105 if gender not in ["male" , "female" , "other" ]:
110106 logging .error ("Gender must be male, female or other, not {}" .format (gender ))
111- raise ConflictError ("Gender must be male, female or other, not {}" .format (gender ), "Try again" )
107+ raise ConflictError ("Gender must be male, female or other, not {}. " .format (gender ))
112108
113109 # Check correct orientation
114110 if orientation not in ["heterosexual" , "homosexual" , "bisexual" , "other" ]:
115111 logging .error (
116- "Sexual Orientation must be heterosexual, homosexual, bisexual or other, not {}" .format (orientation )
112+ "Sexual Orientation must be heterosexual, homosexual, bisexual or other, not {}. " .format (orientation )
117113 )
118114 raise ConflictError (
119- "Sexual Orientation must be heterosexual, homosexual, bisexual or other, not {}" .format (orientation ),
120- "Try again" ,
115+ "Sexual Orientation must be heterosexual, homosexual, bisexual or other, not {}." .format (orientation )
121116 )
122117
123118 # Check correct geohash
@@ -163,7 +158,7 @@ def register(email: str, username: str, password: str, first_name: str, last_nam
163158 pass
164159 else :
165160 logging .debug ("Email {} taken" .format (email ))
166- raise ConflictError ("Email {} taken" .format (email ), "Use another email" )
161+ raise ConflictError ("Email {} taken. " .format (email ), "Use another email. " )
167162
168163 # Check username availability
169164 try :
@@ -172,7 +167,7 @@ def register(email: str, username: str, password: str, first_name: str, last_nam
172167 pass
173168 else :
174169 logging .error ("Username {} taken" .format (username ))
175- raise ConflictError ("Username {} taken" .format (username ), "Try another username" )
170+ raise ConflictError ("Username {} taken. " .format (username ), "Try another username. " )
176171
177172 # Encrypt password
178173 password = hash_password (password )
@@ -387,6 +382,108 @@ def get_matches(self):
387382 match_list .append (Match (match ))
388383 return match_list
389384
385+ def send_message (self , to_id , content ):
386+ Message .create (from_id = self .id , to_id = to_id , content = content )
387+
388+ def get_messages (self ) -> List [Message ]:
389+ with self .db .cursor () as c :
390+ c .execute (
391+ """
392+ SELECT
393+ messages.from_id as from_id,
394+ messages.to_id as to_id,
395+ messages.id as id,
396+ messages.timestamp as timestamp,
397+ messages.seen_timestamp as seen_timestamp,
398+ messages.content as content,
399+ messages.is_liked as is_liked,
400+ messages.is_seen as is_seen
401+ FROM messages
402+ INNER JOIN users on users.id = messages.from_id or users.id = messages.to_id
403+ WHERE users.id = CAST({} AS SIGNED)
404+ """ .format (
405+ self .id
406+ )
407+ )
408+ messages = c .fetchall ()
409+ message_list = []
410+ for message in messages :
411+ message_list .append (Message (message ))
412+ logging .debug ("Getting all messages sent or received by user {}" .format (self .id ))
413+ return message_list
414+
415+ def get_conversation_list (self ) -> List [Message ]:
416+ with self .db .cursor () as c :
417+ c .execute (
418+ """
419+ SELECT t1.*
420+ FROM messages AS t1
421+ INNER JOIN
422+ (
423+ SELECT
424+ LEAST(from_id, to_id) AS from_id,
425+ GREATEST(from_id, to_id) AS to_id,
426+ MAX(id) AS max_id
427+ FROM messages
428+ GROUP BY
429+ LEAST(from_id, to_id),
430+ GREATEST(from_id, to_id)
431+ ) AS t2
432+ ON LEAST(t1.from_id, t1.to_id) = t2.from_id AND
433+ GREATEST(t1.from_id, t1.to_id) = t2.to_id AND
434+ t1.id = t2.max_id
435+ WHERE t1.from_id = {0} OR t1.to_id = {0}
436+ """ .format (
437+ self .id
438+ )
439+ )
440+ conversations = c .fetchall ()
441+ conversation_list = []
442+ for last_message in conversations :
443+ conversation_list .append (Message (last_message ))
444+ return conversation_list
445+
446+ def get_messages_with_user (self , with_user_id ) -> List [Message ]:
447+ with self .db .cursor () as c :
448+ c .execute (
449+ """
450+ SELECT
451+ messages.from_id as from_id,
452+ messages.to_id as to_id,
453+ messages.id as id,
454+ messages.timestamp as timestamp,
455+ messages.seen_timestamp as seen_timestamp,
456+ messages.content as content,
457+ messages.is_liked as is_liked,
458+ messages.is_seen as is_seen
459+ FROM messages
460+ WHERE from_id=CAST({0} AS SIGNED) and to_id=CAST({1} AS SIGNED)
461+
462+ UNION ALL
463+
464+ SELECT messages.from_id as from_id,
465+ messages.to_id as to_id,
466+ messages.id as id,
467+ messages.timestamp as timestamp,
468+ messages.seen_timestamp as seen_timestamp,
469+ messages.content as content,
470+ messages.is_liked as is_liked,
471+ messages.is_seen as is_seen
472+ FROM messages
473+ WHERE from_id=CAST({1} AS SIGNED) and to_id=CAST({0} AS SIGNED)
474+ """ .format (
475+ self .id , with_user_id
476+ )
477+ )
478+ messages = c .fetchall ()
479+ message_list = []
480+ for message in messages :
481+ message_list .append (Message (message ))
482+ logging .debug (
483+ "Getting all messages between user {} and {} (Total: {})" .format (self .id , with_user_id , len (message_list ))
484+ )
485+ return message_list
486+
390487
391488def get_user (uid : Any [int , str ]) -> Optional [User ]:
392489 not_found = 0
@@ -417,6 +514,6 @@ def get_user(uid: Any[int, str]) -> Optional[User]:
417514 # If none of those worked, throw an error
418515 if not_found == 3 :
419516 logging .debug ("User {} not found." .format (uid ))
420- raise NotFoundError ("User {} not found." .format (uid ), "Try again with another uid" )
517+ raise NotFoundError ("User {} not found." .format (uid ))
421518 logging .debug ("Found user {} from {}" .format (f_user .id , uid ))
422519 return f_user
0 commit comments