📄 users.cpython-312.pyc
/home/palash/git/local-ai/server/features/__pycache__/users.cpython-312.pyc
Language: pyc • Lines: 130
�

���j����dZddlZddlZddlZddlmZddlmZddlmZ	ddl
mZddlm
Z
d�Zd	�Zd
�Zd�Zdd�Zd
�Zd�Zd�Zy)u�Per-user context files and HTTP identity resolution.

Identity comes from :mod:`server.auth` — Authentik is the single identity
provider (there is no users.json anymore). This module keeps the names the
chat engine calls: password lookup is gone, context paths are derived from the
username, and ``get_current_user``/``get_current_identity`` resolve the
request's identity through the shared auth layer.
�N)�datetime)�get_current_user)�get_identity)�CONTEXTS_DIR)�Mc�D�tjdd|xsd�}|xsdS)Nz[^A-Za-z0-9_-]�_��unknown)�re�sub)�user�safes  �2/home/palash/git/local-ai/server/features/users.py�_safe_usernamers$��
�6�6�#�S�$�*�"�5�D���9��c��|sytj5tj�tj|<ddd�y#1swYyxYw)zIRecord a username as recently active (heartbeat for active-user display).N)r�_user_last_seen_lock�time�_user_last_seen��usernames r�
_mark_seenrs<����	
�	�	�2�&*�i�i�k����(�#�2�2�2�s�&A�Ac�2�t|�}t|�|S�N)�auth_get_current_userr)�headersrs  rrr"s��$�W�-�H��x���Orc�<�t|�}|rt|d�|S)Nr)�auth_get_identityr)r�identitys  r�get_current_identityr!(s!�� ��)�H���8�J�'�(��Orc����tj��tj5t��fd�tjj�D��}ddd�|rGtj5ttj�}ddd�D�cgc]	}|vs�|��c}SS#1swY�TxYw#1swY�,xYwcc}w)zESorted usernames seen within the window, optionally excluding agents.c3�8�K�|]\}}�|z
�kr|���y�wr�)�.0�u�ts�now�window_secondss   ��r�	<genexpr>zactive_users.<locals>.<genexpr>3s)�����
��!�R��R�x�>�)�
�
�s�N)	rrr�sortedr�items�_tokens_lock�set�_agent_users)r)�exclude_agents�users�agent_usersr&r(s`    @r�active_usersr3/s����
�)�)�+�C�	
�	�	�
��
��,�,�2�2�4�
�
��
�
�
�^�^�	.��a�n�n�-�K�	.� �9�a�A�[�$8��9�9��L�
�
��	.�	.��9s#�2B,�3B8�	C�#C�,B5�8Cc�b�tjjtt	|�dz�S)aDerive the context file path from the username.

    users.json previously stored an arbitrary per-user ``context_file`` path;
    with Authentik as the sole identity store that field no longer exists, so
    every user's context lives at ``CONTEXTS_DIR/<username>.txt``.
    z.txt)�os�path�joinrrrs r�get_user_context_pathr8>s#���7�7�<�<��n�X�&>��&G�H�Hrc��t|�}td|d|�|r^tjj	|�r?	td|�t|�5}|j
�}t|�|cddd�Sy#1swYyxYw#YyxYw)NzContext path�for�Readingr
)r8�printr5r6�exists�open�read)rr6�f�contexts    r�read_user_contextrBHs��� ��*�D�	�.�$��x�0�������t�$�	��)�T�"��d��
�q��&�&�(���g���
�
��

���	��s)�B�A;�0	B�;B�B�B�Bc��t|�}|r�tjtjj	|�d��t|�}t
j�jd�}d|�d|��}|j�r|j�dz|zn|}t|d�5}|j|�ddd�yy#1swYyxYw)NT)�exist_okz%Y-%m-%d %H:%M�[z] z

�w)r8r5�makedirsr6�dirnamerBrr(�strftime�stripr>�write)r�contentr6�existing�	timestamp�entry�new_contentr@s        r�write_user_contextrQWs��� ��*�D��
���B�G�G�O�O�D�)�D�9�$�X�.���L�L�N�+�+�,<�=�	��I�;�b��	�*��=E�^�^�=M�x�~�~�'�&�0�5�8�SX��
�$��_�	!��
�G�G�K� �	!�	!�
�	!�	!�s�,C�C)�xT)�__doc__r5rrr�server.authrrrr�
server.configr�server.features.staterrrr!r3r8rBrQr$rr�<module>rWsM���
�	���A�9�&�#��
2����I��	!r