added users count

This commit is contained in:
Nicolas Constant 2020-12-30 01:43:26 -05:00
parent 47649392ed
commit 4b05123bd3
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
7 changed files with 89 additions and 4 deletions

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Models;
using BirdsiteLive.Models.WellKnownModels;
using BirdsiteLive.Twitter;
@ -15,12 +16,14 @@ namespace BirdsiteLive.Controllers
public class WellKnownController : ControllerBase
{
private readonly ITwitterService _twitterService;
private readonly ITwitterUserDal _twitterUserDal;
private readonly InstanceSettings _settings;
#region Ctor
public WellKnownController(InstanceSettings settings, ITwitterService twitterService)
public WellKnownController(InstanceSettings settings, ITwitterService twitterService, ITwitterUserDal twitterUserDal)
{
_twitterService = twitterService;
_twitterUserDal = twitterUserDal;
_settings = settings;
}
#endregion
@ -48,9 +51,10 @@ namespace BirdsiteLive.Controllers
}
[Route("/nodeinfo/{id}.json")]
public IActionResult NodeInfo(string id)
public async Task<IActionResult> NodeInfo(string id)
{
var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(3);
var twitterUsersCount = await _twitterUserDal.GetTwitterUsersCountAsync();
if (id == "2.0")
{
@ -62,7 +66,7 @@ namespace BirdsiteLive.Controllers
localPosts = 0,
users = new Users()
{
total = 0
total = twitterUsersCount
}
},
software = new Software()
@ -97,7 +101,7 @@ namespace BirdsiteLive.Controllers
localPosts = 0,
users = new Users()
{
total = 0
total = twitterUsersCount
}
},
software = new SoftwareV21()

View file

@ -40,6 +40,19 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
}
}
public async Task<int> GetFollowersCountAsync()
{
var query = $"SELECT COUNT(*) FROM {_settings.FollowersTableName}";
using (var dbConnection = Connection)
{
dbConnection.Open();
var result = (await dbConnection.QueryAsync<int>(query)).FirstOrDefault();
return result;
}
}
public async Task<Follower> GetFollowerAsync(string acct, string host)
{
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE acct = @acct AND host = @host";

View file

@ -49,6 +49,19 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
}
}
public async Task<int> GetTwitterUsersCountAsync()
{
var query = $"SELECT COUNT(*) FROM {_settings.TwitterUserTableName}";
using (var dbConnection = Connection)
{
dbConnection.Open();
var result = (await dbConnection.QueryAsync<int>(query)).FirstOrDefault();
return result;
}
}
public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync()
{
var query = $"SELECT * FROM {_settings.TwitterUserTableName}";

View file

@ -13,5 +13,6 @@ namespace BirdsiteLive.DAL.Contracts
Task UpdateFollowerAsync(Follower follower);
Task DeleteFollowerAsync(int id);
Task DeleteFollowerAsync(string acct, string host);
Task<int> GetFollowersCountAsync();
}
}

View file

@ -10,5 +10,6 @@ namespace BirdsiteLive.DAL.Contracts
Task<SyncTwitterUser[]> GetAllTwitterUsersAsync();
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId);
Task DeleteTwitterUserAsync(string acct);
Task<int> GetTwitterUsersCountAsync();
}
}

View file

@ -137,6 +137,43 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
Assert.AreEqual(0, result.Length);
}
[TestMethod]
public async Task CountFollowersAsync()
{
var dal = new FollowersPostgresDal(_settings);
var result = await dal.GetFollowersCountAsync();
Assert.AreEqual(0, result);
//User 1
var acct = "myhandle1";
var host = "domain.ext";
var following = new[] { 1, 2, 3 };
var followingSync = new Dictionary<int, long>();
var inboxRoute = "/myhandle1/inbox";
var sharedInboxRoute = "/inbox";
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
//User 2
acct = "myhandle2";
host = "domain.ext";
following = new[] { 2, 4, 5 };
inboxRoute = "/myhandle2/inbox";
sharedInboxRoute = "/inbox2";
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
//User 2
acct = "myhandle3";
host = "domain.ext";
following = new[] { 1 };
inboxRoute = "/myhandle3/inbox";
sharedInboxRoute = "/inbox3";
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
result = await dal.GetFollowersCountAsync();
Assert.AreEqual(3, result);
}
[TestMethod]
public async Task CreateUpdateAndGetFollower_Add()
{

View file

@ -115,5 +115,21 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
Assert.IsFalse(result[0].LastTweetPostedId == default);
Assert.IsFalse(result[0].LastTweetSynchronizedForAllFollowersId == default);
}
[TestMethod]
public async Task CountTwitterUsers()
{
var dal = new TwitterUserPostgresDal(_settings);
for (var i = 0; i < 10; i++)
{
var acct = $"myid{i}";
var lastTweetId = 1548L;
await dal.CreateTwitterUserAsync(acct, lastTweetId);
}
var result = await dal.GetTwitterUsersCountAsync();
Assert.AreEqual(10, result);
}
}
}