flame support

This commit is contained in:
otsmr 2025-02-01 00:31:42 +01:00
parent 2117ca3fc1
commit 2dfab0e76f
5 changed files with 86 additions and 15 deletions

View file

@ -9,11 +9,15 @@ class Contact {
{required this.userId, {required this.userId,
required this.displayName, required this.displayName,
required this.accepted, required this.accepted,
required this.flameCounter,
required this.lastUpdateOfFlameCounter,
required this.requested}); required this.requested});
final Int64 userId; final Int64 userId;
final String displayName; final String displayName;
final bool accepted; final bool accepted;
final bool requested; final bool requested;
final int flameCounter;
final DateTime lastUpdateOfFlameCounter;
} }
class DbContacts extends CvModelBase { class DbContacts extends CvModelBase {
@ -34,6 +38,13 @@ class DbContacts extends CvModelBase {
static const columnBlocked = "blocked"; static const columnBlocked = "blocked";
final blocked = CvField<int>(columnBlocked); final blocked = CvField<int>(columnBlocked);
static const columnFlameCounter = "flame_counter";
final flameCounter = CvField<int>(columnFlameCounter);
static const columnLastUpdateOfFlameCounter = "last_update_flame_counter";
final lastUpdateOfFlameCounter =
CvField<DateTime>(columnLastUpdateOfFlameCounter);
static const columnCreatedAt = "created_at"; static const columnCreatedAt = "created_at";
final createdAt = CvField<DateTime>(columnCreatedAt); final createdAt = CvField<DateTime>(columnCreatedAt);
@ -45,6 +56,8 @@ class DbContacts extends CvModelBase {
$columnAccepted INT NOT NULL DEFAULT 0, $columnAccepted INT NOT NULL DEFAULT 0,
$columnRequested INT NOT NULL DEFAULT 0, $columnRequested INT NOT NULL DEFAULT 0,
$columnBlocked INT NOT NULL DEFAULT 0, $columnBlocked INT NOT NULL DEFAULT 0,
$columnFlameCounter INT NOT NULL DEFAULT 0,
$columnLastUpdateOfFlameCounter DATETIME DEFAULT CURRENT_TIMESTAMP,
$columnCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP $columnCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP
) )
"""; """;
@ -58,6 +71,42 @@ class DbContacts extends CvModelBase {
return (await getUsers()).where((u) => u.accepted).toList(); return (await getUsers()).where((u) => u.accepted).toList();
} }
static Future checkAndUpdateFlames(int userId) async {
List<Map<String, dynamic>> result = await dbProvider.db!.query(
tableName,
columns: [columnLastUpdateOfFlameCounter, columnFlameCounter],
where: '$columnUserId = ?',
whereArgs: [userId],
);
if (result.isNotEmpty) {
String lastUpdateString = result.first[columnLastUpdateOfFlameCounter];
DateTime? lastUpdate = DateTime.tryParse(lastUpdateString);
int currentCount = result.first.cast()[columnFlameCounter];
if (lastUpdate != null &&
lastUpdate.isAfter(DateTime.now().subtract(Duration(hours: 24)))) {
_updateFlameCounter(userId, currentCount); // just update the time
} else {
_updateFlameCounter(userId, (currentCount + 1));
}
}
globalCallBackOnContactChange();
}
static Future _updateFlameCounter(int userId, int newCount) async {
Map<String, dynamic> valuesToUpdate = {
columnFlameCounter: newCount,
columnLastUpdateOfFlameCounter: DateTime.now().toIso8601String()
};
await dbProvider.db!.update(
tableName,
valuesToUpdate,
where: "$columnUserId = ?",
whereArgs: [userId],
);
}
static Future<List<Contact>> getUsers() async { static Future<List<Contact>> getUsers() async {
try { try {
var users = await dbProvider.db!.query(tableName, var users = await dbProvider.db!.query(tableName,
@ -66,6 +115,8 @@ class DbContacts extends CvModelBase {
columnDisplayName, columnDisplayName,
columnAccepted, columnAccepted,
columnRequested, columnRequested,
columnFlameCounter,
columnLastUpdateOfFlameCounter,
columnCreatedAt columnCreatedAt
], ],
where: "$columnBlocked = 0"); where: "$columnBlocked = 0");
@ -73,11 +124,25 @@ class DbContacts extends CvModelBase {
List<Contact> parsedUsers = []; List<Contact> parsedUsers = [];
for (int i = 0; i < users.length; i++) { for (int i = 0; i < users.length; i++) {
DateTime lastUpdate =
DateTime.tryParse(users.cast()[i][columnLastUpdateOfFlameCounter])!;
int userId = users.cast()[i][columnUserId];
int flameCounter = users.cast()[i][columnFlameCounter];
if (lastUpdate.isBefore(DateTime.now().subtract(Duration(days: 2)))) {
_updateFlameCounter(userId, 0);
flameCounter = 0;
}
parsedUsers.add( parsedUsers.add(
Contact( Contact(
userId: Int64(users.cast()[i][columnUserId]), userId: Int64(userId),
displayName: users.cast()[i][columnDisplayName], displayName: users.cast()[i][columnDisplayName],
accepted: users[i][columnAccepted] == 1, accepted: users[i][columnAccepted] == 1,
flameCounter: flameCounter,
lastUpdateOfFlameCounter: lastUpdate,
requested: users[i][columnRequested] == 1, requested: users[i][columnRequested] == 1,
), ),
); );

View file

@ -197,15 +197,6 @@ class DbMessages extends CvModelBase {
return messages; return messages;
} }
static Future<List<DbMessage>> getAllMessagesForRetransmitting() async {
var rows = await dbProvider.db!.query(
tableName,
where: "$columnMessageAcknowledgeByServer = 0",
);
List<DbMessage> messages = await convertToDbMessage(rows);
return messages;
}
static Future<List<DbMessage>> getAllMessagesForUser(int otherUserId) async { static Future<List<DbMessage>> getAllMessagesForUser(int otherUserId) async {
var rows = await dbProvider.db!.query( var rows = await dbProvider.db!.query(
tableName, tableName,
@ -219,6 +210,15 @@ class DbMessages extends CvModelBase {
return messages; return messages;
} }
static Future<List<DbMessage>> getAllMessagesForRetransmitting() async {
var rows = await dbProvider.db!.query(
tableName,
where: "$columnMessageAcknowledgeByServer = 0",
);
List<DbMessage> messages = await convertToDbMessage(rows);
return messages;
}
static Future<DbMessage?> getLastMessagesForPreviewForUser( static Future<DbMessage?> getLastMessagesForPreviewForUser(
int otherUserId) async { int otherUserId) async {
var rows = await dbProvider.db!.query( var rows = await dbProvider.db!.query(

View file

@ -8,6 +8,7 @@ import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:twonly/main.dart'; import 'package:twonly/main.dart';
import 'package:twonly/src/app.dart'; import 'package:twonly/src/app.dart';
import 'package:twonly/src/model/contacts_model.dart';
import 'package:twonly/src/model/json/message.dart'; import 'package:twonly/src/model/json/message.dart';
import 'package:twonly/src/model/messages_model.dart'; import 'package:twonly/src/model/messages_model.dart';
import 'package:twonly/src/proto/api/error.pb.dart'; import 'package:twonly/src/proto/api/error.pb.dart';
@ -125,6 +126,7 @@ Future uploadMediaFile(
box.delete("retransmit-$messageId-media"); box.delete("retransmit-$messageId-media");
box.delete("retransmit-$messageId-uploadtoken"); box.delete("retransmit-$messageId-uploadtoken");
await DbContacts.checkAndUpdateFlames(target.toInt());
// Ensures the retransmit of the message // Ensures the retransmit of the message
await encryptAndSendMessage( await encryptAndSendMessage(

View file

@ -148,6 +148,8 @@ Future<client.Response> handleNewMessage(
if (message.kind == MessageKind.video || if (message.kind == MessageKind.video ||
message.kind == MessageKind.image) { message.kind == MessageKind.image) {
await DbContacts.checkAndUpdateFlames(fromUserId.toInt());
dynamic content = message.content!; dynamic content = message.content!;
List<int> downloadToken = content.downloadToken; List<int> downloadToken = content.downloadToken;

View file

@ -1,3 +1,4 @@
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:twonly/src/components/initialsavatar.dart'; import 'package:twonly/src/components/initialsavatar.dart';
import 'package:twonly/src/components/message_send_state_icon.dart'; import 'package:twonly/src/components/message_send_state_icon.dart';
@ -174,20 +175,21 @@ class _UserListItem extends State<UserListItem> {
formatDuration(lastMessageInSeconds), formatDuration(lastMessageInSeconds),
style: TextStyle(fontSize: 12), style: TextStyle(fontSize: 12),
), ),
if (flames > 0) if (widget.user.flameCounter > 0)
Row( Row(
children: [ children: [
const SizedBox(width: 5), const SizedBox(width: 5),
Text(""), Text(""),
const SizedBox(width: 5), const SizedBox(width: 5),
Text( Text(
flames.toString(), widget.user.flameCounter.toString(),
style: TextStyle(fontSize: 12), style: TextStyle(fontSize: 12),
), ),
Icon( const SizedBox(width: 1),
Icons.local_fire_department_sharp, FaIcon(
FontAwesomeIcons.fireFlameCurved,
color: const Color.fromARGB(255, 215, 73, 58), color: const Color.fromARGB(255, 215, 73, 58),
size: 16, size: 10,
), ),
], ],
), ),