diff --git a/start.py b/start.py index ea78e77..5f37212 100644 --- a/start.py +++ b/start.py @@ -151,6 +151,19 @@ async def webhook(): else: return 'Push was not to master branch', 200 +@app.route('/user', methods=['GET']) +async def fetch_user(): + ''' Fetch a user from the instance.''' + user_id = request.args.get('user_id', type=int) + if not user_id: + return jsonify({'error': 'User ID is required'}), 400 + + try: + user = mastodon.account(user_id) + return jsonify(user) + except Exception as e: + return jsonify({'error': str(e)}), 500 + @app.route('/followers', methods=['GET']) async def fetch_followers(): ''' Fetch the followers of a user.''' diff --git a/templates/graph.html b/templates/graph.html index e06debb..c10df3d 100644 --- a/templates/graph.html +++ b/templates/graph.html @@ -388,4 +388,19 @@ }); + // Funnction to fetch a user + async function fetchUser(userId) { + let user = {}; + + let url = `${apiBaseUrl}/user?user_id=${userId}`; + + try { + const response = await fetch(url); + user = await response.json(); + } catch (error) { + console.error('Failed to fetch profile:', error); + } + + return user; + } diff --git a/templates/index.html b/templates/index.html index 5157300..9d4a515 100644 --- a/templates/index.html +++ b/templates/index.html @@ -154,11 +154,15 @@ // Fetch followers and followings const followers = await fetchAllItems(userId, 'followers', apiBaseUrl); const followings = await fetchAllItems(userId, 'following', apiBaseUrl); - // Generate graph data - graphData = generateGraphData(userId, userName, userAvatar, followers, followings); - // Calculate the number of connections for each node - calculateNodeConnections(graphData.nodes, graphData.links); - window.graph = initGraph(graphData); + + fetchUser(userId).then(user => { + // Call your function to generate the graph data + const graphData = generateGraphData(userId, user.username, user.avatar, followers, followings); + // Calculate the number of connections for each node + calculateNodeConnections(graphData.nodes, graphData.links); + // Initialize the graph + window.graph = initGraph(graphData); + }); }