get Note-to-Self room by endpoint + convert to coroutines

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2025-04-29 13:38:06 +02:00
parent c6d1f68442
commit ab19f631ae
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
6 changed files with 62 additions and 95 deletions

View File

@ -251,4 +251,7 @@ interface NcApiCoroutines {
@Url url: String,
@Query("limit") limit: Int
): ChatOverall
@GET
suspend fun getNoteToSelfRoom(@Header("Authorization") authorization: String, @Url url: String): RoomOverall
}

View File

@ -3300,54 +3300,49 @@ class ChatActivity :
conversationUser!!,
intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1)
)
chatViewModel.checkForNoteToSelf(
ApiUtils.getCredentials(conversationUser!!.username, conversationUser!!.token)!!,
ApiUtils.getUrlForRooms(
apiVersion,
conversationUser!!.baseUrl
),
false
)
chatViewModel.getNoteToSelfAvailability.observe(this) { state ->
when (state) {
is ChatViewModel.NoteToSelfAvailableState -> {
this.lifecycleScope.launch {
var shareUri: Uri? = null
val data: HashMap<String?, String?>?
var metaData: String = ""
var objectId: String = ""
if (message.hasFileAttachment()) {
val filename = message.selectedIndividualHashMap!!["name"]
path = applicationContext.cacheDir.absolutePath + "/" + filename
shareUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID,
File(path)
)
this.lifecycleScope.launch {
val noteToSelfConversation = chatViewModel.checkForNoteToSelf(
ApiUtils.getCredentials(conversationUser!!.username, conversationUser!!.token)!!,
ApiUtils.getUrlForNoteToSelf(
apiVersion,
conversationUser!!.baseUrl
)
)
grantUriPermission(
applicationContext.packageName,
shareUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
} else if (message.hasGeoLocation()) {
data = message.messageParameters?.get("object")
objectId = data?.get("id")!!
val name = data["name"]!!
val lat = data["latitude"]!!
val lon = data["longitude"]!!
metaData =
"{\"type\":\"geo-location\",\"id\":\"geo:$lat,$lon\",\"latitude\":\"$lat\"," +
"\"longitude\":\"$lon\",\"name\":\"$name\"}"
}
if (noteToSelfConversation != null) {
var shareUri: Uri? = null
val data: HashMap<String?, String?>?
var metaData = ""
var objectId = ""
if (message.hasFileAttachment()) {
val filename = message.selectedIndividualHashMap!!["name"]
path = applicationContext.cacheDir.absolutePath + "/" + filename
shareUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID,
File(path)
)
shareToNotes(shareUri, state.roomToken, message, objectId, metaData)
}
}
else -> {
Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show()
grantUriPermission(
applicationContext.packageName,
shareUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
} else if (message.hasGeoLocation()) {
data = message.messageParameters?.get("object")
objectId = data?.get("id")!!
val name = data["name"]!!
val lat = data["latitude"]!!
val lon = data["longitude"]!!
metaData =
"{\"type\":\"geo-location\",\"id\":\"geo:$lat,$lon\",\"latitude\":\"$lat\"," +
"\"longitude\":\"$lon\",\"name\":\"$name\"}"
}
shareToNotes(shareUri, noteToSelfConversation.token, message, objectId, metaData)
} else {
Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show()
}
}
}

View File

@ -12,7 +12,6 @@ import com.nextcloud.talk.models.json.capabilities.SpreedCapability
import com.nextcloud.talk.models.json.chat.ChatMessageJson
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage
import com.nextcloud.talk.models.json.conversations.RoomOverall
import com.nextcloud.talk.models.json.conversations.RoomsOverall
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.models.json.opengraph.Reference
import com.nextcloud.talk.models.json.reminder.Reminder
@ -42,7 +41,8 @@ interface ChatNetworkDataSource {
displayName: String
): Observable<ChatOverallSingleMessage>
fun checkForNoteToSelf(credentials: String, url: String, includeStatus: Boolean): Observable<RoomsOverall>
suspend fun checkForNoteToSelf(credentials: String, url: String): RoomOverall
fun shareLocationToNotes(
credentials: String,
url: String,

View File

@ -14,7 +14,6 @@ import com.nextcloud.talk.models.json.capabilities.SpreedCapability
import com.nextcloud.talk.models.json.chat.ChatMessageJson
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage
import com.nextcloud.talk.models.json.conversations.RoomOverall
import com.nextcloud.talk.models.json.conversations.RoomsOverall
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.models.json.opengraph.Reference
import com.nextcloud.talk.models.json.reminder.Reminder
@ -124,11 +123,8 @@ class RetrofitChatNetwork(
it
}
override fun checkForNoteToSelf(
credentials: String,
url: String,
includeStatus: Boolean
): Observable<RoomsOverall> = ncApi.getRooms(credentials, url, includeStatus).map { it }
override suspend fun checkForNoteToSelf(credentials: String, url: String): RoomOverall =
ncApiCoroutines.getNoteToSelfRoom(credentials, url)
override fun shareLocationToNotes(
credentials: String,

View File

@ -34,14 +34,12 @@ import com.nextcloud.talk.models.json.capabilities.SpreedCapability
import com.nextcloud.talk.models.json.chat.ChatMessageJson
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage
import com.nextcloud.talk.models.json.conversations.RoomOverall
import com.nextcloud.talk.models.json.conversations.RoomsOverall
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.models.json.opengraph.Reference
import com.nextcloud.talk.models.json.reminder.Reminder
import com.nextcloud.talk.models.json.userAbsence.UserAbsenceData
import com.nextcloud.talk.repositories.reactions.ReactionsRepository
import com.nextcloud.talk.ui.PlaybackSpeed
import com.nextcloud.talk.utils.ConversationUtils
import com.nextcloud.talk.utils.bundle.BundleKeys
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import com.nextcloud.talk.utils.preferences.AppPreferences
@ -199,13 +197,6 @@ class ChatViewModel @Inject constructor(
val getReminderExistState: LiveData<ViewState>
get() = _getReminderExistState
object NoteToSelfNotAvailableState : ViewState
open class NoteToSelfAvailableState(val roomToken: String) : ViewState
private val _getNoteToSelfAvailability: MutableLiveData<ViewState> = MutableLiveData(NoteToSelfNotAvailableState)
val getNoteToSelfAvailability: LiveData<ViewState>
get() = _getNoteToSelfAvailability
object GetRoomStartState : ViewState
object GetRoomErrorState : ViewState
object GetRoomSuccessState : ViewState
@ -538,10 +529,17 @@ class ChatViewModel @Inject constructor(
})
}
fun checkForNoteToSelf(credentials: String, baseUrl: String, includeStatus: Boolean) {
chatNetworkDataSource.checkForNoteToSelf(credentials, baseUrl, includeStatus).subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(CheckForNoteToSelfObserver())
suspend fun checkForNoteToSelf(credentials: String, baseUrl: String): ConversationModel? {
val response = chatNetworkDataSource.checkForNoteToSelf(credentials, baseUrl)
if (response.ocs?.meta?.statusCode == HTTP_CODE_OK) {
val noteToSelfConversation = ConversationModel.mapToConversationModel(
response.ocs?.data!!,
userProvider.currentUser.blockingGet()
)
return noteToSelfConversation
} else {
return null
}
}
fun shareLocationToNotes(credentials: String, url: String, objectType: String, objectId: String, metadata: String) {
@ -784,36 +782,6 @@ class ChatViewModel @Inject constructor(
}
}
inner class CheckForNoteToSelfObserver : Observer<RoomsOverall> {
override fun onSubscribe(d: Disposable) {
disposableSet.add(d)
}
override fun onNext(roomsOverall: RoomsOverall) {
val rooms = roomsOverall.ocs?.data
rooms?.let {
try {
val noteToSelf = rooms.first {
val model = ConversationModel.mapToConversationModel(it, userProvider.currentUser.blockingGet())
ConversationUtils.isNoteToSelfConversation(model)
}
_getNoteToSelfAvailability.value = NoteToSelfAvailableState(noteToSelf.token)
} catch (e: NoSuchElementException) {
_getNoteToSelfAvailability.value = NoteToSelfNotAvailableState
Log.e(TAG, "Note to self not found $e")
}
}
}
override fun onError(e: Throwable) {
Log.d(TAG, "Error when getting rooms for Note to Self Observer $e")
}
override fun onComplete() {
// unused atm
}
}
@Suppress("Detekt.TooGenericExceptionCaught")
fun outOfOfficeStatusOfUser(credentials: String, baseUrl: String, userId: String) {
viewModelScope.launch {
@ -875,6 +843,7 @@ class ChatViewModel @Inject constructor(
companion object {
private val TAG = ChatViewModel::class.simpleName
const val JOIN_ROOM_RETRY_COUNT: Long = 3
const val HTTP_CODE_OK: Int = 200
}
sealed class OutOfOfficeUIState {

View File

@ -181,6 +181,10 @@ object ApiUtils {
return getUrlForApi(version, baseUrl) + "/room"
}
fun getUrlForNoteToSelf(version: Int, baseUrl: String?): String {
return getUrlForApi(version, baseUrl) + "/room/note-to-self"
}
@JvmStatic
fun getUrlForRoom(version: Int, baseUrl: String?, token: String?): String {
return getUrlForRooms(version, baseUrl) + "/" + token