diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt index 8175952..4cedd26 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt @@ -16,6 +16,7 @@ package de.fabianonline.telegram_backup +import com.github.badoualy.telegram.api.Kotlogram import com.github.badoualy.telegram.tl.api.* import com.github.badoualy.telegram.tl.core.TLVector import com.github.badoualy.telegram.api.TelegramClient @@ -266,13 +267,10 @@ class Database constructor(val file_base: String, val user_manager: UserManager) @Synchronized fun saveMessages(all: TLVector, api_layer: Int, source_type: MessageSource = MessageSource.NORMAL, settings: Settings?) { - //"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " + - //"VALUES " + - //"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - val columns = "(message_id, message_type, source_type, source_id, sender_id, fwd_from_id, text, time, has_media, media_type, media_file, media_size, data, api_layer) " + + val columns = "(message_id, message_type, source_type, source_id, sender_id, fwd_from_id, text, time, has_media, media_type, media_file, media_size, data, api_layer, json) " + "VALUES " + - "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - //1 2 3 4 5 6 7 8 9 10 11 12 13 14 + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + //1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 val ps = conn.prepareStatement("INSERT OR REPLACE INTO messages " + columns) val ps_insert_or_ignore = conn.prepareStatement("INSERT OR IGNORE INTO messages " + columns) @@ -343,6 +341,7 @@ class Database constructor(val file_base: String, val user_manager: UserManager) msg.serializeBody(stream) ps.setBytes(13, stream.toByteArray()) ps.setInt(14, api_layer) + ps.setString(15, msg.toJson()) ps.addBatch() } else if (msg is TLMessageService) { ps_insert_or_ignore.setInt(1, msg.getId()) @@ -381,6 +380,7 @@ class Database constructor(val file_base: String, val user_manager: UserManager) ps_insert_or_ignore.setNull(12, Types.INTEGER) ps_insert_or_ignore.setNull(13, Types.BLOB) ps_insert_or_ignore.setInt(14, api_layer) + ps_insert_or_ignore.setString(15, msg.toJson()) ps_insert_or_ignore.addBatch() } else if (msg is TLMessageEmpty) { ps_insert_or_ignore.setInt(1, msg.getId()) @@ -397,6 +397,7 @@ class Database constructor(val file_base: String, val user_manager: UserManager) ps_insert_or_ignore.setNull(12, Types.INTEGER) ps_insert_or_ignore.setNull(13, Types.BLOB) ps_insert_or_ignore.setInt(14, api_layer) + ps_insert_or_ignore.setNull(15, Types.VARCHAR) ps_insert_or_ignore.addBatch() } else { throw RuntimeException("Unexpected Message type: " + msg.javaClass) @@ -418,18 +419,23 @@ class Database constructor(val file_base: String, val user_manager: UserManager) fun saveChats(all: TLVector) { val ps_insert_or_replace = conn.prepareStatement( "INSERT OR REPLACE INTO chats " + - "(id, name, type) " + + "(id, name, type, json, api_layer) " + "VALUES " + - "(?, ?, ?)") + "(?, ?, ?, ?, ?)") val ps_insert_or_ignore = conn.prepareStatement( "INSERT OR IGNORE INTO chats " + - "(id, name, type) " + + "(id, name, type, json, api_layer) " + "VALUES " + - "(?, ?, ?)") + "(?, ?, ?, ?, ?)") for (abs in all) { ps_insert_or_replace.setInt(1, abs.getId()) ps_insert_or_ignore.setInt(1, abs.getId()) + val json = abs.toJson() + ps_insert_or_replace.setString(4, json) + ps_insert_or_ignore.setString(4, json) + ps_insert_or_replace.setInt(5, Kotlogram.API_LAYER) + ps_insert_or_ignore.setInt(5, Kotlogram.API_LAYER) if (abs is TLChatEmpty) { ps_insert_or_ignore.setNull(2, Types.VARCHAR) ps_insert_or_ignore.setString(3, "empty_chat") @@ -470,14 +476,14 @@ class Database constructor(val file_base: String, val user_manager: UserManager) fun saveUsers(all: TLVector) { val ps_insert_or_replace = conn.prepareStatement( "INSERT OR REPLACE INTO users " + - "(id, first_name, last_name, username, type, phone) " + + "(id, first_name, last_name, username, type, phone, json, api_layer) " + "VALUES " + - "(?, ?, ?, ?, ?, ?)") + "(?, ?, ?, ?, ?, ?, ?, ?)") val ps_insert_or_ignore = conn.prepareStatement( "INSERT OR IGNORE INTO users " + - "(id, first_name, last_name, username, type, phone) " + + "(id, first_name, last_name, username, type, phone, json, api_layer) " + "VALUES " + - "(?, ?, ?, ?, ?, ?)") + "(?, ?, ?, ?, ?, ?, ?, ?)") for (abs in all) { if (abs is TLUser) { val user = abs @@ -487,6 +493,8 @@ class Database constructor(val file_base: String, val user_manager: UserManager) ps_insert_or_replace.setString(4, user.getUsername()) ps_insert_or_replace.setString(5, "user") ps_insert_or_replace.setString(6, user.getPhone()) + ps_insert_or_replace.setString(7, user.toJson()) + ps_insert_or_replace.setInt(8, Kotlogram.API_LAYER) ps_insert_or_replace.addBatch() } else if (abs is TLUserEmpty) { ps_insert_or_ignore.setInt(1, abs.getId()) @@ -495,6 +503,8 @@ class Database constructor(val file_base: String, val user_manager: UserManager) ps_insert_or_ignore.setNull(4, Types.VARCHAR) ps_insert_or_ignore.setString(5, "empty_user") ps_insert_or_ignore.setNull(6, Types.VARCHAR) + ps_insert_or_ignore.setNull(7, Types.VARCHAR) + ps_insert_or_ignore.setInt(8, Kotlogram.API_LAYER) ps_insert_or_ignore.addBatch() } else { throw RuntimeException("Unexpected " + abs.javaClass) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt index 5111c01..e25b95f 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt @@ -461,6 +461,8 @@ internal class DB_Update_11(conn: Connection, db: Database) : DatabaseUpdate(con override fun _doUpdate() { execute("ALTER TABLE messages ADD COLUMN json TEXT NULL") + execute("ALTER TABLE chats ADD COLUMN json TEXT NULL, api_layer INTEGER NULL") + execute("ALTER TABLE users ADD COLUMN json TEXT NULL, api_layer INTEGER NULL") val limit = 5000 var offset = 0 var i = 0 diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Utils.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Utils.kt index 9a2dd17..4dbb574 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Utils.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Utils.kt @@ -17,7 +17,7 @@ package de.fabianonline.telegram_backup import com.github.badoualy.telegram.tl.exception.RpcErrorException -import com.github.badoualy.telegram.tl.api.TLMessage +import com.github.badoualy.telegram.tl.api.TLAbsMessage import java.io.File import java.util.Vector import java.util.concurrent.TimeUnit @@ -226,7 +226,7 @@ fun Any.toPrettyJson(): String = GsonBuilder().setPrettyPrinting().create().toJs class MaxTriesExceededException(): RuntimeException("Max tries exceeded") {} -fun TLMessage.toJson(): String { +fun TLAbsMessage.toJson(): String { val json = Gson().toJsonTree(this) cleanUpMessageJson(json) return json.toString()