diff --git a/lib/pages/settings_emotes/settings_emotes.dart b/lib/pages/settings_emotes/settings_emotes.dart index 2ef905b17..fbef5479c 100644 --- a/lib/pages/settings_emotes/settings_emotes.dart +++ b/lib/pages/settings_emotes/settings_emotes.dart @@ -330,8 +330,11 @@ class EmotesSettingsController extends State { for (final entry in pack.images.entries) { final emote = entry.value; final name = entry.key; - final url = emote.url.getDownloadLink(client); - final response = await get(url); + final url = await emote.url.getDownloadUri(client); + final response = await get( + url, + headers: {'authorization': 'Bearer ${client.accessToken}'}, + ); archive.addFile( ArchiveFile( diff --git a/lib/utils/client_download_content_extension.dart b/lib/utils/client_download_content_extension.dart new file mode 100644 index 000000000..9fff1eccd --- /dev/null +++ b/lib/utils/client_download_content_extension.dart @@ -0,0 +1,53 @@ +import 'dart:typed_data'; + +import 'package:matrix/matrix.dart'; + +extension ClientDownloadContentExtension on Client { + Future downloadMxcCached( + Uri mxc, { + num? width, + num? height, + bool isThumbnail = false, + bool? animated, + ThumbnailMethod? thumbnailMethod, + }) async { + // To stay compatible with previous storeKeys: + final cacheKey = isThumbnail + // ignore: deprecated_member_use + ? mxc.getThumbnail( + this, + width: width, + height: height, + animated: animated, + method: thumbnailMethod!, + ) + : mxc; + + final cachedData = await database?.getFile(cacheKey); + if (cachedData != null) return cachedData; + + final httpUri = isThumbnail + ? await mxc.getThumbnailUri( + this, + width: width, + height: height, + animated: animated, + method: thumbnailMethod, + ) + : await mxc.getDownloadUri(this); + + final response = await httpClient.get( + httpUri, + headers: + accessToken == null ? null : {'authorization': 'Bearer $accessToken'}, + ); + if (response.statusCode != 200) { + throw Exception(); + } + final remoteData = response.bodyBytes; + + await database?.storeFile(cacheKey, remoteData, 0); + + return remoteData; + } +} diff --git a/lib/utils/push_helper.dart b/lib/utils/push_helper.dart index edfdb4941..7a82b370f 100644 --- a/lib/utils/push_helper.dart +++ b/lib/utils/push_helper.dart @@ -1,11 +1,9 @@ -import 'dart:io'; import 'dart:ui'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:collection/collection.dart'; -import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_shortcuts/flutter_shortcuts.dart'; @@ -13,6 +11,7 @@ import 'package:matrix/matrix.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/utils/client_download_content_extension.dart'; import 'package:fluffychat/utils/client_manager.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart'; import 'package:fluffychat/utils/platform_infos.dart'; @@ -177,28 +176,25 @@ Future _tryPushHelper( ); // The person object for the android message style notification - final avatar = event.room.avatar - ?.getThumbnail( - client, - width: 256, - height: 256, - ) - .toString(); + final avatar = event.room.avatar; final senderAvatar = event.room.isDirectChat ? avatar - : event.senderFromMemoryOrFallback.avatarUrl - ?.getThumbnail( - client, - width: 256, - height: 256, - ) - .toString(); - - File? roomAvatarFile, senderAvatarFile; + : event.senderFromMemoryOrFallback.avatarUrl; + + Uint8List? roomAvatarFile, senderAvatarFile; try { roomAvatarFile = avatar == null ? null - : await DefaultCacheManager().getSingleFile(avatar); + : await client + .downloadMxcCached( + avatar, + thumbnailMethod: ThumbnailMethod.scale, + width: 256, + height: 256, + animated: false, + isThumbnail: true, + ) + .timeout(const Duration(seconds: 3)); } catch (e, s) { Logs().e('Unable to get avatar picture', e, s); } @@ -207,7 +203,16 @@ Future _tryPushHelper( ? roomAvatarFile : senderAvatar == null ? null - : await DefaultCacheManager().getSingleFile(senderAvatar); + : await client + .downloadMxcCached( + senderAvatar, + thumbnailMethod: ThumbnailMethod.scale, + width: 256, + height: 256, + animated: false, + isThumbnail: true, + ) + .timeout(const Duration(seconds: 3)); } catch (e, s) { Logs().e('Unable to get avatar picture', e, s); } @@ -225,7 +230,7 @@ Future _tryPushHelper( name: event.senderFromMemoryOrFallback.calcDisplayname(), icon: senderAvatarFile == null ? null - : BitmapFilePathAndroidIcon(senderAvatarFile.path), + : ByteArrayAndroidIcon(senderAvatarFile), ), ); @@ -272,7 +277,7 @@ Future _tryPushHelper( name: event.senderFromMemoryOrFallback.calcDisplayname(), icon: roomAvatarFile == null ? null - : BitmapFilePathAndroidIcon(roomAvatarFile.path), + : ByteArrayAndroidIcon(roomAvatarFile), key: event.roomId, important: event.room.isFavourite, ), @@ -321,7 +326,7 @@ Future _setShortcut( Event event, L10n l10n, String title, - File? avatarFile, + Uint8List? avatarFile, ) async { final flutterShortcuts = FlutterShortcuts(); await flutterShortcuts.initialize(debug: !kReleaseMode); @@ -333,8 +338,7 @@ Future _setShortcut( conversationShortcut: true, icon: avatarFile == null ? null - : ShortcutMemoryIcon(jpegImage: await avatarFile.readAsBytes()) - .toString(), + : ShortcutMemoryIcon(jpegImage: avatarFile).toString(), shortcutIconAsset: avatarFile == null ? ShortcutIconAsset.androidAsset : ShortcutIconAsset.memoryAsset, diff --git a/lib/widgets/local_notifications_extension.dart b/lib/widgets/local_notifications_extension.dart index e3d9ff3c4..99fd00cf9 100644 --- a/lib/widgets/local_notifications_extension.dart +++ b/lib/widgets/local_notifications_extension.dart @@ -6,12 +6,11 @@ import 'package:flutter/material.dart'; import 'package:desktop_notifications/desktop_notifications.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:go_router/go_router.dart'; -import 'package:http/http.dart' as http; import 'package:matrix/matrix.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:universal_html/html.dart' as html; import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/utils/client_download_content_extension.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/widgets/matrix.dart'; @@ -48,50 +47,38 @@ extension LocalNotificationsExtension on MatrixState { hideEdit: true, removeMarkdown: true, ); - final icon = event.senderFromMemoryOrFallback.avatarUrl?.getThumbnail( - client, - width: 64, - height: 64, - method: ThumbnailMethod.crop, - ) ?? - room.avatar?.getThumbnail( - client, - width: 64, - height: 64, - method: ThumbnailMethod.crop, - ); + if (kIsWeb) { + final avatarUrl = event.senderFromMemoryOrFallback.avatarUrl; + + final iconBytes = avatarUrl == null + ? null + : await client.downloadMxcCached( + avatarUrl, + width: 64, + height: 64, + thumbnailMethod: ThumbnailMethod.crop, + isThumbnail: true, + animated: false, + ); + _audioPlayer.play(); + html.Notification( title, body: body, - icon: icon.toString(), + icon: iconBytes == null + ? null + : html.Url.createObjectUrl(html.Blob(iconBytes)), + tag: event.room.id, ); } else if (Platform.isLinux) { - final appIconUrl = room.avatar?.getThumbnail( - room.client, - width: 56, - height: 56, - ); - File? appIconFile; - if (appIconUrl != null) { - final tempDirectory = await getApplicationSupportDirectory(); - final avatarDirectory = - await Directory('${tempDirectory.path}/notiavatars/').create(); - appIconFile = File( - '${avatarDirectory.path}/${Uri.encodeComponent(appIconUrl.toString())}', - ); - if (await appIconFile.exists() == false) { - final response = await http.get(appIconUrl); - await appIconFile.writeAsBytes(response.bodyBytes); - } - } final notification = await linuxNotifications!.notify( title, body: body, replacesId: linuxNotificationIds[roomId] ?? 0, appName: AppConfig.applicationName, - appIcon: appIconFile?.path ?? '', + appIcon: 'fluffychat', actions: [ NotificationAction( DesktopNotificationActions.openChat.name, diff --git a/lib/widgets/mxc_image.dart b/lib/widgets/mxc_image.dart index 2126a8cbe..d776cfc82 100644 --- a/lib/widgets/mxc_image.dart +++ b/lib/widgets/mxc_image.dart @@ -2,10 +2,10 @@ import 'dart:typed_data'; import 'package:flutter/material.dart'; -import 'package:http/http.dart' as http; import 'package:matrix/matrix.dart'; import 'package:fluffychat/config/themes.dart'; +import 'package:fluffychat/utils/client_download_content_extension.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart'; import 'package:fluffychat/widgets/matrix.dart'; @@ -63,8 +63,6 @@ class _MxcImageState extends State { : _imageDataCache[cacheKey] = data; } - bool? _isCached; - Future _load() async { final client = widget.client ?? Matrix.of(context).client; final uri = widget.uri; @@ -77,45 +75,18 @@ class _MxcImageState extends State { final height = widget.height; final realHeight = height == null ? null : height * devicePixelRatio; - final httpUri = widget.isThumbnail - ? uri.getThumbnail( - client, - width: realWidth, - height: realHeight, - animated: widget.animated, - method: widget.thumbnailMethod, - ) - : uri.getDownloadLink(client); - - final storeKey = widget.isThumbnail ? httpUri : uri; - - if (_isCached == null) { - final cachedData = await client.database?.getFile(storeKey); - if (cachedData != null) { - if (!mounted) return; - setState(() { - _imageData = cachedData; - _isCached = true; - }); - return; - } - _isCached = false; - } - - final response = await http.get(httpUri); - if (response.statusCode != 200) { - if (response.statusCode == 404) { - return; - } - throw Exception(); - } - final remoteData = response.bodyBytes; - + final remoteData = await client.downloadMxcCached( + uri, + width: realWidth, + height: realHeight, + thumbnailMethod: widget.thumbnailMethod, + isThumbnail: widget.isThumbnail, + animated: widget.animated, + ); if (!mounted) return; setState(() { _imageData = remoteData; }); - await client.database?.storeFile(storeKey, remoteData, 0); } if (event != null) { @@ -179,7 +150,6 @@ class _MxcImageState extends State { filterQuality: widget.isThumbnail ? FilterQuality.low : FilterQuality.medium, errorBuilder: (context, __, ___) { - _isCached = false; _imageData = null; WidgetsBinding.instance.addPostFrameCallback(_tryLoad); return placeholder(context); diff --git a/pubspec.lock b/pubspec.lock index 98aa4393a..2162bf86f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -450,10 +450,10 @@ packages: dependency: "direct main" description: name: flutter_cache_manager - sha256: "395d6b7831f21f3b989ebedbb785545932adb9afe2622c1ffacf7f4b53a7e544" + sha256: "400b6592f16a4409a7f2bb929a9a7e38c72cceb8ffb99ee57bbf2cb2cecf8386" url: "https://pub.dev" source: hosted - version: "3.3.2" + version: "3.4.1" flutter_driver: dependency: transitive description: flutter @@ -892,10 +892,10 @@ packages: dependency: "direct main" description: name: http - sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938" + sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.2.2" http_multi_server: dependency: transitive description: @@ -1201,11 +1201,12 @@ packages: matrix: dependency: "direct main" description: - name: matrix - sha256: "4357245df2a64c435456d1faee55cb33a9fd30aa0df97aacd6abd52b68f70aa1" - url: "https://pub.dev" - source: hosted - version: "0.32.0" + path: "." + ref: HEAD + resolved-ref: "8f350760c4a418a1553030dc4b81408185e0fad5" + url: "https://github.com/famedly/matrix-dart-sdk.git" + source: git + version: "0.32.2" meta: dependency: transitive description: @@ -1338,10 +1339,10 @@ packages: dependency: "direct main" description: name: path_provider - sha256: c9e7d3a4cd1410877472158bee69963a4579f78b68c65a2b7d40d1a7a88bb161 + sha256: fec0d61223fba3154d87759e3cc27fe2c8dc498f6386c6d6fc80d1afdd1bf378 url: "https://pub.dev" source: hosted - version: "2.1.3" + version: "2.1.4" path_provider_android: dependency: transitive description: @@ -2279,10 +2280,10 @@ packages: dependency: transitive description: name: vm_service - sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.2.4" + version: "14.2.5" wakelock_plus: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 853df1744..4ebb8a8dd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,7 +29,7 @@ dependencies: flutter: sdk: flutter flutter_app_badger: ^1.5.0 - flutter_cache_manager: ^3.3.1 + flutter_cache_manager: ^3.4.1 flutter_foreground_task: ^6.1.3 flutter_highlighter: ^0.1.1 flutter_html: ^3.0.0-beta.2 @@ -63,7 +63,8 @@ dependencies: keyboard_shortcuts: ^0.1.4 latlong2: ^0.9.1 linkify: ^5.0.0 - matrix: ^0.32.0 + matrix: # Until 0.32.3 is released + git: https://github.com/famedly/matrix-dart-sdk.git native_imaging: ^0.1.1 opus_caf_converter_dart: ^1.0.1 package_info_plus: ^6.0.0