Skip to content

Commit

Permalink
Add Set RoomTopic and add some missings in ServiceExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
DevTown committed Feb 27, 2024
1 parent 59960a1 commit 163053e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace System.Runtime.CompilerServices.Dto.Room.Manage
{
public record ChangeTopicRequest(string? topic )
{
public string? topic { get; } = topic;
}

}
16 changes: 16 additions & 0 deletions Matrix.Sdk/Core/Infrastructure/Services/RoomService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices.Dto.Room.Manage;
using Matrix.Sdk.Core.Infrastructure.Dto.Event;
using Newtonsoft.Json;

namespace Matrix.Sdk.Core.Infrastructure.Services
Expand Down Expand Up @@ -79,5 +81,19 @@ public async Task<string> GetRoomNameAsync(string accessToken, string roomId, Ca
var payload = JsonConvert.DeserializeObject<RoomNameResponse>(json);
return payload.name;
}

public async Task<EventResponse> SetTopicAsync(string accessToken,
string roomId,
string topic, CancellationToken cancellationToken)
{
const string eventType = "m.room.topic";
var model = new ChangeTopicRequest(topic);

HttpClient httpClient = CreateHttpClient(accessToken);

var path = $"{ResourcePath}/rooms/{roomId}/state/{eventType}";

return await httpClient.PutAsJsonAsync<EventResponse>(path, model, cancellationToken);
}
}
}
2 changes: 2 additions & 0 deletions Matrix.Sdk/IMatrixClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices.Dto.User;
using Matrix.Sdk.Core.Domain.RoomEvent;
using Matrix.Sdk.Core.Infrastructure.Dto.Event;
using Matrix.Sdk.Core.Infrastructure.Dto.Sync;
using Matrix.Sdk.Core.Infrastructure.Dto.Sync.Event.Room;
using Matrix.Sdk.Core.Infrastructure.Services;
Expand Down Expand Up @@ -60,6 +61,7 @@ public interface IMatrixClient

Task<string> GetString(string url);
Task<string> GetRoomName(string id);
Task<EventResponse> SetRoomTopic(string roomId, string topic);
Task<MatrixProfile> GetUserProfile(string fullUserId);
Task<byte[]> GetMxcImage(string mxcUrl);
}
Expand Down
2 changes: 1 addition & 1 deletion Matrix.Sdk/Matrix.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ItemGroup>
<PackageReference Include="Markdig" Version="0.31.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
10 changes: 8 additions & 2 deletions Matrix.Sdk/MatrixClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices.Dto.User;
using System.Runtime.CompilerServices.Dto.User;
using Matrix.Sdk.Core.Domain.RoomEvent;

namespace Matrix.Sdk
Expand Down Expand Up @@ -213,7 +213,13 @@ public async Task<string> GetRoomName(string roomId)
{
return await _roomService.GetRoomNameAsync(_accessToken!, roomId, _cts.Token);
}


public async Task<EventResponse> SetRoomTopic(string roomId, string topic)
{
string transactionId = CreateTransactionId();
return await _roomService.SetTopicAsync(_accessToken!, roomId, topic, _cts.Token);
}

public async Task<MatrixProfile> GetUserProfile(string userId)
{
return await _userService.GetProfile(_accessToken!, userId, _cts.Token);
Expand Down
2 changes: 1 addition & 1 deletion Matrix.Sdk/MatrixClientServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static IServiceCollection AddMatrixClient(this IServiceCollection service
services.AddSingleton<EventService>();
services.AddSingleton<RoomService>();
services.AddSingleton<UserService>();

services.AddSingleton<MediaService>();
services.AddTransient<IPollingService, PollingService>();
services.AddTransient<IMatrixClient, MatrixClient>();

Expand Down

0 comments on commit 163053e

Please sign in to comment.