Update for core changes

This commit is contained in:
Niels Andriesse 2019-08-28 12:09:05 +10:00
parent fb8104cd18
commit f6c1a3ef9c
5 changed files with 43 additions and 33 deletions

View File

@ -78,6 +78,7 @@ import org.webrtc.voiceengine.WebRtcAudioUtils;
import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider; import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos; import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import org.whispersystems.signalservice.loki.api.LokiGroupChat;
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI; import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI;
import org.whispersystems.signalservice.loki.api.LokiLongPoller; import org.whispersystems.signalservice.loki.api.LokiLongPoller;
import org.whispersystems.signalservice.loki.api.LokiP2PAPI; import org.whispersystems.signalservice.loki.api.LokiP2PAPI;
@ -425,13 +426,17 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
if (lokiLongPoller != null) { lokiLongPoller.startIfNeeded(); } if (lokiLongPoller != null) { lokiLongPoller.startIfNeeded(); }
} }
private LokiGroupChat lokiPublicChat() {
return new LokiGroupChat(LokiGroupChatAPI.getPublicChatServerID(), LokiGroupChatAPI.getPublicChatServer(), "Loki Public Chat", true);
}
private void setUpPublicChatIfNeeded() { private void setUpPublicChatIfNeeded() {
if (lokiPublicChatPoller != null) return; if (lokiPublicChatPoller != null) return;
lokiPublicChatPoller = new LokiGroupChatPoller(this, LokiGroupChatAPI.getPublicChatID()); LokiGroupChat lokiPublicChat = this.lokiPublicChat();
lokiPublicChatPoller = new LokiGroupChatPoller(this, lokiPublicChat);
boolean isPublicChatSetUp = TextSecurePreferences.isPublicChatSetUp(this); boolean isPublicChatSetUp = TextSecurePreferences.isPublicChatSetUp(this);
if (isPublicChatSetUp) return; if (isPublicChatSetUp) return;
String id = LokiGroupChatAPI.getServerURL() + "." + LokiGroupChatAPI.getPublicChatID(); GroupManager.createGroup(lokiPublicChat.getId(), this, new HashSet<>(), null, "Loki Public Chat", false);
GroupManager.createGroup(id, this, new HashSet<>(), null, "Loki Public Chat", false);
TextSecurePreferences.markPublicChatSetUp(this); TextSecurePreferences.markPublicChatSetUp(this);
} }

View File

@ -74,7 +74,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
private static final int STICKERS = 21; private static final int STICKERS = 21;
private static final int lokiV1 = 22; private static final int lokiV1 = 22;
private static final int DATABASE_VERSION = 22; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes private static final int DATABASE_VERSION = lokiV1; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes
private static final String DATABASE_NAME = "signal.db"; private static final String DATABASE_NAME = "signal.db";
private final Context context; private final Context context;

View File

@ -282,7 +282,7 @@ public class PushGroupSendJob extends PushSendJob implements InjectableType {
private @NonNull List<Address> getGroupMessageRecipients(String groupId, long messageId) { private @NonNull List<Address> getGroupMessageRecipients(String groupId, long messageId) {
ArrayList<Address> result = new ArrayList<>(); ArrayList<Address> result = new ArrayList<>();
result.add(Address.fromSerialized(LokiGroupChatAPI.getServerURL())); result.add(Address.fromSerialized(LokiGroupChatAPI.getPublicChatServer()));
return result; return result;
/* /*

View File

@ -30,19 +30,19 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
@JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);" @JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);"
// Group chat auth token cache // Group chat auth token cache
private val groupChatAuthTokenTable = "loki_api_group_chat_auth_token_database" private val groupChatAuthTokenTable = "loki_api_group_chat_auth_token_database"
private val serverURL = "server_url" private val server = "server"
private val token = "token" private val token = "token"
@JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($serverURL TEXT PRIMARY KEY, $token TEXT);" @JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($server TEXT PRIMARY KEY, $token TEXT);"
// Last message server ID cache // Last message server ID cache
private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache" private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache"
private val lastMessageServerIDCacheGroupID = "group_id" private val lastMessageServerIDCacheIndex = "loki_api_last_message_server_id_cache_index"
private val lastMessageServerID = "last_message_server_id" private val lastMessageServerID = "last_message_server_id"
@JvmStatic val createLastMessageServerIDTableCommand = "CREATE TABLE $lastMessageServerIDCache ($lastMessageServerIDCacheGroupID INTEGER PRIMARY KEY, $lastMessageServerID INTEGER DEFAULT 0);" @JvmStatic val createLastMessageServerIDTableCommand = "CREATE TABLE $lastMessageServerIDCache ($lastMessageServerIDCacheIndex STRING PRIMARY KEY, $lastMessageServerID INTEGER DEFAULT 0);"
// First message server ID cache // First message server ID cache
private val firstMessageServerIDCache = "loki_api_first_message_server_id_cache" private val firstMessageServerIDCache = "loki_api_first_message_server_id_cache"
private val firstMessageServerIDCacheGroupID = "group_id" private val firstMessageServerIDCacheIndex = "loki_api_first_message_server_id_cache_index"
private val firstMessageServerID = "first_message_server_id" private val firstMessageServerID = "first_message_server_id"
@JvmStatic val createFirstMessageServerIDTableCommand = "CREATE TABLE $firstMessageServerIDCache ($firstMessageServerIDCacheGroupID INTEGER PRIMARY KEY, $firstMessageServerID INTEGER DEFAULT 0);" @JvmStatic val createFirstMessageServerIDTableCommand = "CREATE TABLE $firstMessageServerIDCache ($firstMessageServerIDCacheIndex STRING PRIMARY KEY, $firstMessageServerID INTEGER DEFAULT 0);"
} }
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? { override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
@ -93,47 +93,51 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
database.insertOrUpdate(receivedMessageHashValuesCache, row, "$userID = ?", wrap(userPublicKey)) database.insertOrUpdate(receivedMessageHashValuesCache, row, "$userID = ?", wrap(userPublicKey))
} }
override fun getGroupChatAuthToken(serverURL: String): String? { override fun getGroupChatAuthToken(server: String): String? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL)) { cursor -> return database.get(groupChatAuthTokenTable, "${Companion.server} = ?", wrap(server)) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(token)) cursor.getString(cursor.getColumnIndexOrThrow(token))
} }
} }
override fun setGroupChatAuthToken(serverURL: String, newValue: String?) { override fun setGroupChatAuthToken(server: String, newValue: String?) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
if (newValue != null) { if (newValue != null) {
val row = wrap(mapOf(Companion.serverURL to serverURL, token to newValue!!)) val row = wrap(mapOf(Companion.server to server, token to newValue))
database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.serverURL} = ?", wrap(serverURL)) database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.server} = ?", wrap(server))
} else { } else {
database.delete(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL)) database.delete(groupChatAuthTokenTable, "${Companion.server} = ?", wrap(server))
} }
} }
override fun getLastMessageServerID(groupID: Long): Long? { override fun getLastMessageServerID(group: Long, server: String): Long? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(lastMessageServerIDCache, "$lastMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) { cursor -> val index = "$server.$group"
return database.get(lastMessageServerIDCache, "$lastMessageServerIDCacheIndex = ?", wrap(index)) { cursor ->
cursor.getInt(lastMessageServerID) cursor.getInt(lastMessageServerID)
}?.toLong() }?.toLong()
} }
override fun setLastMessageServerID(groupID: Long, newValue: Long) { override fun setLastMessageServerID(group: Long, server: String, newValue: Long) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val row = wrap(mapOf( lastMessageServerIDCacheGroupID to groupID.toString(), lastMessageServerID to newValue.toString() )) val index = "$server.$group"
database.insertOrUpdate(lastMessageServerIDCache, row, "$lastMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) val row = wrap(mapOf( lastMessageServerIDCacheIndex to index, lastMessageServerID to newValue.toString() ))
database.insertOrUpdate(lastMessageServerIDCache, row, "$lastMessageServerIDCacheIndex = ?", wrap(index))
} }
override fun getFirstMessageServerID(groupID: Long): Long? { override fun getFirstMessageServerID(group: Long, server: String): Long? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(firstMessageServerIDCache, "$firstMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) { cursor -> val index = "$server.$group"
return database.get(firstMessageServerIDCache, "$firstMessageServerIDCacheIndex = ?", wrap(index)) { cursor ->
cursor.getInt(firstMessageServerID) cursor.getInt(firstMessageServerID)
}?.toLong() }?.toLong()
} }
override fun setFirstMessageServerID(groupID: Long, newValue: Long) { override fun setFirstMessageServerID(group: Long, server: String, newValue: Long) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val row = wrap(mapOf( firstMessageServerIDCacheGroupID to groupID.toString(), firstMessageServerID to newValue.toString() )) val index = "$server.$group"
database.insertOrUpdate(firstMessageServerIDCache, row, "$firstMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) val row = wrap(mapOf( firstMessageServerIDCacheIndex to index, firstMessageServerID to newValue.toString() ))
database.insertOrUpdate(firstMessageServerIDCache, row, "$firstMessageServerIDCacheIndex = ?", wrap(index))
} }
} }

View File

@ -12,9 +12,10 @@ import org.whispersystems.signalservice.api.messages.SignalServiceContent
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
import org.whispersystems.signalservice.api.messages.SignalServiceGroup import org.whispersystems.signalservice.api.messages.SignalServiceGroup
import org.whispersystems.signalservice.api.push.SignalServiceAddress import org.whispersystems.signalservice.api.push.SignalServiceAddress
import org.whispersystems.signalservice.loki.api.LokiGroupChat
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI
class LokiGroupChatPoller(private val context: Context, private val groupID: Long) { class LokiGroupChatPoller(private val context: Context, private val group: LokiGroupChat) {
private val handler = Handler() private val handler = Handler()
private var hasStarted = false private var hasStarted = false
@ -62,9 +63,9 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
} }
private fun pollForNewMessages() { private fun pollForNewMessages() {
api.getMessages(groupID).success { messages -> api.getMessages(group.serverID, group.server).success { messages ->
messages.reversed().map { message -> messages.reversed().map { message ->
val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray() val id = group.id.toByteArray()
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
val x2 = SignalServiceDataMessage(message.timestamp, x1, null, message.body) val x2 = SignalServiceDataMessage(message.timestamp, x1, null, message.body)
val senderDisplayName = "${message.displayName} (...${message.hexEncodedPublicKey.takeLast(8)})" val senderDisplayName = "${message.displayName} (...${message.hexEncodedPublicKey.takeLast(8)})"
@ -72,12 +73,12 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent()) PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent())
} }
}.fail { }.fail {
Log.d("Loki", "Failed to get messages for group chat with ID: $groupID.") Log.d("Loki", "Failed to get messages for group chat with ID: ${group.serverID} on server: ${group.server}.")
} }
} }
private fun pollForDeletedMessages() { private fun pollForDeletedMessages() {
api.getDeletedMessageServerIDs(groupID).success { deletedMessageServerIDs -> api.getDeletedMessageServerIDs(group.serverID, group.server).success { deletedMessageServerIDs ->
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context) val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) } val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) }
val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context) val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context)
@ -87,7 +88,7 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
mmsMessageDatabase.delete(it) mmsMessageDatabase.delete(it)
} }
}.fail { }.fail {
Log.d("Loki", "Failed to get deleted messages for group chat with ID: $groupID.") Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${group.serverID} on server: ${group.server}.")
} }
} }
} }