2020-07-06 06:56:26 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using BirdsiteLive.DAL.Contracts ;
using BirdsiteLive.DAL.Models ;
using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base ;
using BirdsiteLive.DAL.Postgres.Settings ;
using Dapper ;
using Newtonsoft.Json ;
2020-07-06 02:22:34 +02:00
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
{
2020-07-06 06:56:26 +02:00
public class FollowersPostgresDal : PostgresBase , IFollowersDal
2020-07-06 02:22:34 +02:00
{
2020-07-06 06:56:26 +02:00
#region Ctor
public FollowersPostgresDal ( PostgresSettings settings ) : base ( settings )
{
}
#endregion
2021-02-12 06:31:00 +01:00
public async Task CreateFollowerAsync ( string acct , string host , string inboxRoute , string sharedInboxRoute , string actorId , int [ ] followings = null , Dictionary < int , long > followingSyncStatus = null )
2020-07-06 06:56:26 +02:00
{
2020-07-08 03:03:20 +02:00
if ( followings = = null ) followings = new int [ 0 ] ;
if ( followingSyncStatus = = null ) followingSyncStatus = new Dictionary < int , long > ( ) ;
2020-07-06 06:56:26 +02:00
var serializedDic = JsonConvert . SerializeObject ( followingSyncStatus ) ;
acct = acct . ToLowerInvariant ( ) ;
host = host . ToLowerInvariant ( ) ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
await dbConnection . ExecuteAsync (
2021-02-12 06:31:00 +01:00
$"INSERT INTO {_settings.FollowersTableName} (acct,host,inboxRoute,sharedInboxRoute,followings,followingsSyncStatus,actorId) VALUES(@acct,@host,@inboxRoute,@sharedInboxRoute,@followings,CAST(@followingsSyncStatus as json),@actorId)" ,
new { acct , host , inboxRoute , sharedInboxRoute , followings , followingsSyncStatus = serializedDic , actorId } ) ;
2020-07-06 06:56:26 +02:00
}
}
2020-12-30 07:43:26 +01:00
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 ;
}
}
2021-09-12 01:16:52 +02:00
public async Task < int > GetFailingFollowersCountAsync ( )
{
var query = $"SELECT COUNT(*) FROM {_settings.FollowersTableName} WHERE postingErrorCount > 0" ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
var result = ( await dbConnection . QueryAsync < int > ( query ) ) . FirstOrDefault ( ) ;
return result ;
}
}
2020-07-06 06:56:26 +02:00
public async Task < Follower > GetFollowerAsync ( string acct , string host )
{
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE acct = @acct AND host = @host" ;
acct = acct . ToLowerInvariant ( ) ;
host = host . ToLowerInvariant ( ) ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
var result = ( await dbConnection . QueryAsync < SerializedFollower > ( query , new { acct , host } ) ) . FirstOrDefault ( ) ;
return Convert ( result ) ;
}
}
2023-01-11 07:13:23 +01:00
public async Task < Follower > GetFollowerAsync ( string actorId )
{
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE actorid = @actorid" ;
actorId = actorId . ToLowerInvariant ( ) . Trim ( ) ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
var result = ( await dbConnection . QueryAsync < SerializedFollower > ( query , new { actorId } ) ) . FirstOrDefault ( ) ;
return Convert ( result ) ;
}
}
2020-07-06 06:56:26 +02:00
public async Task < Follower [ ] > GetFollowersAsync ( int followedUserId )
{
if ( followedUserId = = default ) throw new ArgumentException ( "followedUserId" ) ;
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE @id=ANY(followings)" ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
var result = await dbConnection . QueryAsync < SerializedFollower > ( query , new { id = followedUserId } ) ;
return result . Select ( Convert ) . ToArray ( ) ;
}
}
2021-02-05 07:12:54 +01:00
public async Task < Follower [ ] > GetAllFollowersAsync ( )
{
2021-02-13 02:10:03 +01:00
var query = $"SELECT * FROM {_settings.FollowersTableName}" ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
var result = await dbConnection . QueryAsync < SerializedFollower > ( query ) ;
return result . Select ( Convert ) . ToArray ( ) ;
}
2021-02-05 07:12:54 +01:00
}
2020-07-08 03:03:20 +02:00
public async Task UpdateFollowerAsync ( Follower follower )
2020-07-06 06:56:26 +02:00
{
2020-07-08 03:03:20 +02:00
if ( follower = = default ) throw new ArgumentException ( "follower" ) ;
if ( follower . Id = = default ) throw new ArgumentException ( "id" ) ;
2020-07-06 06:56:26 +02:00
2020-07-08 03:03:20 +02:00
var serializedDic = JsonConvert . SerializeObject ( follower . FollowingsSyncStatus ) ;
2021-09-11 00:53:11 +02:00
var query = $"UPDATE {_settings.FollowersTableName} SET followings = @followings, followingsSyncStatus = CAST(@followingsSyncStatus as json), postingErrorCount = @postingErrorCount WHERE id = @id" ;
2020-07-06 06:56:26 +02:00
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
2021-09-11 00:53:11 +02:00
await dbConnection . QueryAsync ( query , new { follower . Id , follower . Followings , followingsSyncStatus = serializedDic , postingErrorCount = follower . PostingErrorCount } ) ;
2020-07-06 06:56:26 +02:00
}
}
public async Task DeleteFollowerAsync ( int id )
{
if ( id = = default ) throw new ArgumentException ( "id" ) ;
var query = $"DELETE FROM {_settings.FollowersTableName} WHERE id = @id" ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
await dbConnection . QueryAsync ( query , new { id } ) ;
}
}
public async Task DeleteFollowerAsync ( string acct , string host )
{
2021-02-13 02:10:03 +01:00
if ( string . IsNullOrWhiteSpace ( acct ) ) throw new ArgumentException ( "acct" ) ;
if ( string . IsNullOrWhiteSpace ( host ) ) throw new ArgumentException ( "host" ) ;
2020-07-06 06:56:26 +02:00
acct = acct . ToLowerInvariant ( ) ;
host = host . ToLowerInvariant ( ) ;
var query = $"DELETE FROM {_settings.FollowersTableName} WHERE acct = @acct AND host = @host" ;
using ( var dbConnection = Connection )
{
dbConnection . Open ( ) ;
await dbConnection . QueryAsync ( query , new { acct , host } ) ;
}
}
private Follower Convert ( SerializedFollower follower )
{
if ( follower = = null ) return null ;
return new Follower ( )
{
Id = follower . Id ,
Acct = follower . Acct ,
Host = follower . Host ,
2020-08-11 02:04:12 +02:00
InboxRoute = follower . InboxRoute ,
2021-02-13 02:10:03 +01:00
ActorId = follower . ActorId ,
2020-08-11 02:04:12 +02:00
SharedInboxRoute = follower . SharedInboxRoute ,
2020-07-08 03:03:20 +02:00
Followings = follower . Followings . ToList ( ) ,
2021-09-11 00:53:11 +02:00
FollowingsSyncStatus = JsonConvert . DeserializeObject < Dictionary < int , long > > ( follower . FollowingsSyncStatus ) ,
PostingErrorCount = follower . PostingErrorCount
2020-07-06 06:56:26 +02:00
} ;
}
}
internal class SerializedFollower {
public int Id { get ; set ; }
public int [ ] Followings { get ; set ; }
public string FollowingsSyncStatus { get ; set ; }
public string Acct { get ; set ; }
public string Host { get ; set ; }
2020-08-11 02:04:12 +02:00
public string InboxRoute { get ; set ; }
public string SharedInboxRoute { get ; set ; }
2021-02-13 02:10:03 +01:00
public string ActorId { get ; set ; }
2021-09-11 00:53:11 +02:00
public int PostingErrorCount { get ; set ; }
2020-07-06 02:22:34 +02:00
}
}