
    siP                     (   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZmZ dd
lmZmZmZmZmZ ddlm Z  ddl!m"Z"m#Z# ddl$m%Z% e#d        Z&e#d        Z'e#d        Z(e#e"d               Z)e#e"d               Z*e#e"d               Z+e#e"d               Z,e#e"d               Z-e#d        Z.e#d        Z/e#d        Z0e#d        Z1e#d        Z2e#ed               Z3e#ed               Z4e#d        Z5e#e"d               Z6y) z0
chat/views.py

Views for the Chat application.
    )messages)MaxQ)HttpResponseJsonResponse)get_object_or_404redirectrender)timezone)gettext_lazy)require_POST)AddMembersFormChatMessageFormChatRoomFormStartDirectChatForm)ChatAttachmentChatMessageChatRoomChatRoomMemberMessageReaction)Employee)hx_request_requiredlogin_required)notifyc           
         | j                   j                  }t        j                  j	                  |d      j                  t        d            j                  d      }g }|D ]T  }|j                  ||j                  |      |j                  |      |j                         |j                  |      d       V t        |       }t        |       }||||d}t        | d	|      S )
z7
    Main chat page showing list of conversations.
    Tmembers__employee_id	is_activemessages__created_atlast_message_time-last_message_timeroomdisplay_name
room_imagelast_messageunread_countrequest)roomsdirect_chat_formgroup_chat_formcurrent_employeezchat/chat_home.html)useremployee_getr   objectsfilterannotater   order_byappendget_display_nameget_room_imageget_last_messageget_unread_countr   r   r
   )r*   employee
chat_roomsrooms_with_infor$   r,   r-   contexts           ./var/www/python-projects/worksol/chat/views.py	chat_homer?      s    
 ||((H !!((% )  h45  h#$  O  11(;--h7 113 11(; 
 	 +7;"73O !,*$	G '0'::    c           
         | j                   j                  }t        j                  j	                  |d      j                  t        d            j                  d      }g }|D ]T  }|j                  ||j                  |      |j                  |      |j                         |j                  |      d       V ||d}t        | d|      S )	z2
    HTMX endpoint to refresh chat room list.
    Tr   r   r    r"   r#   )r+   r.   z#chat/components/chat_room_list.html)r/   r0   r   r1   r2   r3   r   r4   r5   r6   r7   r8   r9   r
   )r*   r:   r;   r<   r$   r=   s         r>   chat_room_listrB   C   s    
 ||((H!!((% )  h45  h#$  O  11(;--h7 113 11(; 
 	 !$G '@'JJr@   c           	         | j                   j                  }t        t        ||      }|j                  j                  |      j                         }|r)t        j                         |_	        |j                          |j                  dk(  r|xr |j                  dk(  nd}|j                  j                  d      j                  ddd	      j!                  d
      }t#               }||j%                  |      |j'                  |      ||||j                  dk(  r|j                  j                  d      nd|d}t)        | d|      S )z5
    View a specific chat room and its messages.
    idr   employee_idgroupadminFsenderattachments	reactionsreactions__employee_id
created_atrG   N)r$   r%   r&   r   message_formr.   membersis_adminzchat/chat_room_detail.html)r/   r0   r   r   rP   r2   firstr   nowlast_read_atsave	room_typeroler   select_relatedprefetch_relatedr4   r   r6   r7   r
   )	r*   room_idr:   r$   memberrQ   chat_messagesrO   r=   s	            r>   chat_room_detailr]   b   s'   
 ||((HX'QD \\  X 6<<>F&lln 59NNg4Mv0&++0SXH MM00:KK{$<h|  #$L --h7))(3!$$AESZAZ4<<..}=`d	G '7AAr@   c                    | j                   j                  }t        t        ||      }| j                  j                  dd      }|j                  j                  |      j                  d      j                  dd      j                  d      }|j                  j                  |	      j                         }|r)t        j                         |_        |j!                          |||d
d}t#        | d|      S )z;
    HTMX endpoint to refresh messages in a chat room.
    rD   last_idr   )id__gtrJ   rK   rL   rN   rF   T)r   r.   r$   is_pollchat/components/messages.html)r/   r0   r   r   GETgetr   r2   rX   rY   r4   rP   rR   r   rS   rT   rU   r
   )r*   rZ   r:   r$   last_message_idr\   r[   r=   s           r>   r\   r\      s     ||((HX'QD kkooi3OMM(((@OO}k288L3I 
 \\  X 6<<>F&lln "$	G ':GDDr@   c           
      ,   | j                   j                  }t        t        ||      }| j                  dk(  rt        | j                  | j                        }| j                  j                  dd      j                         }| j                  j                  d      }| j                  j                  d      }d}|rt        d |D              }	|	rd	nd
}t        j                  j                  |||r|nd||r|nd      }
|D ]D  }t        j                  j                  |
||j                   |j"                  |j$                         F |j&                  j)                  |      j+                  d      }|D ]{  }|j,                  j.                  st1        j2                  |j.                  |j,                  j.                  d|j5                  |j,                         dd|j6                   d       } |
g||d}t9        | d|      S t;        d      S )z(
    Send a message in a chat room.
    rD   POSTcontent rK   reply_totextc              3   R   K   | ]  }|j                   j                  d        ! yw)zimage/N)content_type
startswith).0fs     r>   	<genexpr>zsend_message.<locals>.<genexpr>   s$      89))(3s   %'imagefileN)	chat_roomrJ   rh   message_typereply_to_id)messagers   	file_name	file_size	file_typerF   F)is_mutedzsent you a message in 
chatbubblez/chat/room//)	recipientverbiconr	   )r   r.   r$   rb     status)r/   r0   r   r   methodr   rg   FILESrd   stripgetlistallr   r1   creater   namesizerm   rP   excluder2   rG   employee_user_idr   sendr6   rE   r
   r   )r*   rZ   r:   r$   formrh   filesrv   ru   
all_imagesrw   rs   other_membersr[   r=   s                  r>   send_messager      s    ||((HX'QD~~w||W]];,,""9b1779%%m4ll&&z2  =B J '17fL%%,,&GD%'2 - 
  	D""))))))++ * 	 ,,,BIISXIY# 	F!!22--$00AA1$2G2GHZHZ2[1\]%*477)15	 !	 (

 g>HHs##r@   c                    | j                   j                  }| j                  dk(  rt        | j                  |       }|j                         r|j                  d   }t        j                  j                  d|      j                  |      j                         }|r|}nst        j                  j                  d|j                               }t        j                  j                  ||d	       t        j                  j                  ||d	       t        j                  | t!        d
             t#        d      S t#        d      S )z4
    Start a direct chat with another employee.
    rg   r)   rG   direct)rV   r   )r   )rV   
company_idr[   rt   rG   rW   zChat started successfully!*<script>window.location.reload();</script>r   r   )r/   r0   r   r   rg   is_validcleaned_datar   r1   r2   rR   r   get_companyr   r   success_r   )r*   r:   r   other_employeeexisting_roomr$   s         r>   start_direct_chatr      s6    ||((H~~"7<<A==?!..}=N %,,33"%- 4  f%3  eg  $  ''..&'335 /  &&--" (! . 
 &&--" .! .  Wa(D&EF LMMs##r@   c                    | j                   j                  }| j                  dk(  rEt        | j                  | j
                  |       }|j                         r|j                  d      }d|_        |j                         |_
        |j                          t        j                  j                  ||d       |j                  d   D ]$  }t        j                  j                  ||d	       & t        j                  j                  |||j!                          d
d       t#        j$                  | t'        d             t)        d      S t#        j*                  | t'        d             t)        d      S )z"
    Create a new group chat.
    rg   r)   F)commitrH   rI   r   rP   r[   z created this groupsystemrt   rJ   rh   ru   z Group chat created successfully!r   z Please correct the errors below.r   r   )r/   r0   r   r   rg   r   r   rU   rV   r   r   r   r1   r   r   r   get_full_namer   r   r   r   error)r*   r:   r   r$   member_employees        r>   create_group_chatr     sL    ||((H~~GLL'--I==?99E9*D$DN&224DOIIK ""))$ *  $(#4#4Y#? &&--" /! .  &&#11344GH%	 '  Wa(J&KL LMMNN7A&H$IJs##r@   c                    | j                   j                  }t        t        |d|      }|j                  j                  |      j                         }|r|j                  dk7  rt        d      S |j                  j                  dd	      }t        j                  j                  d
      j                  |      }|j                         }|r|j                  |      }||d}t        | d|      S )z:
    HTMX endpoint to get the Add Members form modal.
    rH   rE   rV   r   rF   rI   i  r   rG   Tflatr   )id__inemployee_work_info__company_id)r$   	employeesz&chat/components/add_members_modal.html)r/   r0   r   r   rP   r2   rR   rW   r   values_listr   r1   r   r   r
   )	r*   rZ   r:   r$   r[   existing_member_idsr   companyr=   s	            r>   add_members_formr   F  s     ||((H%	D \\  X 6<<>FV[[G+3'' ,,22=t2L  ''$'7??GZ?[I ""$G$$G$L	 G 'CWMMr@   c           	         | j                   j                  }t        t        |d|      }|j                  j                  |      j                         }|r|j                  dk7  r*t        j                  | t        d             t        d      S | j                  dk(  r+| j                  j                  d      }|rg }|D ]  }t        j                   j                  |d	
      j                         }|s5|j                  j                  |      j#                         r`t$        j                   j'                  ||d       |j)                  |j+                                 |rdt,        j                   j'                  |||j+                          ddj/                  |       d       t        j0                  | t        d             t        d      S )z&
    Add members to a group chat.
    rH   r   rF   rI   zOnly admins can add members.	chat-homerg   rP   T)rE   r   r[   r   z added z, r   r   zMembers added successfully!)r/   r0   r   r   rP   r2   rR   rW   r   r   r   r	   r   rg   r   r   r1   existsr   r   r5   r   r   joinr   )	r*   rZ   r:   r$   r[   
member_idsadded_names	member_id
new_members	            r>   add_group_membersr   i  s   
 ||((H%	D \\  X 6<<>FV[[G+w"@ AB$$~~\\)))4
K' C	%--44	T4RXXZ
dll&9&9j&9&Q&X&X&Z"**11"&$.% 2 
  &&z'?'?'ABC ##**"#'5578		+@V?WX!)	 +    !,I*JKK  r@   c                 V   | j                   j                  }t        t        |d|      }|j                  j                  |      j                         }|r|j                  dk7  r*t        j                  | t        d             t        d      S t        t        ||      }|j                  j                         }|j                          t         j"                  j%                  |||j                          d| d	
       t        j&                  | t        d             t        d      S )z,
    Remove a member from a group chat.
    rH   r   rF   rI   zOnly admins can remove members.r   )rE   rt   z	 removed r   r   zMember removed successfully!)r/   r0   r   r   rP   r2   rR   rW   r   r   r   r	   r   rG   r   deleter   r1   r   r   )r*   rZ   r   r:   r$   current_membermember_to_removeremoved_names           r>   remove_group_memberr     s	   
 ||((H%	D \\((X(>DDFN^00G;w"C DE$$(IQUV#//==?L ))+,Il^D	   Wa >?@K  r@   c                    | j                   j                  }t        t        |d|      }|j                  j                  |      j                         }|r|j                  dk7  r*t        j                  | t        d             t        d      S | j                  dk(  r| j                  j                  dd	      j                         }| j                  j                  d
d	      j                         }| j                   j                  d      }|rQ||_        ||_        |r||_        |j)                          t        j*                  | t        d             t        d      S t        j                  | t        d             t        d      S )z[
    Update group chat details (name, description, image).
    Only admins can update.
    rH   r   rF   rI   z%Only admins can update group details.r   rg   r   ri   descriptionr&   zGroup updated successfully!zGroup name is required.)r/   r0   r   r   rP   r2   rR   rW   r   r   r   r	   r   rg   rd   r   r   r   r   r&   rU   r   )r*   rZ   r:   r$   r[   r   r   r&   s           r>   update_groupr     s?    ||((H%	D \\  X 6<<>FV[[G+w"I JK$$~~||+113ll&&}b9??A]]&&|4
DI*D",IIKWa(E&FG K   NN7A&?$@AK  r@   c                 ,   | j                   j                  }t        t        |d|      }|j                  j                  |      j                         }|r|j                  dk(  rC|j                  j                  |      j                         }|rd|_        |j                          t        j                  j                  |||j                          dd       |j                          t        j                   | t#        d             t%        d	      S )
z
    Leave a group chat.
    rH   r   rF   rI   z left the groupr   r   zYou have left the group.r   )r/   r0   r   r   rP   r2   rR   rW   r   rU   r   r1   r   r   r   r   r   r   r	   )r*   rZ   r:   r$   r[   other_members         r>   leave_groupr     s    
 ||((H%	D \\  X 6<<>F;;'!<<//H/EKKML$+!!!# 	""--/0@!	 	# 	
 	!$>"?@K  r@   c                    | j                   j                  }t        t        ||      }|j                  dk(  rq|j
                  j                  |      j                         }|r|j                  dk7  r6t        j                  | t        d             t        d|j                        S |j                  dk(  r|j                  nd}|j                          t        j                   | t        | d	             t        d
      S )z
    Delete a chat room (direct chat or group).
    For groups, only admins can delete.
    Deletes the chat for all participants.
    rD   rH   rF   rI   z'Only group admins can delete the group.zchat-room-detail)rZ   Chatz has been deleted.r   )r/   r0   r   r   rV   rP   r2   rR   rW   r   r   r   r	   rE   r   r   r   )r*   rZ   r:   r$   r[   	room_names         r>   delete_chatr     s     ||((H%D ~~ $$$:@@B/NN7A&O$PQ.@@ "^^w6		FIKKMWa9+-? @ABK  r@   c                 H   | j                   j                  d      dk(  xs | j                   j                  d      }|st        d      S | j                  j                  }t        t        ||      }| j                  j                  dd      }t        j                  j                  |||	      j                         }|r|j                          n"t        j                  j                  |||	       |j                          g }d
dlm} |j"                  j%                  d      j'                   |d            }	|	D ]V  }
t)        |j"                  j                  |
d         j+                  dd            }|j-                  |
d   |
d   |d       X t/        d|i      S )z7
    Add or remove an emoji reaction to a message.
    X-Requested-WithXMLHttpRequestz
HX-Requestr   r   )rE   chat_room__members__employee_idemojiu   👍)rw   rG   r   r   )CountrE   )count)r    employee_id__employee_first_nameTr   r   )r   r   usersrL   )headersrd   r   r/   r0   r   r   rg   r   r1   r2   rR   r   r   refresh_from_dbdjango.db.modelsr   rL   valuesr3   listr   r5   r   )r*   
message_idis_ajaxr:   rw   r   existingreactions_datar   reaction_groupsrH   r   s               r>   toggle_reactionr   !  s    oo!!"459IInW__M`M`amMnG3''||((H(0G LLWf-E &&-- .  eg	  &&  	' 	
 N&''..w7@@uT{@SO  W&&--E'N-COOPry}O~7^7^
 	 n566r@   c                     | j                   j                  d      dk(  st        d      S | j                  j                  }t        t        ||      }|j                          t        d      S )zG
    Delete a message (only sender can delete their own messages).
    r   r   r   r   )rE   rJ   ri   )r   rd   r   r/   r0   r   r   r   )r*   r   r:   rw   s       r>   delete_messager   U  sa     ??126FF3''||((HG NNr@   c           
         | j                   j                  }| j                  j                  dd      }t        j
                  j                  d      j                  |j                        }|j                         }|r|j                  |      }|r9|j                  t        |      t        |      z  t        |	      z        d
d }n|d
d }|D cg c]X  }|j                  |j                         |j                         |j                         rt        |j                               nddZ }}t        d|i      S c c}w )z3
    Search employees for starting a new chat.
    qri   Tr   )rE   r   )employee_first_name__icontains)employee_last_name__icontains)email__icontainsN
   )rE   r   avatarpositionresults)r/   r0   rc   rd   r   r1   r2   r   rE   r   r   r   
get_avatarget_job_positionstrr   )r*   r:   queryr   r   empr   s          r>   search_employeesr   k  s>   
 ||((HKKOOC$E  ''$'7??8;;?OI ""$G$$G$L	 $$U3E23u%&
 2		 crN	   &&%%'nn&7:7K7K7MC0023SU		
G  G,--s   AEc                     | j                   j                  }d}t        j                  j	                  |d      }|D ]  }||j                  |      z  } t        d|i      S )z>
    Get total unread message count for the current user.
    r   Tr   r(   )r/   r0   r   r1   r2   r9   r   )r*   r:   total_unreadr+   r$   s        r>   r9   r9     sq     ||((HL##% $ E
  8--h778 677r@   N)7__doc__django.contribr   r   r   r   django.httpr   r   django.shortcutsr   r	   r
   django.utilsr   django.utils.translationr   r   django.views.decorators.httpr   
chat.formsr   r   r   r   chat.modelsr   r   r   r   r   employee.modelsr   horilla.decoratorsr   r   notifications.signalsr   r?   rB   r]   r\   r   r   r   r   r   r   r   r   r   r   r   r   r9    r@   r>   <module>r     s   $ # 2 @ @ ! 6 5 Y Y  % B ( #; #;L K K< !B !BH E  E: <$  <$~ +$  +$\ *$  *$Z N  NB *! *!Z ! !D "! "!J  !  !F ! !8 /7  /7d   ( ". ".J 8  8r@   