added users count
This commit is contained in:
parent
47649392ed
commit
4b05123bd3
7 changed files with 89 additions and 4 deletions
|
@ -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()
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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}";
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue