2021-07-09 05:18:48 +02:00
|
|
|
package org.thoughtcrime.securesms.groups
|
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.open_groups.OpenGroupAPIV2
|
2021-01-19 00:48:02 +01:00
|
|
|
import org.session.libsession.utilities.GroupUtil
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient
|
|
|
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
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"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-05-05 05:52:15 +02:00
|
|
|
@JvmStatic
|
|
|
|
@WorkerThread
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun updateGroupInfo(context: Context, server: String, room: String) {
|
|
|
|
val groupId = GroupUtil.getEncodedOpenGroupID("$server.$room".toByteArray())
|
2021-10-04 09:51:19 +02:00
|
|
|
if (!DatabaseComponent.get(context).groupDatabase().hasGroup(groupId)) {
|
2021-05-05 05:52:15 +02:00
|
|
|
throw IllegalStateException("Attempt to update open group info for non-existent DB record: $groupId")
|
|
|
|
}
|
|
|
|
|
|
|
|
val info = OpenGroupAPIV2.getInfo(room, server).get() // store info again?
|
|
|
|
OpenGroupAPIV2.getMemberCount(room, server).get()
|
|
|
|
|
|
|
|
EventBus.getDefault().post(GroupInfoUpdatedEvent(server, room = room))
|
|
|
|
}
|
|
|
|
|
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)"
|
|
|
|
|
2021-05-05 05:52:15 +02:00
|
|
|
data class GroupInfoUpdatedEvent(val url: String, val channel: Long = -1, val room: String = "")
|
2020-02-25 01:28:56 +01:00
|
|
|
}
|