feat(commands): еще немного команд

This commit is contained in:
Jganenokk
2026-06-22 16:37:56 +07:00
parent 4f11203064
commit 31dc6b619c
11 changed files with 712 additions and 47 deletions
+25 -27
View File
@@ -190,8 +190,8 @@ class AppDatabase {
onCreate: (db, _) => _createTables(db),
onUpgrade: (db, oldVersion, newVersion) async {
if (oldVersion < 2) {
await db.execute(
'ALTER TABLE profile ADD COLUMN is_active INTEGER NOT NULL DEFAULT 0',
await _addColumnIfMissing(
db, 'profile', 'is_active', 'INTEGER NOT NULL DEFAULT 0',
);
await db.execute('DROP TABLE IF EXISTS sync_state');
await db.execute(_syncStateSchema);
@@ -210,47 +210,33 @@ class AppDatabase {
await db.execute(_messagesSchema);
}
if (oldVersion < 7) {
await db.execute(
'ALTER TABLE profile ADD COLUMN profile_options TEXT',
);
await _addColumnIfMissing(db, 'profile', 'profile_options', 'TEXT');
}
if (oldVersion < 8) {
await db.execute(
'ALTER TABLE chats_cache ADD COLUMN participants TEXT',
);
await _addColumnIfMissing(db, 'chats_cache', 'participants', 'TEXT');
}
if (oldVersion < 9) {
await db.execute(
'ALTER TABLE contacts ADD COLUMN options TEXT',
);
await db.execute(
'ALTER TABLE chats_cache ADD COLUMN options TEXT',
);
await _addColumnIfMissing(db, 'contacts', 'options', 'TEXT');
await _addColumnIfMissing(db, 'chats_cache', 'options', 'TEXT');
}
if (oldVersion < 10) {
await db.execute(
'ALTER TABLE chats_cache ADD COLUMN owner INTEGER',
);
await db.execute(
'ALTER TABLE chats_cache ADD COLUMN admins TEXT',
);
await _addColumnIfMissing(db, 'chats_cache', 'owner', 'INTEGER');
await _addColumnIfMissing(db, 'chats_cache', 'admins', 'TEXT');
}
if (oldVersion < 11) {
await _createIndexes(db);
}
if (oldVersion < 12) {
await db.execute(
'ALTER TABLE chats_cache ADD COLUMN last_msg_status TEXT',
);
await _addColumnIfMissing(db, 'chats_cache', 'last_msg_status', 'TEXT');
}
if (oldVersion < 13) {
await db.execute(
'ALTER TABLE messages ADD COLUMN deleted INTEGER NOT NULL DEFAULT 0',
await _addColumnIfMissing(
db, 'messages', 'deleted', 'INTEGER NOT NULL DEFAULT 0',
);
}
if (oldVersion < 14) {
await db.execute(
'ALTER TABLE chats_cache ADD COLUMN in_list INTEGER NOT NULL DEFAULT 1',
await _addColumnIfMissing(
db, 'chats_cache', 'in_list', 'INTEGER NOT NULL DEFAULT 1',
);
}
},
@@ -281,6 +267,18 @@ class AppDatabase {
await _createIndexes(db);
}
static Future<void> _addColumnIfMissing(
Database db,
String table,
String column,
String definition,
) async {
final info = await db.rawQuery('PRAGMA table_info($table)');
final exists = info.any((row) => row['name'] == column);
if (exists) return;
await db.execute('ALTER TABLE $table ADD COLUMN $column $definition');
}
static Future<void> _createIndexes(Database db) async {
await db.execute(
'CREATE INDEX IF NOT EXISTS idx_messages_chat ON messages(account_id, chat_id, time DESC)',
+26 -1
View File
@@ -17,10 +17,22 @@ class PacketDispatcher {
final Map<int, PacketHandler> _pushHandlers = {};
final _pushController = StreamController<Packet>.broadcast();
final _errorController = StreamController<String>.broadcast();
/// Стрим всех входящих пушей (cmd == 1)
Stream<Packet> get pushStream => _pushController.stream;
Stream<String> get errorStream => _errorController.stream;
static String? _serverErrorText(dynamic payload) {
if (payload is! Map) return null;
for (final key in ['localizedMessage', 'title']) {
final v = payload[key];
if (v is String && v.trim().isNotEmpty) return v.trim();
}
return null;
}
Timer? _cleanupTimer;
PacketDispatcher() {
@@ -60,10 +72,22 @@ class PacketDispatcher {
if (packet.cmd == CmdType.ok ||
packet.cmd == CmdType.error ||
packet.cmd == CmdType.notFound) {
final payloadLog = packet.opcode == Opcode.login
? '<скрыто: ответ login>'
: payloadForLog(packet.payload);
logger.i(
'<= {ver: ${packet.api}, cmd: ${packet.cmd}, seq: ${packet.seq}, opcode: ${packet.opcode}, payload: ${payloadForLog(packet.payload)}}',
'<= {ver: ${packet.api}, cmd: ${packet.cmd}, seq: ${packet.seq}, opcode: ${packet.opcode}, payload: $payloadLog}',
);
if (packet.isError) {
final isSessionExpired = packet.payload is Map &&
packet.payload['message'] == 'FAIL_LOGIN_TOKEN';
final serverText = _serverErrorText(packet.payload);
if (serverText != null && !isSessionExpired) {
_errorController.add(serverText);
}
}
final completer = _pendingRequests.remove(packet.seq);
_requestTimestamps.remove(packet.seq);
@@ -130,5 +154,6 @@ class PacketDispatcher {
_cleanupTimer?.cancel();
clearPending();
_pushController.close();
_errorController.close();
}
}