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( User get asUser => User.fromState(
// state key should always be set for member events // state key should always be set for member events
stateKey: stateKey!, stateKey: stateKey!,
prevContent: prevContent, prevContent: prevContent,
content: content, content: content,
typeKey: type, typeKey: type,
eventId: eventId, eventId: eventId,
roomId: roomId, senderId: senderId,
senderId: senderId, originServerTs: originServerTs,
originServerTs: originServerTs, unsigned: unsigned,
unsigned: unsigned, room: room,
room: room); );
String get messageType => type == EventTypes.Sticker String get messageType => type == EventTypes.Sticker
? MessageTypes.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. /// Represents a Matrix User which may be a participant in a Matrix Room.
class User extends Event { class User extends Event {
factory User(String id, factory User(
{String? membership, String id, {
String? displayName, String? membership,
String? avatarUrl, String? displayName,
required Room room}) { String? avatarUrl,
required Room room,
}) {
return User.fromState( return User.fromState(
stateKey: id, stateKey: id,
senderId: id,
eventId: 'fake_event',
content: { content: {
if (membership != null) 'membership': membership, if (membership != null) 'membership': membership,
if (displayName != null) 'displayname': displayName, if (displayName != null) 'displayname': displayName,
if (avatarUrl != null) 'avatar_url': avatarUrl, if (avatarUrl != null) 'avatar_url': avatarUrl,
}, },
typeKey: EventTypes.RoomMember, typeKey: EventTypes.RoomMember,
roomId: room.id,
room: room, room: room,
originServerTs: DateTime.now(), originServerTs: DateTime.now(),
); );
} }
User.fromState({ User.fromState({
dynamic prevContent, Map<String, dynamic>? prevContent,
required String stateKey, required String stateKey,
dynamic content, Map<String, dynamic> content = const {},
required String typeKey, required String typeKey,
String eventId = 'fakevent', required String eventId,
String? roomId, required String senderId,
String senderId = 'fakesender',
required DateTime originServerTs, required DateTime originServerTs,
dynamic unsigned, dynamic unsigned,
required Room room, required Room room,

View File

@ -220,6 +220,13 @@ extension CommandsClientExtension on Client {
return ''; return '';
}); });
addCommand('markasdm', (CommandArgs args) async { 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); await args.room.addToDirectChat(args.msg);
return; return;
}); });

View File

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