Add journey sharing and collaboration
This commit is contained in:
1 parent
770ab0e5df
commit
c136f01f2e
4 files changed
+262
-17
No files matched your search
+48
-8
@@ -67,6 +67,27 @@ def get_user_by_id(user_id):
|
||||
return next((u for u in users if u["id"] == user_id), None)
|
||||
|
||||
|
||||
def public_user(user):
|
||||
return {
|
||||
"id": user["id"],
|
||||
"username": user["username"],
|
||||
}
|
||||
|
||||
|
||||
def normalize_user_ids(user_ids):
|
||||
users = load_users()
|
||||
valid_ids = {user["id"] for user in users}
|
||||
normalized = []
|
||||
for user_id in user_ids or []:
|
||||
try:
|
||||
user_id = int(user_id)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if user_id in valid_ids and user_id not in normalized:
|
||||
normalized.append(user_id)
|
||||
return normalized
|
||||
|
||||
|
||||
def allowed_image_file(filename):
|
||||
if "." not in filename:
|
||||
return False
|
||||
@@ -142,6 +163,19 @@ def me():
|
||||
return jsonify({"error": "User not found"}), 401
|
||||
return jsonify({"id": user["id"], "username": user["username"]})
|
||||
|
||||
|
||||
@app.route("/api/users", methods=["GET"])
|
||||
def get_users():
|
||||
if not require_login():
|
||||
return jsonify({"error": "Authentication required"}), 401
|
||||
current_user_id = get_current_user_id()
|
||||
users = [
|
||||
public_user(user)
|
||||
for user in load_users()
|
||||
if user["id"] != current_user_id
|
||||
]
|
||||
return jsonify(users)
|
||||
|
||||
# ==================== Journey helper functions ====================
|
||||
|
||||
def load_all_journeys():
|
||||
@@ -176,6 +210,8 @@ def user_can_view_journey(journey, user_id):
|
||||
return True
|
||||
if journey.get('visibility') == 'shared' and user_id in journey.get('shared_read', []):
|
||||
return True
|
||||
if journey.get('visibility') == 'shared' and user_id in journey.get('shared_edit', []):
|
||||
return True
|
||||
return False
|
||||
|
||||
def user_can_edit_journey(journey, user_id):
|
||||
@@ -226,8 +262,8 @@ def create_journey():
|
||||
'markers': data.get('markers', []),
|
||||
'created_at': datetime.now().isoformat(),
|
||||
'visibility': data.get('visibility', 'private'),
|
||||
'shared_read': data.get('shared_read', []),
|
||||
'shared_edit': data.get('shared_edit', []),
|
||||
'shared_read': normalize_user_ids(data.get('shared_read', [])),
|
||||
'shared_edit': normalize_user_ids(data.get('shared_edit', [])),
|
||||
'comments': data.get('comments', [])
|
||||
}
|
||||
|
||||
@@ -266,12 +302,16 @@ def update_journey(journey_id):
|
||||
journey['description'] = data['description']
|
||||
if 'markers' in data:
|
||||
journey['markers'] = data['markers']
|
||||
if 'visibility' in data:
|
||||
journey['visibility'] = data['visibility']
|
||||
if 'shared_read' in data:
|
||||
journey['shared_read'] = data['shared_read']
|
||||
if 'shared_edit' in data:
|
||||
journey['shared_edit'] = data['shared_edit']
|
||||
sharing_fields = {'visibility', 'shared_read', 'shared_edit'}
|
||||
if sharing_fields.intersection(data.keys()):
|
||||
if journey['owner_id'] != user_id:
|
||||
return jsonify({'error': 'Only the owner can update sharing settings'}), 403
|
||||
if 'visibility' in data:
|
||||
journey['visibility'] = data['visibility']
|
||||
if 'shared_read' in data:
|
||||
journey['shared_read'] = normalize_user_ids(data['shared_read'])
|
||||
if 'shared_edit' in data:
|
||||
journey['shared_edit'] = normalize_user_ids(data['shared_edit'])
|
||||
if 'comments' in data:
|
||||
journey['comments'] = data ['comments']
|
||||
|
||||
|
||||
Reference in new issue
Block a user