feat(service): Be smarter about creating pseudo messages

This commit is contained in:
PapaTutuWawa 2023-06-20 16:42:15 +02:00
parent 2740692772
commit 059a22cbe8
5 changed files with 63 additions and 49 deletions

View File

@ -4,8 +4,12 @@ import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:moxxmpp/moxxmpp.dart' as moxxmpp;
import 'package:moxxyv2/service/message.dart';
import 'package:moxxyv2/service/omemo/implementations.dart';
import 'package:moxxyv2/service/omemo/persistence.dart';
import 'package:moxxyv2/service/service.dart';
import 'package:moxxyv2/shared/events.dart';
import 'package:moxxyv2/shared/models/message.dart';
import 'package:moxxyv2/shared/models/omemo_device.dart' as model;
import 'package:omemo_dart/omemo_dart.dart';
import 'package:synchronized/synchronized.dart';
@ -242,4 +246,29 @@ class OmemoService {
newDevice.id,
);
}
/// Adds a pseudo-message of type [type] to the chat with [conversationJid].
/// Also sends an event to the UI.
Future<void> addPseudoMessage(
String conversationJid,
PseudoMessageType type,
) async {
final ms = GetIt.I.get<MessageService>();
final message = await ms.addMessageFromData(
'',
DateTime.now().millisecondsSinceEpoch,
'',
conversationJid,
'',
false,
false,
false,
pseudoMessageType: type,
);
sendEvent(
MessageAddedEvent(
message: message,
),
);
}
}

View File

@ -4,11 +4,6 @@ import 'package:get_it/get_it.dart';
import 'package:moxxyv2/service/database/constants.dart';
import 'package:moxxyv2/service/database/database.dart';
import 'package:moxxyv2/service/database/helpers.dart';
import 'package:moxxyv2/service/message.dart';
import 'package:moxxyv2/service/service.dart';
import 'package:moxxyv2/service/xmpp_state.dart';
import 'package:moxxyv2/shared/events.dart';
import 'package:moxxyv2/shared/models/message.dart';
import 'package:omemo_dart/omemo_dart.dart';
import 'package:sqflite_common/sql.dart';
@ -110,27 +105,6 @@ Future<OmemoDevice?> loadOmemoDevice(String jid) async {
);
}
Future<void> _addPseudoMessage(
String conversationJid, PseudoMessageType type,) async {
final ms = GetIt.I.get<MessageService>();
final message = await ms.addMessageFromData(
'',
DateTime.now().millisecondsSinceEpoch,
'',
conversationJid,
'',
false,
false,
false,
pseudoMessageType: type,
);
sendEvent(
MessageAddedEvent(
message: message,
),
);
}
Future<void> commitRatchets(List<OmemoRatchetData> ratchets) async {
final db = GetIt.I.get<DatabaseService>().database;
final batch = db.batch();
@ -178,22 +152,6 @@ Future<void> commitRatchets(List<OmemoRatchetData> ratchets) async {
}
await batch.commit();
// Check if we have to create pseudo messages
final ourJid = (await GetIt.I.get<XmppStateService>().getXmppState()).jid;
final replaced = ratchets.any(
(ratchet) => ratchet.added && ratchet.replaced && ratchet.jid != ourJid,);
final added = ratchets.any(
(ratchet) => ratchet.added && !ratchet.replaced && ratchet.jid != ourJid,);
if (replaced) {
// TODO: Conversation Jid
await _addPseudoMessage('', PseudoMessageType.changedDevice);
}
if (added) {
// TODO: Conversation Jid
await _addPseudoMessage('', PseudoMessageType.newDevice);
}
}
Future<void> commitDeviceList(String jid, List<int> devices) async {

View File

@ -1311,6 +1311,27 @@ class XmppService {
);
}
// Check if we have to create pseudo-messages related to OMEMO
final omemoData = event.get<OmemoData>();
if (omemoData != null) {
// Notify of new ratchets
final om = GetIt.I.get<OmemoService>();
if (omemoData.newRatchets.isNotEmpty) {
await om.addPseudoMessage(
conversationJid,
PseudoMessageType.newDevice,
);
}
// Notify of changed ratchets
if (omemoData.replacedRatchets.isNotEmpty) {
await om.addPseudoMessage(
conversationJid,
PseudoMessageType.changedDevice,
);
}
}
// Create the message in the database
final ms = GetIt.I.get<MessageService>();
var message = await ms.addMessageFromData(

View File

@ -954,7 +954,7 @@ packages:
description:
path: "packages/moxxmpp"
ref: HEAD
resolved-ref: d35b9552590b9c7642110d9b9083a424c1c376ec
resolved-ref: "05e3d804a4036e9cd93fd27473a1e970fda3c3fc"
url: "https://codeberg.org/moxxy/moxxmpp.git"
source: git
version: "0.4.0"
@ -1009,11 +1009,12 @@ packages:
omemo_dart:
dependency: "direct main"
description:
name: omemo_dart
sha256: d2ada3a9ad9d587182e5d9d68ac536417fe681ef981bfbd947a7b93d2d6cfb2a
url: "https://git.polynom.me/api/packages/PapaTutuWawa/pub/"
source: hosted
version: "0.5.0"
path: "."
ref: HEAD
resolved-ref: "49c7e114e6cf80dcde55fbbd218bba3182045862"
url: "https://github.com/PapaTutuWawa/omemo_dart.git"
source: git
version: "0.5.1"
package_config:
dependency: transitive
description:

View File

@ -139,9 +139,14 @@ dependency_overrides:
moxxmpp:
git:
url: https://codeberg.org/moxxy/moxxmpp.git
rev: d35b9552590b9c7642110d9b9083a424c1c376ec
rev: 05e3d804a4036e9cd93fd27473a1e970fda3c3fc
path: packages/moxxmpp
omemo_dart:
git:
url: https://github.com/PapaTutuWawa/omemo_dart.git
rev: 49c7e114e6cf80dcde55fbbd218bba3182045862
extra_licenses:
- name: undraw.co
license: "custom license"