session-android/app/src/main/java/org/thoughtcrime/securesms/database/SessionContactDatabase.kt

116 lines
5.3 KiB
Kotlin
Raw Normal View History

2021-07-09 05:25:57 +02:00
package org.thoughtcrime.securesms.database
2021-05-07 08:31:46 +02:00
import android.content.ContentValues
import android.content.Context
Add a global search (#834) * feat: modifying search functionalities to include contacts * feat: add global search UI input layouts and color attributes * feat: add global search repository and model content * feat: adding diff callbacks and wiring up global search vm to views * feat: adding scroll to message, figuring out new query for recipient thread search * feat: messing with the search and highlighting functionality after wiring up bindings * fix: compile error from merge * fix: gradlew build errors * feat: filtering contacts by existing un-archived threads * refactor: prevent note to self breaking, update queries and logic in search repo to include member->group reverse searches * feat: adding home screen new redesigns for search * feat: replacing designs and adding new group subtitle text * feat: small design improvements and incrementing gradle build number to install on device * feat: add scrollbars for search * feat: replace isVisible for cancel button now that GlobalSearchInputLayout.kt replaces header * refactor: all queries are debounced not just all but 2 char * refactor: remove visibility modifiers for cancel icon * refactor: use simplified non-db and context related models in display, remove db get group members call from binding data * fix: use threadId instead of group's address * refactor: better close on cancel, removing only yourself from group member list in open groups * refactor: seed view back to inflated on create and visibility for empty placeholder and seed view text * refactor: fixing build issues and new designs for message list * refactor: use dynamic limit * refactor: include raw session ID string search for non-empty threads * fix: build lint errors * fix: build issues * feat: add in path to the settings activity * refactor: remove wildcard imports
2022-02-07 07:06:27 +01:00
import androidx.core.database.getStringOrNull
2021-05-07 08:31:46 +02:00
import net.sqlcipher.Cursor
import org.session.libsession.messaging.contacts.Contact
import org.session.libsignal.utilities.Base64
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
2021-05-21 07:36:16 +02:00
2021-05-07 08:31:46 +02:00
companion object {
private const val sessionContactTable = "session_contact_database"
const val sessionID = "session_id"
const val name = "name"
const val nickname = "nickname"
const val profilePictureURL = "profile_picture_url"
const val profilePictureFileName = "profile_picture_file_name"
const val profilePictureEncryptionKey = "profile_picture_encryption_key"
const val threadID = "thread_id"
const val isTrusted = "is_trusted"
@JvmStatic val createSessionContactTableCommand =
"CREATE TABLE $sessionContactTable " +
2021-05-21 07:36:16 +02:00
"($sessionID STRING PRIMARY KEY, " +
"$name TEXT DEFAULT NULL, " +
"$nickname TEXT DEFAULT NULL, " +
"$profilePictureURL TEXT DEFAULT NULL, " +
"$profilePictureFileName TEXT DEFAULT NULL, " +
"$profilePictureEncryptionKey BLOB DEFAULT NULL, " +
"$threadID INTEGER DEFAULT -1, " +
"$isTrusted INTEGER DEFAULT 0);"
2021-05-07 08:31:46 +02:00
}
fun getContactWithSessionID(sessionID: String): Contact? {
val database = databaseHelper.readableDatabase
2021-07-09 05:25:57 +02:00
return database.get(sessionContactTable, "${Companion.sessionID} = ?", arrayOf( sessionID )) { cursor ->
2021-05-07 08:31:46 +02:00
contactFromCursor(cursor)
}
}
fun getAllContacts(): Set<Contact> {
val database = databaseHelper.readableDatabase
return database.getAll(sessionContactTable, null, null) { cursor ->
contactFromCursor(cursor)
}.toSet()
}
fun setContactIsTrusted(contact: Contact, isTrusted: Boolean, threadID: Long) {
val database = databaseHelper.writableDatabase
val contentValues = ContentValues(1)
contentValues.put(Companion.isTrusted, if (isTrusted) 1 else 0)
database.update(sessionContactTable, contentValues, "$sessionID = ?", arrayOf( contact.sessionID ))
if (threadID >= 0) {
notifyConversationListeners(threadID)
}
notifyConversationListListeners()
}
2021-05-07 08:31:46 +02:00
fun setContact(contact: Contact) {
val database = databaseHelper.writableDatabase
val contentValues = ContentValues(8)
contentValues.put(sessionID, contact.sessionID)
contentValues.put(name, contact.name)
contentValues.put(nickname, contact.nickname)
contentValues.put(profilePictureURL, contact.profilePictureURL)
contentValues.put(profilePictureFileName, contact.profilePictureFileName)
2021-05-11 09:05:04 +02:00
contact.profilePictureEncryptionKey?.let {
contentValues.put(profilePictureEncryptionKey, Base64.encodeBytes(it))
}
contentValues.put(threadID, contact.threadID)
2021-05-07 08:31:46 +02:00
contentValues.put(isTrusted, if (contact.isTrusted) 1 else 0)
2021-05-21 07:36:16 +02:00
database.insertOrUpdate(sessionContactTable, contentValues, "$sessionID = ?", arrayOf( contact.sessionID ))
2021-05-13 06:25:09 +02:00
notifyConversationListListeners()
2021-05-07 08:31:46 +02:00
}
Add a global search (#834) * feat: modifying search functionalities to include contacts * feat: add global search UI input layouts and color attributes * feat: add global search repository and model content * feat: adding diff callbacks and wiring up global search vm to views * feat: adding scroll to message, figuring out new query for recipient thread search * feat: messing with the search and highlighting functionality after wiring up bindings * fix: compile error from merge * fix: gradlew build errors * feat: filtering contacts by existing un-archived threads * refactor: prevent note to self breaking, update queries and logic in search repo to include member->group reverse searches * feat: adding home screen new redesigns for search * feat: replacing designs and adding new group subtitle text * feat: small design improvements and incrementing gradle build number to install on device * feat: add scrollbars for search * feat: replace isVisible for cancel button now that GlobalSearchInputLayout.kt replaces header * refactor: all queries are debounced not just all but 2 char * refactor: remove visibility modifiers for cancel icon * refactor: use simplified non-db and context related models in display, remove db get group members call from binding data * fix: use threadId instead of group's address * refactor: better close on cancel, removing only yourself from group member list in open groups * refactor: seed view back to inflated on create and visibility for empty placeholder and seed view text * refactor: fixing build issues and new designs for message list * refactor: use dynamic limit * refactor: include raw session ID string search for non-empty threads * fix: build lint errors * fix: build issues * feat: add in path to the settings activity * refactor: remove wildcard imports
2022-02-07 07:06:27 +01:00
fun contactFromCursor(cursor: Cursor): Contact {
2021-05-07 08:31:46 +02:00
val sessionID = cursor.getString(sessionID)
val contact = Contact(sessionID)
2021-05-11 09:05:04 +02:00
contact.name = cursor.getStringOrNull(name)
contact.nickname = cursor.getStringOrNull(nickname)
contact.profilePictureURL = cursor.getStringOrNull(profilePictureURL)
contact.profilePictureFileName = cursor.getStringOrNull(profilePictureFileName)
cursor.getStringOrNull(profilePictureEncryptionKey)?.let {
contact.profilePictureEncryptionKey = Base64.decode(it)
}
2021-05-13 02:24:32 +02:00
contact.threadID = cursor.getLong(threadID)
2021-05-07 08:31:46 +02:00
contact.isTrusted = cursor.getInt(isTrusted) != 0
return contact
}
Add a global search (#834) * feat: modifying search functionalities to include contacts * feat: add global search UI input layouts and color attributes * feat: add global search repository and model content * feat: adding diff callbacks and wiring up global search vm to views * feat: adding scroll to message, figuring out new query for recipient thread search * feat: messing with the search and highlighting functionality after wiring up bindings * fix: compile error from merge * fix: gradlew build errors * feat: filtering contacts by existing un-archived threads * refactor: prevent note to self breaking, update queries and logic in search repo to include member->group reverse searches * feat: adding home screen new redesigns for search * feat: replacing designs and adding new group subtitle text * feat: small design improvements and incrementing gradle build number to install on device * feat: add scrollbars for search * feat: replace isVisible for cancel button now that GlobalSearchInputLayout.kt replaces header * refactor: all queries are debounced not just all but 2 char * refactor: remove visibility modifiers for cancel icon * refactor: use simplified non-db and context related models in display, remove db get group members call from binding data * fix: use threadId instead of group's address * refactor: better close on cancel, removing only yourself from group member list in open groups * refactor: seed view back to inflated on create and visibility for empty placeholder and seed view text * refactor: fixing build issues and new designs for message list * refactor: use dynamic limit * refactor: include raw session ID string search for non-empty threads * fix: build lint errors * fix: build issues * feat: add in path to the settings activity * refactor: remove wildcard imports
2022-02-07 07:06:27 +01:00
fun contactFromCursor(cursor: android.database.Cursor): Contact {
val sessionID = cursor.getString(cursor.getColumnIndexOrThrow(sessionID))
val contact = Contact(sessionID)
contact.name = cursor.getStringOrNull(cursor.getColumnIndexOrThrow(name))
contact.nickname = cursor.getStringOrNull(cursor.getColumnIndexOrThrow(nickname))
contact.profilePictureURL = cursor.getStringOrNull(cursor.getColumnIndexOrThrow(profilePictureURL))
contact.profilePictureFileName = cursor.getStringOrNull(cursor.getColumnIndexOrThrow(profilePictureFileName))
cursor.getStringOrNull(cursor.getColumnIndexOrThrow(profilePictureEncryptionKey))?.let {
contact.profilePictureEncryptionKey = Base64.decode(it)
}
contact.threadID = cursor.getLong(cursor.getColumnIndexOrThrow(threadID))
contact.isTrusted = cursor.getInt(cursor.getColumnIndexOrThrow(isTrusted)) != 0
return contact
}
fun queryContactsByName(constraint: String): Cursor {
return databaseHelper.readableDatabase.query(
sessionContactTable, null, " $name LIKE ? OR $nickname LIKE ?", arrayOf(
"%$constraint%",
"%$constraint%"
),
null, null, null
)
}
2021-05-07 08:31:46 +02:00
}