Skip to content

Commit

Permalink
Merge pull request #13 from DevTown/main
Browse files Browse the repository at this point in the history
Functions from @SlimeQ and more
  • Loading branch information
k-karuna authored Mar 13, 2024
2 parents e8e1251 + 254e938 commit 6e2a2f4
Show file tree
Hide file tree
Showing 44 changed files with 884 additions and 250 deletions.
10 changes: 8 additions & 2 deletions Matrix.Sdk.Sample.Console/DependencyInjectionSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public async Task Run(IServiceProvider serviceProvider)
if (roomEvent is not TextMessageEvent textMessageEvent)
continue;

(string roomId, string senderUserId, string message) = textMessageEvent;
var senderUserId = textMessageEvent.SenderUserId;
var message = textMessageEvent.Message;
var roomId = textMessageEvent.RoomId;

if (client.UserId != senderUserId)
Console.WriteLine($"RoomId: {roomId} received message from {senderUserId}: {message}.");
}
Expand All @@ -60,7 +63,10 @@ public async Task Run(IServiceProvider serviceProvider)
if (roomEvent is not TextMessageEvent textMessageEvent)
continue;

(string roomId, string senderUserId, string message) = textMessageEvent;
var senderUserId = textMessageEvent.SenderUserId;
var message = textMessageEvent.Message;
var roomId = textMessageEvent.RoomId;

if (anotherClient.UserId != senderUserId)
Console.WriteLine($"RoomId: {roomId} received message from {senderUserId}: {message}.");
}
Expand Down
14 changes: 10 additions & 4 deletions Matrix.Sdk.Sample.Console/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Matrix.Sdk.Sample.Console
using System;
using System.Threading;
using System.Threading.Tasks;
using Core.Domain.MatrixRoom;
using Core.Domain.Room;
using Core.Domain.RoomEvent;
using Core.Infrastructure.Dto.Room.Create;
using Serilog;
Expand Down Expand Up @@ -57,8 +57,11 @@ public async Task Run()
{
if (roomEvent is not TextMessageEvent textMessageEvent)
continue;

(string roomId, string senderUserId, string message) = textMessageEvent;

var senderUserId = textMessageEvent.SenderUserId;
var message = textMessageEvent.Message;
var roomId = textMessageEvent.RoomId;

if (client.UserId != senderUserId)
Console.WriteLine($"RoomId: {roomId} received message from {senderUserId}: {message}.");
}
Expand All @@ -71,7 +74,10 @@ public async Task Run()
if (roomEvent is not TextMessageEvent textMessageEvent)
continue;

(string roomId, string senderUserId, string message) = textMessageEvent;
var senderUserId = textMessageEvent.SenderUserId;
var message = textMessageEvent.Message;
var roomId = textMessageEvent.RoomId;

if (anotherClient.UserId != senderUserId)
Console.WriteLine($"RoomId: {roomId} received message from {senderUserId}: {message}.");
}
Expand Down
5 changes: 3 additions & 2 deletions Matrix.Sdk/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ public static class Constants
public class EventType
{
public const string Create = "m.room.create";

public const string Member = "m.room.member";

public const string Message = "m.room.message";
public const string Redaction = "m.room.redaction";
public const string Reaction = "m.reaction";
}

public class MessageType
{
public const string Text = "m.text";
public const string Image = "m.image";
}
}
}
36 changes: 20 additions & 16 deletions Matrix.Sdk/Core/Domain/MatrixRoom/MatrixRoom.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
namespace Matrix.Sdk.Core.Domain.MatrixRoom
namespace Matrix.Sdk.Core.Domain.Room
{
using System.Collections.Generic;
using Infrastructure.Dto.Sync;
using Infrastructure.Dto.Sync.Event.Room;
using RoomEvent;

public record MatrixRoom
public record MatrixRoom(string Id, MatrixRoomStatus Status, List<string> JoinedUserIds)
{
public MatrixRoom(string id, MatrixRoomStatus status, List<string> joinedUserIds)
public static MatrixRoom Create(string roomId, RoomResponse joinedRoom, MatrixRoomStatus status)
{
Id = id;
Status = status;
JoinedUserIds = joinedUserIds;
}
var joinedUserIds = new List<string>();
foreach (RoomEventResponse timelineEvent in joinedRoom.Timeline.Events)
if (JoinRoomEvent.Factory.TryCreateFrom(timelineEvent, roomId, out JoinRoomEvent joinRoomEvent))
joinedUserIds.Add(joinRoomEvent!.SenderUserId);

public MatrixRoom(string id, MatrixRoomStatus status)
{
Id = id;
Status = status;
JoinedUserIds = new List<string>();
return new MatrixRoom(roomId, status, joinedUserIds);
}

public string Id { get; }

public MatrixRoomStatus Status { get; }
public static MatrixRoom CreateInvite(string roomId, InvitedRoom invitedRoom)
{
var joinedUserIds = new List<string>();
foreach (RoomStrippedState timelineEvent in invitedRoom.InviteState.Events)
if (JoinRoomEvent.Factory.TryCreateFromStrippedState(timelineEvent, roomId,
out JoinRoomEvent joinRoomEvent))
joinedUserIds.Add(joinRoomEvent!.SenderUserId);

public List<string> JoinedUserIds { get; }
return new MatrixRoom(roomId, MatrixRoomStatus.Invited, joinedUserIds);
}
}
}
71 changes: 0 additions & 71 deletions Matrix.Sdk/Core/Domain/MatrixRoom/MatrixRoomEventFactory.cs

This file was deleted.

41 changes: 0 additions & 41 deletions Matrix.Sdk/Core/Domain/MatrixRoom/MatrixRoomFactory.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Matrix.Sdk/Core/Domain/MatrixRoom/MatrixRoomStatus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Matrix.Sdk.Core.Domain.MatrixRoom
namespace Matrix.Sdk.Core.Domain.Room
{
public enum MatrixRoomStatus
{
Expand Down
60 changes: 59 additions & 1 deletion Matrix.Sdk/Core/Domain/RoomEvent/BaseRoomEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
using System;
using System.Collections.Generic;
using Matrix.Sdk.Core.Infrastructure.Dto.Sync;
using Matrix.Sdk.Core.Infrastructure.Dto.Sync.Event.Room;

namespace Matrix.Sdk.Core.Domain.RoomEvent
{
public abstract record BaseRoomEvent(string RoomId, string SenderUserId);
public abstract record BaseRoomEvent(string EventId, string RoomId, string SenderUserId, DateTimeOffset Timestamp)
{
public static List<BaseRoomEvent> Create(string roomId, RoomResponse joinedRoom)
{
var roomEvents = new List<BaseRoomEvent>();

foreach (RoomEventResponse timelineEvent in joinedRoom.Timeline.Events)
{
var e = Create(roomId, timelineEvent);
if (e != null) roomEvents.Add(e);

}
return roomEvents;
}

public static BaseRoomEvent Create(string roomId, RoomEventResponse timelineEvent)
{
if (JoinRoomEvent.Factory.TryCreateFrom(timelineEvent, roomId, out JoinRoomEvent joinRoomEvent)) return joinRoomEvent;
if (CreateRoomEvent.Factory.TryCreateFrom(timelineEvent, roomId, out var createRoomEvent)) return createRoomEvent;
if (InviteToRoomEvent.Factory.TryCreateFrom(timelineEvent, roomId, out var inviteToRoomEvent)) return inviteToRoomEvent;
if (TextMessageEvent.Factory.TryCreateFrom(timelineEvent, roomId, out var textMessageEvent)) return textMessageEvent;
if (ImageMessageEvent.Factory.TryCreateFrom(timelineEvent, roomId, out var imageMessageEvent)) return imageMessageEvent;
if (RedactionEvent.Factory.TryCreateFrom(timelineEvent, roomId, out var redactionEvent)) return redactionEvent;
if (ReactionEvent.Factory.TryCreateFrom(timelineEvent, roomId, out var reactionEvent)) return reactionEvent;
return null;
}

public static List<BaseRoomEvent> CreateFromInvited(string roomId, InvitedRoom invitedRoom)
{
var roomEvents = new List<BaseRoomEvent>();

foreach (RoomStrippedState inviteStateEvent in invitedRoom.InviteState.Events)
{
var e = CreateFromInvited(roomId, inviteStateEvent);
if (e != null)
{
roomEvents.Add(e);
}
}
return roomEvents;
}

public static BaseRoomEvent CreateFromInvited(string roomId, RoomStrippedState inviteStateEvent)
{
if (JoinRoomEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var joinRoomEvent)) return joinRoomEvent;
if (CreateRoomEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var createRoomEvent)) return createRoomEvent;
if (InviteToRoomEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var inviteToRoomEvent)) return inviteToRoomEvent;
if (TextMessageEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var textMessageEvent)) return textMessageEvent;
if (ImageMessageEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var imageMessageEvent)) return imageMessageEvent;
if (RedactionEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var redactionEvent)) return redactionEvent;
if (ReactionEvent.Factory.TryCreateFromStrippedState(inviteStateEvent, roomId, out var reactionEvent)) return reactionEvent;
return null;
}
}
}
17 changes: 10 additions & 7 deletions Matrix.Sdk/Core/Domain/RoomEvent/CreateRoomEvent.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using System;

namespace Matrix.Sdk.Core.Domain.RoomEvent
{
using Infrastructure.Dto.Sync.Event;
using Infrastructure.Dto.Sync.Event.Room;
using Infrastructure.Dto.Sync.Event.Room.State;

public record CreateRoomEvent(string RoomId, string SenderUserId, string RoomCreatorUserId) : BaseRoomEvent(RoomId,
SenderUserId)
public record CreateRoomEvent(string EventId, string RoomId, string SenderUserId, string RoomCreatorUserId, DateTimeOffset Timestamp) : BaseRoomEvent(EventId, RoomId,
SenderUserId, Timestamp)
{
public static class Factory
{
public static bool TryCreateFrom(RoomEvent roomEvent, string roomId, out CreateRoomEvent createRoomEvent)
public static bool TryCreateFrom(RoomEventResponse roomEvent, string roomId, out CreateRoomEvent createRoomEvent)
{
RoomCreateContent content = roomEvent.Content.ToObject<RoomCreateContent>();
if (roomEvent.EventType == EventType.Create && content != null)
{
createRoomEvent = new CreateRoomEvent(roomId, roomEvent.SenderUserId, content.RoomCreatorUserId);
createRoomEvent = new CreateRoomEvent(roomEvent.EventId, roomId, roomEvent.SenderUserId, content.RoomCreatorUserId, roomEvent.Timestamp);
return true;
}

createRoomEvent = new CreateRoomEvent(string.Empty, string.Empty, string.Empty);
createRoomEvent = null;
return false;
}

Expand All @@ -29,11 +31,12 @@ public static bool TryCreateFromStrippedState(RoomStrippedState roomStrippedStat
if (roomStrippedState.EventType == EventType.Create && content != null)
{
createRoomEvent =
new CreateRoomEvent(roomId, roomStrippedState.SenderUserId, content.RoomCreatorUserId);
new CreateRoomEvent(string.Empty, roomId, roomStrippedState.SenderUserId, content.RoomCreatorUserId, DateTimeOffset.MinValue);
return true;

}

createRoomEvent = new CreateRoomEvent(string.Empty, string.Empty, string.Empty);
createRoomEvent = null;
return false;
}
}
Expand Down
23 changes: 23 additions & 0 deletions Matrix.Sdk/Core/Domain/RoomEvent/EditEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Matrix.Sdk.Core.Infrastructure.Dto.Event;
using Newtonsoft.Json;

namespace Matrix.Sdk.Core.Domain.RoomEvent
{
public record EditEvent(MessageType MessageType, string Message, string EventId)
{
public string body = $"* {Message}";
public MessageType msgtype { get; } = MessageType;

[JsonProperty("m.new_content")]
public MessageEvent newContent = new MessageEvent(MessageType, Message);

[JsonProperty("m.relates_to")]
public RelatesTo mRelatesTo = new RelatesTo(EventId);

public record RelatesTo(string evntid)
{
public string event_id = evntid;
public string rel_type = "m.replace";
}
}
}
Loading

0 comments on commit 6e2a2f4

Please sign in to comment.