From e80e8dc76111503ca6cbc4e74ccebd212bb2c6a1 Mon Sep 17 00:00:00 2001 From: Anton Chekulaev Date: Tue, 10 Nov 2020 14:19:05 +1100 Subject: [PATCH] Attachment keys removed from the backup. Open group avatar downloading temporary disabled. Backup importer cleanup. --- .../securesms/backup/FullBackupImporter.kt | 43 +++++++++++++++---- .../securesms/loki/api/PublicChatManager.kt | 7 +-- .../securesms/util/TextSecurePreferences.java | 2 - 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.kt b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.kt index dfc6a0529b..5ead3349c2 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.kt +++ b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.kt @@ -57,7 +57,14 @@ object FullBackupImporter { var frame: BackupFrame while (!inputStream.readFrame().also { frame = it }.end) { if (count++ % 100 == 0) EventBus.getDefault().post(BackupEvent.createProgress(count)) - if (frame.hasVersion()) processVersion(db, frame.version) else if (frame.hasStatement()) processStatement(db, frame.statement) else if (frame.hasPreference()) processPreference(context, frame.preference) else if (frame.hasAttachment()) processAttachment(context, attachmentSecret, db, frame.attachment, inputStream) else if (frame.hasSticker()) processSticker(context, attachmentSecret, db, frame.sticker, inputStream) else if (frame.hasAvatar()) processAvatar(context, frame.avatar, inputStream) + when { + frame.hasVersion() -> processVersion(db, frame.version) + frame.hasStatement() -> processStatement(db, frame.statement) + frame.hasPreference() -> processPreference(context, frame.preference) + frame.hasAttachment() -> processAttachment(context, attachmentSecret, db, frame.attachment, inputStream) + frame.hasSticker() -> processSticker(context, attachmentSecret, db, frame.sticker, inputStream) + frame.hasAvatar() -> processAvatar(context, frame.avatar, inputStream) + } } trimEntriesForExpiredMessages(context, db) db.setTransactionSuccessful() @@ -88,13 +95,25 @@ object FullBackupImporter { } val parameters: MutableList = LinkedList() for (parameter in statement.parametersList) { - if (parameter.hasStringParamter()) parameters.add(parameter.stringParamter) else if (parameter.hasDoubleParameter()) parameters.add(parameter.doubleParameter) else if (parameter.hasIntegerParameter()) parameters.add(parameter.integerParameter) else if (parameter.hasBlobParameter()) parameters.add(parameter.blobParameter.toByteArray()) else if (parameter.hasNullparameter()) parameters.add(null) + when { + parameter.hasStringParamter() -> parameters.add(parameter.stringParamter) + parameter.hasDoubleParameter() -> parameters.add(parameter.doubleParameter) + parameter.hasIntegerParameter() -> parameters.add(parameter.integerParameter) + parameter.hasBlobParameter() -> parameters.add(parameter.blobParameter.toByteArray()) + parameter.hasNullparameter() -> parameters.add(null) + } + } + if (parameters.size > 0) { + db.execSQL(statement.statement, parameters.toTypedArray()) + } else { + db.execSQL(statement.statement) } - if (parameters.size > 0) db.execSQL(statement.statement, parameters.toTypedArray()) else db.execSQL(statement.statement) } @Throws(IOException::class) - private fun processAttachment(context: Context, attachmentSecret: AttachmentSecret, db: SQLiteDatabase, attachment: Attachment, inputStream: BackupRecordInputStream) { + private fun processAttachment(context: Context, attachmentSecret: AttachmentSecret, + db: SQLiteDatabase, attachment: Attachment, + inputStream: BackupRecordInputStream) { val partsDirectory = context.getDir(AttachmentDatabase.DIRECTORY, Context.MODE_PRIVATE) val dataFile = File.createTempFile("part", ".mms", partsDirectory) val output = ModernEncryptingPartOutputStream.createFor(attachmentSecret, dataFile, false) @@ -104,11 +123,14 @@ object FullBackupImporter { contentValues.put(AttachmentDatabase.THUMBNAIL, null as String?) contentValues.put(AttachmentDatabase.DATA_RANDOM, output.first) db.update(AttachmentDatabase.TABLE_NAME, contentValues, - AttachmentDatabase.ROW_ID + " = ? AND " + AttachmentDatabase.UNIQUE_ID + " = ?", arrayOf(attachment.rowId.toString(), attachment.attachmentId.toString())) + "${AttachmentDatabase.ROW_ID} = ? AND ${AttachmentDatabase.UNIQUE_ID} = ?", + arrayOf(attachment.rowId.toString(), attachment.attachmentId.toString())) } @Throws(IOException::class) - private fun processSticker(context: Context, attachmentSecret: AttachmentSecret, db: SQLiteDatabase, sticker: Sticker, inputStream: BackupRecordInputStream) { + private fun processSticker(context: Context, attachmentSecret: AttachmentSecret, + db: SQLiteDatabase, sticker: Sticker, + inputStream: BackupRecordInputStream) { val stickerDirectory = context.getDir(AttachmentDatabase.DIRECTORY, Context.MODE_PRIVATE) val dataFile = File.createTempFile("sticker", ".mms", stickerDirectory) val output = ModernEncryptingPartOutputStream.createFor(attachmentSecret, dataFile, false) @@ -122,7 +144,8 @@ object FullBackupImporter { @Throws(IOException::class) private fun processAvatar(context: Context, avatar: Avatar, inputStream: BackupRecordInputStream) { - inputStream.readAttachmentTo(FileOutputStream(AvatarHelper.getAvatarFile(context, Address.fromExternal(context, avatar.name))), avatar.length) + inputStream.readAttachmentTo(FileOutputStream( + AvatarHelper.getAvatarFile(context, Address.fromExternal(context, avatar.name))), avatar.length) } @SuppressLint("ApplySharedPref") @@ -167,10 +190,12 @@ object FullBackupImporter { val where = AttachmentDatabase.MMS_ID + trimmedCondition db.query(AttachmentDatabase.TABLE_NAME, columns, where, null, null, null, null).use { cursor -> while (cursor != null && cursor.moveToNext()) { - DatabaseFactory.getAttachmentDatabase(context).deleteAttachment(AttachmentId(cursor.getLong(0), cursor.getLong(1))) + DatabaseFactory.getAttachmentDatabase(context) + .deleteAttachment(AttachmentId(cursor.getLong(0), cursor.getLong(1))) } } - db.query(ThreadDatabase.TABLE_NAME, arrayOf(ThreadDatabase.ID), ThreadDatabase.EXPIRES_IN + " > 0", null, null, null, null).use { cursor -> + db.query(ThreadDatabase.TABLE_NAME, arrayOf(ThreadDatabase.ID), + ThreadDatabase.EXPIRES_IN + " > 0", null, null, null, null).use { cursor -> while (cursor != null && cursor.moveToNext()) { DatabaseFactory.getThreadDatabase(context).update(cursor.getLong(0), false) } diff --git a/src/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt b/src/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt index 6b710c5ea4..ed81260d24 100644 --- a/src/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt +++ b/src/org/thoughtcrime/securesms/loki/api/PublicChatManager.kt @@ -75,9 +75,10 @@ class PublicChatManager(private val context: Context) { // Create the group if we don't have one if (threadID < 0) { if (info.profilePictureURL.isNotEmpty()) { - val profilePictureAsByteArray = ApplicationContext.getInstance(context).publicChatAPI - ?.downloadOpenGroupProfilePicture(server, info.profilePictureURL) - profilePicture = BitmapUtil.fromByteArray(profilePictureAsByteArray) + //TODO Use DownloadUtilities to pull the avatar from the server. +// val profilePictureAsByteArray = ApplicationContext.getInstance(context).publicChatAPI +// ?.downloadOpenGroupProfilePicture(server, info.profilePictureURL) +// profilePicture = BitmapUtil.fromByteArray(profilePictureAsByteArray) } val result = GroupManager.createOpenGroup(chat.id, context, profilePicture, chat.displayName) threadID = result.threadId diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index f70989532a..a70fb315a8 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -1356,8 +1356,6 @@ public class TextSecurePreferences { addBackupEntryInt (prefList, preferences, prefsFileName, LOCAL_REGISTRATION_ID_PREF); addBackupEntryString(prefList, preferences, prefsFileName, LOCAL_NUMBER_PREF); addBackupEntryString(prefList, preferences, prefsFileName, PROFILE_NAME_PREF); - addBackupEntryString(prefList, preferences, prefsFileName, ATTACHMENT_ENCRYPTED_SECRET); - addBackupEntryString(prefList, preferences, prefsFileName, ATTACHMENT_UNENCRYPTED_SECRET); addBackupEntryString(prefList, preferences, prefsFileName, PROFILE_AVATAR_URL_PREF); addBackupEntryInt (prefList, preferences, prefsFileName, PROFILE_AVATAR_ID_PREF); addBackupEntryString(prefList, preferences, prefsFileName, PROFILE_KEY_PREF);