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
- MessageKind -> Ausbauen?
- Nachrichten nach 24h Stunden löschen
- Pro Invitation codes
- Push Notification (Android)
- Contact view page
- Verify contact view
- Context Menu
- Settings
- Notification
- Real deployment aufsetzen, direkt auf Netcup?

View file

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

View file

@ -13,7 +13,6 @@ class DbMessage {
required this.messageId,
required this.messageOtherId,
required this.otherUserId,
required this.messageKind,
required this.messageContent,
required this.messageOpenedAt,
required this.messageAcknowledgeByUser,
@ -26,7 +25,6 @@ class DbMessage {
// is this null then the message was sent from the user itself
int? messageOtherId;
int otherUserId;
MessageKind messageKind;
MessageContent messageContent;
DateTime? messageOpenedAt;
bool messageAcknowledgeByUser;
@ -42,7 +40,8 @@ class DbMessage {
bool get messageReceived => messageOtherId != null;
bool isMedia() {
return messageKind == MessageKind.image || messageKind == MessageKind.video;
return messageContent is TextMessageContent ||
messageContent is MediaMessageContent;
}
MessageSendState getSendState() {
@ -276,17 +275,19 @@ class DbMessages extends CvModelBase {
return messages[0];
}
static Future _updateByMessageId(
int messageId, Map<String, dynamic> data) async {
static Future _updateByMessageId(int messageId, Map<String, dynamic> data,
{bool notifyFlutterState = true}) async {
await dbProvider.db!.update(
tableName,
data,
where: "$columnMessageId = ?",
whereArgs: [messageId],
);
int? fromUserId = await getFromUserIdByMessageId(messageId);
if (fromUserId != null) {
globalCallBackOnMessageChange(fromUserId);
if (notifyFlutterState) {
int? fromUserId = await getFromUserIdByMessageId(messageId);
if (fromUserId != null) {
globalCallBackOnMessageChange(fromUserId);
}
}
}
@ -354,25 +355,39 @@ class DbMessages extends CvModelBase {
List<CvField> get fields =>
[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(
List<dynamic> fromDb) async {
try {
List<DbMessage> parsedUsers = [];
for (int i = 0; i < fromDb.length; i++) {
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(
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;
if (messageOtherId != null) {
if (content is MediaMessageContent) {
@ -386,7 +401,6 @@ class DbMessages extends CvModelBase {
messageId: fromDb[i][columnMessageId],
messageOtherId: messageOtherId,
otherUserId: fromDb[i][columnOtherUserId],
messageKind: messageKind,
messageContent: content,
isDownloaded: isDownloaded,
messageOpenedAt: messageOpenedAt,

View file

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