chore(service): Move XmppState into its own service
This commit is contained in:
parent
9ae047b2d0
commit
f7665403b9
@ -9,7 +9,7 @@ import 'package:moxxyv2/service/conversation.dart';
|
||||
import 'package:moxxyv2/service/preferences.dart';
|
||||
import 'package:moxxyv2/service/roster.dart';
|
||||
import 'package:moxxyv2/service/service.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/avatar.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
import 'package:moxxyv2/shared/helpers.dart';
|
||||
@ -197,8 +197,8 @@ class AvatarService {
|
||||
Future<void> requestOwnAvatar() async {
|
||||
final am = GetIt.I.get<XmppConnection>()
|
||||
.getManagerById<UserAvatarManager>(userAvatarManager)!;
|
||||
final xmpp = GetIt.I.get<XmppService>();
|
||||
final state = await xmpp.getXmppState();
|
||||
final xss = GetIt.I.get<XmppStateService>();
|
||||
final state = await xss.getXmppState();
|
||||
final jid = state.jid!;
|
||||
final idResult = await am.getAvatarId(jid);
|
||||
if (idResult.isType<AvatarError>()) {
|
||||
@ -225,7 +225,7 @@ class AvatarService {
|
||||
jid,
|
||||
state.avatarUrl,
|
||||
);
|
||||
await xmpp.modifyXmppState((state) => state.copyWith(
|
||||
await xss.modifyXmppState((state) => state.copyWith(
|
||||
avatarUrl: avatarPath,
|
||||
avatarHash: avatarData.hash,
|
||||
),);
|
||||
|
@ -38,7 +38,6 @@ import 'package:moxxyv2/service/not_specified.dart';
|
||||
import 'package:moxxyv2/service/omemo/omemo.dart';
|
||||
import 'package:moxxyv2/service/omemo/types.dart';
|
||||
import 'package:moxxyv2/service/roster.dart';
|
||||
import 'package:moxxyv2/service/state.dart';
|
||||
import 'package:moxxyv2/shared/models/conversation.dart';
|
||||
import 'package:moxxyv2/shared/models/media.dart';
|
||||
import 'package:moxxyv2/shared/models/message.dart';
|
||||
@ -47,6 +46,7 @@ import 'package:moxxyv2/shared/models/reaction.dart';
|
||||
import 'package:moxxyv2/shared/models/roster.dart';
|
||||
import 'package:moxxyv2/shared/models/sticker.dart' as sticker;
|
||||
import 'package:moxxyv2/shared/models/sticker_pack.dart' as sticker_pack;
|
||||
import 'package:moxxyv2/shared/models/xmpp_state.dart';
|
||||
import 'package:omemo_dart/omemo_dart.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:random_string/random_string.dart';
|
||||
|
@ -22,9 +22,9 @@ import 'package:moxxyv2/service/omemo/omemo.dart';
|
||||
import 'package:moxxyv2/service/preferences.dart';
|
||||
import 'package:moxxyv2/service/roster.dart';
|
||||
import 'package:moxxyv2/service/service.dart';
|
||||
import 'package:moxxyv2/service/state.dart';
|
||||
import 'package:moxxyv2/service/stickers.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/commands.dart';
|
||||
import 'package:moxxyv2/shared/eventhandler.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
@ -33,6 +33,7 @@ import 'package:moxxyv2/shared/models/preferences.dart';
|
||||
import 'package:moxxyv2/shared/models/reaction.dart';
|
||||
import 'package:moxxyv2/shared/models/sticker.dart' as sticker;
|
||||
import 'package:moxxyv2/shared/models/sticker_pack.dart' as sticker_pack;
|
||||
import 'package:moxxyv2/shared/models/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/synchronized_queue.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
@ -127,8 +128,8 @@ Future<void> performLogin(LoginCommand command, { dynamic extra }) async {
|
||||
}
|
||||
|
||||
Future<PreStartDoneEvent> _buildPreStartDoneEvent(PreferencesState preferences) async {
|
||||
final xmpp = GetIt.I.get<XmppService>();
|
||||
final state = await xmpp.getXmppState();
|
||||
final xss = GetIt.I.get<XmppStateService>();
|
||||
final state = await xss.getXmppState();
|
||||
|
||||
await GetIt.I.get<RosterService>().loadRosterFromDatabase();
|
||||
|
||||
@ -138,7 +139,7 @@ Future<PreStartDoneEvent> _buildPreStartDoneEvent(PreferencesState preferences)
|
||||
if (storagePerm.isDenied /*&& !state.askedStoragePermission*/) {
|
||||
permissions.add(Permission.storage.value);
|
||||
|
||||
await xmpp.modifyXmppState((state) => state.copyWith(
|
||||
await xss.modifyXmppState((state) => state.copyWith(
|
||||
askedStoragePermission: true,
|
||||
),);
|
||||
}
|
||||
@ -352,7 +353,7 @@ Future<void> performSetPreferences(SetPreferencesCommand command, { dynamic extr
|
||||
// If sticker visibility was changed, apply the settings to the PubSub node
|
||||
final pm = GetIt.I.get<XmppConnection>()
|
||||
.getManagerById<PubSubManager>(pubsubManager)!;
|
||||
final ownJid = (await GetIt.I.get<XmppService>().getXmppState()).jid!;
|
||||
final ownJid = (await GetIt.I.get<XmppStateService>().getXmppState()).jid!;
|
||||
if (command.preferences.isStickersNodePublic && !oldPrefs.isStickersNodePublic) {
|
||||
// Set to open
|
||||
unawaited(
|
||||
@ -480,9 +481,9 @@ Future<void> performRequestDownload(RequestDownloadCommand command, { dynamic ex
|
||||
}
|
||||
|
||||
Future<void> performSetAvatar(SetAvatarCommand command, { dynamic extra }) async {
|
||||
await GetIt.I.get<XmppService>().modifyXmppState((state) => state.copyWith(
|
||||
avatarUrl: command.path,
|
||||
avatarHash: command.hash,
|
||||
await GetIt.I.get<XmppStateService>().modifyXmppState((state) => state.copyWith(
|
||||
avatarUrl: command.path,
|
||||
avatarHash: command.hash,
|
||||
),);
|
||||
await GetIt.I.get<AvatarService>().publishAvatar(command.path, command.hash);
|
||||
}
|
||||
@ -566,9 +567,9 @@ Future<void> performSignOut(SignOutCommand command, { dynamic extra }) async {
|
||||
final id = extra as String;
|
||||
|
||||
final conn = GetIt.I.get<XmppConnection>();
|
||||
final xmpp = GetIt.I.get<XmppService>();
|
||||
final xss = GetIt.I.get<XmppStateService>();
|
||||
await conn.disconnect();
|
||||
await xmpp.modifyXmppState((state) => XmppState());
|
||||
await xss.modifyXmppState((state) => XmppState());
|
||||
|
||||
sendEvent(
|
||||
SignedOutEvent(),
|
||||
|
@ -3,7 +3,7 @@ import 'package:get_it/get_it.dart';
|
||||
import 'package:moxxmpp/moxxmpp.dart';
|
||||
import 'package:moxxyv2/service/roster.dart';
|
||||
import 'package:moxxyv2/service/service.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
import 'package:moxxyv2/shared/models/roster.dart';
|
||||
|
||||
@ -12,7 +12,7 @@ class MoxxyRosterStateManager extends BaseRosterStateManager {
|
||||
Future<RosterCacheLoadResult> loadRosterCache() async {
|
||||
final rs = GetIt.I.get<RosterService>();
|
||||
return RosterCacheLoadResult(
|
||||
(await GetIt.I.get<XmppService>().getXmppState()).lastRosterVersion,
|
||||
(await GetIt.I.get<XmppStateService>().getXmppState()).lastRosterVersion,
|
||||
(await rs.getRoster()).map((item) => XmppRosterItem(
|
||||
jid: item.jid,
|
||||
name: item.title,
|
||||
@ -26,8 +26,8 @@ class MoxxyRosterStateManager extends BaseRosterStateManager {
|
||||
@override
|
||||
Future<void> commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added) async {
|
||||
final rs = GetIt.I.get<RosterService>();
|
||||
final xs = GetIt.I.get<XmppService>();
|
||||
await xs.modifyXmppState((state) => state.copyWith(
|
||||
final xss = GetIt.I.get<XmppStateService>();
|
||||
await xss.modifyXmppState((state) => state.copyWith(
|
||||
lastRosterVersion: version,
|
||||
),);
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:moxxmpp/moxxmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
|
||||
class MoxxyStreamManagementManager extends StreamManagementManager {
|
||||
@override
|
||||
@ -18,14 +18,14 @@ class MoxxyStreamManagementManager extends StreamManagementManager {
|
||||
|
||||
@override
|
||||
Future<void> commitState() async {
|
||||
await GetIt.I.get<XmppService>().modifyXmppState((s) => s.copyWith(
|
||||
await GetIt.I.get<XmppStateService>().modifyXmppState((s) => s.copyWith(
|
||||
smState: state,
|
||||
),);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> loadState() async {
|
||||
final state = await GetIt.I.get<XmppService>().getXmppState();
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
if (state.smState != null) {
|
||||
await setState(state.smState!);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import 'package:moxxyv2/service/moxxmpp/omemo.dart';
|
||||
import 'package:moxxyv2/service/omemo/implementations.dart';
|
||||
import 'package:moxxyv2/service/omemo/types.dart';
|
||||
import 'package:moxxyv2/service/service.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
import 'package:moxxyv2/shared/models/message.dart';
|
||||
import 'package:moxxyv2/shared/models/omemo_device.dart' as model;
|
||||
@ -126,7 +126,7 @@ class OmemoService {
|
||||
/// If, however, [jid] is our own JID, then nothing is done.
|
||||
Future<void> addNewDeviceMessage(String jid, int deviceId) async {
|
||||
// Add a pseudo message if it is not about our own devices
|
||||
final xmppState = await GetIt.I.get<XmppService>().getXmppState();
|
||||
final xmppState = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
if (jid == xmppState.jid) return;
|
||||
|
||||
final ms = GetIt.I.get<MessageService>();
|
||||
|
@ -32,6 +32,7 @@ import 'package:moxxyv2/service/preferences.dart';
|
||||
import 'package:moxxyv2/service/roster.dart';
|
||||
import 'package:moxxyv2/service/stickers.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/commands.dart';
|
||||
import 'package:moxxyv2/shared/eventhandler.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
@ -159,6 +160,7 @@ Future<void> entrypoint() async {
|
||||
GetIt.I.registerSingleton<CryptographyService>(CryptographyService());
|
||||
GetIt.I.registerSingleton<ContactsService>(ContactsService());
|
||||
GetIt.I.registerSingleton<StickersService>(StickersService());
|
||||
GetIt.I.registerSingleton<XmppStateService>(XmppStateService());
|
||||
final xmpp = XmppService();
|
||||
GetIt.I.registerSingleton<XmppService>(xmpp);
|
||||
|
||||
|
@ -12,7 +12,7 @@ import 'package:moxxyv2/service/httpfiletransfer/client.dart';
|
||||
import 'package:moxxyv2/service/httpfiletransfer/helpers.dart';
|
||||
import 'package:moxxyv2/service/preferences.dart';
|
||||
import 'package:moxxyv2/service/service.dart';
|
||||
import 'package:moxxyv2/service/xmpp.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
import 'package:moxxyv2/shared/helpers.dart';
|
||||
import 'package:moxxyv2/shared/models/sticker.dart';
|
||||
@ -79,7 +79,7 @@ class StickersService {
|
||||
_stickerPacks.remove(id);
|
||||
|
||||
// Retract from PubSub
|
||||
final state = await GetIt.I.get<XmppService>().getXmppState();
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
final result = await GetIt.I.get<moxxmpp.XmppConnection>()
|
||||
.getManagerById<moxxmpp.StickersManager>(moxxmpp.stickersManager)!
|
||||
.retractStickerPack(moxxmpp.JID.fromString(state.jid!), id);
|
||||
@ -91,7 +91,7 @@ class StickersService {
|
||||
|
||||
Future<void> _publishStickerPack(moxxmpp.StickerPack pack) async {
|
||||
final prefs = await GetIt.I.get<PreferencesService>().getPreferences();
|
||||
final state = await GetIt.I.get<XmppService>().getXmppState();
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
final result = await GetIt.I.get<moxxmpp.XmppConnection>()
|
||||
.getManagerById<moxxmpp.StickersManager>(moxxmpp.stickersManager)!
|
||||
.publishStickerPack(
|
||||
|
@ -27,8 +27,8 @@ import 'package:moxxyv2/service/omemo/omemo.dart';
|
||||
import 'package:moxxyv2/service/preferences.dart';
|
||||
import 'package:moxxyv2/service/roster.dart';
|
||||
import 'package:moxxyv2/service/service.dart';
|
||||
import 'package:moxxyv2/service/state.dart';
|
||||
import 'package:moxxyv2/service/stickers.dart';
|
||||
import 'package:moxxyv2/service/xmpp_state.dart';
|
||||
import 'package:moxxyv2/shared/error_types.dart';
|
||||
import 'package:moxxyv2/shared/eventhandler.dart';
|
||||
import 'package:moxxyv2/shared/events.dart';
|
||||
@ -78,30 +78,13 @@ class XmppService {
|
||||
/// Subscription to events by the XmppConnection
|
||||
StreamSubscription<dynamic>? _xmppConnectionSubscription;
|
||||
|
||||
/// Persistent state around the connection, like the SM token, ...
|
||||
// TODO(Unknown): Move somewhere else
|
||||
XmppState? _state;
|
||||
|
||||
Future<XmppState> getXmppState() async {
|
||||
if (_state != null) return _state!;
|
||||
|
||||
_state = await GetIt.I.get<DatabaseService>().getXmppState();
|
||||
return _state!;
|
||||
}
|
||||
|
||||
/// A wrapper to modify the [XmppState] and commit it.
|
||||
Future<void> modifyXmppState(XmppState Function(XmppState) func) async {
|
||||
_state = func(_state!);
|
||||
await GetIt.I.get<DatabaseService>().saveXmppState(_state!);
|
||||
}
|
||||
|
||||
/// Stores whether the app is open or not. Useful for notifications.
|
||||
void setAppState(bool open) {
|
||||
_appOpen = open;
|
||||
}
|
||||
|
||||
Future<ConnectionSettings?> getConnectionSettings() async {
|
||||
final state = await getXmppState();
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
|
||||
if (state.jid == null || state.password == null) {
|
||||
return null;
|
||||
@ -389,11 +372,13 @@ class XmppService {
|
||||
}
|
||||
|
||||
Future<void> connect(ConnectionSettings settings, bool triggeredFromUI) async {
|
||||
final lastResource = (await getXmppState()).resource;
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
final lastResource = state.resource;
|
||||
|
||||
_loginTriggeredFromUI = triggeredFromUI;
|
||||
GetIt.I.get<XmppConnection>().setConnectionSettings(settings);
|
||||
unawaited(GetIt.I.get<XmppConnection>().connect(
|
||||
unawaited(
|
||||
GetIt.I.get<XmppConnection>().connect(
|
||||
lastResource: lastResource,
|
||||
waitForConnection: true,
|
||||
),
|
||||
@ -402,7 +387,8 @@ class XmppService {
|
||||
}
|
||||
|
||||
Future<XmppConnectionResult> connectAwaitable(ConnectionSettings settings, bool triggeredFromUI) async {
|
||||
final lastResource = (await getXmppState()).resource;
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
final lastResource = state.resource;
|
||||
|
||||
_loginTriggeredFromUI = triggeredFromUI;
|
||||
GetIt.I.get<XmppConnection>().setConnectionSettings(settings);
|
||||
@ -672,9 +658,9 @@ class XmppService {
|
||||
|
||||
// TODO(Unknown): Maybe have something better
|
||||
final settings = connection.getConnectionSettings();
|
||||
await modifyXmppState((state) => state.copyWith(
|
||||
jid: settings.jid.toString(),
|
||||
password: settings.password,
|
||||
await GetIt.I.get<XmppStateService>().modifyXmppState((state) => state.copyWith(
|
||||
jid: settings.jid.toString(),
|
||||
password: settings.password,
|
||||
),);
|
||||
|
||||
_log.finest('Connection connected. Is resumed? ${event.resumed}');
|
||||
@ -717,7 +703,7 @@ class XmppService {
|
||||
|
||||
if (_loginTriggeredFromUI) {
|
||||
// TODO(Unknown): Trigger another event so the UI can see this aswell
|
||||
await modifyXmppState((state) => state.copyWith(
|
||||
await GetIt.I.get<XmppStateService>().modifyXmppState((state) => state.copyWith(
|
||||
jid: connection.getConnectionSettings().jid.toString(),
|
||||
displayName: connection.getConnectionSettings().jid.local,
|
||||
avatarUrl: '',
|
||||
@ -728,7 +714,7 @@ class XmppService {
|
||||
}
|
||||
|
||||
Future<void> _onResourceBindingSuccess(ResourceBindingSuccessEvent event, { dynamic extra }) async {
|
||||
await modifyXmppState((state) => state.copyWith(
|
||||
await GetIt.I.get<XmppStateService>().modifyXmppState((state) => state.copyWith(
|
||||
resource: event.resource,
|
||||
),);
|
||||
}
|
||||
@ -1016,7 +1002,7 @@ class XmppService {
|
||||
return;
|
||||
}
|
||||
|
||||
final state = await getXmppState();
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
final sender = event.fromJid.toBare().toString();
|
||||
final isCarbon = sender == state.jid;
|
||||
final reactions = List<Reaction>.from(msg.reactions);
|
||||
@ -1144,7 +1130,7 @@ class XmppService {
|
||||
if (!_isMessageEventMessage(event) && event.other['encryption_error'] == null) return;
|
||||
if (event.other['encryption_error'] is InvalidKeyExchangeException) return;
|
||||
|
||||
final state = await getXmppState();
|
||||
final state = await GetIt.I.get<XmppStateService>().getXmppState();
|
||||
final prefs = await GetIt.I.get<PreferencesService>().getPreferences();
|
||||
// The (portential) roster item of the chat partner
|
||||
final rosterItem = await GetIt.I.get<RosterService>().getRosterItemByJid(conversationJid);
|
||||
|
21
lib/service/xmpp_state.dart
Normal file
21
lib/service/xmpp_state.dart
Normal file
@ -0,0 +1,21 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:moxxyv2/service/database/database.dart';
|
||||
import 'package:moxxyv2/shared/models/xmpp_state.dart';
|
||||
|
||||
class XmppStateService {
|
||||
/// Persistent state around the connection, like the SM token, etc.
|
||||
XmppState? _state;
|
||||
|
||||
Future<XmppState> getXmppState() async {
|
||||
if (_state != null) return _state!;
|
||||
|
||||
_state = await GetIt.I.get<DatabaseService>().getXmppState();
|
||||
return _state!;
|
||||
}
|
||||
|
||||
/// A wrapper to modify the [XmppState] and commit it.
|
||||
Future<void> modifyXmppState(XmppState Function(XmppState) func) async {
|
||||
_state = func(_state!);
|
||||
await GetIt.I.get<DatabaseService>().saveXmppState(_state!);
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@ import 'dart:convert';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:moxxmpp/moxxmpp.dart';
|
||||
|
||||
part 'state.freezed.dart';
|
||||
part 'state.g.dart';
|
||||
part 'xmpp_state.freezed.dart';
|
||||
part 'xmpp_state.g.dart';
|
||||
|
||||
class StreamManagementStateConverter implements JsonConverter<StreamManagementState, Map<String, dynamic>> {
|
||||
const StreamManagementStateConverter();
|
Loading…
Reference in New Issue
Block a user