Merge branch 'krille/fake-user-event' into 'main'

fix: Fake User object

Closes #343

See merge request famedly/company/frontend/famedlysdk!1221
This commit is contained in:
Nicolas Werner 2023-02-17 09:02:59 +00:00
commit 92999339a9
4 changed files with 34 additions and 32 deletions

View File

@ -243,17 +243,17 @@ class Event extends MatrixEvent {
}
User get asUser => User.fromState(
// state key should always be set for member events
stateKey: stateKey!,
prevContent: prevContent,
content: content,
typeKey: type,
eventId: eventId,
roomId: roomId,
senderId: senderId,
originServerTs: originServerTs,
unsigned: unsigned,
room: room);
// state key should always be set for member events
stateKey: stateKey!,
prevContent: prevContent,
content: content,
typeKey: type,
eventId: eventId,
senderId: senderId,
originServerTs: originServerTs,
unsigned: unsigned,
room: room,
);
String get messageType => type == EventTypes.Sticker
? MessageTypes.Sticker

View File

@ -20,33 +20,35 @@ import 'package:matrix/matrix.dart';
/// Represents a Matrix User which may be a participant in a Matrix Room.
class User extends Event {
factory User(String id,
{String? membership,
String? displayName,
String? avatarUrl,
required Room room}) {
factory User(
String id, {
String? membership,
String? displayName,
String? avatarUrl,
required Room room,
}) {
return User.fromState(
stateKey: id,
senderId: id,
eventId: 'fake_event',
content: {
if (membership != null) 'membership': membership,
if (displayName != null) 'displayname': displayName,
if (avatarUrl != null) 'avatar_url': avatarUrl,
},
typeKey: EventTypes.RoomMember,
roomId: room.id,
room: room,
originServerTs: DateTime.now(),
);
}
User.fromState({
dynamic prevContent,
Map<String, dynamic>? prevContent,
required String stateKey,
dynamic content,
Map<String, dynamic> content = const {},
required String typeKey,
String eventId = 'fakevent',
String? roomId,
String senderId = 'fakesender',
required String eventId,
required String senderId,
required DateTime originServerTs,
dynamic unsigned,
required Room room,

View File

@ -220,6 +220,13 @@ extension CommandsClientExtension on Client {
return '';
});
addCommand('markasdm', (CommandArgs args) async {
final mxid = args.msg;
if (!mxid.isValidMatrixId) {
throw Exception('You must enter a valid mxid when using /maskasdm');
}
if (await args.room.requestUser(mxid, requestProfile: false) == null) {
throw Exception('User $mxid is not in this room');
}
await args.room.addToDirectChat(args.msg);
return;
});

View File

@ -314,7 +314,7 @@ void main() {
test('markasdm', () async {
FakeMatrixApi.calledEndpoints.clear();
await room.sendTextEvent('/markasdm @fakealice:example.com');
await room.sendTextEvent('/markasdm @test:fakeServer.notExisting');
expect(
json.decode(FakeMatrixApi
.calledEndpoints[
@ -325,7 +325,7 @@ void main() {
json.decode(FakeMatrixApi
.calledEndpoints[
'/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct']
?.first)?['@fakealice:example.com'],
?.first)?['@test:fakeServer.notExisting'],
['!1234:fakeServer.notExisting']);
expect(
json
@ -335,19 +335,12 @@ void main() {
?.first)
.entries
.any((e) =>
e.key != '@fakealice:example.com' &&
e.key != '@test:fakeServer.notExisting' &&
e.key != '@alice:example.com' &&
e.value.contains('!1234:fakeServer.notExisting')),
false);
FakeMatrixApi.calledEndpoints.clear();
await room.sendTextEvent('/markasdm');
expect(
json.decode(FakeMatrixApi
.calledEndpoints[
'/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct']
?.first)?[''],
['!1234:fakeServer.notExisting']);
});
test('markasgroup', () async {