From 36fdd92250823bf023a8cc501f26546059e27656 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 20 Oct 2025 23:15:00 +0000 Subject: [PATCH] Fix: Adjust partial index for MongoDB compatibility Feat: Add API endpoint to update user editor preference Co-authored-by: amirbiron --- database/bookmarks_manager.py | 5 ++++- webapp/app.py | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/database/bookmarks_manager.py b/database/bookmarks_manager.py index a0dbc1991..4cfe1a116 100644 --- a/database/bookmarks_manager.py +++ b/database/bookmarks_manager.py @@ -75,8 +75,11 @@ def _ensure_indexes(self): [("user_id", ASCENDING), ("file_id", ASCENDING), ("anchor_id", ASCENDING)], unique=True, name="unique_user_file_anchor", + # שים לב: יש גרסאות MongoDB שאינן תומכות ב-$ne בתוך partial index (מתורגם ל-$not). + # כדי לשמור תאימות מלאה, נשתמש בסינון שמוודא קיום וסוג מחרוזת בלבד, כאשר ערכים ריקים + # אינם נשמרים מלכתחילה (to_dict לא שומר anchor_id ריק, וב-startup אנו מבצעים unset). partialFilterExpression={ - "anchor_id": {"$type": "string", "$ne": ""} + "anchor_id": {"$exists": True, "$type": "string"} }, ), # אינדקס לחיפוש מהיר לפי משתמש וקובץ diff --git a/webapp/app.py b/webapp/app.py index 8e1151be6..3e4183f5f 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -3941,6 +3941,42 @@ def api_ui_prefs(): except Exception: return jsonify({'ok': False, 'error': 'שגיאה לא צפויה'}), 500 + +# --- User preferences (generic) --- +@app.route('/api/user/preferences', methods=['POST']) +@login_required +def update_user_preferences(): + """עדכון העדפות משתמש כלליות (כעת: סוג עורך). + + מבנה קלט צפוי (JSON): { "editor_type": "simple" | "codemirror" } + """ + try: + payload = request.get_json(silent=True) or {} + editor_type = (payload.get('editor_type') or '').strip().lower() + if editor_type not in {'simple', 'codemirror'}: + return jsonify({'ok': False, 'error': 'Invalid editor type'}), 400 + + # עדכון session כדי להשפיע מיידית ב-render + session['preferred_editor'] = editor_type + + # שמירה ב-DB תחת ui_prefs.editor לשימור חוצה דיפלוימנטים + try: + from datetime import datetime, timezone + db = get_db() + user_id = session['user_id'] + db.users.update_one( + {'user_id': user_id}, + {'$set': {'ui_prefs.editor': editor_type, 'updated_at': datetime.now(timezone.utc)}}, + upsert=True, + ) + except Exception: + # לא מפילים את הבקשה במקרה של DB בעייתי – נשמור לפחות בסשן + pass + + return jsonify({'ok': True, 'editor_type': editor_type}) + except Exception: + return jsonify({'ok': False, 'error': 'שגיאה לא צפויה'}), 500 + # --- Public statistics for landing/mini web app --- @app.route('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/api/public_stats') def api_public_stats():