Skip to content

Commit

Permalink
Remove generic IOnlineClientManager and its implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ismcagdas committed Mar 21, 2024
1 parent 76cd027 commit 8588af4
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public class AbpRedisCacheOptions
public int DatabaseId { get; set; }

public string OnlineClientsStoreKey = "Abp.RealTime.OnlineClients";

/// <summary>
/// Required for serialization
/// </summary>
public AbpRedisCacheOptions()
{

}

public AbpRedisCacheOptions(IAbpStartupConfiguration abpStartupConfiguration)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@

namespace Abp.Runtime.Caching.Redis.RealTime
{
public class RedisOnlineClientStore<T> : RedisOnlineClientStore, IOnlineClientStore<T>
{
public RedisOnlineClientStore(
IAbpRedisCacheDatabaseProvider database,
AbpRedisCacheOptions options) : base(database, options)
{
}
}

public class RedisOnlineClientStore : IOnlineClientStore, ISingletonDependency
{
private readonly IAbpRedisCacheDatabaseProvider _database;
Expand Down Expand Up @@ -113,44 +104,22 @@ public async Task<IReadOnlyList<IOnlineClient>> GetAllAsync()

public async Task<IReadOnlyList<IOnlineClient>> GetAllByUserIdAsync(UserIdentifier userIdentifier)
{
var clients = new List<OnlineClient>();
if (!await IsUserOnlineAsync(userIdentifier))
{
return clients;
}

var database = GetDatabase();

var userClientsValue = await database.HashGetAsync(_userStoreKey, userIdentifier.ToUserIdentifierString());
if (!userClientsValue.HasValue)
{
return clients;
}

var userClients = JsonConvert.DeserializeObject<List<string>>(userClientsValue);
foreach (var connectionId in userClients)
var clientsEntries = await database.HashGetAllAsync(_clientStoreKey);
var clients = new List<IOnlineClient>();
foreach (var entry in clientsEntries)
{
var clientValue = await database.HashGetAsync(_clientStoreKey, connectionId);
if (clientValue.IsNullOrEmpty)
{
continue;
}

clients.Add(JsonConvert.DeserializeObject<OnlineClient>(clientValue));
clients.Add(JsonConvert.DeserializeObject<OnlineClient>(entry.Value));
}

return clients;
return clients
.Where(e => e.TenantId == userIdentifier.TenantId && e.UserId == userIdentifier.UserId)
.ToImmutableList();
}

private IDatabase GetDatabase()
{
return _database.GetDatabase();
}

private async Task<bool> IsUserOnlineAsync(UserIdentifier user)
{
var database = GetDatabase();
return await database.HashExistsAsync(_userStoreKey, user.ToUserIdentifierString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static void UseRedis(this ICachingConfiguration cachingConfiguration, Act

iocManager.RegisterIfNot<ICacheManager, AbpRedisCacheManager>();
iocManager.RegisterIfNot<IOnlineClientStore, RedisOnlineClientStore>();
iocManager.RegisterIfNot(typeof(IOnlineClientStore<>), typeof(RedisOnlineClientStore<>));

optionsAction(iocManager.Resolve<AbpRedisCacheOptions>());
}
Expand Down
3 changes: 0 additions & 3 deletions src/Abp/AbpKernelModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public override void Initialize()

IocManager.IocContainer.Install(new EventBusInstaller(IocManager));

IocManager.Register(typeof(IOnlineClientManager<>), typeof(OnlineClientManager<>), DependencyLifeStyle.Singleton);
IocManager.Register(typeof(IOnlineClientStore<>), typeof(InMemoryOnlineClientStore<>), DependencyLifeStyle.Singleton);

IocManager.Register(typeof(EventTriggerAsyncBackgroundJob<>), DependencyLifeStyle.Transient);

IocManager.RegisterAssemblyByConvention(typeof(AbpKernelModule).GetAssembly(),
Expand Down
5 changes: 0 additions & 5 deletions src/Abp/RealTime/IOnlineClientStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

namespace Abp.RealTime
{
public interface IOnlineClientStore<T> : IOnlineClientStore
{

}

public interface IOnlineClientStore
{
/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/Abp/RealTime/InMemoryOnlineClientStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

namespace Abp.RealTime
{
public class InMemoryOnlineClientStore<T> : InMemoryOnlineClientStore, IOnlineClientStore<T>
{
}

public class InMemoryOnlineClientStore : IOnlineClientStore, ISingletonDependency
{
/// <summary>
Expand Down
21 changes: 3 additions & 18 deletions src/Abp/RealTime/OnlineClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

namespace Abp.RealTime
{
public class OnlineClientManager<T> : OnlineClientManager, IOnlineClientManager<T>
{
public OnlineClientManager(IOnlineClientStore<T> store) : base(store)
{

}
}

/// <summary>
/// Implements <see cref="IOnlineClientManager"/>.
/// </summary>
Expand All @@ -40,22 +32,15 @@ public OnlineClientManager(IOnlineClientStore store)

public virtual async Task AddAsync(IOnlineClient client)
{
var userWasAlreadyOnline = false;
var user = client.ToUserIdentifierOrNull();

if (user != null)
if (user != null && !await this.IsOnlineAsync(user))
{
userWasAlreadyOnline = await this.IsOnlineAsync(user);
UserConnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
}

await Store.AddAsync(client);

ClientConnected.InvokeSafely(this, new OnlineClientEventArgs(client));

if (user != null && !userWasAlreadyOnline)
{
UserConnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
}
}

public virtual async Task<bool> RemoveAsync(string connectionId)
Expand Down
13 changes: 0 additions & 13 deletions test/Abp.Tests/RealTime/InMemoryOnlineClientStore_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ namespace Abp.Tests.RealTime
public class InMemoryOnlineClientStore_Tests
{
private readonly InMemoryOnlineClientStore _store;
private readonly InMemoryOnlineClientStore<ChatChannel> _chatStore;

public InMemoryOnlineClientStore_Tests()
{
_store = new InMemoryOnlineClientStore();
_chatStore = new InMemoryOnlineClientStore<ChatChannel>();
}

[Fact]
Expand All @@ -29,17 +27,6 @@ public async Task Test_All()
(await _store.GetAllAsync()).Count.ShouldBe(1);
(await _store.RemoveAsync(connectionId)).ShouldBeTrue();
(await _store.GetAllAsync()).Count.ShouldBe(0);

(await _chatStore.GetAllAsync()).Count.ShouldBe(0);
connectionId = Guid.NewGuid().ToString("N");

await _chatStore.AddAsync(new OnlineClient(connectionId, "127.0.0.1", 1, 2));
(await _chatStore.GetAllAsync()).Count.ShouldBe(1);
}

internal class ChatChannel
{

}
}
}

0 comments on commit 8588af4

Please sign in to comment.