WIP: Support for pinned messages #49
2 changed files with 14 additions and 36 deletions
cleanup PinnedEventsController
commit
177c057337
|
|
@ -1,7 +1,7 @@
|
||||||
import "package:collection/collection.dart";
|
|
||||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||||
import "package:nexus/controllers/client_controller.dart";
|
import "package:nexus/controllers/client_controller.dart";
|
||||||
|
import "package:nexus/controllers/event_controller.dart";
|
||||||
import "package:nexus/controllers/rooms_controller.dart";
|
import "package:nexus/controllers/rooms_controller.dart";
|
||||||
import "package:nexus/models/content/content.dart";
|
import "package:nexus/models/content/content.dart";
|
||||||
import "package:nexus/models/content/pinned_events.dart";
|
import "package:nexus/models/content/pinned_events.dart";
|
||||||
|
|
@ -26,42 +26,19 @@ class PinnedEventsController extends AsyncNotifier<IList<Event>> {
|
||||||
|
|
||||||
if (pinnedStateEvent == null) return .new();
|
if (pinnedStateEvent == null) return .new();
|
||||||
|
|
||||||
final pinnedContent = switch (pinnedStateEvent.content) {
|
if (pinnedStateEvent.content case PinnedEventsContent content) {
|
||||||
PinnedEventsContent content => content,
|
return (await Future.wait(
|
||||||
_ => null,
|
content.pinnedEvents.map(
|
||||||
};
|
(eventId) => ref.watch(
|
||||||
|
EventController.provider(
|
||||||
if (pinnedContent == null || pinnedContent.pinned.isEmpty) {
|
.new(eventId: eventId, roomId: roomId),
|
||||||
return .new();
|
).future,
|
||||||
}
|
|
||||||
|
|
||||||
final localEvents = <Event>[];
|
|
||||||
final missingIds = <String>[];
|
|
||||||
|
|
||||||
for (final eventId in pinnedContent.pinned) {
|
|
||||||
final localEvent = room.events.values.firstWhereOrNull(
|
|
||||||
(e) => e.eventId == eventId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (localEvent != null) {
|
|
||||||
localEvents.add(localEvent);
|
|
||||||
} else {
|
|
||||||
missingIds.add(eventId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (missingIds.isNotEmpty) {
|
|
||||||
final client = ref.read(ClientController.provider.notifier);
|
|
||||||
final fetched = await Future.wait(
|
|
||||||
missingIds.map(
|
|
||||||
(id) => client.getEvent(.new(roomId: roomId, eventId: id)),
|
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
|
)).nonNulls.toIList();
|
||||||
localEvents.addAll(fetched.whereType<Event>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return localEvents.toIList();
|
return .new();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> togglePin(Event event) async {
|
Future<void> togglePin(Event event) async {
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,9 @@ part "pinned_events.g.dart";
|
||||||
@freezed
|
@freezed
|
||||||
abstract class PinnedEventsContent extends Content with _$PinnedEventsContent {
|
abstract class PinnedEventsContent extends Content with _$PinnedEventsContent {
|
||||||
PinnedEventsContent._();
|
PinnedEventsContent._();
|
||||||
factory PinnedEventsContent({@Default(IList.empty()) IList<String> pinned}) =
|
factory PinnedEventsContent({
|
||||||
_PinnedEventsContent;
|
@Default(IList.empty()) @JsonKey(name: "pinned") IList<String> pinnedEvents,
|
||||||
|
}) = _PinnedEventsContent;
|
||||||
|
|
||||||
factory PinnedEventsContent.fromJson(Map<String, Object?> json) =>
|
factory PinnedEventsContent.fromJson(Map<String, Object?> json) =>
|
||||||
_$PinnedEventsContentFromJson(json);
|
_$PinnedEventsContentFromJson(json);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue