public async Task AddContextToMastodonUser(MastodonUser mastodonUser, MastodonUserContextOptions mastodonUserContextOptions = null) { var effectiveMastodonUserContext = mastodonUserContextOptions ?? new MastodonUserContextOptions(); var mastodonClient = GetOrCreateMastodonClient(); if (effectiveMastodonUserContext.IncludeFollowers) { // Get the follower of this user mastodonUser.Followers = (await mastodonClient.GetAccountFollowers(mastodonUser.MastodonUserId.ToLong())) .Select(u => u.ToMastodonUser().Then(mu => mu.FollowsRelevantUser = true)).ToList(); mastodonUser.IsFollowedByActiveUser = mastodonUser.Followers.Any(f => f.MastodonUserId == UserMastodonConnectionDetails.MastodonUserID); } if (effectiveMastodonUserContext.IncludeFollowing) { // Get the users this user us following mastodonUser.Following = (await mastodonClient.GetAccountFollowing(mastodonUser.MastodonUserId.ToLong())) .Select(u => u.ToMastodonUser().Then(mu => mu.IsFollowedByRelevantUser = true)).ToList(); mastodonUser.FollowsActiveUser = mastodonUser.Following.Any(f => f.MastodonUserId == UserMastodonConnectionDetails.MastodonUserID); } if ((effectiveMastodonUserContext.IncludeIsFollowedByActiveUser || effectiveMastodonUserContext.IncludeFollowsActiveUser) && (!mastodonUser.IsFollowedByActiveUser.HasValue || !mastodonUser.FollowsActiveUser.HasValue)) { // We haven't gotten any follow data, but we still need to know if this user has a relationship with the active user var userRelationships = (await mastodonClient.GetAccountRelationships(mastodonUser.MastodonUserId.ToLong())).ToArray(); mastodonUser.FollowsActiveUser = userRelationships.Length == 1 ? userRelationships[0].FollowedBy : false; mastodonUser.IsFollowedByActiveUser = userRelationships.Length == 1 ? userRelationships[0].Following : false; } }
public static bool IsFollowedBy(this MastodonUser mastodonUser, string mastodonUserID) { if (mastodonUser.Followers == null) { throw new ArgumentNullException(nameof(mastodonUser.Followers), "The list of users that follow this user is null. Make sure to ask for following users when you query the API"); } return(mastodonUser.Followers.Any(f => f.MastodonUserId == mastodonUserID)); }
private Task AddContextToMastodonUser(MastodonUser mastodonUser, MastodonUserContextOptions mastodonUserContextOptions) { var effectiveMastodonUserContextOptions = mastodonUserContextOptions ?? new MastodonUserContextOptions(); if (effectiveMastodonUserContextOptions.IncludeFollowers) { mastodonUser.Followers = GetFollowerUsers(mastodonUser.MastodonUserId).ToList(); } if (effectiveMastodonUserContextOptions.IncludeFollowing) { mastodonUser.Following = GetFollowingUsers(mastodonUser.MastodonUserId).ToList(); } if (effectiveMastodonUserContextOptions.IncludeFollowsActiveUser) { mastodonUser.FollowsActiveUser = GetFollowingUserIDs(mastodonUser.MastodonUserId).Contains(ActiveUserID); } if (effectiveMastodonUserContextOptions.IncludeIsFollowedByActiveUser) { mastodonUser.IsFollowedByActiveUser = GetFollowingUserIDs(ActiveUserID).Contains(ActiveUserID); } return(Task.CompletedTask); }
public void IsFollowedByTest() { // Create Mastodon User MastodonUser user = new MastodonUser { MastodonUserId = "12", Followers = new List <MastodonUser> { new MastodonUser { MastodonUserId = "55" } } }; // Perform test Assert.True(user.IsFollowedBy("55")); // Make user with null Following MastodonUser userNullFollowers = new MastodonUser { MastodonUserId = "15" }; // Perform null catch test bool passed = false; try { userNullFollowers.IsFollowedBy("67"); } catch { passed = true; } Assert.True(passed); }