fluffychat/lib/widgets/matrix.dart
ggurdin 36a4654b2d
Fluffychat merge (#2055)
* Translated using Weblate (Tamil)

Currently translated at 100.0% (696 of 696 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ta/

* Translated using Weblate (Italian)

Currently translated at 100.0% (696 of 696 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/it/

* Translated using Weblate (Czech)

Currently translated at 77.5% (540 of 696 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/cs/

* Translated using Weblate (Tamil)

Currently translated at 100.0% (696 of 696 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ta/

* build: Bump version to v1.24.0

* fix: Only try again load mxc image on io exception

* fix: swipe_to_action upgrade to 0.3.0

Fixes #1415

* Update Mastodon Link in README.md

* feat: Swipe to next or previous image in image viewer

* chore: Follow up image viewer swipe

* chore: Follow up image viewer

* chore: Follow up imageviewer

* chore: Follow up image viewer

* chore: Try out new matrix dart sdk

* build: Update matrix dart sdk

* fix: Do not leave old room if join new room failed

* feat: Display file description on all file events

* chore: Show close icon for device verification warning

* feat: Prevent sending messages if other party has no encryption keys

* chore: Follow up other party has no keys

* build: Update flutter to 3.27.2

* chore: Update matrix dart sdk

* chore: Update lastEvent after redaction

* chore: Follow up reply color

* chore: Follow up colors reply

* chore: Follow up snackbar theme

* build: Update Flutter Version

* chore: Design adjustments

* chore: Adjust emoji picker design

* chore: Follow up emoji picker design

* chore: Follow up design

* chore: adjust design

* chore: Adjust colors

* build: Update matrix dart sdk

* fix: Textfields in dialogs on iOS

* chore: UX Feedback when selecting files needs some time

* chore: Do only show fileDescription if filename is not null

* chore: Follow up format

* refactor: Improve sso login UX on web

* refactor: New html rendering

* refactor: Switch to ubuntu font

* refactor: Remove unused class

* chore: Design adjustments

* chore: Follow up html rendering

* chore: Follow up html rendering

* chore: Adjust share scaffold dialog design

* chore: Better connection status indicator

* chore: Follow up connection status

* chore: Follow up sync status

* chore: Slightly adjust welcome screen

* chore: Adjust button icon colors

* chore: Follow up connection status

* chore: Add start to ordered list

* chore: Follow up html lists

* chore: Follow up html rendering

* chore: Add tooltip to links in html

* chore: Add explanation for PlayStore Safety Standards

* chore: Use UbuntuMono

* feat: Pick share keys with

* chore: Adjust design

* chore: Add medium font

* chore: Follow up title spacing

* chore: Follow up colors

* chore: Adjust design of adaptive dialogs

* chore: Follow up colors

* chore: Follow up colors

* chore: Design follow up

* chore: Follow up colors

* feat: Display all push rules and allow to enable disable them

* feat: Inspect and delete push rules

* Translated using Weblate (Italian)

Currently translated at 99.8% (695 of 696 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/it/

* Translated using Weblate (Arabic)

Currently translated at 100.0% (698 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ar/

* Translated using Weblate (Slovak)

Currently translated at 29.3% (205 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/sk/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (698 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (Dutch)

Currently translated at 86.5% (604 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/nl/

* Translated using Weblate (Galician)

Currently translated at 100.0% (698 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/gl/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (698 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (Dutch)

Currently translated at 88.1% (615 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/nl/

* Translated using Weblate (Chinese (Traditional Han script))

Currently translated at 91.8% (641 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hant/

* Translated using Weblate (Estonian)

Currently translated at 99.4% (694 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/et/

* Translated using Weblate (Basque)

Currently translated at 100.0% (698 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/eu/

* Translated using Weblate (Ukrainian)

Currently translated at 100.0% (698 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/uk/

* Translated using Weblate (Korean)

Currently translated at 98.8% (690 of 698 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ko/

* Translated using Weblate (Estonian)

Currently translated at 99.2% (696 of 701 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/et/

* Translated using Weblate (Basque)

Currently translated at 100.0% (701 of 701 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/eu/

* Translated using Weblate (Galician)

Currently translated at 99.8% (700 of 701 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/gl/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (701 of 701 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (Korean)

Currently translated at 100.0% (701 of 701 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ko/

* Translated using Weblate (Estonian)

Currently translated at 100.0% (704 of 704 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/et/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (704 of 704 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (Irish)

Currently translated at 100.0% (704 of 704 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ga/

* Translated using Weblate (Latvian)

Currently translated at 100.0% (704 of 704 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/lv/

* chore: Adjust navrail design

* chore: Follow up push rules

* chore: Follow up push rule settings

* feat: Select share keys with property in security settings

* feat: Use dynamic gradient for chat bubbles

* chore: follow up chat bubble colors

* chore: Follow up message bubble colors

* fix: Image search rendering problem

* chore: Follow up bubble color

* chore: Message bubble color follow up

* chore: Follow up bubble color

* chore: Follow up message bubble color

* chore: Follow up linebreak formatting

* chore: Follow up code blocks

* build: Update to flutter 3.27.4

* docs: Fix snap store icon

* refactor: Display navigationrail in settings page

* build: Add locale config for android

* build: Fix ios debug build

* Translated using Weblate (German)

Currently translated at 99.5% (702 of 705 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/de/

* Translated using Weblate (Estonian)

Currently translated at 100.0% (705 of 705 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/et/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (705 of 705 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (German)

Currently translated at 94.6% (713 of 753 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/de/

* Translated using Weblate (Estonian)

Currently translated at 100.0% (753 of 753 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/et/

* Translated using Weblate (Basque)

Currently translated at 95.0% (716 of 753 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/eu/

* Translated using Weblate (Ukrainian)

Currently translated at 92.8% (699 of 753 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/uk/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (753 of 753 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (Latvian)

Currently translated at 100.0% (753 of 753 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/lv/

* Translated using Weblate (Estonian)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/et/

* Translated using Weblate (Chinese (Simplified Han script))

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/zh_Hans/

* Translated using Weblate (Catalan)

Currently translated at 99.8% (758 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ca/

* Translated using Weblate (Latvian)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/lv/

* Translated using Weblate (Korean)

Currently translated at 92.8% (705 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ko/

* Translated using Weblate (Irish)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/ga/

* chore: Follow up linebreaks in html rendering

* chore: Follow up html rendering br tag

* Translated using Weblate (Ukrainian)

Currently translated at 92.0% (699 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/uk/

* Translated using Weblate (Indonesian)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/id/

* Translated using Weblate (Galician)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/gl/

* Translated using Weblate (Spanish)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/es/

* Translated using Weblate (Croatian)

Currently translated at 83.2% (632 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/hr/

* Translated using Weblate (Ukrainian)

Currently translated at 93.5% (710 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/uk/

* refactor: Update arb file types

* build: Upgrade gradle

* refactor: Follow up fix types in localization files

* build: Automerge weblate PRs

* build: Follow up auto merge weblate

* build: Update PAT

* build: Update weblate auto merge

* build: Add missing permissions

* build: Update weblate auto merge

* build: remove weblate auto merge

* Translated using Weblate (German)

Currently translated at 94.0% (714 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/de/

* Translated using Weblate (Spanish)

Currently translated at 100.0% (759 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/es/

* Translated using Weblate (Ukrainian)

Currently translated at 93.8% (712 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/uk/

* build: Update flutter web uild

* chore: Follow up connection status header

* build: Switch to flutter_shortcuts_new

* refactor: Remove broken callkeep implementation

* refactor: Migrate uni_links to app_links

* refactor: Migrate to maintained badge package

* build: Update flutter_olm to 2.0.0

* build: Update native-imaging

* chore: Remove gradle workaround

* build: Update dependencies for flutter

* build: Update dependencies to remove more flutter android v1 references

* build: Update gradle version

* refactor: Switch to maintained qr code package

* chore: Make login with matrix id more prominent again

* build: Update fcm_shared_isolate

* build: Update native_imaging

* build: Add changelog for v1.25.0

* Translated using Weblate (French)

Currently translated at 84.4% (641 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/fr/

* Translated using Weblate (French)

Currently translated at 84.5% (642 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/fr/

* Translated using Weblate (French)

Currently translated at 86.6% (658 of 759 strings)

Translation: FluffyChat/Translations
Translate-URL: https://hosted.weblate.org/projects/fluffychat/translations/fr/

* build: Use correct flutter version in snapcraft

* build: Detect flutter path better

* build: Update snapcraft

* build: Follow up snapcraft build

* build: Install flutter via git in snapcraft

* chore: Follow up typo

* fix: Request notification permissions on iOS before getToken

* chore: Follow up request iOS permissions

* Revert "chore: Follow up request iOS permissions"

This reverts commit 2625e89a33.

* chore: Combine mimetype types in send file dialog logic

* build: Update flutter to 3.29.0

* Revert "build: Update flutter to 3.29.0"

* fix: Crash in settings when using MAS

* build: Fix build tailwindcss for website

* fix pubspec

* update index.html

* generated

* fluffychat merge

* update matrix SDK

* generated

* fix message bubble background color

* generated

---------

Co-authored-by: தமிழ்நேரம் <anishprabu.t@gmail.com>
Co-authored-by: Angelo Schirinzi <Odi-3@users.noreply.hosted.weblate.org>
Co-authored-by: Erin <erin@erindesu.cz>
Co-authored-by: Christian <christian-pauly@posteo.de>
Co-authored-by: Krille-chan <christian-kussowski@posteo.de>
Co-authored-by: Krille <c.kussowski@famedly.com>
Co-authored-by: EpicKiwi <me@epickiwi.fr>
Co-authored-by: Christian Tietze <me@christiantietze.de>
Co-authored-by: Rex_sa <rex.sa@pm.me>
Co-authored-by: Anonymous <noreply@weblate.org>
Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org>
Co-authored-by: Jelv <post@jelv.nl>
Co-authored-by: josé m <correoxm@disroot.org>
Co-authored-by: 玖然 <noctiro@gmail.com>
Co-authored-by: Priit Jõerüüt <hwlate@joeruut.com>
Co-authored-by: xabirequejo <xabi.rn@gmail.com>
Co-authored-by: Bezruchenko Simon <worcposj44@gmail.com>
Co-authored-by: kdh8219 <kdh8219@monamo.dev>
Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com>
Co-authored-by: Edgars Andersons <Edgars+Weblate@gaitenis.id.lv>
Co-authored-by: Jana <j.kussowski@gmail.com>
Co-authored-by: Ettore Atalan <atalanttore@googlemail.com>
Co-authored-by: Ihor Hordiichuk <igor_ck@outlook.com>
Co-authored-by: fadelkon <fadelkon@posteo.net>
Co-authored-by: Linerly <linerly@proton.me>
Co-authored-by: Alfredo Sola <alfredo@sola.es>
Co-authored-by: Milo Ivir <mail@milotype.de>
Co-authored-by: Antonin Del Fabbro <message@antonin.one>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-03-06 11:29:03 -05:00

546 lines
17 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
import 'package:provider/provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:universal_html/html.dart' as html;
import 'package:url_launcher/url_launcher_string.dart';
import 'package:fluffychat/pangea/common/controllers/pangea_controller.dart';
import 'package:fluffychat/pangea/common/utils/any_state_holder.dart';
import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/uia_request_manager.dart';
import 'package:fluffychat/utils/voip_plugin.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
import 'package:fluffychat/widgets/fluffy_chat_app.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';
import 'package:fluffychat/widgets/local_notifications_extension.dart';
import '../config/app_config.dart';
import '../config/setting_keys.dart';
import '../pages/key_verification/key_verification_dialog.dart';
import '../utils/account_bundles.dart';
import '../utils/background_push.dart';
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class Matrix extends StatefulWidget {
final Widget? child;
final List<Client> clients;
final Map<String, String>? queryParameters;
final SharedPreferences store;
const Matrix({
this.child,
required this.clients,
required this.store,
this.queryParameters,
super.key,
});
@override
MatrixState createState() => MatrixState();
/// Returns the (nearest) Client instance of your application.
static MatrixState of(BuildContext context) =>
Provider.of<MatrixState>(context, listen: false);
}
class MatrixState extends State<Matrix> with WidgetsBindingObserver {
int _activeClient = -1;
String? activeBundle;
// #Pangea
static late PangeaController pangeaController;
static PangeaAnyState pAnyState = PangeaAnyState();
// Pangea#
SharedPreferences get store => widget.store;
XFile? loginAvatar;
String? loginUsername;
bool? loginRegistrationSupported;
BackgroundPush? backgroundPush;
Client get client {
if (widget.clients.isEmpty) {
widget.clients.add(getLoginClient());
}
if (_activeClient < 0 || _activeClient >= widget.clients.length) {
// #Pangea
currentBundle!.first!.homeserver =
Uri.parse("https://${AppConfig.defaultHomeserver}");
// Pangea#
return currentBundle!.first!;
}
// #Pangea
widget.clients[_activeClient].homeserver =
Uri.parse("https://${AppConfig.defaultHomeserver}");
// Pangea#
return widget.clients[_activeClient];
}
VoipPlugin? voipPlugin;
bool get isMultiAccount => widget.clients.length > 1;
int getClientIndexByMatrixId(String matrixId) =>
widget.clients.indexWhere((client) => client.userID == matrixId);
late String currentClientSecret;
RequestTokenResponse? currentThreepidCreds;
void setActiveClient(Client? cl) {
final i = widget.clients.indexWhere((c) => c == cl);
if (i != -1) {
_activeClient = i;
// TODO: Multi-client VoiP support
createVoipPlugin();
} else {
Logs().w('Tried to set an unknown client ${cl!.userID} as active');
}
}
List<Client?>? get currentBundle {
if (!hasComplexBundles) {
return List.from(widget.clients);
}
final bundles = accountBundles;
if (bundles.containsKey(activeBundle)) {
return bundles[activeBundle];
}
return bundles.values.first;
}
Map<String?, List<Client?>> get accountBundles {
final resBundles = <String?, List<_AccountBundleWithClient>>{};
for (var i = 0; i < widget.clients.length; i++) {
final bundles = widget.clients[i].accountBundles;
for (final bundle in bundles) {
if (bundle.name == null) {
continue;
}
resBundles[bundle.name] ??= [];
resBundles[bundle.name]!.add(
_AccountBundleWithClient(
client: widget.clients[i],
bundle: bundle,
),
);
}
}
for (final b in resBundles.values) {
b.sort(
(a, b) => a.bundle!.priority == null
? 1
: b.bundle!.priority == null
? -1
: a.bundle!.priority!.compareTo(b.bundle!.priority!),
);
}
return resBundles
.map((k, v) => MapEntry(k, v.map((vv) => vv.client).toList()));
}
bool get hasComplexBundles => accountBundles.values.any((v) => v.length > 1);
Client? _loginClientCandidate;
Client getLoginClient() {
if (widget.clients.isNotEmpty && !client.isLogged()) {
return client;
}
final candidate = _loginClientCandidate ??= ClientManager.createClient(
'${AppConfig.applicationName}-${DateTime.now().millisecondsSinceEpoch}',
store,
)..onLoginStateChanged
.stream
.where((l) => l == LoginState.loggedIn)
.first
.then((_) {
if (!widget.clients.contains(_loginClientCandidate)) {
widget.clients.add(_loginClientCandidate!);
}
ClientManager.addClientNameToStore(
_loginClientCandidate!.clientName,
store,
);
_registerSubs(_loginClientCandidate!.clientName);
_loginClientCandidate = null;
FluffyChatApp.router.go('/rooms');
});
// #Pangea
candidate.homeserver = Uri.parse("https://${AppConfig.defaultHomeserver}");
// Pangea#
return candidate;
}
Client? getClientByName(String name) =>
widget.clients.firstWhereOrNull((c) => c.clientName == name);
final onRoomKeyRequestSub = <String, StreamSubscription>{};
final onKeyVerificationRequestSub = <String, StreamSubscription>{};
final onNotification = <String, StreamSubscription>{};
final onLoginStateChanged = <String, StreamSubscription<LoginState>>{};
final onUiaRequest = <String, StreamSubscription<UiaRequest>>{};
StreamSubscription<html.Event>? onFocusSub;
StreamSubscription<html.Event>? onBlurSub;
String? _cachedPassword;
Timer? _cachedPasswordClearTimer;
String? get cachedPassword => _cachedPassword;
set cachedPassword(String? p) {
Logs().d('Password cached');
_cachedPasswordClearTimer?.cancel();
_cachedPassword = p;
_cachedPasswordClearTimer = Timer(const Duration(minutes: 10), () {
_cachedPassword = null;
Logs().d('Cached Password cleared');
});
}
bool webHasFocus = true;
String? get activeRoomId {
final route = FluffyChatApp.router.routeInformationProvider.value.uri.path;
if (!route.startsWith('/rooms/')) return null;
return route.split('/')[2];
}
final linuxNotifications =
PlatformInfos.isLinux ? NotificationsClient() : null;
final Map<String, int> linuxNotificationIds = {};
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
initMatrix();
if (PlatformInfos.isWeb) {
initConfig().then((_) => initSettings());
} else {
initSettings();
}
// #Pangea
Sentry.configureScope(
(scope) => scope.setUser(
SentryUser(
id: client.userID,
name: client.userID,
),
),
);
pangeaController = PangeaController(matrix: widget, matrixState: this);
// Pangea#
}
Future<void> initConfig() async {
try {
final configJsonString =
utf8.decode((await http.get(Uri.parse('config.json'))).bodyBytes);
final configJson = json.decode(configJsonString);
AppConfig.loadFromJson(configJson);
} on FormatException catch (_) {
Logs().v('[ConfigLoader] config.json not found');
} catch (e) {
Logs().v('[ConfigLoader] config.json not found', e);
}
}
void _registerSubs(String name) {
final c = getClientByName(name);
if (c == null) {
Logs().w(
'Attempted to register subscriptions for non-existing client $name',
);
return;
}
onRoomKeyRequestSub[name] ??=
c.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
if (widget.clients.any(
((cl) =>
cl.userID == request.requestingDevice.userId &&
cl.identityKey == request.requestingDevice.curve25519Key),
)) {
Logs().i(
'[Key Request] Request is from one of our own clients, forwarding the key...',
);
await request.forwardKey();
}
});
onKeyVerificationRequestSub[name] ??= c.onKeyVerificationRequest.stream
.listen((KeyVerification request) async {
var hidPopup = false;
request.onUpdate = () {
if (!hidPopup &&
{KeyVerificationState.done, KeyVerificationState.error}
.contains(request.state)) {
FluffyChatApp.router.pop('dialog');
}
hidPopup = true;
};
request.onUpdate = null;
hidPopup = true;
await KeyVerificationDialog(request: request).show(
FluffyChatApp.router.routerDelegate.navigatorKey.currentContext ??
context,
);
});
onLoginStateChanged[name] ??=
c.onLoginStateChanged.stream.listen((state) async {
final loggedInWithMultipleClients = widget.clients.length > 1;
// #Pangea
// if (state == LoginState.loggedOut) {
// InitWithRestoreExtension.deleteSessionBackup(name);
// }
// Pangea#
if (loggedInWithMultipleClients && state != LoginState.loggedIn) {
_cancelSubs(c.clientName);
widget.clients.remove(c);
ClientManager.removeClientNameFromStore(c.clientName, store);
ScaffoldMessenger.of(
FluffyChatApp.router.routerDelegate.navigatorKey.currentContext ??
context,
).showSnackBar(
SnackBar(
content: Text(L10n.of(context).oneClientLoggedOut),
),
);
if (state != LoginState.loggedIn) {
FluffyChatApp.router.go('/rooms');
}
} else {
// #Pangea
if (state == LoginState.loggedIn) {
final futures = [
pangeaController.userController.reinitialize(),
pangeaController.subscriptionController.reinitialize(),
];
await Future.wait(futures);
}
String routeDestination;
if (state == LoginState.loggedIn) {
routeDestination =
await pangeaController.userController.isUserDataAvailableAndL2Set
? '/rooms'
: "/user_age";
} else {
routeDestination = '/home';
}
FluffyChatApp.router.go(routeDestination);
// FluffyChatApp.router
// .go(state == LoginState.loggedIn ? '/rooms' : '/home');
// Pangea#
}
});
onUiaRequest[name] ??= c.onUiaRequest.stream.listen(uiaRequestHandler);
if (PlatformInfos.isWeb || PlatformInfos.isLinux) {
c.onSync.stream.first.then((s) {
html.Notification.requestPermission();
onNotification[name] ??=
c.onNotification.stream.listen(showLocalNotification);
});
}
}
void _cancelSubs(String name) {
onRoomKeyRequestSub[name]?.cancel();
onRoomKeyRequestSub.remove(name);
onKeyVerificationRequestSub[name]?.cancel();
onKeyVerificationRequestSub.remove(name);
onLoginStateChanged[name]?.cancel();
onLoginStateChanged.remove(name);
onNotification[name]?.cancel();
onNotification.remove(name);
}
void initMatrix() {
for (final c in widget.clients) {
_registerSubs(c.clientName);
}
if (kIsWeb) {
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
}
if (PlatformInfos.isMobile) {
backgroundPush = BackgroundPush(
this,
onFcmError: (errorMsg, {Uri? link}) async {
final result = await showOkCancelAlertDialog(
context: FluffyChatApp
.router.routerDelegate.navigatorKey.currentContext ??
context,
title: L10n.of(context).pushNotificationsNotAvailable,
message: errorMsg,
okLabel:
link == null ? L10n.of(context).ok : L10n.of(context).learnMore,
cancelLabel: L10n.of(context).doNotShowAgain,
);
if (result == OkCancelResult.ok && link != null) {
launchUrlString(
link.toString(),
mode: LaunchMode.externalApplication,
);
}
if (result == OkCancelResult.cancel) {
await store.setBool(SettingKeys.showNoGoogle, true);
}
},
);
}
createVoipPlugin();
}
void createVoipPlugin() async {
if (store.getBool(SettingKeys.experimentalVoip) == false) {
voipPlugin = null;
return;
}
voipPlugin = VoipPlugin(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
Logs().v('AppLifecycleState = $state');
final foreground = state != AppLifecycleState.inactive &&
state != AppLifecycleState.paused;
client.syncPresence =
state == AppLifecycleState.resumed ? null : PresenceType.unavailable;
if (PlatformInfos.isMobile) {
client.backgroundSync = foreground;
client.requestHistoryOnLimitedTimeline = !foreground;
Logs().v('Set background sync to', foreground);
}
}
void initSettings() {
AppConfig.fontSizeFactor =
double.tryParse(store.getString(SettingKeys.fontSizeFactor) ?? '') ??
AppConfig.fontSizeFactor;
AppConfig.renderHtml =
store.getBool(SettingKeys.renderHtml) ?? AppConfig.renderHtml;
AppConfig.swipeRightToLeftToReply =
store.getBool(SettingKeys.swipeRightToLeftToReply) ??
AppConfig.swipeRightToLeftToReply;
AppConfig.hideRedactedEvents =
store.getBool(SettingKeys.hideRedactedEvents) ??
AppConfig.hideRedactedEvents;
AppConfig.hideUnknownEvents =
store.getBool(SettingKeys.hideUnknownEvents) ??
AppConfig.hideUnknownEvents;
AppConfig.hideUnimportantStateEvents =
store.getBool(SettingKeys.hideUnimportantStateEvents) ??
AppConfig.hideUnimportantStateEvents;
AppConfig.separateChatTypes =
store.getBool(SettingKeys.separateChatTypes) ??
AppConfig.separateChatTypes;
AppConfig.autoplayImages =
store.getBool(SettingKeys.autoplayImages) ?? AppConfig.autoplayImages;
AppConfig.sendTypingNotifications =
store.getBool(SettingKeys.sendTypingNotifications) ??
AppConfig.sendTypingNotifications;
AppConfig.sendPublicReadReceipts =
store.getBool(SettingKeys.sendPublicReadReceipts) ??
AppConfig.sendPublicReadReceipts;
AppConfig.sendOnEnter =
store.getBool(SettingKeys.sendOnEnter) ?? AppConfig.sendOnEnter;
AppConfig.experimentalVoip = store.getBool(SettingKeys.experimentalVoip) ??
AppConfig.experimentalVoip;
AppConfig.showPresences =
store.getBool(SettingKeys.showPresences) ?? AppConfig.showPresences;
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
onRoomKeyRequestSub.values.map((s) => s.cancel());
onKeyVerificationRequestSub.values.map((s) => s.cancel());
onLoginStateChanged.values.map((s) => s.cancel());
onNotification.values.map((s) => s.cancel());
client.httpClient.close();
onFocusSub?.cancel();
onBlurSub?.cancel();
linuxNotifications?.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => this,
child: widget.child,
);
}
Future<void> dehydrateAction(BuildContext context) async {
final response = await showOkCancelAlertDialog(
context: context,
isDestructive: true,
title: L10n.of(context).dehydrate,
message: L10n.of(context).dehydrateWarning,
);
if (response != OkCancelResult.ok) {
return;
}
final result = await showFutureLoadingDialog(
context: context,
future: client.exportDump,
);
final export = result.result;
if (export == null) return;
final exportBytes = Uint8List.fromList(
const Utf8Codec().encode(export),
);
final exportFileName =
'fluffychat-export-${DateFormat(DateFormat.YEAR_MONTH_DAY).format(DateTime.now())}.fluffybackup';
final file = MatrixFile(bytes: exportBytes, name: exportFileName);
file.save(context);
}
}
class _AccountBundleWithClient {
final Client? client;
final AccountBundle? bundle;
_AccountBundleWithClient({this.client, this.bundle});
}