Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions commet/lib/client/matrix/matrix_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class MatrixRoom extends Room {
String get developerInfo =>
const JsonEncoder.withIndent(' ').convert(_matrixRoom.states);

Color? hashColor;
@override
Color get defaultColor {
var comp = client.getComponent<DirectMessagesComponent>();
Expand All @@ -130,12 +131,22 @@ class MatrixRoom extends Room {
}
}

return getColorOfUser(identifier);
if (hashColor != null) return hashColor!;

hashColor = MatrixPeer.hashColor(identifier);

return hashColor!;
}

// cache the result of push rule because this was becoming an expensive operation for ui stuff
matrix.PushRuleState? _pushRule;
@override
PushRule get pushRule {
switch (_matrixRoom.pushRuleState) {
if (_pushRule == null) {
_pushRule = _matrixRoom.pushRuleState;
}

switch (_pushRule!) {
case matrix.PushRuleState.notify:
return PushRule.notify;
case matrix.PushRuleState.mentionsOnly:
Expand All @@ -145,6 +156,27 @@ class MatrixRoom extends Room {
}
}

@override
Future<void> setPushRule(PushRule rule) async {
var newRule = _matrixRoom.pushRuleState;

switch (rule) {
case PushRule.notify:
newRule = matrix.PushRuleState.notify;
break;
case PushRule.mentionsOnly:
newRule = matrix.PushRuleState.mentionsOnly;
break;
case PushRule.dontNotify:
newRule = matrix.PushRuleState.dontNotify;
break;
}

await _matrixRoom.setPushRuleState(newRule);
_pushRule = _matrixRoom.pushRuleState;
_onUpdate.add(null);
}

@override
ImageProvider<Object>? get avatar {
final comp = client.getComponent<DirectMessagesComponent>();
Expand Down Expand Up @@ -521,26 +553,6 @@ class MatrixRoom extends Room {
await _matrixRoom.enableEncryption();
}

@override
Future<void> setPushRule(PushRule rule) async {
var newRule = _matrixRoom.pushRuleState;

switch (rule) {
case PushRule.notify:
newRule = matrix.PushRuleState.notify;
break;
case PushRule.mentionsOnly:
newRule = matrix.PushRuleState.mentionsOnly;
break;
case PushRule.dontNotify:
newRule = matrix.PushRuleState.dontNotify;
break;
}

await _matrixRoom.setPushRuleState(newRule);
_onUpdate.add(null);
}

@override
Future<void> setDisplayName(String newName) async {
_displayName = newName;
Expand Down Expand Up @@ -762,6 +774,7 @@ class MatrixRoom extends Room {

void onRoomSyncUpdate(matrix.SyncUpdate event) {
var update = event.rooms?.join?[_matrixRoom.id];

if (update == null) return;

_onUpdate.add(null);
Expand Down
72 changes: 47 additions & 25 deletions commet/lib/client/matrix/matrix_space.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,46 @@ class MatrixSpace extends Space {

@override
Color get color => MatrixPeer.hashColor(_matrixRoom.id);

// cache the result of push rule because this was becoming an expensive operation for ui stuff
matrix.PushRuleState? _pushRule;
@override
PushRule get pushRule {
switch (_matrixRoom.pushRuleState) {
if (_pushRule == null) {
_pushRule = _matrixRoom.pushRuleState;
}

switch (_pushRule!) {
case matrix.PushRuleState.notify:
return PushRule.notify;
case matrix.PushRuleState.mentionsOnly:
return PushRule.notify;
return PushRule.mentionsOnly;
case matrix.PushRuleState.dontNotify:
return PushRule.dontNotify;
}
}

@override
Future<void> setPushRule(PushRule rule) async {
var newRule = _matrixRoom.pushRuleState;

switch (rule) {
case PushRule.notify:
newRule = matrix.PushRuleState.notify;
break;
case PushRule.mentionsOnly:
newRule = matrix.PushRuleState.mentionsOnly;
break;
case PushRule.dontNotify:
newRule = matrix.PushRuleState.dontNotify;
break;
}

await _matrixRoom.setPushRuleState(newRule);
_pushRule = _matrixRoom.pushRuleState;
_onUpdate.add(null);
}

@override
RoomVisibility get visibility {
switch (_matrixRoom.joinRules) {
Expand Down Expand Up @@ -147,14 +175,18 @@ class MatrixSpace extends Space {

late List<StreamSubscription> _subscriptions;

bool _isTopLevel = false;
@override
bool get isTopLevel {
bool get isTopLevel => _isTopLevel;

void _updateTopLevelStatus() {
for (var room in _matrixClient.rooms.where((r) => r.isSpace)) {
if (room.spaceChildren.any((child) => child.roomId == _matrixRoom.id)) {
return false;
_isTopLevel = false;
return;
}
}
return true;
_isTopLevel = true;
}

MatrixSpace(
Expand All @@ -173,6 +205,9 @@ class MatrixSpace extends Space {
client.onRoomAdded.listen((_) => updateRoomsList()),
client.onRoomRemoved.listen(onClientRoomRemoved),
client.matrixClient.onSync.stream.listen(onMatrixSync),
client.matrixClient.onRoomState.stream
.where((i) => i.roomId == room.id)
.listen(onStateChanged),

// Subscribe to all child update events
_rooms.onAdd.listen(_onRoomAdded),
Expand All @@ -187,6 +222,12 @@ class MatrixSpace extends Space {
}

updateRoomsList();
_updateTopLevelStatus();
}

void onStateChanged(
({String roomId, matrix.StrippedStateEvent state}) event) {
refresh();
}

@override
Expand Down Expand Up @@ -313,26 +354,6 @@ class MatrixSpace extends Space {
_onUpdate.add(null);
}

@override
Future<void> setPushRule(PushRule rule) async {
var newRule = _matrixRoom.pushRuleState;

switch (rule) {
case PushRule.notify:
newRule = matrix.PushRuleState.notify;
break;
case PushRule.mentionsOnly:
newRule = matrix.PushRuleState.mentionsOnly;
break;
case PushRule.dontNotify:
newRule = matrix.PushRuleState.dontNotify;
break;
}

await _matrixRoom.setPushRuleState(newRule);
_onUpdate.add(null);
}

@override
Future<void> loadExtra() async {
var response =
Expand Down Expand Up @@ -405,6 +426,7 @@ class MatrixSpace extends Space {

for (var id in update.keys) {
if (roomsWithChildren.any((i) => i.identifier == id)) {
_updateTopLevelStatus();
_onUpdate.add(null);
}
}
Expand Down
Loading