added get threads endpoint
This commit is contained in:
1 parent
af7d682a38
commit
c9cf3412d8
5 files changed
+37
-3
No files matched your search
+25
-2
@@ -1,10 +1,12 @@
|
||||
import time
|
||||
import dataclasses
|
||||
|
||||
import flask
|
||||
import flask_login
|
||||
|
||||
from . import helpers
|
||||
import database
|
||||
import mgU
|
||||
|
||||
|
||||
forum = flask.Blueprint(
|
||||
@@ -42,6 +44,27 @@ def send_message() -> tuple[str, int]:
|
||||
|
||||
@forum.route('/forum/get_threads')
|
||||
@flask_login.login_required
|
||||
def get_forum_threads() -> tuple[str, int]:
|
||||
return '', 200
|
||||
def get_forum_threads() -> tuple[flask.Response, int]:
|
||||
"""
|
||||
Get all threads from forums the current user has access to.
|
||||
"""
|
||||
user_id: str = flask_login.current_user.get_id()
|
||||
|
||||
# Get all forums the user has access to
|
||||
user_forums: list[mgU.Forum] = database.get_user_forums(int(user_id))
|
||||
|
||||
if not user_forums:
|
||||
return flask.jsonify({"threads": []}), 200
|
||||
|
||||
# Get all threads from all user's forums
|
||||
all_threads: list[mgU.Thread] = []
|
||||
for forum in user_forums:
|
||||
if forum.id: # Make sure forum has an ID
|
||||
forum_threads = database.get_forum_threads(forum.id)
|
||||
all_threads.extend(forum_threads)
|
||||
|
||||
# Convert threads to dictionary format
|
||||
threads_dict = {"threads": [dataclasses.asdict(thread) for thread in all_threads]}
|
||||
|
||||
return flask.jsonify(threads_dict), 200
|
||||
|
||||
Reference in new issue
Block a user