diff --git a/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs b/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs
deleted file mode 100644
index 1ceb095..0000000
--- a/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs
+++ /dev/null
@@ -1,51 +0,0 @@
-using System.Collections.Concurrent;
-using ArcaneLibs.Extensions;
-using LibMatrix;
-using LibMatrix.Homeservers;
-using LibMatrix.Services;
-using MxApiExtensions.Services;
-
-namespace ModAS.Server.Services;
-
-public class AuthenticatedHomeserverProviderService(
- AuthenticationService authenticationService,
- HomeserverProviderService homeserverProviderService,
- IHttpContextAccessor request,
- ModASConfiguration config,
- AppServiceRegistration asRegistration
- ) {
- public HttpContext? _context = request.HttpContext;
- public ConcurrentDictionary<string, AuthenticatedHomeserverGeneric> KnownUsers { get; set; } = new();
-
- public async Task<AuthenticatedHomeserverGeneric> GetImpersonatedHomeserver(string mxid) {
- if (!KnownUsers.TryGetValue(mxid, out var homeserver)) {
- homeserver = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, asRegistration.AppServiceToken, config.HomeserverUrl);
- KnownUsers.TryAdd(mxid, homeserver);
- }
- //var hs = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, asRegistration.AsToken, config.HomeserverUrl);
- await homeserver.SetImpersonate(mxid);
- // KnownUsers.TryAdd(mxid, homeserver);
- return homeserver;
- }
-
- public async Task<AuthenticatedHomeserverGeneric> GetHomeserver() {
- var token = authenticationService.GetToken();
- if (token == null) {
- throw new MatrixException {
- ErrorCode = "M_MISSING_TOKEN",
- Error = "Missing access token"
- };
- }
-
- var mxid = await authenticationService.GetMxidFromToken(token);
- if (mxid == "@anonymous:*") {
- throw new MatrixException {
- ErrorCode = "M_MISSING_TOKEN",
- Error = "Missing access token"
- };
- }
-
- var hsCanonical = string.Join(":", mxid.Split(':').Skip(1));
- return await homeserverProviderService.GetAuthenticatedWithToken(hsCanonical, token);
- }
-}
\ No newline at end of file
diff --git a/ModAS.Server/Services/ModASConfiguration.cs b/ModAS.Server/Services/ModASConfiguration.cs
index dc7c552..063e838 100644
--- a/ModAS.Server/Services/ModASConfiguration.cs
+++ b/ModAS.Server/Services/ModASConfiguration.cs
@@ -1,10 +1,10 @@
namespace MxApiExtensions.Services;
-
public class ModASConfiguration {
public ModASConfiguration(IConfiguration configuration) {
configuration.GetRequiredSection("ModAS").Bind(this);
}
+
public string ServerName { get; set; }
public string HomeserverUrl { get; set; }
}
\ No newline at end of file
diff --git a/ModAS.Server/Services/RoomContextService.cs b/ModAS.Server/Services/RoomContextService.cs
new file mode 100644
index 0000000..b3673c1
--- /dev/null
+++ b/ModAS.Server/Services/RoomContextService.cs
@@ -0,0 +1,111 @@
+using ArcaneLibs.Extensions;
+using Elastic.Apm;
+using Elastic.Apm.Api;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec.State;
+using LibMatrix.RoomTypes;
+using MxApiExtensions.Services;
+
+namespace ModAS.Server.Services;
+
+public class RoomContextService(UserProviderService userProvider, ModASConfiguration config) {
+ public Dictionary<string, RoomContext> RoomContexts { get; } = new();
+ public Dictionary<string, List<string>> LocalUsersByRoom { get; private set; } = new();
+
+ public async Task<RoomContext?> GetRoomContext(GenericRoom room) {
+ if (RoomContexts.TryGetValue(room.RoomId, out var roomContext) && !roomContext.NeedsUpdate) return roomContext;
+
+ var newRoomContext = await FetchRoomContext(room.RoomId);
+ if (newRoomContext is not null) RoomContexts[room.RoomId] = newRoomContext;
+ return roomContext;
+ }
+
+ public async Task<RoomContext?> GetRoomContext(string roomId) {
+ if (RoomContexts.TryGetValue(roomId, out var roomContext) && !roomContext.NeedsUpdate) return roomContext;
+
+ var newRoomContext = await FetchRoomContext(roomId);
+ if (newRoomContext is not null) RoomContexts[roomId] = newRoomContext;
+ return roomContext;
+ }
+
+ public async Task<RoomContext?> FetchRoomContext(string roomId) {
+ var span = currentTransaction.StartSpan($"FetchRoomContext - {roomId}", ApiConstants.TypeApp);
+ if (!LocalUsersByRoom.ContainsKey(roomId))
+ await UpdateLocalUserRoomLists();
+ if (!LocalUsersByRoom.TryGetValue(roomId, out var localUsers)) return null;
+ if (localUsers.Count == 0) return null;
+
+ var roomContext = new RoomContext {
+ RoomId = roomId
+ };
+
+ var room = (await userProvider.GetImpersonatedHomeserver(localUsers.First())).GetRoom(roomId);
+ var roomMembers = await room.GetMembersListAsync(false);
+
+ roomContext.UserCountByMembership = roomMembers.GroupBy(x => (x.TypedContent as RoomMemberEventContent)?.Membership)
+ .ToDictionary(x => x.Key, x => x.Count());
+ roomContext.LocalUsers = roomMembers.Select(x => x.StateKey).Where(x => x.EndsWith(':' + config.ServerName)).ToList();
+ roomContext.CurrentLocalUsers = roomMembers.Where(x => x.StateKey.EndsWith(':' + config.ServerName) && (x.TypedContent as RoomMemberEventContent)?.Membership == "join")
+ .Select(x => x.StateKey).ToList();
+
+ var powerLevels = await room.GetPowerLevelsAsync();
+ roomContext.LocalUsersByStatePermission = powerLevels?.Events?
+ .Select(@event => (@event.Key, roomContext.CurrentLocalUsers.Where(clu => powerLevels.UserHasStatePermission(clu, @event.Key))))
+ .Where(x => x.Item2.Any())
+ .ToDictionary(x => x.Key, x => x.Item2.ToList());
+
+ roomContext.LastUpdate = DateTime.Now;
+
+ span.End();
+ return roomContext;
+ }
+
+ private async Task UpdateLocalUserRoomLists() {
+ var span = currentTransaction.StartSpan("UpdateLocalUserRoomLists", ApiConstants.TypeApp);
+ var newLocalUsersByRoom = new Dictionary<string, List<string>>();
+ var users = await userProvider.GetValidUsers();
+ var getRoomsSpan = currentTransaction.StartSpan("GetRooms", ApiConstants.TypeApp);
+ var userRoomLists = users.Values.Select(ahs => (ahs, ahs.GetJoinedRooms())).ToList();
+ await Task.WhenAll(userRoomLists.Select(x => x.Item2));
+ getRoomsSpan.End();
+ foreach (var (ahs, rooms) in userRoomLists) {
+ foreach (var room in rooms.Result) {
+ newLocalUsersByRoom.TryAdd(room.RoomId, new List<string>());
+ newLocalUsersByRoom[room.RoomId].Add(ahs.UserId);
+ }
+ }
+ span.End();
+ LocalUsersByRoom = newLocalUsersByRoom;
+ }
+
+ public class RoomContext {
+ public string RoomId { get; set; }
+ public List<string> LocalUsers { get; set; }
+ public List<string> CurrentLocalUsers { get; set; }
+ public Dictionary<string, int> UserCountByMembership { get; set; }
+ public Dictionary<string, List<string>> LocalUsersByStatePermission { get; set; }
+
+ public DateTime LastUpdate { get; set; }
+ public bool NeedsUpdate => DateTime.Now - LastUpdate > TimeSpan.FromMinutes(5);
+ }
+
+ public async Task<GenericRoom?> GetRoomReferenceById(string roomId) {
+ var roomContext = await GetRoomContext(roomId);
+ var localUsers = roomContext.LocalUsers.Select(userProvider.GetImpersonatedHomeserver).ToAsyncEnumerable();
+ await foreach (var localUser in localUsers) {
+ var room = localUser.GetRoom(roomId);
+ try {
+ if (await room.GetCreateEventAsync() is not null)
+ return room;
+ }
+ catch (MatrixException e) {
+ if (e is { ErrorCode: "M_UNAUTHORIZED" }) continue;
+ throw;
+ }
+ }
+
+ return null;
+ }
+
+ private static ITransaction currentTransaction => Agent.Tracer.CurrentTransaction;
+}
\ No newline at end of file
diff --git a/ModAS.Server/Services/RoomStateCacheService.cs b/ModAS.Server/Services/RoomStateCacheService.cs
new file mode 100644
index 0000000..3fdf29a
--- /dev/null
+++ b/ModAS.Server/Services/RoomStateCacheService.cs
@@ -0,0 +1,66 @@
+using System.Collections.Frozen;
+using ArcaneLibs.Extensions;
+using Elastic.Apm;
+using Elastic.Apm.Api;
+using LibMatrix;
+using LibMatrix.RoomTypes;
+
+namespace ModAS.Server.Services;
+
+public class RoomStateCacheService(RoomContextService roomContextService) {
+ public FrozenDictionary<string, FrozenSet<StateEventResponse>> RoomStateCache { get; private set; } = FrozenDictionary<string, FrozenSet<StateEventResponse>>.Empty;
+ private SemaphoreSlim updateLock = new(1, 1);
+ public async Task<FrozenSet<StateEventResponse>> GetRoomState(string roomId, GenericRoom? roomReference = null) {
+ if (RoomStateCache.TryGetValue(roomId, out var roomState)) return roomState;
+ return await InvalidateRoomState(roomId, roomReference);
+ }
+
+ public async Task<FrozenSet<StateEventResponse>> InvalidateRoomState(string roomId, GenericRoom? roomReference = null) {
+ var invalidateSpan = currentTransaction.StartSpan($"invalidateRoomState - {roomId}", ApiConstants.TypeApp);
+ var getRoomReferenceSpan = currentTransaction.StartSpan($"getRoomReference - {roomId}", ApiConstants.TypeApp);
+ if (roomReference is null) {
+ var rc = await roomContextService.GetRoomContext(roomId);
+ if (rc is null) return FrozenSet<StateEventResponse>.Empty;
+ roomReference = await roomContextService.GetRoomReferenceById(roomId);
+ }
+
+ if (roomReference is null) {
+ currentTransaction.CaptureException(new Exception("Could not get room reference for room state invalidation"), roomId, true);
+ return FrozenSet<StateEventResponse>.Empty;
+ }
+
+ getRoomReferenceSpan.End();
+
+ var updateSpan = currentTransaction.StartSpan($"updateRoomState - {roomId}", ApiConstants.TypeApp);
+ await updateLock.WaitAsync();
+ var unfrozen = RoomStateCache.ToDictionary();
+ unfrozen[roomId] = (await roomReference.GetFullStateAsListAsync()).ToFrozenSet();
+ RoomStateCache = unfrozen.ToFrozenDictionary();
+ updateSpan.End();
+ updateLock.Release();
+
+ invalidateSpan.End();
+ if (!RoomStateCache.ContainsKey(roomId)) {
+ currentTransaction.CaptureException(new Exception("Room state cache does not contain room after invalidation"), roomId, false);
+ if (!unfrozen.ContainsKey(roomId))
+ currentTransaction.CaptureException(new Exception("Unfrozen room state cache does not contain room after invalidation either..."), roomId, false);
+ }
+
+ return RoomStateCache[roomId];
+ }
+
+ public async Task EnsureCachedFromRoomList(IEnumerable<GenericRoom> rooms) {
+ await updateLock.WaitAsync();
+ var unfrozen = RoomStateCache.ToDictionary();
+
+ var tasks = rooms.Select(async room => {
+ if (RoomStateCache.ContainsKey(room.RoomId)) return;
+ unfrozen[room.RoomId] = (await room.GetFullStateAsListAsync()).ToFrozenSet();
+ }).ToList();
+ await Task.WhenAll(tasks);
+ RoomStateCache = unfrozen.ToFrozenDictionary();
+ updateLock.Release();
+ }
+
+ private static ITransaction currentTransaction => Agent.Tracer.CurrentTransaction;
+}
\ No newline at end of file
diff --git a/ModAS.Server/Services/UserProviderService.cs b/ModAS.Server/Services/UserProviderService.cs
new file mode 100644
index 0000000..bb281c4
--- /dev/null
+++ b/ModAS.Server/Services/UserProviderService.cs
@@ -0,0 +1,95 @@
+using System.Collections.Concurrent;
+using ArcaneLibs.Extensions;
+using Elastic.Apm;
+using Elastic.Apm.Api;
+using LibMatrix;
+using LibMatrix.Homeservers;
+using LibMatrix.RoomTypes;
+using LibMatrix.Services;
+using MxApiExtensions.Services;
+
+namespace ModAS.Server.Services;
+
+public class UserProviderService(
+ AuthenticationService authenticationService,
+ HomeserverProviderService homeserverProviderService,
+ IHttpContextAccessor request,
+ ModASConfiguration config,
+ AppServiceRegistration asRegistration
+) {
+ public HttpContext? _context = request.HttpContext;
+
+ private static ITransaction currentTransaction => Agent.Tracer.CurrentTransaction;
+
+ private SemaphoreSlim updateLock = new(1,1);
+
+ public ConcurrentDictionary<string, AuthenticatedHomeserverGeneric> KnownUsers { get; set; } = new();
+ public ConcurrentDictionary<string, DateTime> UserValidationExpiry { get; set; } = new();
+ public ConcurrentDictionary<string, List<string>> CachedUserRooms { get; set; } = new();
+ public ConcurrentDictionary<string, DateTime> CachedUserRoomsExpiry { get; set; } = new();
+
+
+ public async Task<AuthenticatedHomeserverGeneric> GetImpersonatedHomeserver(string mxid) {
+ var span = currentTransaction.StartSpan("GetImpersonatedHomeserver", ApiConstants.TypeApp);
+ if (!KnownUsers.TryGetValue(mxid, out var homeserver)) {
+ var getUserSpan = currentTransaction.StartSpan($"GetUser - {mxid}", ApiConstants.TypeApp);
+ homeserver = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, asRegistration.AppServiceToken, config.HomeserverUrl, mxid);
+ KnownUsers.TryAdd(mxid, homeserver);
+ getUserSpan.End();
+ }
+
+ await homeserver.SetImpersonate(mxid);
+ span.End();
+ return homeserver;
+ }
+
+ public async Task<Dictionary<string, AuthenticatedHomeserverGeneric>> GetValidUsers() {
+ var span = currentTransaction.StartSpan("GetValidUsers", ApiConstants.TypeApp);
+ var tasks = KnownUsers.Select(kvp => ValidateUser(kvp.Key, kvp.Value)).ToList();
+ var results = await Task.WhenAll(tasks);
+ var validUsers = results.Where(r => r.Value is not null).ToDictionary(r => r.Key, r => r.Value!);
+ span.End();
+ return validUsers;
+ }
+
+ public async Task<List<GenericRoom>> GetUserRoomsCached(string mxid) {
+ var span = currentTransaction.StartSpan($"GetUserRoomsCached - {mxid}", ApiConstants.TypeApp);
+ var hs = await GetImpersonatedHomeserver(mxid);
+ if (CachedUserRoomsExpiry.TryGetValue(mxid, out var expiry) && expiry > DateTime.Now) {
+ if (CachedUserRooms.TryGetValue(mxid, out var rooms)) {
+ span.End();
+ return rooms.Select(hs.GetRoom).ToList();
+ }
+ }
+
+ var userRooms = await hs.GetJoinedRooms();
+ await updateLock.WaitAsync();
+ CachedUserRooms[mxid] = userRooms.Select(r => r.RoomId).ToList();
+ CachedUserRoomsExpiry[mxid] = DateTime.Now + TimeSpan.FromMinutes(5);
+ updateLock.Release();
+ span.End();
+ return userRooms;
+ }
+
+ private async Task<KeyValuePair<string, AuthenticatedHomeserverGeneric?>> ValidateUser(string mxid, AuthenticatedHomeserverGeneric hs) {
+ if(UserValidationExpiry.TryGetValue(mxid, out var expires))
+ if(DateTime.Now < expires) return new KeyValuePair<string, AuthenticatedHomeserverGeneric?>(mxid, hs);
+ var span = currentTransaction.StartSpan($"ValidateUser - {mxid}", ApiConstants.TypeApp);
+ try {
+ await hs.GetJoinedRooms();
+ await updateLock.WaitAsync();
+ UserValidationExpiry[mxid] = DateTime.Now + TimeSpan.FromMinutes(5);
+ updateLock.Release();
+ return new KeyValuePair<string, AuthenticatedHomeserverGeneric?>(mxid, hs);
+ }
+ catch (MatrixException e) {
+ if (e.ErrorCode == "M_FORBIDDEN") {
+ return new KeyValuePair<string, AuthenticatedHomeserverGeneric?>(mxid, null);
+ }
+ throw;
+ }
+ finally {
+ span.End();
+ }
+ }
+}
\ No newline at end of file
|