mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-12 07:17:45 +00:00
feat: join groups logic
This commit is contained in:
@@ -4,7 +4,6 @@ import android.animation.Animator
|
||||
import android.animation.AnimatorListenerAdapter
|
||||
import android.graphics.BitmapFactory
|
||||
import android.os.Bundle
|
||||
import android.util.Patterns
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@@ -76,25 +75,22 @@ class JoinPublicChatActivity : PassphraseRequiredActionBarActivity(), ScanQRCode
|
||||
}
|
||||
|
||||
fun joinPublicChatIfPossible(url: String) {
|
||||
if (!Patterns.WEB_URL.matcher(url).matches() || !url.startsWith("https://")) {
|
||||
return Toast.makeText(this, R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
// add http if just an IP style / host style URL is entered but leave it if scheme is included
|
||||
val properString = if (!url.startsWith("http")) "http://$url" else url
|
||||
val httpUrl = HttpUrl.parse(url) ?: return Toast.makeText(this,R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
||||
val httpUrl = HttpUrl.parse(properString) ?: return Toast.makeText(this,R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
||||
|
||||
val room = httpUrl.pathSegments().firstOrNull()
|
||||
val publicKey = httpUrl.queryParameter("public_key")
|
||||
val isV2OpenGroup = !room.isNullOrEmpty()
|
||||
showLoader()
|
||||
val channel: Long = 1
|
||||
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
if (isV2OpenGroup) {
|
||||
val server = httpUrl.newBuilder().removeAllQueryParameters("public_key").removePathSegment(0).build().toString()
|
||||
OpenGroupUtilities.addGroup(this@JoinPublicChatActivity, server, room, publicKey)
|
||||
val server = HttpUrl.Builder().scheme(httpUrl.scheme()).host(httpUrl.host()).build()
|
||||
OpenGroupUtilities.addGroup(this@JoinPublicChatActivity, server.toString().removeSuffix("/"), room!!, publicKey!!)
|
||||
} else {
|
||||
val channel: Long = 1
|
||||
OpenGroupUtilities.addGroup(this@JoinPublicChatActivity, url, channel)
|
||||
}
|
||||
MultiDeviceProtocol.forceSyncConfigurationNowIfNeeded(this@JoinPublicChatActivity)
|
||||
@@ -195,10 +191,7 @@ class EnterChatURLFragment : Fragment() {
|
||||
private fun joinPublicChatIfPossible() {
|
||||
val inputMethodManager = requireContext().getSystemService(BaseActionBarActivity.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
inputMethodManager.hideSoftInputFromWindow(chatURLEditText.windowToken, 0)
|
||||
var chatURL = chatURLEditText.text.trim().toString().toLowerCase().replace("http://", "https://")
|
||||
if (!chatURL.toLowerCase().startsWith("https")) {
|
||||
chatURL = "https://$chatURL"
|
||||
}
|
||||
val chatURL = chatURLEditText.text.trim().toString().toLowerCase()
|
||||
(requireActivity() as JoinPublicChatActivity).joinPublicChatIfPossible(chatURL)
|
||||
}
|
||||
}
|
||||
|
@@ -80,15 +80,6 @@ class PublicChatManager(private val context: Context) {
|
||||
return addChat(server, channel, channelInfo)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun addChat(server: String, room: String): OpenGroupV2 {
|
||||
// Ensure the auth token is acquired.
|
||||
OpenGroupAPIV2.getAuthToken(room, server).get()
|
||||
|
||||
val channelInfo = OpenGroupAPIV2.getInfo(room, server).get()
|
||||
return addChat(server, room, channelInfo)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
public fun addChat(server: String, channel: Long, info: OpenGroupInfo): OpenGroup {
|
||||
val chat = PublicChat(channel, server, info.displayName, true)
|
||||
@@ -117,17 +108,21 @@ class PublicChatManager(private val context: Context) {
|
||||
|
||||
@WorkerThread
|
||||
fun addChat(server: String, room: String, info: OpenGroupAPIV2.Info): OpenGroupV2 {
|
||||
val chat = OpenGroupV2(server, room, info.id, info.name, info.imageID)
|
||||
val threadID = GroupManager.getOpenGroupThreadID(chat.id, context)
|
||||
val chat = OpenGroupV2(server, room, info.id, info.name)
|
||||
var threadID = GroupManager.getOpenGroupThreadID(chat.id, context)
|
||||
var profilePicture: Bitmap? = null
|
||||
if (threadID < 0) {
|
||||
val imageID = info.imageID
|
||||
if (!imageID.isNullOrEmpty()) {
|
||||
val profilePictureAsByteArray = OpenGroupAPIV2.downloadOpenGroupProfilePicture(info.id,server)
|
||||
val profilePictureAsByteArray = try {
|
||||
OpenGroupAPIV2.downloadOpenGroupProfilePicture(info.id,server).get()
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
profilePicture = BitmapUtil.fromByteArray(profilePictureAsByteArray)
|
||||
}
|
||||
val result = GroupManager.createOpenGroup(chat.id, context, profilePicture, info.name)
|
||||
threadID = result.threadId
|
||||
}
|
||||
DatabaseFactory.getLokiThreadDatabase(context).setOpenGroupChat(chat, threadID)
|
||||
Util.runOnMain { startPollersIfNeeded() }
|
||||
return chat
|
||||
}
|
||||
|
||||
|
@@ -94,6 +94,17 @@ class LokiThreadDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa
|
||||
}
|
||||
}
|
||||
|
||||
fun setOpenGroupChat(openGroupV2: OpenGroupV2, threadID: Long) {
|
||||
if (threadID < 0) {
|
||||
return
|
||||
}
|
||||
val database = databaseHelper.writableDatabase
|
||||
val contentValues = ContentValues(2)
|
||||
contentValues.put(Companion.threadID, threadID)
|
||||
contentValues.put(publicChat, JsonUtil.toJson(openGroupV2.toJson()))
|
||||
database.insertOrUpdate(publicChatTable, contentValues, "${Companion.threadID} = ?", arrayOf(threadID.toString()))
|
||||
}
|
||||
|
||||
override fun getPublicChat(threadID: Long): PublicChat? {
|
||||
if (threadID < 0) {
|
||||
return null
|
||||
|
@@ -3,8 +3,10 @@ package org.thoughtcrime.securesms.loki.utilities
|
||||
import android.content.Context
|
||||
import androidx.annotation.WorkerThread
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.session.libsession.messaging.MessagingConfiguration
|
||||
import org.session.libsession.messaging.opengroups.OpenGroup
|
||||
import org.session.libsession.messaging.opengroups.OpenGroupAPI
|
||||
import org.session.libsession.messaging.opengroups.OpenGroupAPIV2
|
||||
import org.session.libsession.messaging.opengroups.OpenGroupV2
|
||||
import org.session.libsession.utilities.GroupUtil
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
@@ -21,14 +23,24 @@ object OpenGroupUtilities {
|
||||
|
||||
@JvmStatic
|
||||
@WorkerThread
|
||||
fun addGroup(context: Context, server: String, room: String, publicKey: String?): OpenGroupV2 {
|
||||
fun addGroup(context: Context, server: String, room: String, publicKey: String): OpenGroupV2 {
|
||||
val groupId = "$server.$room"
|
||||
val threadID = GroupManager.getOpenGroupThreadID(groupId, context)
|
||||
val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getOpenGroupChat(threadID)
|
||||
if (openGroup != null) return openGroup
|
||||
|
||||
MessagingConfiguration.shared.storage.setOpenGroupPublicKey(server,publicKey)
|
||||
OpenGroupAPIV2.getAuthToken(room, server).get()
|
||||
val groupInfo = OpenGroupAPIV2.getInfo(room,server).get()
|
||||
val application = ApplicationContext.getInstance(context)
|
||||
val group = application.publicChatManager.addChat(server, room, publicKey)
|
||||
|
||||
val group = application.publicChatManager.addChat(server, room, groupInfo)
|
||||
|
||||
val storage = MessagingConfiguration.shared.storage
|
||||
storage.removeLastDeletionServerId(room, server)
|
||||
storage.removeLastMessageServerId(room, server)
|
||||
|
||||
return group
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
|
Reference in New Issue
Block a user