o
    54g                     @   s   d Z ddlmZ ddlmZmZ ddlmZ ddlmZm	Z	 ddl
mZ g dZd d
edejfddZdd Zejeje	gZdd eD Zdee defddZdee deeejejf  dee fddZdejdefddZ			d!dejfddZeZdS )"z
Firebase Cloud Messaging
Previously known as GCM / C2DM
Documentation is available on the Firebase Developer website:
https://firebase.google.com/docs/cloud-messaging/
    )copy)ListUnion)	messaging)FirebaseErrorInvalidArgumentError   )get_manager)titlebodyiconimagesoundbadgecolortagclick_actionbody_loc_keybody_loc_argstitle_loc_keytitle_loc_argsandroid_channel_idFdatareturnc                 K   s  |   } d| v r| dds|rdS d}i }d| v r"| dd|d< tD ]}| |d}|r2|||< ||d}|r>|||< q$|rY|dd|d< |dd|d	< tjdi |}tj| d
dpf|d
d| ddpq|dd| ddp||dd| ddp|dd| |d}tj| |d}	| ddp|dd}
| ddp|dd}| ddp|dd}|
r|
dr|
|	_	n|p|
|	_
||	_|	S )ak  
	Constructs a messaging.Message from the old dictionary.

	FCM_NOTIFICATION_PAYLOAD_KEYS are being put into the AndroidNotification
	FCM_OPTIONS_KEYS are being put into the AndroidConfig
	FCM_TARGETS_KEYS is mapped to either topic, token or condition

	If dry_run is included and its value is True, no message will be returned, so nothing is accidentally sent.
	dry_runFNmessager   r   
channel_idr   notification_countcollapse_keyprioritytime_to_liverestricted_package_name)r   r   ttlr!   r   notification)r   androidto	conditionnotification_keyz/topic/ )r   popFCM_NOTIFICATIONS_PAYLOAD_KEYSr   AndroidNotificationAndroidConfiggetMessage
startswithtopictokenr&   )r   r   kwargsandroid_notificationnotification_payloadkeyvalue_from_extravalue_from_kwargsandroid_configr   r%   r&   r'   r(   r(   \/var/www/html/notificationserver/venv/lib/python3.10/site-packages/push_notifications/gcm.pydict_to_fcm_message   sT   


r:   c                 c   s.    t dt| |D ]}| |||  V  q	dS )zA
	Yield successive chunks from list  l with a maximum size  n
	r   N)rangelen)lnir(   r(   r9   _chunks[   s   r@   c                 C   s   g | ]}|j qS r(   )code.0xr(   r(   r9   
<listcomp>j   s    rE   excc                 C   s:   | sdS t | }|tkr| tv S |tko| jdkp|tv S )NFzInvalid registration)typestrfcm_error_list_strr   causefcm_error_list)rF   exc_typer(   r(   r9   $_validate_exception_for_deactivationm   s   rM   registration_idsresultsc                    sf   |sg S t |d tjrdd t| D }n	 fdd|D }ddlm} |jj|djdd	 |S )
Nr   c                 S   s   g | ]\}}t |jr|qS r(   )rM   	exception)rC   itemr1   r(   r(   r9   rE      s    z:_deactivate_devices_with_error_results.<locals>.<listcomp>c                    s    g | ]}t |jr |j qS r(   )rM   reasonindexrB   rN   r(   r9   rE      s    r   )	GCMDevice)registration_id__inF)active)	
isinstancer   SendResponsezipmodelsrU   objectsfilterupdate)rN   rO   deactivated_idsrU   r(   rT   r9   &_deactivate_devices_with_error_resultsx   s   
r`   r   r1   c                 C   s   || _ t| S )N)r1   r   )r   r1   r(   r(   r9   _prepare_message   s   ra   Nc                    s   t  |}|rt  |nd}| du r  jdu r  jdu r dS t| ts,| r*| gnd} | rYg }t| |D ]} fdd|D }	tj	|	||dj
}
||
 q5t| | t|S tg S )a  
	Sends an FCM notification to one or more registration_ids. The registration_ids
	can be a list or a single string.

	:param registration_ids: A list of registration ids or a single string
	:param message: The Message object, use `dict_to_fcm_message` to convert dict to Message
	:param application_id: The application id to use.
	:param dry_run: If True, no message will be sent.

	:return: A BatchResponse object
	Nc                    s   g | ]}t  |qS r(   )ra   )rC   r1   r   r(   r9   rE      s    
z send_message.<locals>.<listcomp>)r   app)r	   get_max_recipientsget_firebase_appr0   r&   rX   listr@   r   	send_each	responsesextendr`   BatchResponse)rN   r   application_idr   r2   max_recipientsrc   retchunkmessagesrh   r(   rb   r9   send_message   s"   




rp   )F)NF)__doc__r   typingr   r   firebase_adminr   firebase_admin.exceptionsr   r   confr	   r*   dictr.   r:   r@   UnregisteredErrorSenderIdMismatchErrorrK   rI   boolrM   rH   rY   	ErrorInfor`   ra   rp   send_bulk_messager(   r(   r(   r9   <module>   s:    A


-