delete >24h messages & remove messageKind where possible

This commit is contained in:
otsmr 2025-02-08 17:07:48 +01:00
parent ac91e954f7
commit 7291f1656c
4 changed files with 104 additions and 87 deletions

View file

@ -4,10 +4,11 @@ Don't be lonely, get twonly! Send pictures to a friend in real time and be sure
## TODOS bevor first beta ## TODOS bevor first beta
- MessageKind -> Ausbauen?
- Nachrichten nach 24h Stunden löschen
- Pro Invitation codes - Pro Invitation codes
- Push Notification (Android) - Push Notification (Android)
- Contact view page
- Verify contact view
- Context Menu
- Settings - Settings
- Notification - Notification
- Real deployment aufsetzen, direkt auf Netcup? - Real deployment aufsetzen, direkt auf Netcup?

View file

@ -134,7 +134,7 @@ class MediaMessageContent extends MessageContent {
} }
class TextMessageContent extends MessageContent { class TextMessageContent extends MessageContent {
final String text; String text;
TextMessageContent({required this.text}); TextMessageContent({required this.text});
static TextMessageContent fromJson(Map json) { static TextMessageContent fromJson(Map json) {

View file

@ -13,7 +13,6 @@ class DbMessage {
required this.messageId, required this.messageId,
required this.messageOtherId, required this.messageOtherId,
required this.otherUserId, required this.otherUserId,
required this.messageKind,
required this.messageContent, required this.messageContent,
required this.messageOpenedAt, required this.messageOpenedAt,
required this.messageAcknowledgeByUser, required this.messageAcknowledgeByUser,
@ -26,7 +25,6 @@ class DbMessage {
// is this null then the message was sent from the user itself // is this null then the message was sent from the user itself
int? messageOtherId; int? messageOtherId;
int otherUserId; int otherUserId;
MessageKind messageKind;
MessageContent messageContent; MessageContent messageContent;
DateTime? messageOpenedAt; DateTime? messageOpenedAt;
bool messageAcknowledgeByUser; bool messageAcknowledgeByUser;
@ -42,7 +40,8 @@ class DbMessage {
bool get messageReceived => messageOtherId != null; bool get messageReceived => messageOtherId != null;
bool isMedia() { bool isMedia() {
return messageKind == MessageKind.image || messageKind == MessageKind.video; return messageContent is TextMessageContent ||
messageContent is MediaMessageContent;
} }
MessageSendState getSendState() { MessageSendState getSendState() {
@ -276,17 +275,19 @@ class DbMessages extends CvModelBase {
return messages[0]; return messages[0];
} }
static Future _updateByMessageId( static Future _updateByMessageId(int messageId, Map<String, dynamic> data,
int messageId, Map<String, dynamic> data) async { {bool notifyFlutterState = true}) async {
await dbProvider.db!.update( await dbProvider.db!.update(
tableName, tableName,
data, data,
where: "$columnMessageId = ?", where: "$columnMessageId = ?",
whereArgs: [messageId], whereArgs: [messageId],
); );
int? fromUserId = await getFromUserIdByMessageId(messageId); if (notifyFlutterState) {
if (fromUserId != null) { int? fromUserId = await getFromUserIdByMessageId(messageId);
globalCallBackOnMessageChange(fromUserId); if (fromUserId != null) {
globalCallBackOnMessageChange(fromUserId);
}
} }
} }
@ -354,25 +355,39 @@ class DbMessages extends CvModelBase {
List<CvField> get fields => List<CvField> get fields =>
[messageId, messageKind, messageContentJson, messageOpenedAt, sendAt]; [messageId, messageKind, messageContentJson, messageOpenedAt, sendAt];
// TODO: The message meta is needed to maintain the flame. Delete if not.
// This function should calculate if this message is needed for the flame calculation and delete the message complete and not only
// the message content.
static Future deleteTextContent(
int messageId, TextMessageContent oldMessage) async {
oldMessage.text = "";
Map<String, dynamic> data = {
columnMessageContentJson: jsonEncode(oldMessage.toJson()),
};
await _updateByMessageId(messageId, data, notifyFlutterState: false);
}
static Future<List<DbMessage>> convertToDbMessage( static Future<List<DbMessage>> convertToDbMessage(
List<dynamic> fromDb) async { List<dynamic> fromDb) async {
try { try {
List<DbMessage> parsedUsers = []; List<DbMessage> parsedUsers = [];
for (int i = 0; i < fromDb.length; i++) { for (int i = 0; i < fromDb.length; i++) {
dynamic messageOpenedAt = fromDb[i][columnMessageOpenedAt]; dynamic messageOpenedAt = fromDb[i][columnMessageOpenedAt];
if (messageOpenedAt != null) {
messageOpenedAt = DateTime.tryParse(fromDb[i][columnMessageOpenedAt]);
// if (messageOpenedAt != null) {
// if (messageOpenedAt.difference()) {
// }
// }
}
int? messageOtherId = fromDb[i][columnMessageOtherId];
MessageContent content = MessageContent.fromJson( MessageContent content = MessageContent.fromJson(
jsonDecode(fromDb[i][columnMessageContentJson])); jsonDecode(fromDb[i][columnMessageContentJson]));
MessageKind messageKind =
MessageKindExtension.fromIndex(fromDb[i][columnMessageKind]); var tmp = content;
if (tmp is TextMessageContent && messageOpenedAt != null) {
messageOpenedAt = DateTime.tryParse(fromDb[i][columnMessageOpenedAt]);
if (messageOpenedAt != null) {
if ((DateTime.now()).difference(messageOpenedAt).inHours >= 24) {
deleteTextContent(fromDb[i][columnMessageId], tmp);
}
}
}
int? messageOtherId = fromDb[i][columnMessageOtherId];
bool isDownloaded = true; bool isDownloaded = true;
if (messageOtherId != null) { if (messageOtherId != null) {
if (content is MediaMessageContent) { if (content is MediaMessageContent) {
@ -386,7 +401,6 @@ class DbMessages extends CvModelBase {
messageId: fromDb[i][columnMessageId], messageId: fromDb[i][columnMessageId],
messageOtherId: messageOtherId, messageOtherId: messageOtherId,
otherUserId: fromDb[i][columnOtherUserId], otherUserId: fromDb[i][columnOtherUserId],
messageKind: messageKind,
messageContent: content, messageContent: content,
isDownloaded: isDownloaded, isDownloaded: isDownloaded,
messageOpenedAt: messageOpenedAt, messageOpenedAt: messageOpenedAt,

View file

@ -38,75 +38,69 @@ class ChatListEntry extends StatelessWidget {
Widget child = Container(); Widget child = Container();
switch (message.messageKind) { if (content is TextMessageContent) {
case MessageKind.textMessage: child = Container(
if (content is TextMessageContent) { constraints: BoxConstraints(
child = Container( maxWidth: MediaQuery.of(context).size.width * 0.8,
constraints: BoxConstraints( ),
maxWidth: MediaQuery.of(context).size.width * 0.8, padding: EdgeInsets.symmetric(
), vertical: 4, horizontal: 10), // Add some padding around the text
padding: EdgeInsets.symmetric( decoration: BoxDecoration(
vertical: 4, color: right
horizontal: 10), // Add some padding around the text ? const Color.fromARGB(107, 124, 77, 255)
decoration: BoxDecoration( : const Color.fromARGB(
color: right 83, 68, 137, 255), // Set the background color
? const Color.fromARGB(107, 124, 77, 255) borderRadius: BorderRadius.circular(12.0), // Set border radius
: const Color.fromARGB( ),
83, 68, 137, 255), // Set the background color child: Text(
borderRadius: BorderRadius.circular(12.0), // Set border radius content.text,
), style: TextStyle(
child: Text( color: Colors.white, // Set text color for contrast
content.text, fontSize: 17,
style: TextStyle( ),
color: Colors.white, // Set text color for contrast textAlign: TextAlign.left, // Center the text
fontSize: 17, ),
), );
textAlign: TextAlign.left, // Center the text } else if (content is MediaMessageContent && !content.isVideo) {
), Color color = message.messageContent
); .getColor(Theme.of(context).colorScheme.primary);
} child = GestureDetector(
break; onTap: () {
case MessageKind.image: if (state == MessageSendState.received && !isDownloading) {
Color color = message.messageContent if (message.isDownloaded) {
.getColor(Theme.of(context).colorScheme.primary); Navigator.push(
child = GestureDetector( context,
onTap: () { MaterialPageRoute(builder: (context) {
if (state == MessageSendState.received && !isDownloading) { return MediaViewerView(user, message);
if (message.isDownloaded) { }),
Navigator.push( );
context, } else {
MaterialPageRoute(builder: (context) { tryDownloadMedia(token, force: true);
return MediaViewerView(user, message);
}),
);
} else {
tryDownloadMedia(token, force: true);
}
} }
}, }
child: Container( },
padding: EdgeInsets.all(10), child: Container(
width: 150, padding: EdgeInsets.all(10),
decoration: BoxDecoration( width: 150,
border: Border.all( decoration: BoxDecoration(
color: color, // Set the background color border: Border.all(
width: 1.0, // Set the border width here color: color, // Set the background color
), width: 1.0, // Set the border width here
borderRadius: BorderRadius.circular(12.0), // Set border radius
), ),
child: Align( borderRadius: BorderRadius.circular(12.0), // Set border radius
alignment: Alignment.centerRight, ),
child: MessageSendStateIcon( child: Align(
message, alignment: Alignment.centerRight,
mainAxisAlignment: child: MessageSendStateIcon(
right ? MainAxisAlignment.center : MainAxisAlignment.center, message,
), mainAxisAlignment:
right ? MainAxisAlignment.center : MainAxisAlignment.center,
), ),
), ),
); ),
default: );
return Container();
} }
return Align( return Align(
alignment: right ? Alignment.centerRight : Alignment.centerLeft, alignment: right ? Alignment.centerRight : Alignment.centerLeft,
child: Padding( child: Padding(
@ -164,7 +158,7 @@ class _ChatItemDetailsViewState extends State<ChatItemDetailsView> {
if (updateOpenStatus) { if (updateOpenStatus) {
_messages.where((x) => x.messageOpenedAt == null).forEach((message) { _messages.where((x) => x.messageOpenedAt == null).forEach((message) {
if (message.messageOtherId != null && if (message.messageOtherId != null &&
message.messageKind == MessageKind.textMessage) { message.messageContent is TextMessageContent) {
if (!alreadyReportedOpened.contains(message.messageOtherId!)) { if (!alreadyReportedOpened.contains(message.messageOtherId!)) {
userOpenedOtherMessage( userOpenedOtherMessage(
message.otherUserId, message.messageOtherId!); message.otherUserId, message.messageOtherId!);
@ -210,6 +204,14 @@ class _ChatItemDetailsViewState extends State<ChatItemDetailsView> {
(_messages[i - 1].messageOtherId != null && (_messages[i - 1].messageOtherId != null &&
_messages[i].messageOtherId != null); _messages[i].messageOtherId != null);
} }
if (_messages[i].messageOpenedAt != null) {
if ((DateTime.now())
.difference(_messages[i].messageOpenedAt!)
.inHours >=
24) {
return Container();
}
}
return ChatListEntry( return ChatListEntry(
_messages[i], _messages[i],
widget.user, widget.user,