Skip to content

Commit

Permalink
Add search for user #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Scobiform committed Apr 20, 2024
1 parent c1c9389 commit 471a910
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
13 changes: 13 additions & 0 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand Down
15 changes: 15 additions & 0 deletions templates/graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
</script>
14 changes: 9 additions & 5 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

</script>
Expand Down

0 comments on commit 471a910

Please sign in to comment.