Skip to content

Commit

Permalink
Add ChannelManager for user-channel interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
urumo committed Nov 5, 2024
1 parent 9e402b7 commit 6251c20
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Argon.Api/Grains.Interfaces/IChannelManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Argon.Api.Grains.Interfaces;

using Entities;
using Sfu;

public interface IChannelManager : IGrainWithGuidKey
{
[Alias("Join")]
Task<RealtimeToken> Join(Guid userId);

[Alias("Leave")]
Task Leave(Guid userId);

[Alias("GetChannel")]
Task<ChannelDto> GetChannel();
}
16 changes: 16 additions & 0 deletions src/Argon.Api/Grains.Persistence.States/UsersJoinedToChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Argon.Api.Grains.Persistence.States;

using System.Runtime.Serialization;
using Entities;
using MemoryPack;
using MessagePack;

[DataContract]
[MemoryPackable(GenerateType.VersionTolerant)]
[MessagePackObject]
[Serializable]
[GenerateSerializer]
public sealed record UsersJoinedToChannel
{
public List<UsersToServerRelation> Users { get; set; } = new();
}
52 changes: 52 additions & 0 deletions src/Argon.Api/Grains/ChannelManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Argon.Api.Grains;

using Entities;
using Interfaces;
using Microsoft.EntityFrameworkCore;
using Persistence.States;
using Sfu;

public class ChannelManager(
IArgonSelectiveForwardingUnit sfu,
ApplicationDbContext context,
[PersistentState("joinedUsers", "OrleansStorage")]
IPersistentState<UsersJoinedToChannel> joinedUsers
) : Grain, IChannelManager
{
public async Task<RealtimeToken> Join(Guid userId)
{
var channel = await GetChannel();

var user = (await context.Servers.Include(x => x.UsersToServerRelations)
.FirstAsync(x => x.Id == channel.ServerId))
.UsersToServerRelations.First(x => x.UserId == userId);

joinedUsers.State.Users.Add(user);
await joinedUsers.WriteStateAsync();

return await sfu.IssueAuthorizationTokenAsync(
new ArgonUserId(userId),
new ArgonChannelId(
new ArgonServerId(channel.ServerId),
this.GetPrimaryKey()
),
SfuPermission.DefaultUser // TODO: sort out permissions
);
}

public Task Leave(Guid userId)
{
joinedUsers.State.Users.RemoveAll(x => x.UserId == userId);
return joinedUsers.WriteStateAsync();
}

public async Task<ChannelDto> GetChannel()
{
return await Get();
}

private async Task<Channel> Get()
{
return await context.Channels.FirstAsync(c => c.Id == this.GetPrimaryKey());
}
}

0 comments on commit 6251c20

Please sign in to comment.