about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs
blob: a4c6aa82fe80c70f00a0bcaf76d6560604b5a8cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using LibMatrix.HomeserverEmulator.Services;

namespace LibMatrix.HomeserverEmulator.Extensions;

public static class EventExtensions {
    public static LegacyMatrixEventResponse ToStateEvent(this LegacyMatrixEvent legacyMatrixEvent, UserStore.User user, RoomStore.Room room) {
        return new LegacyMatrixEventResponse {
            RawContent = legacyMatrixEvent.RawContent,
            EventId = "$" + string.Join("", Random.Shared.GetItems("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789".ToCharArray(), 100)),
            RoomId = room.RoomId,
            Sender = user.UserId,
            StateKey = legacyMatrixEvent.StateKey,
            Type = legacyMatrixEvent.Type,
            OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds()
        };
    }

    public static List<LegacyMatrixEventResponse> GetCalculatedState(this IEnumerable<LegacyMatrixEventResponse> events) {
        return events.Where(s => s.StateKey != null)
            .OrderByDescending(s => s.OriginServerTs)
            .DistinctBy(x => (x.Type, x.StateKey))
            .ToList();
    }
}