|
5 | 5 | db = firestore.client()
|
6 | 6 |
|
7 | 7 | @privacy_group_bp.route('/auth/group/privacy', methods=['PATCH'])
|
8 |
| -def set_privacy(): |
9 |
| - """ |
10 |
| - Route to set a group's privacy (private or public). Only the group creator can update the privacy. |
11 |
| - """ |
| 8 | +def update_group_privacy(): |
12 | 9 | id_token = request.headers.get('Authorization')
|
13 |
| - |
14 | 10 | if not id_token:
|
15 | 11 | return jsonify({"error": "Missing Firebase ID token"}), 401
|
16 | 12 |
|
17 | 13 | try:
|
18 | 14 | decoded_token = auth.verify_id_token(id_token)
|
19 | 15 | user_id = decoded_token['uid']
|
20 | 16 |
|
21 |
| - group_data = request.json |
22 |
| - if not group_data or 'group_id' not in group_data or 'privacy' not in group_data: |
23 |
| - return jsonify({"error": "Missing 'group_id' or 'privacy' in request body"}), 400 |
| 17 | + data = request.json |
| 18 | + group_id = data.get("group_id") |
| 19 | + privacy = data.get("privacy") |
24 | 20 |
|
25 |
| - group_id = group_data['group_id'] |
26 |
| - privacy = group_data['privacy'].lower() |
| 21 | + if not group_id or not privacy: |
| 22 | + return jsonify({"error": "Missing 'group_id' or 'privacy' in request body"}), 400 |
27 | 23 |
|
28 |
| - if privacy not in ['private', 'public']: |
29 |
| - return jsonify({"error": "Invalid privacy value. Must be 'private' or 'public'"}), 400 |
| 24 | + if privacy not in ["private", "public"]: |
| 25 | + return jsonify({"error": "Invalid privacy setting"}), 400 |
30 | 26 |
|
31 | 27 | group_ref = db.collection('groups').document(group_id)
|
32 |
| - group = group_ref.get() |
33 |
| - |
34 |
| - if not group.exists: |
35 |
| - return jsonify({"error": "Group not found"}), 404 |
36 |
| - |
37 |
| - group_data = group.to_dict() |
38 |
| - |
39 |
| - if group_data['created_by'] != user_id: |
40 |
| - return jsonify({"error": "Only the group creator can update the privacy"}), 403 |
41 |
| - |
42 |
| - group_ref.update({"privacy": privacy}) |
43 |
| - return jsonify({"success": True, "message": f"Group privacy set to {privacy}"}), 200 |
| 28 | + group_data = group_ref.get() |
44 | 29 |
|
45 |
| - except auth.InvalidIdTokenError: |
46 |
| - return jsonify({"error": "Invalid Firebase ID token"}), 401 |
| 30 | + if group_data.exists and group_data.to_dict().get("created_by") == user_id: |
| 31 | + group_ref.update({"privacy": privacy}) |
| 32 | + return jsonify({"success": True, "message": "Group privacy updated"}), 200 |
| 33 | + else: |
| 34 | + return jsonify({"error": "Group not found or insufficient permissions"}), 403 |
47 | 35 | except Exception as e:
|
48 | 36 | return jsonify({"error": str(e)}), 500
|
0 commit comments