@@ -348,9 +348,11 @@ def get_user_credits_log():
348348@user_bp .route ('/profile' , methods = ['GET' ])
349349@login_required
350350def get_profile ():
351- """Get current user's profile with billing info"""
351+ """Get current user's profile with billing info and notification settings """
352352 try :
353+ import json
353354 from app .services .billing_service import get_billing_service
355+ from app .utils .db import get_db_connection
354356
355357 user_id = getattr (g , 'user_id' , None )
356358 if not user_id :
@@ -367,6 +369,27 @@ def get_profile():
367369 billing_info = get_billing_service ().get_user_billing_info (user_id )
368370 user ['billing' ] = billing_info
369371
372+ # Add notification settings
373+ with get_db_connection () as db :
374+ cur = db .cursor ()
375+ cur .execute ("SELECT notification_settings FROM qd_users WHERE id = ?" , (user_id ,))
376+ row = cur .fetchone ()
377+ cur .close ()
378+
379+ settings_str = (row .get ('notification_settings' ) if row else '' ) or ''
380+ notification_settings = {}
381+ if settings_str :
382+ try :
383+ notification_settings = json .loads (settings_str )
384+ except Exception :
385+ notification_settings = {}
386+
387+ # Default values
388+ if 'default_channels' not in notification_settings :
389+ notification_settings ['default_channels' ] = ['browser' ]
390+
391+ user ['notification_settings' ] = notification_settings
392+
370393 return jsonify ({
371394 'code' : 1 ,
372395 'msg' : 'success' ,
@@ -529,6 +552,129 @@ def get_my_referrals():
529552 return jsonify ({'code' : 0 , 'msg' : str (e ), 'data' : None }), 500
530553
531554
555+ @user_bp .route ('/notification-settings' , methods = ['GET' ])
556+ @login_required
557+ def get_notification_settings ():
558+ """
559+ Get current user's notification settings.
560+
561+ Returns:
562+ notification_settings: {
563+ default_channels: ['browser', 'telegram', ...],
564+ telegram_chat_id: str,
565+ email: str (optional, override for notifications),
566+ discord_webhook: str (optional)
567+ }
568+ """
569+ try :
570+ import json
571+ from app .utils .db import get_db_connection
572+
573+ user_id = getattr (g , 'user_id' , None )
574+ if not user_id :
575+ return jsonify ({'code' : 0 , 'msg' : 'Not authenticated' , 'data' : None }), 401
576+
577+ with get_db_connection () as db :
578+ cur = db .cursor ()
579+ cur .execute ("SELECT notification_settings, email FROM qd_users WHERE id = ?" , (user_id ,))
580+ row = cur .fetchone ()
581+ cur .close ()
582+
583+ if not row :
584+ return jsonify ({'code' : 0 , 'msg' : 'User not found' , 'data' : None }), 404
585+
586+ # Parse notification_settings JSON
587+ settings_str = row .get ('notification_settings' ) or ''
588+ settings = {}
589+ if settings_str :
590+ try :
591+ settings = json .loads (settings_str )
592+ except Exception :
593+ settings = {}
594+
595+ # Default values
596+ if 'default_channels' not in settings :
597+ settings ['default_channels' ] = ['browser' ]
598+ if 'email' not in settings :
599+ settings ['email' ] = row .get ('email' ) or ''
600+
601+ return jsonify ({
602+ 'code' : 1 ,
603+ 'msg' : 'success' ,
604+ 'data' : settings
605+ })
606+ except Exception as e :
607+ logger .error (f"get_notification_settings failed: { e } " )
608+ return jsonify ({'code' : 0 , 'msg' : str (e ), 'data' : None }), 500
609+
610+
611+ @user_bp .route ('/notification-settings' , methods = ['PUT' ])
612+ @login_required
613+ def update_notification_settings ():
614+ """
615+ Update current user's notification settings.
616+
617+ Request body:
618+ default_channels: list of str (optional, e.g. ['browser', 'telegram'])
619+ telegram_bot_token: str (optional, user's own Telegram bot token)
620+ telegram_chat_id: str (optional)
621+ email: str (optional, for notification override)
622+ discord_webhook: str (optional)
623+ """
624+ try :
625+ import json
626+ from app .utils .db import get_db_connection
627+
628+ user_id = getattr (g , 'user_id' , None )
629+ if not user_id :
630+ return jsonify ({'code' : 0 , 'msg' : 'Not authenticated' , 'data' : None }), 401
631+
632+ data = request .get_json () or {}
633+
634+ # Validate channels
635+ valid_channels = ['browser' , 'email' , 'telegram' , 'discord' , 'webhook' , 'phone' ]
636+ default_channels = data .get ('default_channels' , [])
637+ if not isinstance (default_channels , list ):
638+ default_channels = ['browser' ]
639+ default_channels = [c for c in default_channels if c in valid_channels ]
640+ if not default_channels :
641+ default_channels = ['browser' ]
642+
643+ # Build settings object
644+ settings = {
645+ 'default_channels' : default_channels ,
646+ 'telegram_bot_token' : str (data .get ('telegram_bot_token' ) or '' ).strip (),
647+ 'telegram_chat_id' : str (data .get ('telegram_chat_id' ) or '' ).strip (),
648+ 'email' : str (data .get ('email' ) or '' ).strip (),
649+ 'discord_webhook' : str (data .get ('discord_webhook' ) or '' ).strip (),
650+ 'webhook_url' : str (data .get ('webhook_url' ) or '' ).strip (),
651+ 'phone' : str (data .get ('phone' ) or '' ).strip (),
652+ }
653+
654+ # Remove empty values (but keep default_channels and telegram_bot_token even if partially filled)
655+ settings = {k : v for k , v in settings .items () if v or k == 'default_channels' }
656+
657+ settings_json = json .dumps (settings , ensure_ascii = False )
658+
659+ with get_db_connection () as db :
660+ cur = db .cursor ()
661+ cur .execute (
662+ "UPDATE qd_users SET notification_settings = ?, updated_at = NOW() WHERE id = ?" ,
663+ (settings_json , user_id )
664+ )
665+ db .commit ()
666+ cur .close ()
667+
668+ return jsonify ({
669+ 'code' : 1 ,
670+ 'msg' : 'Notification settings updated' ,
671+ 'data' : settings
672+ })
673+ except Exception as e :
674+ logger .error (f"update_notification_settings failed: { e } " )
675+ return jsonify ({'code' : 0 , 'msg' : str (e ), 'data' : None }), 500
676+
677+
532678@user_bp .route ('/change-password' , methods = ['POST' ])
533679@login_required
534680def change_password ():
0 commit comments