Added chat list endpoint

Returns all chats that are associated with a user
This commit is contained in:
2025-09-02 17:45:44 +01:00
parent ef577c11f7
commit 08cc5dd165

20
main.py
View File

@@ -1,6 +1,7 @@
from flask import Flask, jsonify, request, render_template
from pymongo import MongoClient
from bson.objectid import ObjectId
from bson.json_util import dumps, loads
from datetime import datetime
from argon2 import PasswordHasher
import random
@@ -80,6 +81,25 @@ def checkChatPermission(token, chatId, permission):
else:
return False, "Invalid Token"
# Chat List Endpoint:
# Get all the chats associated with a user
# Arguments: token (required)
@app.route('/api/user/chats', methods = ['GET'])
def getUserChats():
# Get user auth token
token = request.json['token']
a, userId = checkUserPermission(token, True)
if (a == True):
returnedChats = list(chatCollection.find({'permissions.' + userId : "view"}))
chats = []
for doc in returnedChats:
if '_id' in doc and isinstance(doc['_id'], ObjectId):
doc['_id'] = str(doc['_id'])
chats.append(doc)
jsonChats = json.dumps(chats, indent=2)
return jsonChats
# Chat Details Endpoint:
# Get or change details about a chat using the chatId
# Arguments: token (required), details (required), model, name