feat: Use matrix authenticated media
This commit is contained in:
parent
f17b09f56c
commit
158a6855c3
7 changed files with 134 additions and 115 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<MxcImage> {
|
|||
: _imageDataCache[cacheKey] = data;
|
||||
}
|
||||
|
||||
bool? _isCached;
|
||||
|
||||
Future<void> _load() async {
|
||||
final client = widget.client ?? Matrix.of(context).client;
|
||||
final uri = widget.uri;
|
||||
|
|
@ -77,45 +75,18 @@ class _MxcImageState extends State<MxcImage> {
|
|||
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<MxcImage> {
|
|||
filterQuality:
|
||||
widget.isThumbnail ? FilterQuality.low : FilterQuality.medium,
|
||||
errorBuilder: (context, __, ___) {
|
||||
_isCached = false;
|
||||
_imageData = null;
|
||||
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
||||
return placeholder(context);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue