...
 
Commits (2)
......@@ -4,71 +4,52 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:matrix_sdk/src/event/room/room_event.dart';
import 'package:matrix_sdk/src/event/room/state/state_event.dart';
import 'package:matrix_sdk/src/identifier.dart';
import 'package:meta/meta.dart';
import '../../event.dart';
class PowerLevelsChangeEvent extends StateEvent {
@override
final PowerLevelsChange content;
PowerLevelsChangeEvent(this.content, RoomEventArgs args)
: super(args, stateKey: '');
final PowerLevelsChange previousContent;
/// Contains information whether certain properties of the
/// [PowerLevelsChangeEvent] have changed compared to the previous
/// [PowerLevelsChangeEvent].
///
/// If it's null, it means the previous content was not provided and what
/// exactly was changed is unknown.
final Changed changed;
PowerLevelsChangeEvent(
this.content,
RoomEventArgs args, {
this.previousContent,
}) : changed = previousContent != null
? Changed._(previousContent, content)
: null,
super(args, stateKey: '');
factory PowerLevelsChangeEvent.fromJson(RoomEventArgs args, dynamic json) {
if (!hasStateKey(json)) {
return null;
}
final content = json['content'];
final int ban = content['ban'] ?? 50;
final int invite = content['invite'] ?? 50;
final int kick = content['kick'] ?? 50;
final int redact = content['redact'] ?? 50;
final int eventsDefault = content['events_default'] ?? 0;
final jsonEvents = content['events'];
Map<Type, int> events;
if (jsonEvents != null) {
jsonEvents.removeWhere((type, powerLevel) => Event.typeOf(type) == null);
events = jsonEvents?.map((eventType, powerLevel) {
powerLevel = powerLevel is String ? int.parse(powerLevel) : powerLevel;
return MapEntry<Type, int>(Event.typeOf(eventType), powerLevel);
})?.cast<Type, int>();
}
final int stateDefault = content['state_default'] ?? 50;
final int userDefault = content['users_default'] ?? 0;
final Map<UserId, int> users = content['users'].map((userId, powerLevel) {
powerLevel = powerLevel is String ? int.parse(powerLevel) : powerLevel;
return MapEntry(UserId(userId), powerLevel);
}).cast<UserId, int>();
final roomNotifications = json.containsKey('notifications')
? json['notifications']['room'] ?? 50
: 50;
final previousContent =
json.containsKey('unsigned') ? json['unsigned']['prev_content'] : null;
return PowerLevelsChangeEvent(
PowerLevelsChange(
banLevel: ban,
inviteLevel: invite,
kickLevel: kick,
redactLevel: redact,
stateEventsDefaultLevel: stateDefault,
eventsDefaultLevel: eventsDefault,
eventLevels: events,
userDefaultLevel: userDefault,
userLevels: users,
roomNotificationLevel: roomNotifications,
),
PowerLevelsChange.fromJson(json['content']),
args,
previousContent: previousContent != null
? PowerLevelsChange.fromJson(previousContent)
: null,
);
}
}
......@@ -101,6 +82,52 @@ class PowerLevelsChange extends StateEventContent {
this.roomNotificationLevel = 50,
});
factory PowerLevelsChange.fromJson(Map<String, dynamic> content) {
final int ban = content['ban'] ?? 50;
final int invite = content['invite'] ?? 50;
final int kick = content['kick'] ?? 50;
final int redact = content['redact'] ?? 50;
final int eventsDefault = content['events_default'] ?? 0;
final jsonEvents = content['events'];
Map<Type, int> events;
if (jsonEvents != null) {
jsonEvents.removeWhere((type, powerLevel) => Event.typeOf(type) == null);
events = jsonEvents?.map((eventType, powerLevel) {
powerLevel = powerLevel is String ? int.parse(powerLevel) : powerLevel;
return MapEntry<Type, int>(Event.typeOf(eventType), powerLevel);
})?.cast<Type, int>();
}
final int stateDefault = content['state_default'] ?? 50;
final int userDefault = content['users_default'] ?? 0;
final Map<UserId, int> users = content['users'].map((userId, powerLevel) {
powerLevel = powerLevel is String ? int.parse(powerLevel) : powerLevel;
return MapEntry(UserId(userId), powerLevel);
}).cast<UserId, int>();
final int roomNotifications = content.containsKey('notifications')
? content['notifications']['room']?.round() ?? 50
: 50;
return PowerLevelsChange(
banLevel: ban,
inviteLevel: invite,
kickLevel: kick,
redactLevel: redact,
stateEventsDefaultLevel: stateDefault,
eventsDefaultLevel: eventsDefault,
eventLevels: events,
userDefaultLevel: userDefault,
userLevels: users,
roomNotificationLevel: roomNotifications,
);
}
@override
Map<String, dynamic> toJson() => super.toJson()
..addAll({
......@@ -148,3 +175,41 @@ class PowerLevelsChange extends StateEventContent {
);
}
}
class Changed {
final PowerLevelsChange _previous;
final PowerLevelsChange _current;
Changed._(this._previous, this._current);
bool get banLevel => _previous.banLevel != _current.banLevel;
bool get inviteLevel => _previous.inviteLevel != _current.inviteLevel;
bool get kickLevel => _previous.kickLevel != _current.kickLevel;
bool get redactLevel => _previous.redactLevel != _current.redactLevel;
bool get stateEventsDefaultLevel =>
_previous.stateEventsDefaultLevel != _current.stateEventsDefaultLevel;
bool get eventsDefaultLevel =>
_previous.eventsDefaultLevel != _current.eventsDefaultLevel;
bool eventLevel({@required Type of}) {
final previous = _previous.eventLevels[of];
final current = _current.eventLevels[of];
return previous != current;
}
bool get userDefaultLevel =>
_previous.userDefaultLevel != _current.userDefaultLevel;
bool userLevel({@required UserId of}) {
final previous = _previous.userLevels[of];
final current = _current.userLevels[of];
return previous != current;
}
bool get roomNotificationLevel =>
_previous.roomNotificationLevel != _current.roomNotificationLevel;
}
......@@ -217,12 +217,12 @@ void testLocalUser() {
test('stops sync process', () async {
StreamSubscription sub;
sub = user.sync.listen((_) {
sub = user.sync.listen((_) async {
expect(user.isSyncing, isTrue);
user.stopSync();
await user.stopSync();
expect(user.isSyncing, isFalse);
sub.cancel();
await sub.cancel();
});
await user.startSync();
......