From 6bd289b291b3366da3421d52f432e01891af80dd Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Fri, 14 Apr 2023 15:36:53 -0400 Subject: [PATCH] added follower count --- src/BirdsiteLive/Controllers/UsersController.cs | 16 +++++++++++++++- src/BirdsiteLive/Models/DisplayTwitterUser.cs | 2 ++ src/BirdsiteLive/Views/Users/Index.cshtml | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index 32b1fa4..5392068 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -11,6 +11,8 @@ using BirdsiteLive.ActivityPub; using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Regexes; using BirdsiteLive.Common.Settings; +using BirdsiteLive.DAL.Contracts; +using BirdsiteLive.DAL.Models; using BirdsiteLive.Domain; using BirdsiteLive.Models; using BirdsiteLive.Tools; @@ -30,16 +32,20 @@ namespace BirdsiteLive.Controllers private readonly IUserService _userService; private readonly IStatusService _statusService; private readonly InstanceSettings _instanceSettings; + private readonly IFollowersDal _followersDal; + private readonly ITwitterUserDal _twitterUserDal; private readonly ILogger _logger; #region Ctor - public UsersController(ICachedTwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ICachedTwitterTweetsService twitterTweetService, ILogger logger) + public UsersController(ICachedTwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ICachedTwitterTweetsService twitterTweetService, IFollowersDal followersDal, ITwitterUserDal twitterUserDal, ILogger logger) { _twitterUserService = twitterUserService; _userService = userService; _statusService = statusService; _instanceSettings = instanceSettings; _twitterTweetService = twitterTweetService; + _followersDal = followersDal; + _twitterUserDal = twitterUserDal; _logger = logger; } #endregion @@ -119,6 +125,12 @@ namespace BirdsiteLive.Controllers if (isSaturated) return View("ApiSaturated"); if (notFound) return View("UserNotFound"); + Follower[] followers = new Follower[] { }; + + var userDal = await _twitterUserDal.GetTwitterUserAsync(user.Acct); + if (userDal != null) + followers = await _followersDal.GetFollowersAsync(userDal.Id); + var displayableUser = new DisplayTwitterUser { Name = user.Name, @@ -127,6 +139,8 @@ namespace BirdsiteLive.Controllers Url = user.Url, ProfileImageUrl = user.ProfileImageUrl, Protected = user.Protected, + FollowerCount = followers.Length, + MostPopularServer = followers.GroupBy(x => x.Host).OrderByDescending(x => x.Count()).Select(x => x.Key).FirstOrDefault("N/A"), InstanceHandle = $"@{user.Acct.ToLowerInvariant()}@{_instanceSettings.Domain}" }; diff --git a/src/BirdsiteLive/Models/DisplayTwitterUser.cs b/src/BirdsiteLive/Models/DisplayTwitterUser.cs index 3a93875..a91a24d 100644 --- a/src/BirdsiteLive/Models/DisplayTwitterUser.cs +++ b/src/BirdsiteLive/Models/DisplayTwitterUser.cs @@ -8,6 +8,8 @@ public string Url { get; set; } public string ProfileImageUrl { get; set; } public bool Protected { get; set; } + public int FollowerCount { get; set; } + public string MostPopularServer { get; set; } public string InstanceHandle { get; set; } } diff --git a/src/BirdsiteLive/Views/Users/Index.cshtml b/src/BirdsiteLive/Views/Users/Index.cshtml index b99d3a5..a70e847 100644 --- a/src/BirdsiteLive/Views/Users/Index.cshtml +++ b/src/BirdsiteLive/Views/Users/Index.cshtml @@ -28,6 +28,9 @@
+
+ This account has @ViewData.Model.FollowerCount followers on the fediverse. The server with the most followers for this account is: @ViewData.Model.MostPopularServer +

@if (ViewData.Model.Protected)