Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions app/src/main/java/com/nextcloud/talk/activities/CallActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -135,6 +136,7 @@ import com.nextcloud.talk.signaling.SignalingMessageReceiver.CallParticipantMess
import com.nextcloud.talk.signaling.SignalingMessageReceiver.LocalParticipantMessageListener
import com.nextcloud.talk.signaling.SignalingMessageReceiver.OfferMessageListener
import com.nextcloud.talk.signaling.SignalingMessageSender
import com.nextcloud.talk.ui.LocalImageAuthHeader
import com.nextcloud.talk.ui.dialog.AudioOutputDialog
import com.nextcloud.talk.ui.dialog.MoreCallActionsDialog
import com.nextcloud.talk.utils.ApiUtils
Expand Down Expand Up @@ -607,37 +609,39 @@ class CallActivity : CallBaseActivity() {

binding!!.composeParticipantGrid.setContent {
MaterialTheme {
val screenShareParticipantUiState by callViewModel.activeScreenShareSession.collectAsState()
val participantUiStates by callViewModel.participants.collectAsState(initial = emptyList())
val speakers by callViewModel.speakers.collectAsState()
val viewMode by callViewModel.viewMode.collectAsState()
val speakerViewOrder by callViewModel.speakerViewOrder.collectAsState()

if (screenShareParticipantUiState == null) {
if (viewMode.isSpeakerView) {
ParticipantSpeakerView(
eglBase = rootEglBase!!,
participantUiStates = participantUiStates,
speakerViewOrder = speakerViewOrder,
isParticipantBarVisible = viewMode == CallViewMode.SPEAKER_WITH_PARTICIPANT_BAR,
isVoiceOnlyCall = isVoiceOnlyCall,
onVisibleSessionKeysChanged = callViewModel::setVisibleSessionKeys,
onScreenShareIconClick = {
callViewModel.setActiveScreenShareSession(it)
}
)
} else {
ParticipantGrid(
participantUiStates = participantUiStates,
eglBase = rootEglBase!!,
isVoiceOnlyCall = isVoiceOnlyCall,
speakers = speakers,
onVisibleSessionKeysChanged = callViewModel::setVisibleSessionKeys,
onClick = {},
onScreenShareIconClick = {
callViewModel.setActiveScreenShareSession(it)
}
)
CompositionLocalProvider(LocalImageAuthHeader provides conversationUser.getCredentials()) {
val screenShareParticipantUiState by callViewModel.activeScreenShareSession.collectAsState()
val participantUiStates by callViewModel.participants.collectAsState(initial = emptyList())
val speakers by callViewModel.speakers.collectAsState()
val viewMode by callViewModel.viewMode.collectAsState()
val speakerViewOrder by callViewModel.speakerViewOrder.collectAsState()

if (screenShareParticipantUiState == null) {
if (viewMode.isSpeakerView) {
ParticipantSpeakerView(
eglBase = rootEglBase!!,
participantUiStates = participantUiStates,
speakerViewOrder = speakerViewOrder,
isParticipantBarVisible = viewMode == CallViewMode.SPEAKER_WITH_PARTICIPANT_BAR,
isVoiceOnlyCall = isVoiceOnlyCall,
onVisibleSessionKeysChanged = callViewModel::setVisibleSessionKeys,
onScreenShareIconClick = {
callViewModel.setActiveScreenShareSession(it)
}
)
} else {
ParticipantGrid(
participantUiStates = participantUiStates,
eglBase = rootEglBase!!,
isVoiceOnlyCall = isVoiceOnlyCall,
speakers = speakers,
onVisibleSessionKeysChanged = callViewModel::setVisibleSessionKeys,
onClick = {},
onScreenShareIconClick = {
callViewModel.setActiveScreenShareSession(it)
}
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.nextcloud.talk.R
import com.nextcloud.talk.activities.ParticipantUiState
import com.nextcloud.talk.models.json.participants.ParticipantDto
import com.nextcloud.talk.ui.ActorAvatarImage
import com.nextcloud.talk.ui.LocalImageAuthHeader
import com.nextcloud.talk.ui.withAuthHeader
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.CharacterAvatarUtils
import com.nextcloud.talk.utils.DisplayUtils.isDarkModeOn
Expand All @@ -51,8 +54,14 @@ fun AvatarWithFallback(participant: ParticipantUiState, displayName: String, mod
} else {
val avatarUrl = getUrlForAvatar(participant = participant)
if (avatarUrl.isNotEmpty()) {
val authHeader = LocalImageAuthHeader.current.takeIf {
participant.actorType == ParticipantDto.ActorType.FEDERATED
}
AsyncImage(
model = avatarUrl,
model = ImageRequest.Builder(LocalContext.current)
.data(avatarUrl)
.withAuthHeader(authHeader)
.build(),
contentDescription = stringResource(R.string.avatar),
contentScale = ContentScale.Crop,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ class ScheduledMessagesActivity : BaseActivity() {
onCopyScheduledMessage: (ChatMessage) -> Unit
) {
val snackBarHostState = remember { SnackbarHostState() }
val authHeader = remember(user) { ApiUtils.getCredentials(user.username, user.token) }
val scheduledState by scheduledMessagesViewModel.getScheduledMessagesState.collectAsStateWithLifecycle()
val sendNowState by scheduledMessagesViewModel.sendNowState.collectAsStateWithLifecycle()
val rescheduleState by scheduledMessagesViewModel.rescheduleState.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -575,10 +576,15 @@ class ScheduledMessagesActivity : BaseActivity() {
message,
parentMessage,
delayedThreadLabel,
replyToThreadLabel
replyToThreadLabel,
authHeader
) {
val base =
message.toScheduledMessageUiModel(user, roomToken, parentMessage)
val base = message.toScheduledMessageUiModel(
user,
roomToken,
parentMessage,
authHeader
)
val isThreadReply =
!isThreadView && (message.threadId ?: 0L) > 0
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ data class ChatMessage(

var sendAt: Int? = null,

var avatarUrl: String? = null,

var isUnread: Boolean = false

) {
Expand Down Expand Up @@ -201,6 +199,18 @@ data class ChatMessage(
else -> sharedApplication!!.getString(R.string.nc_guest)
}

/**
* The display name of the actor, followed by the server of a federated user in brackets,
* e.g. "Alice (cloud.example.com)".
*/
fun getNullsafeActorDisplayNameWithServer(): String = withServer(getNullsafeActorDisplayName(), actorType, actorId)

/**
* The display name of the actor who pinned the message, followed by the server of a federated user in brackets.
*/
fun getPinnedActorDisplayNameWithServer(): String =
withServer(pinnedActorDisplayName.orEmpty(), pinnedActorType, pinnedActorId)

val createdAt: Date = Date(timestamp * MILLIES)

// needed a equals and hashcode function to fix detekt errors
Expand Down Expand Up @@ -325,6 +335,18 @@ data class ChatMessage(
private const val TAG = "ChatMessage"
private const val MILLIES: Long = 1000L
private const val ACTOR_TYPE_DELETED_USERS = "deleted_users"
const val ACTOR_TYPE_FEDERATED_USERS = "federated_users"

/**
* [name], followed by the server of a federated user in brackets, e.g. "Alice (cloud.example.com)",
* when [type] and [id] are those of a federated user.
*/
fun withServer(name: String, type: String?, id: String?): String =
if (type == ACTOR_TYPE_FEDERATED_USERS) {
"$name (${id.orEmpty().substringAfterLast('@')})"
} else {
name
}
private val regexOptions = setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)
private val MARKDOWN_LINK_REGEX = """\[[^\]]+\]\((https?://[^\s)]+)\)""".toRegex(regexOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class ChatMessageUi(
val threadReplies: Int,
val incoming: Boolean,
val isDeleted: Boolean,
val avatarUrl: String?,
val authHeader: String? = null,
val statusIcon: MessageStatusIcon,
val timestamp: Long,
val date: LocalDate,
Expand All @@ -60,7 +60,23 @@ data class ChatMessageUi(
val isHiddenByCollapse: Boolean = false,
val isExpanded: Boolean = false,
val referenceId: String? = null
)
) {
/**
* The avatar of the author on the account's own server, which proxies the avatar of federated authors from
* their server. [darkTheme] picks the variant generated for authors without a picture.
* Null without a server to ask, or for a federated author outside of a conversation.
*/
fun avatarUrl(darkTheme: Boolean): String? {
val baseUrl = activeUserBaseUrl ?: return null
return if (actorType == ChatMessage.ACTOR_TYPE_FEDERATED_USERS) {
roomToken?.takeIf { it.isNotEmpty() }?.let {
ApiUtils.getUrlForFederatedAvatar(baseUrl, it, actorId.orEmpty(), if (darkTheme) 1 else 0, false)
}
} else {
ApiUtils.getUrlForAvatar(baseUrl, actorId, false, darkTheme)
}
}
}

data class MessageReactionUi(val emoji: String, val amount: Int, val isSelfReaction: Boolean)

Expand Down Expand Up @@ -117,27 +133,30 @@ enum class MessageStatusIcon {
}

// Domain model (ChatMessage) to UI model (ChatMessageUi)
// When mapping a list of messages, pass the [authHeader] of [user] in so it is not encoded again for each one
@Suppress("LongParameterList")
fun ChatMessage.toUiModel(
user: User,
chatMessage: ChatMessage,
lastCommonReadMessageId: Int,
parentMessage: ChatMessage?,
isClassified: Boolean = false
isClassified: Boolean = false,
authHeader: String? = ApiUtils.getCredentials(user.username, user.token)
): ChatMessageUi =
ChatMessageUi(
id = jsonMessageId,
message = getRichText(),
plainMessage = message.orEmpty(),
renderMarkdown = renderMarkdown == true,
actorDisplayName = getNullsafeActorDisplayName(),
actorDisplayName = getNullsafeActorDisplayNameWithServer(),
actorType = actorType,
actorId = actorId,
threadTitle = threadTitle.orEmpty(),
isThread = isThread,
threadReplies = threadReplies ?: 0,
incoming = incoming,
isDeleted = isDeleted,
avatarUrl = avatarUrl,
authHeader = authHeader,
statusIcon = resolveStatusIcon(
jsonMessageId,
lastCommonReadMessageId,
Expand All @@ -158,6 +177,7 @@ fun ChatMessage.toUiModel(
parentMessage = parentMessage?.toUiModel(
user = user,
chatMessage = parentMessage,
authHeader = authHeader,
lastCommonReadMessageId = 0,
parentMessage = null
),
Expand All @@ -174,23 +194,24 @@ fun ChatMessage.toUiModel(
fun ChatMessage.toScheduledMessageUiModel(
user: User,
roomToken: String,
parentMessage: ChatMessage? = null
parentMessage: ChatMessage? = null,
authHeader: String? = ApiUtils.getCredentials(user.username, user.token)
): ChatMessageUi {
val sendAtTimestamp = sendAt?.toLong() ?: timestamp
return ChatMessageUi(
id = token?.toIntOrNull() ?: token.hashCode(),
message = getRichText(),
plainMessage = message.orEmpty(),
renderMarkdown = renderMarkdown != false,
actorDisplayName = getNullsafeActorDisplayName(),
actorDisplayName = getNullsafeActorDisplayNameWithServer(),
actorType = actorType,
actorId = actorId,
isThread = isThread,
threadTitle = threadTitle.orEmpty(),
threadReplies = threadReplies ?: 0,
incoming = false,
isDeleted = isDeleted,
avatarUrl = null,
authHeader = authHeader,
statusIcon = MessageStatusIcon.SCHEDULED,
timestamp = sendAtTimestamp,
date = Instant.ofEpochSecond(sendAtTimestamp).atZone(ZoneId.systemDefault()).toLocalDate(),
Expand All @@ -201,7 +222,7 @@ fun ChatMessage.toScheduledMessageUiModel(
messageParameters = normalizeMessageParameters(),
reactions = emptyList(),
isEdited = lastEditTimestamp != 0L,
parentMessage = parentMessage?.toScheduledMessageUiModel(user, roomToken),
parentMessage = parentMessage?.toScheduledMessageUiModel(user, roomToken, authHeader = authHeader),
replyable = false,
isGrouped = false,
isGroupedWithNext = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import com.nextcloud.talk.threadsoverview.data.ThreadsRepository
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.CapabilitiesUtil.hasSpreedFeatureCapability
import com.nextcloud.talk.utils.CharacterAvatarUtils
import com.nextcloud.talk.utils.ContactUtils
import com.nextcloud.talk.utils.ConversationUtils
import com.nextcloud.talk.utils.Mimetype
Expand Down Expand Up @@ -792,7 +791,6 @@ class ChatViewModel @AssistedInject constructor(
map { entities ->
entities.map { entity ->
entity.toDomainModel().apply {
avatarUrl = getAvatarUrl(this)
incoming = actorId != userId
}
}
Expand Down Expand Up @@ -1296,14 +1294,16 @@ class ChatViewModel @AssistedInject constructor(
.distinct()

val isClassified = conversation != null && ConversationUtils.isClassified(conversation, capabilities)
val user = currentUserFlow.value
val user = currentUser
val authHeader = ApiUtils.getCredentials(user.username, user.token)
applyMessageGrouping(messages)
applySystemMessageGrouping(messages)
reorderKnownSendSequence(messages)
val uiMessages = messages.map { message ->
val parent: ChatMessage? = combinedMap[message.parentMessageId]
message.toUiModel(
user = user ?: currentUser,
user = user,
authHeader = authHeader,
chatMessage = message,
lastCommonReadMessageId = lastCommonRead,
parentMessage = parent,
Expand Down Expand Up @@ -1700,24 +1700,6 @@ class ChatViewModel @AssistedInject constructor(

// val timeString = DateUtils.getLocalTimeStringFromTimestamp(message.timestamp)

/**
* Avatar to request from the server for a message, empty for the actors the server has none for
* - those get their avatar drawn on the client instead, see [CharacterAvatarUtils].
*/
fun getAvatarUrl(message: ChatMessage): String =
if (!message.hasClientSideAvatar()) {
ApiUtils.getUrlForAvatar(
currentUser.baseUrl,
message.actorId,
false
)
} else {
""
}

private fun ChatMessage.hasClientSideAvatar(): Boolean =
CharacterAvatarUtils.avatarFor(actorType, actorId, actorDisplayName, guestLabel = null) != null

fun initData() {
chatRepository.initData(
currentUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class MessageInputViewModel :
_inputState.update {
it.copy(
quotedJsonId = message?.jsonMessageId,
quotedDisplayName = message?.actorDisplayName,
quotedDisplayName = message?.getNullsafeActorDisplayNameWithServer(),
quotedMessageText = message?.getRichText(),
quotedImageUrl = "" // TODO
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ import coil.request.ImageRequest
import coil.size.Size
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import com.nextcloud.talk.ui.withAuthHeader

fun loadImage(imageUri: String?, context: Context, errorPlaceholderImage: Int): ImageRequest {
fun loadImage(
imageUri: String?,
context: Context,
errorPlaceholderImage: Int,
authHeader: String? = null
): ImageRequest {
val imageRequest = ImageRequest.Builder(context)
.data(imageUri)
.withAuthHeader(authHeader)
.transformations(CircleCropTransformation())
.error(errorPlaceholderImage)
.placeholder(errorPlaceholderImage)
Expand Down
Loading
Loading