...
 
Commits (3)
......@@ -119,4 +119,32 @@ class PowerLevelsChange extends StateEventContent {
'room': roomNotificationLevel,
},
});
PowerLevelsChange copyWith({
int banLevel,
int inviteLevel,
int kickLevel,
int redactLevel,
int stateEventsDefaultLevel,
int eventsDefaultLevel,
Map<Type, int> eventLevels,
int userDefaultLevel,
Map<UserId, int> userLevels,
int roomNotificationLevel,
}) {
return PowerLevelsChange(
banLevel: banLevel ?? this.banLevel,
inviteLevel: inviteLevel ?? this.inviteLevel,
kickLevel: kickLevel ?? this.kickLevel,
redactLevel: redactLevel ?? this.redactLevel,
stateEventsDefaultLevel:
stateEventsDefaultLevel ?? this.stateEventsDefaultLevel,
eventsDefaultLevel: eventsDefaultLevel ?? this.eventsDefaultLevel,
eventLevels: eventLevels ?? this.eventLevels,
userDefaultLevel: userDefaultLevel ?? this.userDefaultLevel,
userLevels: userLevels ?? this.userLevels,
roomNotificationLevel:
roomNotificationLevel ?? this.roomNotificationLevel,
);
}
}
......@@ -179,8 +179,13 @@ class Room extends Identifiable<RoomId> {
bool canChangeTopic(UserId senderId) => canSend<TopicChangeEvent>(senderId);
/// Whether the user with [senderId] can change the power levels of this room.
bool canChangePowerLevels(UserId senderId) =>
canSend<PowerLevelsChangeEvent>(senderId);
///
/// If [userId] is provided, returns whether the user with [senderId] can
/// change the power level of [userId].
bool canChangePowerLevels(UserId senderId, {UserId userId}) =>
canSend<PowerLevelsChangeEvent>(senderId) && userId != null
? powerLevels.of(userId) < powerLevels.of(senderId)
: true;
/// Whether the user with [senderId] can upgrade this room.
bool canUpgrade(UserId senderId) => canSend<RoomUpgradeEvent>(senderId);
......@@ -521,6 +526,25 @@ class JoinedRoom extends Room {
return senderLevel >= (powerLevels.events[E] ?? defaultLevel);
}
/// Sends a [PowerLevelsChangeEvent], changing the power level of the user
/// with [id] to [to]. Use [canChangePowerLevel] using [userId] to check if
/// the [LocalUser] can actually change the power level of the user with [id].
///
/// Returns the [Stream] with the current [SentState] of the
/// [PowerLevelsChangeEvent].
Stream<SentState> changePowerLevelOf(UserId id, {@required int to}) async* {
final userLevels = Map<UserId, int>.from(
stateEvents.powerLevelsChange.content.userLevels,
);
userLevels[id] = to;
yield* send(
stateEvents.powerLevelsChange.content.copyWith(
userLevels: userLevels,
),
);
}
/// Send an [Event] to the [Room]. Can be any [RoomEvent]
/// including [StateEvent]s.
///
......
......@@ -26,6 +26,7 @@ import 'package:rxdart/rxdart.dart';
import 'package:http/http.dart' as http;
import 'device.dart';
import 'event/event.dart';
import 'exception.dart';
import 'membership.dart';
import 'notifications/pushers.dart';
......@@ -141,13 +142,26 @@ class User extends Identifiable<UserId> implements Comparable<User> {
bool canChangeTopicIn(Room room) => room.canChangeTopic(id);
/// Whether the user can change the power levels of [room].
bool canChangePowerLevelsIn(Room room) => room.canChangePowerLevels(id);
///
/// If [of] is provided, returns whether the user can change the power level
/// of [of] in [room].
bool canChangePowerLevelsIn(Room room, {User of}) =>
room.canChangePowerLevels(id, userId: of.id);
/// Whether the user can upgrade [room].
bool canUpgrade(Room room) => room.canUpgrade(id);
/// Whether the user can message in [room].
bool canMessageIn(Room room) => room.canMessage(id);
/// Sends a [PowerLevelsChangeEvent], changing the power level of [this] user
/// to [to]. Use [canChangePowerLevelsIn] using [of] to check if
/// the [LocalUser] can actually change the power level of [this] user.
///
/// Returns the [Stream] with the current [SentState] of the
/// [PowerLevelsChangeEvent].
Stream<SentState> setPowerLevelIn(JoinedRoom room, {@required int to}) =>
room.changePowerLevelOf(id, to: to);
}
/// A user which is authenticated and can send messages, join rooms etc.
......