1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
using System.Text.Json.Serialization;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.EventTypes.Spec.State.RoomInfo;
using LibMatrix.HomeserverEmulator.Services;
using LibMatrix.Responses;
using LibMatrix.RoomTypes;
using Microsoft.AspNetCore.Mvc;
namespace LibMatrix.HomeserverEmulator.Controllers.Rooms;
[ApiController]
[Route("/_matrix/client/{version}/")]
public class RoomsController(ILogger<RoomsController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase {
//createRoom
[HttpPost("createRoom")]
public async Task<RoomIdResponse> CreateRoom([FromBody] CreateRoomRequest request) {
var token = tokenService.GetAccessTokenOrNull(HttpContext);
if (token == null)
throw new MatrixException() {
ErrorCode = "M_MISSING_TOKEN",
Error = "Missing token"
};
var user = await userStore.GetUserByToken(token);
if (user == null)
throw new MatrixException() {
ErrorCode = "M_UNKNOWN_TOKEN",
Error = "No such user"
};
var room = new RoomStore.Room($"!{Guid.NewGuid()}:{tokenService.GenerateServerName(HttpContext)}");
var createEvent = room.SetStateInternal(new() {
Type = RoomCreateEventContent.EventId,
RawContent = new() {
["creator"] = user.UserId
}
});
foreach (var (key, value) in request.CreationContent) {
createEvent.RawContent[key] = value.DeepClone();
}
if (!string.IsNullOrWhiteSpace(request.Name))
room.SetStateInternal(new StateEvent() {
Type = RoomNameEventContent.EventId,
TypedContent = new RoomNameEventContent() {
Name = request.Name
}
});
if (!string.IsNullOrWhiteSpace(request.RoomAliasName))
room.SetStateInternal(new StateEvent() {
Type = RoomCanonicalAliasEventContent.EventId,
TypedContent = new RoomCanonicalAliasEventContent() {
Alias = $"#{request.RoomAliasName}:localhost"
}
});
if (request.InitialState is { Count: > 0 }) {
foreach (var stateEvent in request.InitialState) {
room.SetStateInternal(stateEvent);
}
}
room.AddUser(user.UserId);
// user.Rooms.Add(room.RoomId, room);
return new() {
RoomId = room.RoomId
};
}
[HttpPost("rooms/{roomId}/upgrade")]
public async Task<object> UpgradeRoom(string roomId, [FromBody] UpgradeRoomRequest request) {
var token = tokenService.GetAccessTokenOrNull(HttpContext);
if (token == null)
throw new MatrixException() {
ErrorCode = "M_MISSING_TOKEN",
Error = "Missing token"
};
var user = await userStore.GetUserByToken(token);
if (user == null)
throw new MatrixException() {
ErrorCode = "M_UNKNOWN_TOKEN",
Error = "No such user"
};
var oldRoom = roomStore.GetRoomById(roomId);
if (oldRoom == null)
throw new MatrixException() {
ErrorCode = "M_NOT_FOUND",
Error = "Room not found"
};
var room = new RoomStore.Room($"!{Guid.NewGuid()}:{tokenService.GenerateServerName(HttpContext)}");
var eventTypesToTransfer = new[] {
RoomServerACLEventContent.EventId,
RoomEncryptionEventContent.EventId,
RoomNameEventContent.EventId,
RoomAvatarEventContent.EventId,
RoomTopicEventContent.EventId,
RoomGuestAccessEventContent.EventId,
RoomHistoryVisibilityEventContent.EventId,
RoomJoinRulesEventContent.EventId,
RoomPowerLevelEventContent.EventId,
};
var createEvent = room.SetStateInternal(new() {
Type = RoomCreateEventContent.EventId,
RawContent = new() {
["creator"] = user.UserId
}
});
oldRoom.State.Where(x => eventTypesToTransfer.Contains(x.Type)).ToList().ForEach(x => room.SetStateInternal(x));
room.AddUser(user.UserId);
// user.Rooms.Add(room.RoomId, room);
return new {
replacement_room = room.RoomId
};
}
[HttpPost("rooms/{roomId}/leave")] // TODO: implement
public async Task<object> LeaveRoom(string roomId) {
var token = tokenService.GetAccessTokenOrNull(HttpContext);
if (token == null)
throw new MatrixException() {
ErrorCode = "M_MISSING_TOKEN",
Error = "Missing token"
};
var user = await userStore.GetUserByToken(token);
if (user == null)
throw new MatrixException() {
ErrorCode = "M_UNKNOWN_TOKEN",
Error = "No such user"
};
var room = roomStore.GetRoomById(roomId);
if (room == null)
throw new MatrixException() {
ErrorCode = "M_NOT_FOUND",
Error = "Room not found"
};
// room.RemoveUser(user.UserId);
// room.SetStateInternal(new StateEventResponse() { });
return new {
room_id = room.RoomId
};
}
}
public class UpgradeRoomRequest {
[JsonPropertyName("new_version")]
public required string NewVersion { get; set; }
}
|