2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.utilities
|
2020-02-25 01:28:56 +01:00
|
|
|
|
|
|
|
import android.content.Context
|
2020-11-20 08:59:13 +01:00
|
|
|
import androidx.annotation.WorkerThread
|
|
|
|
import org.greenrobot.eventbus.EventBus
|
2021-04-28 09:41:30 +02:00
|
|
|
import org.session.libsession.messaging.MessagingModuleConfiguration
|
2021-04-26 03:14:45 +02:00
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroup
|
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupAPI
|
2021-04-28 09:41:30 +02:00
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupAPIV2
|
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupV2
|
2021-05-03 03:29:04 +02:00
|
|
|
import org.session.libsession.messaging.threads.recipients.Recipient
|
2021-01-19 00:48:02 +01:00
|
|
|
import org.session.libsession.utilities.GroupUtil
|
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-03-31 06:15:50 +02:00
|
|
|
import org.session.libsession.utilities.preferences.ProfileKeyUtil
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
|
|
|
import org.thoughtcrime.securesms.groups.GroupManager
|
2021-05-03 03:29:04 +02:00
|
|
|
import java.util.*
|
2020-02-25 01:28:56 +01:00
|
|
|
|
2020-11-20 08:59:13 +01:00
|
|
|
//TODO Refactor so methods declare specific type of checked exceptions and not generalized Exception.
|
2020-02-25 01:28:56 +01:00
|
|
|
object OpenGroupUtilities {
|
2020-02-28 03:04:57 +01:00
|
|
|
|
2020-11-20 08:59:13 +01:00
|
|
|
private const val TAG = "OpenGroupUtilities"
|
|
|
|
|
2021-04-21 09:00:57 +02:00
|
|
|
@JvmStatic
|
|
|
|
@WorkerThread
|
2021-04-22 09:26:40 +02:00
|
|
|
fun addGroup(context: Context, server: String, room: String, publicKey: String): OpenGroupV2 {
|
2021-04-21 09:00:57 +02:00
|
|
|
val groupId = "$server.$room"
|
|
|
|
val threadID = GroupManager.getOpenGroupThreadID(groupId, context)
|
|
|
|
val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getOpenGroupChat(threadID)
|
|
|
|
if (openGroup != null) return openGroup
|
|
|
|
|
2021-04-28 09:41:30 +02:00
|
|
|
MessagingModuleConfiguration.shared.storage.setOpenGroupPublicKey(server,publicKey)
|
2021-04-22 09:26:40 +02:00
|
|
|
OpenGroupAPIV2.getAuthToken(room, server).get()
|
|
|
|
val groupInfo = OpenGroupAPIV2.getInfo(room,server).get()
|
2021-04-21 09:00:57 +02:00
|
|
|
val application = ApplicationContext.getInstance(context)
|
2021-04-22 09:26:40 +02:00
|
|
|
|
2021-04-23 09:49:24 +02:00
|
|
|
val group = application.publicChatManager.addChat(server, room, groupInfo, publicKey)
|
2021-04-22 09:26:40 +02:00
|
|
|
|
2021-04-28 09:41:30 +02:00
|
|
|
val storage = MessagingModuleConfiguration.shared.storage
|
2021-04-22 09:26:40 +02:00
|
|
|
storage.removeLastDeletionServerId(room, server)
|
|
|
|
storage.removeLastMessageServerId(room, server)
|
|
|
|
|
|
|
|
return group
|
2021-04-21 09:00:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 08:59:13 +01:00
|
|
|
@JvmStatic
|
|
|
|
@WorkerThread
|
|
|
|
@Throws(Exception::class)
|
2021-03-31 06:15:50 +02:00
|
|
|
fun addGroup(context: Context, url: String, channel: Long): OpenGroup {
|
2020-11-20 08:59:13 +01:00
|
|
|
// Check for an existing group.
|
2021-04-26 05:12:05 +02:00
|
|
|
val groupID = OpenGroup.getId(channel, url)
|
2020-11-20 08:59:13 +01:00
|
|
|
val threadID = GroupManager.getOpenGroupThreadID(groupID, context)
|
|
|
|
val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
|
2021-04-26 05:12:05 +02:00
|
|
|
if (openGroup != null) { return openGroup }
|
2020-11-20 08:59:13 +01:00
|
|
|
|
|
|
|
// Add the new group.
|
|
|
|
val application = ApplicationContext.getInstance(context)
|
|
|
|
val displayName = TextSecurePreferences.getProfileName(context)
|
|
|
|
|
|
|
|
val group = application.publicChatManager.addChat(url, channel)
|
|
|
|
|
2020-08-20 02:32:31 +02:00
|
|
|
DatabaseFactory.getLokiAPIDatabase(context).removeLastMessageServerID(channel, url)
|
|
|
|
DatabaseFactory.getLokiAPIDatabase(context).removeLastDeletionServerID(channel, url)
|
2021-03-31 06:15:50 +02:00
|
|
|
OpenGroupAPI.getMessages(channel, url)
|
|
|
|
OpenGroupAPI.setDisplayName(displayName, url)
|
|
|
|
OpenGroupAPI.join(channel, url)
|
2020-08-20 02:32:31 +02:00
|
|
|
val profileKey: ByteArray = ProfileKeyUtil.getProfileKey(context)
|
|
|
|
val profileUrl: String? = TextSecurePreferences.getProfilePictureURL(context)
|
2021-03-31 06:15:50 +02:00
|
|
|
OpenGroupAPI.setProfilePicture(url, profileKey, profileUrl)
|
2020-11-20 08:59:13 +01:00
|
|
|
return group
|
2020-02-25 01:28:56 +01:00
|
|
|
}
|
2020-11-20 08:59:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls the general public chat data from the server and updates related records.
|
|
|
|
* Fires [GroupInfoUpdatedEvent] on [EventBus] upon success.
|
|
|
|
*
|
|
|
|
* Consider using [org.thoughtcrime.securesms.loki.api.PublicChatInfoUpdateWorker] for lazy approach.
|
|
|
|
*/
|
|
|
|
@JvmStatic
|
|
|
|
@WorkerThread
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun updateGroupInfo(context: Context, url: String, channel: Long) {
|
|
|
|
// Check if open group has a related DB record.
|
2021-04-26 05:12:05 +02:00
|
|
|
val groupId = GroupUtil.getEncodedOpenGroupID(OpenGroup.getId(channel, url).toByteArray())
|
2020-11-20 08:59:13 +01:00
|
|
|
if (!DatabaseFactory.getGroupDatabase(context).hasGroup(groupId)) {
|
|
|
|
throw IllegalStateException("Attempt to update open group info for non-existent DB record: $groupId")
|
|
|
|
}
|
|
|
|
|
2021-03-31 06:15:50 +02:00
|
|
|
val info = OpenGroupAPI.getChannelInfo(channel, url).get()
|
2020-11-20 08:59:13 +01:00
|
|
|
|
2021-03-31 06:15:50 +02:00
|
|
|
OpenGroupAPI.updateProfileIfNeeded(channel, url, groupId, info, false)
|
2020-11-20 08:59:13 +01:00
|
|
|
|
|
|
|
EventBus.getDefault().post(GroupInfoUpdatedEvent(url, channel))
|
|
|
|
}
|
|
|
|
|
2021-05-03 03:29:04 +02:00
|
|
|
/**
|
|
|
|
* Return a generated name for users in the style of `$name (...$hex.takeLast(8))` for public groups
|
|
|
|
*/
|
|
|
|
@JvmStatic
|
|
|
|
fun getDisplayName(recipient: Recipient): String {
|
|
|
|
return String.format(Locale.ROOT, PUBLIC_GROUP_STRING_FORMAT, recipient.name, recipient.address.serialize().takeLast(8))
|
|
|
|
}
|
|
|
|
|
|
|
|
const val PUBLIC_GROUP_STRING_FORMAT = "%s (...%s)"
|
|
|
|
|
2020-11-20 08:59:13 +01:00
|
|
|
data class GroupInfoUpdatedEvent(val url: String, val channel: Long)
|
2020-02-25 01:28:56 +01:00
|
|
|
}
|