Skip to content

Commit

Permalink
Click to expand.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Oct 17, 2024
1 parent ab0c94e commit ef6e107
Showing 1 changed file with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
}
else
{
<text>Selected user: <b>@selectedUser.Id</b></text>
<text>
Selected user: <b>@selectedUser.Id</b>
&nbsp;
<button class="btn btn-primary" @onclick="Expand">Expand with their followings</button>
&nbsp; <small>(We only show 50 followings for each user)</small>
</text>
}
</p>

Expand All @@ -31,7 +36,8 @@
NodeColorMapper="n => n.Color"
NodeImageMapper="n => n.Image"
NodeRepulsionMapper="_ => 1200"
EdgeSpringConstantMapper="_ => 0.8"
EdgeSpringConstantMapper="_ => 0.7"
EdgeSpringLengthMapper="_ => 200"
EdgeFromMapper="e => e.from"
EdgeToMapper="e => e.to"
EdgeWidthMapper="_ => 5"
Expand All @@ -43,6 +49,8 @@
private bool running = true;
private string? error;
private User? selectedUser;
List<Follow> edges = [];
List<User> users = [];

protected override async Task OnAfterRenderAsync(bool firstRender)
{
Expand All @@ -54,34 +62,11 @@

string primaryUserId = "@[email protected]";

var response = await httpClient.GetAsync($"https://kristoffer-strube.dk/API/mastodon/Following/{primaryUserId}");

if (!response.IsSuccessStatusCode)
{
error = await response.Content.ReadAsStringAsync();
return;
}

var followingUrls = await response.Content.ReadFromJsonAsync<string[]>();
var following = followingUrls!.Select(url => url[8..].Split("/users/")).Select(parts => new User($"@{parts[1]}@{parts[0]}", "#7070f0"));

User primaryUser = new(primaryUserId, "#70f070", size: 50);
await GetImageForUser(primaryUser);
users.Add(primaryUser);

List<User> users = [primaryUser, .. following];

Task.WhenAll(users.Select(async user =>
{
var response = await httpClient.GetAsync($"https://kristoffer-strube.dk/API/mastodon/Profile/{user.Id}");

if (!response.IsSuccessStatusCode)
return;

Person? person = await response.Content.ReadFromJsonAsync<Person>();

user.Image = person?.Icon?.FirstOrDefault() is Image { } image ? image.Url.FirstOrDefault()?.Href?.ToString() : null;
}));

List<Follow> edges = following.Select(f => new Follow(primaryUser, f)).ToList();
await AddMoreUsers(primaryUser, 30);

await GraphEditor.LoadGraph(users, edges);

Expand All @@ -100,6 +85,59 @@
}
}

public async Task Expand()
{
await AddMoreUsers(selectedUser!, 20);
await GraphEditor.UpdateGraph(users, edges);
GraphEditor.MoveEdgesToBack();
}

private async Task AddMoreUsers(User fromUser, int size)
{
var response = await httpClient.GetAsync($"https://kristoffer-strube.dk/API/mastodon/Following/{fromUser.Id}");

if (!response.IsSuccessStatusCode)
{
error = await response.Content.ReadAsStringAsync();
return;
}

var followingUrls = await response.Content.ReadFromJsonAsync<string[]>();
var following = followingUrls!
.Select(url => url.Length > 12 && url[8..].Split("/users/") is { Length: 2 } parts ? parts : null)
.Where(parts => parts is not null)
.Select(parts => new User($"@{parts[1]}@{parts[0]}", "#7070f0", size: size))

Check warning on line 109 in samples/KristofferStrube.Blazor.GraphEditor.WasmExample/Pages/FollowingGraph.razor

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
.OrderByDescending(u => users.Any(existing => existing.Equals(u)) ? 1 : 0)
.Take(50)
.ToList();
var allNewUsers = following.Where(f => !users.Any(u => u.Equals(f))).ToList();

users = [.. users, .. allNewUsers];

foreach (var follow in following)
{
var newEdge = new Follow(fromUser, follow);
if (!edges.Contains(newEdge))
{
edges.Add(new Follow(fromUser, follow));
}
}

Task.WhenAll(allNewUsers.Select(GetImageForUser));

Check warning on line 126 in samples/KristofferStrube.Blazor.GraphEditor.WasmExample/Pages/FollowingGraph.razor

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}

public async Task GetImageForUser(User user)
{
var response = await httpClient.GetAsync($"https://kristoffer-strube.dk/API/mastodon/Profile/{user.Id}");

if (!response.IsSuccessStatusCode)
return;

Person? person = await response.Content.ReadFromJsonAsync<Person>();

user.Image = person?.Icon?.FirstOrDefault() is Image { } image ? image.Url.FirstOrDefault()?.Href?.ToString() : null;

Check warning on line 138 in samples/KristofferStrube.Blazor.GraphEditor.WasmExample/Pages/FollowingGraph.razor

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'source' in 'ILink? Enumerable.FirstOrDefault<ILink>(IEnumerable<ILink> source)'.
}

public class User(string id, string color, float size = 30) : IEquatable<User>
{
public string Id = id;
Expand Down

0 comments on commit ef6e107

Please sign in to comment.