fix box problem with foreground service

This commit is contained in:
otsmr 2025-02-09 23:06:12 +01:00
parent 80e8682357
commit 9485203383
5 changed files with 21 additions and 21 deletions

View file

@ -216,7 +216,7 @@ Future sendImage(
} }
} }
Future tryDownloadMedia(int messageId, List<int> mediaToken, Future tryDownloadMedia(int messageId, int fromUserId, List<int> mediaToken,
{bool force = false}) async { {bool force = false}) async {
if (globalIsAppInBackground) return; if (globalIsAppInBackground) return;
if (!force) { if (!force) {
@ -237,6 +237,7 @@ Future tryDownloadMedia(int messageId, List<int> mediaToken,
} }
globalCallBackOnDownloadChange(mediaToken, true); globalCallBackOnDownloadChange(mediaToken, true);
box.put("${mediaToken}_messageId", messageId); box.put("${mediaToken}_messageId", messageId);
box.put("${mediaToken}_fromUserId", fromUserId);
apiProvider.triggerDownload(mediaToken, offset); apiProvider.triggerDownload(mediaToken, offset);
} }
@ -255,7 +256,7 @@ Future userOpenedOtherMessage(int fromUserId, int messageOtherId) async {
} }
Future<Uint8List?> getDownloadedMedia( Future<Uint8List?> getDownloadedMedia(
List<int> mediaToken, int messageOtherId) async { List<int> mediaToken, int messageOtherId, int otherUserId) async {
final box = await getMediaStorage(); final box = await getMediaStorage();
Uint8List? media; Uint8List? media;
try { try {
@ -265,13 +266,11 @@ Future<Uint8List?> getDownloadedMedia(
} }
if (media == null) return null; if (media == null) return null;
int fromUserId = box.get("${mediaToken}_fromUserId"); await userOpenedOtherMessage(otherUserId, messageOtherId);
await userOpenedOtherMessage(fromUserId, messageOtherId);
box.delete(mediaToken.toString()); box.delete(mediaToken.toString());
box.put("${mediaToken}_downloaded", "deleted"); box.put("${mediaToken}_downloaded", "deleted");
box.delete("${mediaToken}_fromUserId");
box.delete("${mediaToken}_messageId"); box.delete("${mediaToken}_messageId");
box.delete("${mediaToken}_fromUserId");
return media; return media;
} }

View file

@ -2,7 +2,6 @@ import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:fixnum/fixnum.dart'; import 'package:fixnum/fixnum.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:libsignal_protocol_dart/libsignal_protocol_dart.dart'; import 'package:libsignal_protocol_dart/libsignal_protocol_dart.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:twonly/globals.dart'; import 'package:twonly/globals.dart';
@ -50,6 +49,10 @@ Future handleServerMessage(server.ServerToClient msg) async {
} }
Future<client.Response> handleDownloadData(DownloadData data) async { Future<client.Response> handleDownloadData(DownloadData data) async {
if (globalIsAppInBackground) {
// download should only be done when the app is open
return client.Response()..error = ErrorCode.InternalError;
}
debugPrint("Downloading: ${data.uploadToken} ${data.fin}"); debugPrint("Downloading: ${data.uploadToken} ${data.fin}");
final box = await getMediaStorage(); final box = await getMediaStorage();
@ -61,7 +64,6 @@ Future<client.Response> handleDownloadData(DownloadData data) async {
if (messageId != null) { if (messageId != null) {
int? fromUserId = await DbMessages.deleteMessageById(messageId); int? fromUserId = await DbMessages.deleteMessageById(messageId);
box.delete(boxId); box.delete(boxId);
// int? fromUserId = box.get("${data.uploadToken}_fromUserId");
if (fromUserId != null) { if (fromUserId != null) {
globalCallBackOnMessageChange(fromUserId); globalCallBackOnMessageChange(fromUserId);
} }
@ -181,16 +183,12 @@ Future<client.Response> handleNewMessage(
message.kind == MessageKind.image) { message.kind == MessageKind.image) {
await DbContacts.checkAndUpdateFlames(fromUserId.toInt(), await DbContacts.checkAndUpdateFlames(fromUserId.toInt(),
timestamp: message.timestamp); timestamp: message.timestamp);
if (!globalIsAppInBackground) {
final content = message.content; final content = message.content;
if (content is MediaMessageContent) { if (content is MediaMessageContent) {
List<int> downloadToken = content.downloadToken; List<int> downloadToken = content.downloadToken;
Box box = await getMediaStorage(); tryDownloadMedia(messageId, fromUserId.toInt(), downloadToken);
box.put("${downloadToken}_fromUserId", fromUserId.toInt());
if (box.get("${downloadToken}_fromUserId") == null) {
debugPrint("BOX IS NOT WORKING");
} }
tryDownloadMedia(messageId, downloadToken);
} }
} }
localPushNotificationNewMessage( localPushNotificationNewMessage(

View file

@ -80,7 +80,8 @@ class ChatListEntry extends StatelessWidget {
}), }),
); );
} else { } else {
tryDownloadMedia(message.messageId, token, force: true); tryDownloadMedia(message.messageId, message.otherUserId, token,
force: true);
} }
} }
}, },

View file

@ -208,7 +208,9 @@ class _UserListItem extends State<UserListItem> {
onTap: () { onTap: () {
if (isDownloading) return; if (isDownloading) return;
if (!widget.lastMessage.isDownloaded) { if (!widget.lastMessage.isDownloaded) {
tryDownloadMedia(widget.lastMessage.messageId, token, force: true); tryDownloadMedia(widget.lastMessage.messageId,
widget.lastMessage.otherUserId, token,
force: true);
return; return;
} }
if (state == MessageSendState.received && if (state == MessageSendState.received &&

View file

@ -73,8 +73,8 @@ class _MediaViewerViewState extends State<MediaViewerView> {
flutterLocalNotificationsPlugin.cancel(widget.message.messageId); flutterLocalNotificationsPlugin.cancel(widget.message.messageId);
List<int> token = content.downloadToken; List<int> token = content.downloadToken;
_imageByte = _imageByte = await getDownloadedMedia(
await getDownloadedMedia(token, widget.message.messageOtherId!); token, widget.message.messageOtherId!, widget.message.otherUserId);
if (_imageByte == null) { if (_imageByte == null) {
// image already deleted // image already deleted
if (context.mounted) { if (context.mounted) {