o
    54g?                     @   s   d Z ddl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mZ dddZdddddddg i dddfddZ	dddZdddZ	dddZdS )z
Apple Push Notification Service
Documentation is available on the iOS Developer Library:
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html
    N)client)credentials)errors)payload   )models)get_manager)	APNSErrorAPNSUnsupportedPriorityAPNSServerErrorc                 C   s|   | d u r(t  |st  |}t|} nt  |\}}}| p't|||} tj| t  	|t  
|d}|  |S )N)use_sandboxuse_alternative_port)r   has_auth_token_credsget_apns_certificateapns2_credentialsCertificateCredentialsget_apns_auth_credsTokenCredentialsapns2_client
APNsClientget_apns_use_sandboxget_apns_use_alternative_portconnect)credsapplication_idcertkeyPathkeyIdteamIdr    r   ]/var/www/html/notificationserver/venv/lib/python3.10/site-packages/push_notifications/apns.py_apns_create_socket   s   

r!   Fc                 C   sX   |s|s|	rt j|r|ni ||	|d}n|}t|r|| }t j||||||
|||d	S )N)bodybody_localized_keybody_localized_argsaction_localized_key)	alertbadgesoundcategoryurl_argscustom	thread_idcontent_availablemutable_content)apns2_payloadPayloadAlertcallablePayload)tokenr&   r   r'   r(   r)   r-   action_loc_keyloc_keyloc_argsextrar.   r,   r*   apns2_alertr   r   r    _apns_prepare)   s   r9   c           
         s   t ||d}i }dd |d< |d stt d |d< dd }|r?ztt||d< W n ty>   td| w dd |d< |rb fdd| D }	|j	|	t
 j|d	fi |S t|  fi }	|j| |	t
 j|d	fi | d S )
N)r   r   
expirationi ' priorityzUnsupported priority %dcollapse_idc                    s(   g | ]}t j|t| fi d qS ))r3   r   )r   Notificationr9   ).0ridr&   kwargsr   r    
<listcomp>S   s
    
z_apns_send.<locals>.<listcomp>)r   )r!   popinttimer   NotificationPrioritystr
ValueErrorr
   send_notification_batchr   get_apns_topicr9   send_notification)
registration_idr&   batchr   r   rA   r   notification_kwargsr;   datar   r@   r    
_apns_send=   s:   
rP   c              
   K   sr   zt | |f||d| W dS  tjy8 } zt|tjr-tjjj| d}d|_	|
  t|jjdd}~ww )as  
	Sends an APNS notification to a single registration_id.
	This will send the notification as form data.
	If sending multiple notifications, it is more efficient to use
	apns_send_bulk_message()

	Note that if set alert should always be a string. If it is not set,
	it won"t be included in the notification. You will need to pass None
	to this for silent notifications.
	)r   r   )rL   F)statusN)rP   apns2_errorsAPNsException
isinstanceUnregisteredr   
APNSDeviceobjectsgetactivesaver   	__class____name__)rL   r&   r   r   rA   apns2_exceptiondevicer   r   r    apns_send_messaged   s"   r_   c                 K   sH   t | |fd||d|}dd | D }tjjj|djdd |S )a!  
	Sends an APNS notification to one or more registration_ids.
	The registration_ids argument needs to be a list.

	Note that if set alert should always be a string. If it is not set,
	it won"t be included in the notification. You will need to pass None
	to this for silent notifications.
	T)rM   r   r   c                 S   s   g | ]
\}}|d kr|qS )rU   r   )r>   r3   resultr   r   r    rB      s    z*apns_send_bulk_message.<locals>.<listcomp>)registration_id__inF)rY   )rP   itemsr   rV   rW   filterupdate)registration_idsr&   r   r   rA   resultsinactive_tokensr   r   r    apns_send_bulk_message~   s   rh   )NN)FNN)__doc__rE   apns2r   r   r   r   r   rR   r   r/    r   confr   
exceptionsr	   r
   r   r!   r9   rP   r_   rh   r   r   r   r    <module>   s&    



'