parent
ef2ceff0c1
commit
55523b3baf
@ -0,0 +1,83 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil
|
||||||
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
|
import org.thoughtcrime.securesms.dependencies.InjectableType
|
||||||
|
import org.thoughtcrime.securesms.groups.GroupManager
|
||||||
|
import org.thoughtcrime.securesms.jobmanager.Data
|
||||||
|
import org.thoughtcrime.securesms.jobmanager.Job
|
||||||
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
|
||||||
|
import org.thoughtcrime.securesms.jobs.BaseJob
|
||||||
|
import org.thoughtcrime.securesms.logging.Log
|
||||||
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||||
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender
|
||||||
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage
|
||||||
|
import org.whispersystems.signalservice.loki.api.LokiPublicChat
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class MultiDeviceOpenGroupUpdateJob private constructor(parameters: Parameters) : BaseJob(parameters), InjectableType {
|
||||||
|
companion object {
|
||||||
|
const val KEY = "MultiDeviceGroupUpdateJob"
|
||||||
|
private val TAG = MultiDeviceOpenGroupUpdateJob::class.java.simpleName
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var messageSender: SignalServiceMessageSender
|
||||||
|
|
||||||
|
constructor() : this(Parameters.Builder()
|
||||||
|
.addConstraint(NetworkConstraint.KEY)
|
||||||
|
.setQueue("MultiDeviceGroupUpdateJob")
|
||||||
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
||||||
|
.setMaxAttempts(Parameters.UNLIMITED)
|
||||||
|
.build())
|
||||||
|
|
||||||
|
override fun getFactoryKey(): String {
|
||||||
|
return KEY
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(): Data {
|
||||||
|
return Data.EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
|
public override fun onRun() {
|
||||||
|
if (!TextSecurePreferences.isMultiDevice(context)) {
|
||||||
|
Log.i(TAG, "Not multi device, aborting...")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val openGroups = mutableListOf<LokiPublicChat>()
|
||||||
|
DatabaseFactory.getGroupDatabase(context).groups.use { reader ->
|
||||||
|
while (true) {
|
||||||
|
val record = reader.next ?: return@use
|
||||||
|
if (!record.isPublicChat) { continue; }
|
||||||
|
|
||||||
|
val threadID = GroupManager.getThreadIdFromGroupId(record.encodedId, context)
|
||||||
|
val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
|
||||||
|
if (openGroup != null) {
|
||||||
|
openGroups.add(openGroup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (openGroups.size > 0) {
|
||||||
|
messageSender.sendMessage(0, SignalServiceSyncMessage.forOpenGroups(openGroups),
|
||||||
|
UnidentifiedAccessUtil.getAccessForSync(context))
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "No open groups to sync.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override fun onShouldRetry(exception: Exception): Boolean {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCanceled() { }
|
||||||
|
|
||||||
|
class Factory : Job.Factory<MultiDeviceOpenGroupUpdateJob> {
|
||||||
|
override fun create(parameters: Parameters, data: Data): MultiDeviceOpenGroupUpdateJob {
|
||||||
|
return MultiDeviceOpenGroupUpdateJob(parameters)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import nl.komponents.kovenant.Promise
|
||||||
|
import nl.komponents.kovenant.then
|
||||||
|
import org.thoughtcrime.securesms.ApplicationContext
|
||||||
|
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil
|
||||||
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
|
import org.thoughtcrime.securesms.groups.GroupManager
|
||||||
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||||
|
import org.whispersystems.signalservice.loki.api.LokiPublicChat
|
||||||
|
|
||||||
|
object OpenGroupUtilities {
|
||||||
|
@JvmStatic fun addGroup(context: Context, url: String, channel: Long): Promise<LokiPublicChat, Exception> {
|
||||||
|
// Check if we have an existing group
|
||||||
|
val groupId = LokiPublicChat.getId(channel, url)
|
||||||
|
val threadID = GroupManager.getPublicChatThreadId(groupId, context)
|
||||||
|
val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
|
||||||
|
if (openGroup != null) {
|
||||||
|
return Promise.of(openGroup)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the new group
|
||||||
|
val application = ApplicationContext.getInstance(context)
|
||||||
|
val displayName = TextSecurePreferences.getProfileName(context)
|
||||||
|
val lokiPublicChatAPI = application.lokiPublicChatAPI ?: throw Error("LokiPublicChatAPI is not initialized")
|
||||||
|
return application.lokiPublicChatManager.addChat(url, channel).then { group ->
|
||||||
|
DatabaseFactory.getLokiAPIDatabase(context).removeLastMessageServerID(channel, url)
|
||||||
|
DatabaseFactory.getLokiAPIDatabase(context).removeLastDeletionServerID(channel, url)
|
||||||
|
lokiPublicChatAPI.getMessages(channel, url)
|
||||||
|
lokiPublicChatAPI.setDisplayName(displayName, url)
|
||||||
|
lokiPublicChatAPI.join(channel, url)
|
||||||
|
val profileKey: ByteArray = ProfileKeyUtil.getProfileKey(context)
|
||||||
|
val profileUrl: String? = TextSecurePreferences.getProfileAvatarUrl(context)
|
||||||
|
lokiPublicChatAPI.setProfilePicture(url, profileKey, profileUrl)
|
||||||
|
group
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue