
    si%                        d Z ddlmZ ddlmZ ddlmZ ddlmZ	 ddl
mZ ddlmZ ddlmZmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z eee      d        Z eed      d        Zy)z
chat/models.py

Models for the In-App Communication/Chat System.
Supports direct messaging, group chats, file sharing, and emoji reactions.
    )models)
pre_delete)receiver)gettext_lazy)Company)Employee)HorillaModelupload_pathc            	          e Zd ZdZd ed      fd ed      fgZ ej                  ddd ed       ed	      
      Z ej                  ded ed            Z	 ej                  dd ed            Z ej                  edd ed            Z ej                  eej"                  dd ed      d      Z G d d      Zd ZddZddZd Zd Zy)ChatRoomzV
    Represents a chat room - can be either a direct message (1:1) or group chat.
    directzDirect Messagegroupz
Group Chat   Tz	Room Namez0Name of the chat room (required for group chats))
max_lengthnullblankverbose_name	help_text
   z	Room Typer   choicesdefaultr   Descriptionr   r   r   z
Room Image)	upload_tor   r   r   r   
chat_rooms)	on_deleter   r   r   related_namec                   2    e Zd Z ed      Z ed      ZdgZy)ChatRoom.Meta	Chat Roomz
Chat Rooms-created_atN__name__
__module____qualname___r   verbose_name_pluralordering     //var/www/python-projects/worksol/chat/models.pyMetar    =   s    ~o!?r+   r-   c                    | j                   dk(  r| j                  xs d| j                   S | j                  j	                         d d }|j                         dk(  r!|d   j                   d|d   j                   S d| j                   S )Nr   Group #   r   z &    zChat Room #)	room_typenamepkmembersallcountemployee_id)selfr5   s     r,   __str__zChatRoom.__str__B   s    >>W$993'$'' 33,,""$Ra(==?aaj,,-S1G1G0HIITWWI&&r+   Nc                     | j                   dk(  r| j                  xs d| j                   S |rF| j                  j	                  |      j                         }|r|j                  j                         S t        |       S )zu
        Get display name for the chat room.
        For direct chats, returns the other participant's name.
        r   r/   r8   )	r2   r3   r4   r5   excludefirstr8   get_full_namestrr9   current_employeeother_members      r,   get_display_namezChatRoom.get_display_nameJ   sp    
 >>W$993'$'' 33<<//<L/MSSUL#//==??4yr+   c                    | j                   dk(  r"| j                  r| j                  j                  S |rU| j                   dk(  rF| j                  j	                  |      j                         }|r|j                  j                         S y)zZ
        Get room image URL. For direct chats, returns the other person's avatar.
        r   r   r<   N)r2   
room_imageurlr5   r=   r>   r8   
get_avatarrA   s      r,   get_room_imagezChatRoom.get_room_imageW   sp     >>W$??&&&( :<<//<L/MSSUL#//::<<r+   c                 T    | j                   j                  d      j                         S )z.Get the most recent message in this chat room.r"   )messagesorder_byr>   r9   s    r,   get_last_messagezChatRoom.get_last_messagec   s     }}%%m4::<<r+   c                 T   | j                   j                  |      j                         }|rP|j                  rD| j                  j                  |j                        j                  |      j                         S |r*| j                  j                  |      j                         S y)z5Get count of unread messages for a specific employee.r<   )created_at__gtsenderr   )r5   filterr>   last_read_atrK   r=   r7   )r9   employeemembers      r,   get_unread_countzChatRoom.get_unread_countg   s    $$$:@@Bf))==''v7J7J'KSS T eg ==(((9??AAr+   N)r$   r%   r&   __doc__r'   ROOM_TYPE_CHOICESr   	CharFieldr3   r2   	TextFielddescription
ImageFieldr
   rF   
ForeignKeyr   CASCADE
company_idr-   r:   rD   rI   rN   rW   r*   r+   r,   r   r      s   
 
1%&'	!L/"
 6{^FGD !  !{^	I #&""}%K
 #""|_	J #""..y\!J# #
'
=	r+   r   c                      e Zd ZdZd ed      fd ed      fgZ ej                  eej                  d ed            Z
 ej                  eej                  d	 ed
            Z ej                  ded ed            Z ej                  dd ed            Z ej"                  d ed            Z G d d      Zd Zy)ChatRoomMemberz/
    Represents membership in a chat room.
    adminAdminrV   Memberr5   r!   r   r   r   chat_membershipsr   r   Roler   TzLast Read Atr   FzIs Mutedr   r   c                   4    e Zd Z ed      Z ed      ZddgZy)ChatRoomMember.MetazChat Room MemberzChat Room Members	chat_roomr8   Nr$   r%   r&   r'   r   r(   unique_togetherr*   r+   r,   r-   rl      s#    +, 34&6r+   r-   c                 8    | j                    d| j                   S )N in )r8   rm   rM   s    r,   r:   zChatRoomMember.__str__   s    ""#4'788r+   N)r$   r%   r&   rY   r'   ROLE_CHOICESr   r_   r   r`   rm   r   r8   r[   roleDateTimeFieldrT   BooleanFieldis_mutedr-   r:   r*   r+   r,   rc   rc   s   s    
 
!G*	1X;L
 "!!..{^	I $&##..'z]	K 6vY	D (6''~&L
 #v""z]H
7 7
9r+   rc   c            
       B   e Zd ZdZd ed      fd ed      fd ed      fd ed	      fgZ ej                  eej                  d
 ed            Z
 ej                  eej                  d ed            Z ej                  dd ed            Z ej                  ded ed            Z ej                  dej"                  ddd ed            Z ej&                  d ed            Z ej*                  dd ed            Z G d d      Zd Zd  Zd! Zy")#ChatMessagez.
    Represents a message in a chat room.
    textTextimageImagefileFilesystemSystemrK   r!   rg   sent_messagesSenderTContentr   r   zMessage Typer   r9   replieszReply To)r   r   r   r   r   Fz	Is Editedrj   z	Edited Atc                   2    e Zd Z ed      Z ed      ZdgZy)ChatMessage.MetazChat MessagezChat Messages
created_atNr#   r*   r+   r,   r-   r      s    (0 >r+   r-   c                 :    d| j                    d| j                   S )NzMessage from rq   )rR   rm   rM   s    r,   r:   zChatMessage.__str__   s    t{{m4/?@@r+   c                 6    | j                   j                         S )zGet sender's avatar URL.)rR   rH   rM   s    r,   get_sender_avatarzChatMessage.get_sender_avatar   s    {{%%''r+   c                 6    | j                   j                         S )zGet sender's full name.)rR   r?   rM   s    r,   get_sender_namezChatMessage.get_sender_name   s    {{((**r+   N)r$   r%   r&   rY   r'   MESSAGE_TYPE_CHOICESr   r_   r   r`   rm   r   rR   r\   contentr[   message_typeSET_NULLreply_toru   	is_editedrt   	edited_atr-   r:   r   r   r*   r+   r,   rx   rx      s^   
 
6	!G*	6	1X;	 "!!..{^	I V..$x[	F fy\G
 $6##$~&	L !v  //z]H $##{^I %$${^I" "
A(+r+   rx   c                       e Zd ZdZd ed      fd ed      fgZ ej                  eej                  d ed            Z
 ej                  e ed      	      Z ej                  d
 ed            Z ej                   d ed            Z ej                  ddd ed            Z ej                  ded ed            Z G d d      Zd Zd Z fdZ xZS )ChatAttachmentzB
    Represents a file or image attachment in a chat message.
    r{   r|   r}   r~   attachmentsMessagerg   )r   r   r   z	File Namer   r   r   zFile Size (bytes)rj   d   Tz	File Type)r   r   r   r   r   zAttachment Typer   c                   ,    e Zd Z ed      Z ed      Zy)ChatAttachment.MetazChat AttachmentzChat AttachmentsN)r$   r%   r&   r'   r   r(   r*   r+   r,   r-   r     s    *+ 23r+   r-   c                     | j                   S rX   )	file_namerM   s    r,   r:   zChatAttachment.__str__  s    ~~r+   c                 4     g d}t         fd|D              S )z Check if attachment is an image.)z.jpgz.jpegz.pngz.gifz.webpz.svgc              3   p   K   | ]-  }j                   j                         j                  |       / y wrX   )r   lowerendswith).0extr9   s     r,   	<genexpr>z*ChatAttachment.is_image.<locals>.<genexpr>  s)     TC4>>'')2237Ts   36)any)r9   image_extensionss   ` r,   is_imagezChatAttachment.is_image  s    MTCSTTTr+   c                 b    | j                         rd| _        nd| _        t        |   |i | y)z,Set attachment_type based on file extension.r{   r}   N)r   attachment_typesupersave)r9   argskwargs	__class__s      r,   r   zChatAttachment.save  s-    ==?#*D #)D d%f%r+   )r$   r%   r&   rY   r'   ATTACHMENT_TYPE_CHOICESr   r_   rx   r`   message	FileFieldr
   r}   r[   r   PositiveIntegerField	file_size	file_typer   r-   r:   r   r   __classcell__)r   s   @r,   r   r      s   
 
!G*	6
  f.."y\	G 6vYD !  {^I ,++*+I !  {^	I 'f&&'()	O4 4U
& &r+   r   c                       e Zd ZdZ ej
                  eej                  d ed            Z	 ej
                  e
ej                  d ed            Z ej                  d ed      	      Z G d
 d      Zd Zy)MessageReactionz4
    Represents an emoji reaction to a message.
    	reactionsr   rg   message_reactionsr   2   Emojir   c                   4    e Zd Z ed      Z ed      Zg dZy)MessageReaction.MetazMessage ReactionzMessage Reactions)r   r8   emojiNrn   r*   r+   r,   r-   r   <  s    +, 34=r+   r-   c                 f    | j                    d| j                   d| j                  j                   S )Nz	 reacted z to message #)r8   r   r   r4   rM   s    r,   r:   zMessageReaction.__str__A  s-    ""#9TZZLdllooEVWWr+   N)r$   r%   r&   rY   r   r_   rx   r`   r'   r   r   r8   r[   r   r-   r:   r*   r+   r,   r   r   &  s      f.. y\	G $&##..(z]	K FwZE
> >
Xr+   r   rQ   c                 v    |j                   r	 |j                   j                  d       yy# t        $ r Y yw xY w)z6Delete the room image file when a ChatRoom is deleted.Fr   N)rF   delete	ExceptionrR   instancer   s      r,   delete_chat_room_imager   F  sB     	&&E&2   		   , 	88zchat.ChatAttachmentc                 v    |j                   r	 |j                   j                  d       yy# t        $ r Y yw xY w)z<Delete the attachment file when a ChatAttachment is deleted.Fr   N)r}   r   r   r   s      r,   delete_attachment_filer   P  s>     }}	MM  e ,   		r   N)rY   	django.dbr   django.db.models.signalsr   django.dispatchr   django.utils.translationr   r'   base.modelsr   employee.modelsr   horilla.modelsr	   r
   r   rc   rx   r   r   r   r   r*   r+   r,   <module>r      s     / $ 6  $ 4^| ^B,9\ ,9^C+, C+L;&\ ;&|Xl X@ 
*X& ' 
*23 4r+   