-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ChannelManager for user-channel interactions
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/Argon.Api/Grains.Persistence.States/UsersJoinedToChannel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |