From 98e869f0641b386360c8a0435598263f22f0c0eb Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sat, 11 Sep 2021 19:16:52 -0400 Subject: [PATCH] added failing follower count in DAL --- .../DataAccessLayers/FollowersPostgresDal.cs | 13 +++++ .../Contracts/IFollowersDal.cs | 1 + .../FollowersPostgresDalTests.cs | 50 ++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs index bcdda0f..db2f9f7 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs @@ -53,6 +53,19 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers } } + public async Task GetFailingFollowersCountAsync() + { + var query = $"SELECT COUNT(*) FROM {_settings.FollowersTableName} WHERE postingErrorCount > 0"; + + using (var dbConnection = Connection) + { + dbConnection.Open(); + + var result = (await dbConnection.QueryAsync(query)).FirstOrDefault(); + return result; + } + } + public async Task GetFollowerAsync(string acct, string host) { var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE acct = @acct AND host = @host"; diff --git a/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/IFollowersDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/IFollowersDal.cs index 86caa02..fe87b28 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/IFollowersDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/IFollowersDal.cs @@ -15,5 +15,6 @@ namespace BirdsiteLive.DAL.Contracts Task DeleteFollowerAsync(int id); Task DeleteFollowerAsync(string acct, string host); Task GetFollowersCountAsync(); + Task GetFailingFollowersCountAsync(); } } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/FollowersPostgresDalTests.cs b/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/FollowersPostgresDalTests.cs index 62cb3e6..a22df0f 100644 --- a/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/FollowersPostgresDalTests.cs +++ b/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/FollowersPostgresDalTests.cs @@ -237,7 +237,7 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers actorId = $"https://{host}/{acct}"; await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, actorId, following, followingSync); - //User 2 + //User 3 acct = "myhandle3"; host = "domain.ext"; following = new[] { 1 }; @@ -250,6 +250,54 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers Assert.AreEqual(3, result); } + [TestMethod] + public async Task CountFailingFollowersAsync() + { + var dal = new FollowersPostgresDal(_settings); + + var result = await dal.GetFailingFollowersCountAsync(); + Assert.AreEqual(0, result); + + //User 1 + var acct = "myhandle1"; + var host = "domain.ext"; + var following = new[] { 1, 2, 3 }; + var followingSync = new Dictionary(); + var inboxRoute = "/myhandle1/inbox"; + var sharedInboxRoute = "/inbox"; + var actorId = $"https://{host}/{acct}"; + await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, actorId, following, followingSync); + + //User 2 + acct = "myhandle2"; + host = "domain.ext"; + following = new[] { 2, 4, 5 }; + inboxRoute = "/myhandle2/inbox"; + sharedInboxRoute = "/inbox2"; + actorId = $"https://{host}/{acct}"; + await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, actorId, following, followingSync); + + var follower = await dal.GetFollowerAsync(acct, host); + follower.PostingErrorCount = 1; + await dal.UpdateFollowerAsync(follower); + + //User 3 + acct = "myhandle3"; + host = "domain.ext"; + following = new[] { 1 }; + inboxRoute = "/myhandle3/inbox"; + sharedInboxRoute = "/inbox3"; + actorId = $"https://{host}/{acct}"; + await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, actorId, following, followingSync); + + follower = await dal.GetFollowerAsync(acct, host); + follower.PostingErrorCount = 50; + await dal.UpdateFollowerAsync(follower); + + result = await dal.GetFailingFollowersCountAsync(); + Assert.AreEqual(2, result); + } + [TestMethod] public async Task CreateUpdateAndGetFollower_Add() {