o
    ^°±jHc  ã                   @   sÆ  U d dl Z d dlZd dlmZ e j e j e j e¡¡d¡Ze	ƒ Z
e	ed< dZdddd	d
dddœZdedefdd„Zdefdd„Zd†dedededefdd„Zdededefdd„Zdedefdd„Zdefdd„ZdZd‡ded edB dee fd!d"„Zd‡ded edB dee fd#d$„Zded%ededB fd&d'„Zded edB dee fd(d)„Zded edB defd*d+„Zded edB d,edefd-d.„Z		/	 dˆded edB d0ed1ed2ed3ed4ed,edefd5d6„Zded%ed0efd7d8„Z 	d†ded%ed2ed3efd9d:„Z!ded%ed4efd;d<„Z"ded%ed,efd=d>„Z#ded%ed?edB d@efdAdB„Z$ded%efdCdD„Z%ded%efdEdF„Z&ded%edGefdHdI„Z'ded%efdJdK„Z(d‰dedLedee fdMdN„Z)dedefdOdP„Z*d‡dedQedB dee fdRdS„Z+		dŠded edB dTedefdUdV„Z,dedWedXedB dYedef
dZd[„Z-dedee fd\d]„Z.dedWede/fd^d_„Z0dedWefd`da„Z1dedefdbdc„Z2dedee fddde„Z3dedWede/fdfdg„Z4dedWedXedB dYefdhdi„Z5dedWefdjdk„Z6dedee fdldm„Z7	nd‹dedoedpedqedB dredB dsefdtdu„Z8dedvefdwdx„Z9dedee fdydz„Z:dedoedpedqedB dredB dsed{efd|d}„Z;dedvefd~d„Z<dedvedWede/fd€d�„Z=dedvede>eef fd‚dƒ„Z?dedvefd„d…„Z@dS )Œé    N)ÚdatetimeÚdatabaseÚ
_INIT_DONE)ÚtextÚphotoÚvideoÚdocumentÚaudioÚurlÚsubmenuu   ðŸ“�u   ðŸ–¼u   ðŸŽ¬u   ðŸ“Žu   ðŸŽµu   ðŸ”—u   ðŸ“‚Úbot_usernameÚreturnc                 C   s&   t jtdd� t j t|  ¡ › d�¡S )NT)Úexist_okz.db)ÚosÚmakedirsÚDB_DIRÚpathÚjoinÚlower)r   © r   ú*/var/www/kodo/factory/cobots/buttons/db.pyÚget_db_path   s   r   c              	   Ã   s\  �|   ¡ }|tv rd S t t| ƒ¡4 I d H š‹}| d¡I d H  | d¡I d H  | d¡I d H  | d¡I d H  | d¡I d H  | d¡I d H  | d¡I d H  z| d¡I d H  | d	¡I d H  | ¡ I d H  W n	 tyq   Y nw g d
¢}|D ]\}}| d||f¡I d H  qx| ¡ I d H  t |   ¡ ¡ W d   ƒI d H  d S 1 I d H s§w   Y  d S )NzŒ
            CREATE TABLE IF NOT EXISTS settings (
                key   TEXT PRIMARY KEY,
                value TEXT
            )
        a\  
            CREATE TABLE IF NOT EXISTS users (
                id          INTEGER PRIMARY KEY,
                username    TEXT,
                first_name  TEXT,
                is_banned   INTEGER DEFAULT 0,
                is_blocked  INTEGER DEFAULT 0,
                last_active TEXT,
                joined_at   TEXT
            )
        zÙ
            CREATE TABLE IF NOT EXISTS admins (
                id         INTEGER PRIMARY KEY,
                username   TEXT,
                first_name TEXT,
                added_at   TEXT
            )
        aß  
            CREATE TABLE IF NOT EXISTS mandatory_channels (
                id               INTEGER PRIMARY KEY AUTOINCREMENT,
                channel_id       TEXT UNIQUE NOT NULL,
                channel_username TEXT,
                channel_title    TEXT NOT NULL,
                invite_link      TEXT,
                display_type     TEXT DEFAULT 'buttons',
                is_active        INTEGER DEFAULT 1,
                added_at         TEXT
            )
        av  
            CREATE TABLE IF NOT EXISTS funded_channels (
                id               INTEGER PRIMARY KEY AUTOINCREMENT,
                channel_id       TEXT UNIQUE NOT NULL,
                channel_username TEXT,
                channel_title    TEXT NOT NULL,
                invite_link      TEXT,
                display_type     TEXT DEFAULT 'buttons',
                target_count     INTEGER NOT NULL DEFAULT 100,
                current_count    INTEGER DEFAULT 0,
                is_active        INTEGER DEFAULT 1,
                added_at         TEXT,
                completed_at     TEXT
            )
        a]  
            CREATE TABLE IF NOT EXISTS funded_subscriptions (
                id                INTEGER PRIMARY KEY AUTOINCREMENT,
                funded_channel_id INTEGER NOT NULL,
                user_id           INTEGER NOT NULL,
                subscribed_at     TEXT,
                UNIQUE(funded_channel_id, user_id)
            )
        a½  
            CREATE TABLE IF NOT EXISTS buttons (
                id               INTEGER PRIMARY KEY AUTOINCREMENT,
                parent_id        INTEGER DEFAULT NULL,
                label            TEXT    NOT NULL,
                response_type    TEXT    NOT NULL,
                response_data    TEXT    DEFAULT '',
                response_caption TEXT    DEFAULT '',
                columns          INTEGER DEFAULT 5,
                row_number       INTEGER DEFAULT 0,
                sort_order       INTEGER DEFAULT 0,
                press_count      INTEGER DEFAULT 0,
                is_active        INTEGER DEFAULT 1,
                created_at       TEXT
            )
        z;ALTER TABLE buttons ADD COLUMN row_number INTEGER DEFAULT 0z*UPDATE buttons SET row_number = sort_order))Úis_openÚ1)Úclosed_messageu(   ðŸ”´ Ø§Ù„Ø¨ÙˆØª Ù…Ù‚Ù�ÙˆÙ„ Ø­Ø§Ù„ÙŠØ§Ù‹.)Úwelcome_messageÚ )Úwelcome_imager   )Únotify_new_usersr   )Únotify_blockedr   )Úfund_notifyr   )Úroot_columnsÚ5z9INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?))	r   r   Ú	aiosqliteÚconnectr   ÚexecuteÚcommitÚ	ExceptionÚadd)r   ÚkeyÚdbÚdefaultsÚdkeyÚvaluer   r   r   Úinit_db    s8   €	ÿ
þ.šr.   r   r)   Údefaultc              
   Ã   s¾   �t  t| ƒ¡4 I d H šF}| d|f¡4 I d H š$}| ¡ I d H }|r&|d n|W  d   ƒI d H  W  d   ƒI d H  S 1 I d H sBw   Y  W d   ƒI d H  d S 1 I d H sXw   Y  d S )Nz&SELECT value FROM settings WHERE key=?r   ©r#   r$   r   r%   Úfetchone)r   r)   r/   r*   ÚcurÚrowr   r   r   Úget_settingŒ   s   €þÿ.ÿr4   r-   c              	   Ã   sn   �t  t| ƒ¡4 I d H š}| d||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s0w   Y  d S )Nz:INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)©r#   r$   r   r%   r&   )r   r)   r-   r*   r   r   r   Úset_setting“   s   €
ÿ.ür6   c              
   Ã   ó´   �t  t| ƒ¡4 I d H šA}| d¡4 I d H š!}dd„ | ¡ I d H D ƒW  d   ƒI d H  W  d   ƒI d H  S 1 I d H s=w   Y  W d   ƒI d H  d S 1 I d H sSw   Y  d S )NzSELECT key, value FROM settingsc                 S   s   i | ]	}|d  |d “qS )r   é   r   ©Ú.0Úrr   r   r   Ú
<dictcomp>ž   ó    z$get_all_settings.<locals>.<dictcomp>©r#   r$   r   r%   Úfetchall©r   r*   r2   r   r   r   Úget_all_settings›   s   €ÿÿ.ÿrA   c                 C   sR   | d | d | d | d | d | d | d | d | d	 | d
 t | d ƒ| d dœS )Nr   r8   é   é   é   é   é   é   é   é	   é
   é   )ÚidÚ	parent_idÚlabelÚresponse_typeÚresponse_dataÚresponse_captionÚcolumnsÚ
row_numberÚ
sort_orderÚpress_countÚ	is_activeÚ
created_at)Úbool)r3   r   r   r   Ú_row_to_btn    s   
ôrY   z~id,parent_id,label,response_type,response_data,response_caption,columns,row_number,sort_order,press_count,is_active,created_atrM   c              
   Ã   óÀ   �t  t| ƒ¡4 I d H šG}| dt› d�|f¡4 I d H š!}dd„ | ¡ I d H D ƒW  d   ƒI d H  W  d   ƒI d H  S 1 I d H sCw   Y  W d   ƒI d H  d S 1 I d H sYw   Y  d S )NúSELECT zR FROM buttons WHERE is_active=1 AND parent_id IS ? ORDER BY row_number, sort_orderc                 S   ó   g | ]}t |ƒ‘qS r   ©rY   r9   r   r   r   Ú
<listcomp>½   ó    z&get_active_buttons.<locals>.<listcomp>©r#   r$   r   r%   Ú_BTN_FIELDSr?   ©r   rM   r*   r2   r   r   r   Úget_active_buttons¶   ó   €
ýûÿ.ÿrc   c              
   Ã   rZ   )Nr[   zB FROM buttons WHERE parent_id IS ? ORDER BY row_number, sort_orderc                 S   r\   r   r]   r9   r   r   r   r^   Ç   r_   z,get_all_buttons_at_level.<locals>.<listcomp>r`   rb   r   r   r   Úget_all_buttons_at_levelÀ   rd   re   Ú	button_idc              
   Ã   sÆ   �t  t| ƒ¡4 I d H šJ}| dt› d�|f¡4 I d H š$}| ¡ I d H }|r*t|ƒnd W  d   ƒI d H  W  d   ƒI d H  S 1 I d H sFw   Y  W d   ƒI d H  d S 1 I d H s\w   Y  d S )Nr[   z FROM buttons WHERE id=?)r#   r$   r   r%   ra   r1   rY   )r   rf   r*   r2   r3   r   r   r   Ú
get_buttonÊ   s   €ÿüÿ.ÿrg   c                 Ã   sR   �t | |ƒI d H }i }|D ]}|d }| |d¡d ||< qdd„ t| ¡ ƒD ƒS )NrS   r   r8   c                 S   s   g | ]	\}}||d œ‘qS ))rS   Úcountr   )r:   ÚrnÚcntr   r   r   r^   Ø   r=   z!get_rows_info.<locals>.<listcomp>)re   ÚgetÚsortedÚitems)r   rM   ÚbuttonsÚrowsÚbtnri   r   r   r   Úget_rows_infoÒ   s   €rq   c              
   Ã   s¾   �t  t| ƒ¡4 I d H šF}| d|f¡4 I d H š$}| ¡ I d H }|r&|d ndW  d   ƒI d H  W  d   ƒI d H  S 1 I d H sBw   Y  W d   ƒI d H  d S 1 I d H sXw   Y  d S )NzJSELECT COALESCE(MAX(row_number), -1) + 1 FROM buttons WHERE parent_id IS ?r   r0   )r   rM   r*   r2   r3   r   r   r   Úget_next_row_numberÛ   s   €þûÿ.ÿrr   rS   c              
   Ã   sÀ   �t  t| ƒ¡4 I d H šG}| d||f¡4 I d H š$}| ¡ I d H }|r'|d ndW  d   ƒI d H  W  d   ƒI d H  S 1 I d H sCw   Y  W d   ƒI d H  d S 1 I d H sYw   Y  d S )Nz[SELECT COALESCE(MAX(sort_order), -1) + 1 FROM buttons WHERE parent_id IS ? AND row_number=?r   r0   )r   rM   rS   r*   r2   r3   r   r   r   Úget_next_sort_in_rowå   s   €ýúÿ.ÿrs   rE   rN   rO   rP   rQ   rR   c                 Ã   sž   �t  ¡  ¡ }t| ||ƒI d H }	t t| ƒ¡4 I d H š'}
|
 d||||||||	|f	¡I d H }|
 ¡ I d H  |j	W  d   ƒI d H  S 1 I d H sHw   Y  d S )Nz°INSERT INTO buttons (parent_id,label,response_type,response_data,response_caption,columns,row_number,sort_order,press_count,is_active,created_at) VALUES (?,?,?,?,?,?,?,?,0,1,?))
r   ÚutcnowÚ	isoformatrs   r#   r$   r   r%   r&   Ú	lastrowid)r   rM   rN   rO   rP   rQ   rR   rS   ÚnowrT   r*   r2   r   r   r   Ú
add_buttonï   s   €
ÿ
ü0÷rx   c              	   Ã   ón   �t  t| ƒ¡4 I d H š}| d||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s0w   Y  d S )Nz%UPDATE buttons SET label=? WHERE id=?r5   )r   rf   rN   r*   r   r   r   Úupdate_button_label  ó
   €.þrz   c              	   Ã   sp   �t  t| ƒ¡4 I d H š}| d|||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s1w   Y  d S )NzAUPDATE buttons SET response_data=?, response_caption=? WHERE id=?r5   )r   rf   rP   rQ   r*   r   r   r   Úupdate_button_content  s   €
þ.ûr|   c              	   Ã   ry   )Nz'UPDATE buttons SET columns=? WHERE id=?r5   )r   rf   rR   r*   r   r   r   Úupdate_button_columns  r{   r}   c              	   Ã   sž   �t | |ƒI d H }|sd S t| |d |ƒI d H }t t| ƒ¡4 I d H š}| d|||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H sHw   Y  d S )NrM   ú8UPDATE buttons SET row_number=?, sort_order=? WHERE id=?)rg   rs   r#   r$   r   r%   r&   )r   rf   rS   rp   rT   r*   r   r   r   Úupdate_button_row  s   €
þ.ûr   Únew_parent_idÚnew_rowc              	   Ã   s„   �t | ||ƒI d H }t t| ƒ¡4 I d H š }| d||||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s;w   Y  d S )NzEUPDATE buttons SET parent_id=?, row_number=?, sort_order=? WHERE id=?)rs   r#   r$   r   r%   r&   )r   rf   r€   r�   rT   r*   r   r   r   Úmove_button_to$  s   €

þ.ûr‚   c              	   Ã   ól   �t  t| ƒ¡4 I d H š}| d|f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s/w   Y  d S )Nz7UPDATE buttons SET is_active = 1 - is_active WHERE id=?r5   ©r   rf   r*   r   r   r   Útoggle_button/  ó   €
ÿ.ür…   c           	   
   Ã   s  �t  t| ƒ¡4 I d H šh}|g}|g}|rS| d¡}| d|f¡4 I d H š!}dd„ | ¡ I d H D ƒ}| |¡ | |¡ W d   ƒI d H  n1 I d H sLw   Y  |s|D ]}| d|f¡I d H  qU| ¡ I d H  W d   ƒI d H  d S 1 I d H szw   Y  d S )Nr   z(SELECT id FROM buttons WHERE parent_id=?c                 S   s   g | ]}|d  ‘qS )r   r   r9   r   r   r   r^   @  r_   z)delete_button_cascade.<locals>.<listcomp>zDELETE FROM buttons WHERE id=?)r#   r$   r   Úpopr%   r?   Úextendr&   )	r   rf   r*   Ú	to_deleteÚqueueÚcur_idr2   ÚchildrenÚbidr   r   r   Údelete_button_cascade7  s&   €
ÿ
(ûþ.órŽ   Ú	directionc              	   ƒ   s8  �t | ˆ ƒI d H }|sd S t| |d ƒI d H }t‡ fdd„t|ƒD ƒd ƒ}|d u r+d S |dkr:|dkr:||d  }n|dkrM|t|ƒd k rM||d  }nd S t t| ƒ¡4 I d H š5}| d|d	 |d
 ˆ f¡I d H  | d|d	 |d
 |d f¡I d H  | 	¡ I d H  W d   ƒI d H  d S 1 I d H s•w   Y  d S )NrM   c                 3   s$   � | ]\}}|d  ˆ kr|V  qdS )rL   Nr   )r:   ÚiÚb©rf   r   r   Ú	<genexpr>M  s   €" zmove_button.<locals>.<genexpr>Úupr   r8   Údownr~   rS   rT   rL   )
rg   re   ÚnextÚ	enumerateÚlenr#   r$   r   r%   r&   )r   rf   r�   rp   Úall_btnsÚidxÚneighborr*   r   r’   r   Úmove_buttonH  s0   €
þ
þ.÷rœ   c              	   Ã   rƒ   )Nz;UPDATE buttons SET press_count = press_count + 1 WHERE id=?r5   r„   r   r   r   Úincrement_pressb  r†   r�   Úlimitc              
   Ã   s¸   �t  t| ƒ¡4 I d H šC}| d|f¡4 I d H š!}dd„ | ¡ I d H D ƒW  d   ƒI d H  W  d   ƒI d H  S 1 I d H s?w   Y  W d   ƒI d H  d S 1 I d H sUw   Y  d S )NzXSELECT id,label,response_type,press_count FROM buttons ORDER BY press_count DESC LIMIT ?c                 S   s*   g | ]}|d  |d |d |d dœ‘qS )r   r8   rB   rC   )rL   rN   rO   rU   r   r9   r   r   r   r^   q  s    ÿÿz#get_top_buttons.<locals>.<listcomp>r>   )r   rž   r*   r2   r   r   r   Úget_top_buttonsj  s   €ýþûÿ.ÿrŸ   c              
   Ã   s®   �t  t| ƒ¡4 I d H š>}| d¡4 I d H š}| ¡ I d H d W  d   ƒI d H  W  d   ƒI d H  S 1 I d H s:w   Y  W d   ƒI d H  d S 1 I d H sPw   Y  d S )NzSELECT COUNT(*) FROM buttonsr   r0   r@   r   r   r   Úcount_buttonsw  s   €ÿÿ.ÿr    Ú
exclude_idc              
   ƒ   sø   �t  t| ƒ¡4 I d H š6}| d¡4 I d H š}dd„ | ¡ I d H D ƒ}W d   ƒI d H  n1 I d H s3w   Y  W d   ƒI d H  n1 I d H sHw   Y  |rz|h‰|g}|rq| d¡‰ ‡ fdd„|D ƒ}ˆ |¡ | |¡ |sW‡fdd„|D ƒ}|S )NzDSELECT id,parent_id,label FROM buttons WHERE response_type='submenu'c                 S   ó$   g | ]}|d  |d |d dœ‘qS )r   r8   rB   )rL   rM   rN   r   r9   r   r   r   r^   ‚  ó    ÿz$get_all_submenus.<locals>.<listcomp>r   c                    s    g | ]}|d  ˆ kr|d ‘qS )rM   rL   r   r9   )r‹   r   r   r^   ‰  s     c                    s   g | ]
}|d  ˆ vr|‘qS )rL   r   r9   )Úexcludedr   r   r^   Œ  s    )r#   r$   r   r%   r?   r‡   Úupdaterˆ   )r   r¡   r*   r2   ro   rŠ   rŒ   r   )r‹   r¤   r   Úget_all_submenus}  s,   €ÿÿ*ý(ÿ


ür¦   Úprefixc                 Ã   s  �t | |ƒI d H }g }t|ƒD ]t\}}|t|ƒd k}|rdnd}t |d d¡}	|d r/dnd}
|d	 d
kr?d|d	 › d�nd}d|d d › d�}| |› |› d|
› d|	› d|d › |› |› �
¡ |d dkrƒ||rndnd }t| |d |ƒI d H }|rƒ| |¡ qd |¡S )Nr8   u   â””â”€u   â”œâ”€rO   u   â€¢rV   u   ðŸŸ¢u   ðŸ”´rU   r   z (u    âœ¦)r   u    â”¤rS   u   â”œú rN   r   z   u   â”‚  rL   Ú
)re   r—   r˜   Ú
TYPE_ICONSrk   ÚappendÚbuild_tree_textr   )r   rM   r§   rn   Úlinesr�   rp   Úis_lastÚ	connectorÚiconÚstatusÚpressesÚrow_tagÚchild_prefixÚ
child_treer   r   r   r¬   �  s$   € 0
€
r¬   Úuser_idÚusernameÚ
first_namec              
   Ã   sF  �t  ¡  ¡ }t t| ƒ¡4 I d H š„}| d|f¡4 I d H š}| ¡ I d H }W d   ƒI d H  n1 I d H s6w   Y  |se| d|||||f¡I d H  | ¡ I d H  |||ddddœW  d   ƒI d H  S | d||||f¡I d H  | ¡ I d H  |d |d |d |d	 |d
 ddœW  d   ƒI d H  S 1 I d H sœw   Y  d S )NzHSELECT id,username,first_name,is_banned,is_blocked FROM users WHERE id=?zRINSERT INTO users (id,username,first_name,last_active,joined_at) VALUES(?,?,?,?,?)r   T)rL   r·   r¸   Ú	is_bannedÚ
is_blockedÚis_newzAUPDATE users SET username=?,first_name=?,last_active=? WHERE id=?r8   rB   rC   rD   F©	r   rt   ru   r#   r$   r   r%   r1   r&   )r   r¶   r·   r¸   rw   r*   r2   r3   r   r   r   Úget_or_create_user¤  s:   €þ(ü
þÿô

þÿ0ír½   c              
   Ã   r7   )NzKSELECT id,username,first_name FROM users WHERE is_banned=0 AND is_blocked=0c                 S   r¢   ©r   r8   rB   )rL   r·   r¸   r   r9   r   r   r   r^   Ã  r£   z(get_all_active_users.<locals>.<listcomp>r>   r@   r   r   r   Úget_all_active_users¾  s   €ÿÿýÿ.ÿr¿   c              
   Ã   sè   �t  t| ƒ¡4 I d H š[}| d|f¡4 I d H š'}| ¡ I d H s5	 W d   ƒI d H  W d   ƒI d H  dS W d   ƒI d H  n1 I d H sEw   Y  | d|f¡I d H  | ¡ I d H  	 W d   ƒI d H  dS 1 I d H smw   Y  d S )NzSELECT id FROM users WHERE id=?Fz'UPDATE users SET is_banned=1 WHERE id=?T)r#   r$   r   r%   r1   r&   ©r   r¶   r*   r2   r   r   r   Úban_userÇ  s   €þÿ(ÿ0úrÁ   c              	   Ã   rƒ   )Nz'UPDATE users SET is_banned=0 WHERE id=?r5   ©r   r¶   r*   r   r   r   Ú
unban_userÑ  ó
   €.þrÃ   c              
   Ã   sJ  �t  t| ƒ¡4 I d H šƒ}| d¡4 I d H š}| ¡ I d H d }W d   ƒI d H  n1 I d H s0w   Y  | d¡4 I d H š}| ¡ I d H d }W d   ƒI d H  n1 I d H sXw   Y  | d¡4 I d H š}| ¡ I d H d }W d   ƒI d H  n1 I d H s€w   Y  W d   ƒI d H  n1 I d H s•w   Y  ||||| | dœS )NzSELECT COUNT(*) FROM usersr   z,SELECT COUNT(*) FROM users WHERE is_banned=1z-SELECT COUNT(*) FROM users WHERE is_blocked=1)ÚtotalÚbannedÚblockedÚactiver0   )r   r*   r2   rÅ   rÆ   rÇ   r   r   r   Úget_statistics×  s   €(ÿ(ÿ*ÿ(û
ÿrÉ   c              
   Ã   r7   )Nz)SELECT id,username,first_name FROM adminsc                 S   r¢   r¾   r   r9   r   r   r   r^   å  r£   zget_admins.<locals>.<listcomp>r>   r@   r   r   r   Ú
get_adminsâ  s   €ÿÿÿ.ÿrÊ   c              
   Ã   s²   �t  t| ƒ¡4 I d H š@}| d|f¡4 I d H š}t| ¡ I d H ƒW  d   ƒI d H  W  d   ƒI d H  S 1 I d H s<w   Y  W d   ƒI d H  d S 1 I d H sRw   Y  d S )Nz SELECT id FROM admins WHERE id=?)r#   r$   r   r%   rX   r1   rÀ   r   r   r   Úis_adminé  s   €ÿÿ.ÿrË   c              	   Ã   s~   �t  ¡  ¡ }t t| ƒ¡4 I d H š }| d||||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s8w   Y  d S )NzOINSERT OR REPLACE INTO admins (id,username,first_name,added_at) VALUES(?,?,?,?)©r   rt   ru   r#   r$   r   r%   r&   )r   r¶   r·   r¸   rw   r*   r   r   r   Ú	add_adminï  s   €

þ.ûrÍ   c              	   Ã   rƒ   )NzDELETE FROM admins WHERE id=?r5   rÂ   r   r   r   Úremove_adminú  rÄ   rÎ   c              
   Ã   r7   )NzvSELECT id,channel_id,channel_username,channel_title,invite_link,display_type FROM mandatory_channels WHERE is_active=1c              	   S   s6   g | ]}|d  |d |d |d |d |d dœ‘qS )r   r8   rB   rC   rD   rE   )rL   Ú
channel_idÚchannel_usernameÚchannel_titleÚinvite_linkÚdisplay_typer   r9   r   r   r   r^     s
    þ
ÿz*get_mandatory_channels.<locals>.<listcomp>r>   r@   r   r   r   Úget_mandatory_channelsÿ  s   €ÿþüÿ.ÿrÔ   rn   rÏ   rÑ   rÐ   rÒ   rÓ   c              
   Ã   s‚   �t  ¡  ¡ }t t| ƒ¡4 I d H š"}| d||||||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s:w   Y  d S )Nz—INSERT OR REPLACE INTO mandatory_channels (channel_id,channel_username,channel_title,invite_link,display_type,is_active,added_at) VALUES(?,?,?,?,?,1,?)rÌ   )r   rÏ   rÑ   rÐ   rÒ   rÓ   rw   r*   r   r   r   Úadd_mandatory_channel
  s   €
ü.ùrÕ   Úchannel_db_idc              	   Ã   rƒ   )Nz4UPDATE mandatory_channels SET is_active=0 WHERE id=?r5   ©r   rÖ   r*   r   r   r   Úremove_mandatory_channel  r†   rØ   c              
   Ã   r7   )NzŽSELECT id,channel_id,channel_username,channel_title,invite_link,display_type,target_count,current_count FROM funded_channels WHERE is_active=1c                 S   sB   g | ]}|d  |d |d |d |d |d |d |d dœ‘qS )	r   r8   rB   rC   rD   rE   rF   rG   )rL   rÏ   rÐ   rÑ   rÒ   rÓ   Útarget_countÚcurrent_countr   r9   r   r   r   r^   %  s    ý
þz'get_funded_channels.<locals>.<listcomp>r>   r@   r   r   r   Úget_funded_channels  s   €ÿýüÿ.ÿrÛ   rÙ   c           	      Ã   s„   �t  ¡  ¡ }t t| ƒ¡4 I d H š#}| d|||||||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s;w   Y  d S )Nz³INSERT OR REPLACE INTO funded_channels (channel_id,channel_username,channel_title,invite_link,display_type,target_count,current_count,is_active,added_at) VALUES(?,?,?,?,?,?,0,1,?)rÌ   )	r   rÏ   rÑ   rÐ   rÒ   rÓ   rÙ   rw   r*   r   r   r   Úadd_funded_channel+  s   €ÿ
ü.ørÜ   c              	   Ã   rƒ   )Nz1UPDATE funded_channels SET is_active=0 WHERE id=?r5   r×   r   r   r   Úremove_funded_channel:  r†   rÝ   c              
   Ã   s  �t  ¡  ¡ }t t| ƒ¡4 I d H šh}| d||f¡4 I d H š'}| ¡ I d H r<	 W d   ƒI d H  W d   ƒI d H  dS W d   ƒI d H  n1 I d H sLw   Y  | d|||f¡I d H  | d|f¡I d H  | ¡ I d H  	 W d   ƒI d H  dS 1 I d H s€w   Y  d S )NzKSELECT id FROM funded_subscriptions WHERE funded_channel_id=? AND user_id=?FzXINSERT INTO funded_subscriptions (funded_channel_id,user_id,subscribed_at) VALUES(?,?,?)zCUPDATE funded_channels SET current_count=current_count+1 WHERE id=?Tr¼   )r   rÖ   r¶   rw   r*   r2   r   r   r   Úrecord_funded_subB  s2   €þûÿ(ü
þ
þ0ðrÞ   c              
   Ã   sÆ   �t  t| ƒ¡4 I d H šJ}| d|f¡4 I d H š(}| ¡ I d H }|r*|d |d fndW  d   ƒI d H  W  d   ƒI d H  S 1 I d H sFw   Y  W d   ƒI d H  d S 1 I d H s\w   Y  d S )NzASELECT current_count,target_count FROM funded_channels WHERE id=?r   r8   )r   r   r0   )r   rÖ   r*   r2   r3   r   r   r   Úget_funded_channel_countW  s   €þûÿ.ÿrß   c              	   Ã   sz   �t  ¡  ¡ }t t| ƒ¡4 I d H š}| d||f¡I d H  | ¡ I d H  W d   ƒI d H  d S 1 I d H s6w   Y  d S )NzAUPDATE funded_channels SET is_active=0, completed_at=? WHERE id=?rÌ   )r   rÖ   rw   r*   r   r   r   Úmark_funded_completea  s   €
þ.ûrà   )r   )N)r   r   rE   r   )rE   )Nr   )rn   )Ar   r#   r   r   r   ÚdirnameÚabspathÚ__file__r   Úsetr   Ú__annotations__ÚRESPONSE_TYPESrª   Ústrr   r.   r4   r6   ÚdictrA   rY   ra   ÚintÚlistrc   re   rg   rq   rr   rs   rx   rz   r|   r}   r   r‚   r…   rŽ   rœ   r�   rŸ   r    r¦   r¬   r½   r¿   rX   rÁ   rÃ   rÉ   rÊ   rË   rÍ   rÎ   rÔ   rÕ   rØ   rÛ   rÜ   rÝ   rÞ   Útuplerß   rà   r   r   r   r   Ú<module>   sê   
ùlÿ  

	
ýÿÿþþý
ýÿÿ
ÿ

ÿ
ÿ þÿþ
þ
ÿÿ
ÿ	

ÿ
ÿþÿÿ
þÿÿþ
þ
