Пример #1
0
        private IEnumerable <MastodonUser> GetUsers(MastodonUserContextOptions mastodonUserContextOptions, PagingOptions pagingOptions)
        {
            var mastodonUsers = _followRelationships.Keys.Select(BuildUser).ToArray();

            AddContextToMastodonUsers(mastodonUsers, mastodonUserContextOptions).Synchronously();
            return(mastodonUsers);
        }
Пример #2
0
        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;
            }
        }
Пример #3
0
 public async Task<MastodonUser> GetMastodonAccount(string id, MastodonUserContextOptions mastodonUserContextOptions = null)
 {
     var mastodonClient = GetOrCreateMastodonClient();
     var mastodonUser = (await mastodonClient.GetAccount(id.ToLong())).ToMastodonUser();
     await AddContextToMastodonUser(mastodonUser, mastodonUserContextOptions);
     return mastodonUser;
 }
Пример #4
0
 public async Task<MastodonUser> GetActiveUserMastodonAccount(MastodonUserContextOptions mastodonUserContextOptions = null)
 {
     var mastodonClient = GetOrCreateMastodonClient();
     var mastodonUser = (await mastodonClient.GetCurrentUser()).ToMastodonUser();
     await AddContextToMastodonUser(mastodonUser, mastodonUserContextOptions);
     return mastodonUser;
 }
Пример #5
0
        private async Task <MastodonUser> GetUserByID(string id, MastodonUserContextOptions mastodonUserContextOptions)
        {
            var user = BuildUser(id);

            await AddContextToMastodonUser(user, mastodonUserContextOptions);

            return(user);
        }
Пример #6
0
 public async Task<IList<MastodonUser>> GetUsersByName(string name, MastodonUserContextOptions mastodonUserContextOptions = null, PagingOptions pagingOptions = null)
 {
     var effectivePagingOptions = pagingOptions ?? new PagingOptions();
     var mastodonClient = GetOrCreateMastodonClient();
     var mastodonUsersApiTask = mastodonClient.SearchAccounts(name, effectivePagingOptions.Limit);
     var mastodonUsers = (await mastodonUsersApiTask).Select(u => u.ToMastodonUser()).ToList();
     await AddContextToMastodonUsers(mastodonUsers, mastodonUserContextOptions);
     return mastodonUsers;
 }
Пример #7
0
        private async Task <PagedList <MastodonUser> > GetFollowers(string id, MastodonUserContextOptions mastodonUserContextOptions, PagingOptions pagingOptions)
        {
            var users = GetFollowerUsers(id).ToArray();

            await AddContextToMastodonUsers(users, mastodonUserContextOptions);

            return(new PagedList <MastodonUser> {
                Elements = users
            });
        }
Пример #8
0
        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);
        }
Пример #9
0
 private Task <IList <MastodonUser> > GetUsersByName(string name, MastodonUserContextOptions mastodonUserContextOptions, PagingOptions pagingOptions)
 {
     return(Task.FromResult(GetUsers(mastodonUserContextOptions, null).Where(u => u.MastodonDisplayName.Contains(name, StringComparison.OrdinalIgnoreCase)).ToArray() as IList <MastodonUser>));
 }
Пример #10
0
 private Task <MastodonUser> GetActiveUser(MastodonUserContextOptions mastodonUserContextOptions)
 {
     return(GetUserByID(ActiveUserID, mastodonUserContextOptions));
 }
Пример #11
0
 private async Task AddContextToMastodonUsers(IEnumerable <MastodonUser> mastodonUsers, MastodonUserContextOptions mastodonUserContextOptions)
 {
     foreach (var mastodonUser in mastodonUsers)
     {
         await AddContextToMastodonUser(mastodonUser, mastodonUserContextOptions);
     }
 }
Пример #12
0
        public async Task AddContextToMastodonUsers(IEnumerable<MastodonUser> mastodonUsers, MastodonUserContextOptions mastodonUserContextOptions = null)
        {
            var effectiveMastodonUserContext = mastodonUserContextOptions ?? new MastodonUserContextOptions();
            var mastodonClient = GetOrCreateMastodonClient();

            foreach (var mastodonUser in mastodonUsers)
            {
                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.IsFollowedBy(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.Follows(UserMastodonConnectionDetails.MastodonUserID);
                }
            }

            if ((effectiveMastodonUserContext.IncludeIsFollowedByActiveUser || effectiveMastodonUserContext.IncludeFollowsActiveUser) && (!effectiveMastodonUserContext.IncludeFollowers || !effectiveMastodonUserContext.IncludeFollowing))
            {
                // We didn't fetch both the full followers and full following lists for the users. But we still need to know
                // whether these users follow or are followed by the active user. Fortunately we can make a single API call for this.
                var relationships = (await mastodonClient.GetAccountRelationships(mastodonUsers.Select(u => u.MastodonUserId.ToLong()))).Select(r => r.ToMastodonRelationship()).ToArray();
                foreach (var mastodonUser in mastodonUsers)
                {
                    var relevantRelationships = relationships.Where(r => r.ID == mastodonUser.MastodonUserId).ToArray();
                    mastodonUser.FollowsActiveUser = relevantRelationships.Any(r => r.FollowedBy);
                    mastodonUser.IsFollowedByActiveUser = relevantRelationships.Any(r => r.Following);
                }
            }
        }
Пример #13
0
 public async Task<PagedList<MastodonUser>> GetFollowers(string followingUserID, MastodonUserContextOptions mastodonUserContextOptions = null, PagingOptions pagingOptions = null)
 {
     var effectivePagingOptions = pagingOptions ?? new PagingOptions();
     var mastodonClient = GetOrCreateMastodonClient();
     var mastodonUsersApiResult = await mastodonClient.GetAccountFollowers(followingUserID.ToLong(), effectivePagingOptions.MaxID.ToNullableLong(), effectivePagingOptions.SinceID.ToNullableLong(), effectivePagingOptions.Limit);
     var result = PagedList<MastodonUser>.Create(mastodonUsersApiResult, u => u.ToMastodonUser());
     await AddContextToMastodonUsers(result.Elements, mastodonUserContextOptions);
     return result;
 }