add PMP support

Fixes #175
This commit is contained in:
Henry Hiles 2026-11-26 15:31:38 -05:00
commit 3b7a4c0cd4
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
4 changed files with 469 additions and 1 deletions

View file

@ -5,6 +5,8 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/client.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/per_message_profiles.dart";
import "package:nexus/models/account_data.dart";
import "package:nexus/models/push_rules.dart";
import "package:nexus/models/requests/update_push_rule.dart";
@ -73,6 +75,40 @@ class AccountDataController extends Notifier<AccountData> {
(data) => data.ignoredUserList.contains(userId) == ignored,
);
Future<void> setPerMessageProfiles(PerMessageProfiles profiles) =>
_sendAndWait(
() => ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(type: AccountData.perMessageProfilesKey, content: profiles),
),
(data) => data.perMessageProfiles == profiles,
);
Future<void> setRoomProfile(String roomId, String profileId) async {
const type = AccountData.perMessageProfilesKey;
final current = ref
.read(RoomsController.provider)[roomId]
?.accountData[type];
await ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(
type: type,
roomId: roomId,
content: {...?current, "default_profile_id": profileId},
),
);
await ref
.read(RoomsController.provider.notifier)
.awaitRooms(
(rooms) =>
rooms[roomId]?.accountData[type]?["default_profile_id"] ==
profileId,
);
}
/// Saves [rule] of [kind], creating it if it's new, or otherwise changing only
/// its actions and whether it's enabled.
Future<void> savePushRule(PushRuleKind kind, PushRule rule) {

View file

@ -0,0 +1,56 @@
import "package:collection/collection.dart";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter/foundation.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/account_data.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/account_data.dart";
import "package:nexus/models/per_message_profiles.dart";
typedef RoomPersonas = ({
IList<StoredProfile> profiles,
StoredProfile? current,
});
class PersonasController(final String roomId) extends Notifier<RoomPersonas> {
@override
RoomPersonas build() {
final global = ref.watch(
AccountDataController.provider.select((data) => data.perMessageProfiles),
);
final roomJson = ref.watch(
RoomsController.provider.select(
(rooms) =>
rooms[roomId]?.accountData[AccountData.perMessageProfilesKey],
),
);
PerMessageProfiles? room;
if (roomJson != null) {
try {
room = .fromJson(roomJson);
} catch (error) {
debugPrint("Ignoring invalid room per-message profiles: $error");
}
}
final roomIds = room?.profiles.map((profile) => profile.id).toISet();
final profiles = (room?.profiles ?? const IList<StoredProfile>.empty())
.addAll(
global.profiles.where(
(profile) => !(roomIds?.contains(profile.id) ?? false),
),
);
final currentId = room?.defaultProfileId ?? global.defaultProfileId;
return (
profiles: profiles,
current: currentId == null || currentId.isEmpty
? null
: profiles.firstWhereOrNull((profile) => profile.id == currentId),
);
}
static final provider = NotifierProvider.autoDispose
.family<PersonasController, RoomPersonas, String>(PersonasController.new);
}

View file

@ -189,6 +189,7 @@ class RoomsController extends Notifier<IMap<String, Room>> {
? incoming.timeline
: existing.timeline.addAll(incoming.timeline),
receipts: mergeReceipts(existing.receipts, incoming.receipts),
accountData: existing.accountData.addAll(incoming.accountData),
) ??
incoming.copyWith(
receipts: mergeReceipts(const .empty(), incoming.receipts),

View file

@ -43,6 +43,7 @@ import "package:nexus/widgets/settings/block_user_field.dart";
import "package:nexus/widgets/settings/dialog_list_tile.dart";
import "package:nexus/widgets/settings/enable_image_packs_sheet.dart";
import "package:nexus/widgets/settings/keyword_field.dart";
import "package:nexus/widgets/settings/persona_sheet.dart";
import "package:nexus/widgets/users/user_bottom_sheet.dart";
import "package:nexus/widgets/encryption/reset_encryption_sheet.dart";
@ -51,6 +52,9 @@ class SettingsSectionsController
@override
Future<IMap<String, IList<SettingsCategory>>> build() async {
final settings = await ref.watch(SettingsController.provider.future);
final personas = ref.watch(
AccountDataController.provider.select((data) => data.perMessageProfiles),
);
// Not awaited, so a slow homeserver doesn't hold up the rest of settings
final accountManagement = ref
.watch(AccountManagementController.provider)
@ -443,6 +447,99 @@ class SettingsSectionsController
),
]),
),
.new(
title: "Personas",
icon: Icons.groups,
settings: .new([
.new(
title: "Enable Personas",
description: "Send messages as other names and avatars, chosen per room from the composer.",
icon: Icons.groups,
builder: (title, description, icon) => SwitchListTile(
title: Text(title),
subtitle: Text(description),
secondary: Icon(icon),
value: settings.personas,
onChanged: (value) => ref
.read(SettingsController.provider.notifier)
.set(settings.copyWith(personas: value))
.onError(showError),
),
),
if (settings.personas) ...[
if (personas.profiles.isNotEmpty)
.new(
title: "Default Persona",
description: "Who you send as in rooms you haven't chosen a persona for.",
icon: Icons.person_pin,
builder: (title, description, icon) =>
DialogListTile<String>(
icon: Icon(icon),
title: title,
subtitle: Text(description),
value: personas.defaultProfileId ?? "",
options: [
"",
...personas.profiles.map((profile) => profile.id),
],
getName: (id) => switch (personas.byId(id)) {
final profile? => profile.displayName ?? id,
null => "Yourself",
},
onChanged: (id) => accountData
.setPerMessageProfiles(
personas.copyWith(
defaultProfileId: id.isEmpty ? null : id,
),
)
.onError(showError),
),
),
header(
"Personas",
"The names and avatars you can send messages as.",
Icons.groups,
),
...personas.profiles.map(
(persona) => Setting(
title: persona.displayName ?? persona.id,
description: "A persona.",
icon: Icons.person,
builder: (title, _, _) => Builder(
builder: (context) => ListTile(
leading: AvatarOrHash(persona.avatarUrl, title),
title: Text(title),
subtitle: switch (persona.triggers.firstOrNull) {
final trigger? => Text(
"Trigger: ${trigger.prefix}text${trigger.suffix}",
),
null => null,
},
trailing: Icon(Icons.chevron_right),
onTap: () => context.showAdaptiveSheet(
(_) => PersonaSheet(persona: persona),
),
),
),
),
),
.new(
title: "Add Persona",
description: "Create a name and avatar to send messages as.",
icon: Icons.add,
builder: (title, _, icon) => Builder(
builder: (context) => ListTile(
leading: Icon(icon),
title: Text(title),
onTap: () => context.showAdaptiveSheet(
(_) => const PersonaSheet(),
),
),
),
),
],
]),
),
.new(
title: "Image Packs",
icon: Icons.emoji_emotions,