Skip to content

Commit 851e7f7

Browse files
Disallow sending polls in threads. (#5970)
Co-authored-by: Aleksandar Apostolov <[email protected]>
1 parent aa31083 commit 851e7f7

File tree

8 files changed

+122
-25
lines changed

8 files changed

+122
-25
lines changed

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,8 @@ internal constructor(
17191719
/**
17201720
* Send a message with a poll to the given channel.
17211721
*
1722+
* IMPORTANT: Polls cannot be sent inside a thread!
1723+
*
17221724
* @param channelType The channel type. ie messaging.
17231725
* @param channelId The channel id. ie 123.
17241726
* @param pollConfig The poll configuration.

stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ui/MessagesActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class MessagesActivity : ComponentActivity() {
218218
val isShowingAttachments = attachmentsPickerViewModel.isShowingAttachments
219219
val currentMessagesState by listViewModel.currentMessagesState
220220
val selectedMessageState = currentMessagesState.selectedMessageState
221+
val messageMode by composerViewModel.messageMode.collectAsState()
221222
val user by listViewModel.user.collectAsState()
222223
val lazyListState = rememberMessageListState()
223224

@@ -294,6 +295,7 @@ class MessagesActivity : ComponentActivity() {
294295
attachmentsPickerViewModel.changeAttachmentState(false)
295296
attachmentsPickerViewModel.dismissAttachments()
296297
},
298+
messageMode = messageMode,
297299
)
298300
}
299301

stream-chat-android-compose/api/stream-chat-android-compose.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ public final class io/getstream/chat/android/compose/ui/messages/MessagesScreenK
21862186
}
21872187

21882188
public final class io/getstream/chat/android/compose/ui/messages/attachments/AttachmentsPickerKt {
2189-
public static final fun AttachmentsPicker (Lio/getstream/chat/android/compose/viewmodel/messages/AttachmentsPickerViewModel;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Ljava/util/List;Landroidx/compose/ui/graphics/Shape;Landroidx/compose/runtime/Composer;II)V
2189+
public static final fun AttachmentsPicker (Lio/getstream/chat/android/compose/viewmodel/messages/AttachmentsPickerViewModel;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Ljava/util/List;Landroidx/compose/ui/graphics/Shape;Lio/getstream/chat/android/ui/common/state/messages/MessageMode;Landroidx/compose/runtime/Composer;II)V
21902190
}
21912191

21922192
public abstract interface class io/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerAction {

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/messages/MessagesScreen.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
5757
import androidx.compose.ui.platform.testTag
5858
import androidx.compose.ui.res.stringResource
5959
import androidx.compose.ui.unit.dp
60+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
6061
import androidx.lifecycle.viewmodel.compose.viewModel
6162
import io.getstream.chat.android.compose.R
6263
import io.getstream.chat.android.compose.state.mediagallerypreview.MediaGalleryPreviewResultType
@@ -642,6 +643,7 @@ public fun BoxScope.AttachmentsPickerMenu(
642643
composerViewModel: MessageComposerViewModel,
643644
) {
644645
val isShowingAttachments = attachmentsPickerViewModel.isShowingAttachments
646+
val messageMode by composerViewModel.messageMode.collectAsStateWithLifecycle()
645647

646648
// Ensure keyboard is closed when the attachments picker is shown (if instructed by ChatTheme)
647649
val keyboardController = LocalSoftwareKeyboardController.current
@@ -708,6 +710,7 @@ public fun BoxScope.AttachmentsPickerMenu(
708710
attachmentsPickerViewModel.dismissAttachments()
709711
}
710712
},
713+
messageMode = messageMode,
711714
)
712715
}
713716
}

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/messages/attachments/AttachmentsPicker.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import io.getstream.chat.android.compose.ui.util.mirrorRtl
6060
import io.getstream.chat.android.compose.viewmodel.messages.AttachmentsPickerViewModel
6161
import io.getstream.chat.android.models.Attachment
6262
import io.getstream.chat.android.models.Channel
63+
import io.getstream.chat.android.ui.common.state.messages.MessageMode
6364

6465
/**
6566
* Represents the bottom bar UI that allows users to pick attachments. The picker renders its
@@ -74,7 +75,9 @@ import io.getstream.chat.android.models.Channel
7475
* @param modifier Modifier for styling.
7576
* @param tabFactories The list of attachment picker tab factories.
7677
* @param shape The shape of the dialog.
78+
* @param messageMode The message mode, used to determine if the default "Polls" tab is enabled.
7779
*/
80+
@Suppress("LongMethod")
7881
@Composable
7982
public fun AttachmentsPicker(
8083
attachmentsPickerViewModel: AttachmentsPickerViewModel,
@@ -85,6 +88,7 @@ public fun AttachmentsPicker(
8588
modifier: Modifier = Modifier,
8689
tabFactories: List<AttachmentsPickerTabFactory> = ChatTheme.attachmentsPickerTabFactories,
8790
shape: Shape = ChatTheme.shapes.bottomSheet,
91+
messageMode: MessageMode = MessageMode.Normal,
8892
) {
8993
val saveAttachmentsOnDismiss = ChatTheme.attachmentPickerTheme.saveAttachmentsOnDismiss
9094
val dismissAction = {
@@ -96,7 +100,7 @@ public fun AttachmentsPicker(
96100
BackHandler(onBack = dismissAction)
97101
// Cross-validate requested tabFactories with the allowed ones from BE
98102
val filter = remember { AttachmentsPickerTabFactoryFilter() }
99-
val allowedFactories = filter.filterAllowedFactories(tabFactories, attachmentsPickerViewModel.channel)
103+
val allowedFactories = filter.filterAllowedFactories(tabFactories, attachmentsPickerViewModel.channel, messageMode)
100104
val defaultTabIndex = allowedFactories
101105
.indexOfFirst { it.isPickerTabEnabled(attachmentsPickerViewModel.channel) }
102106
.takeIf { it >= 0 }

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentsPickerTabFactoryFilter.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package io.getstream.chat.android.compose.ui.messages.attachments.factory
1818

1919
import io.getstream.chat.android.compose.util.extensions.isPollEnabled
2020
import io.getstream.chat.android.models.Channel
21+
import io.getstream.chat.android.ui.common.state.messages.MessageMode
2122

2223
/**
2324
* Filter to determine if the tab factory should be shown or not.
@@ -29,16 +30,20 @@ internal class AttachmentsPickerTabFactoryFilter {
2930
*
3031
* @param factories The list of factories to filter.
3132
* @param channel The channel to check against.
33+
* @param messageMode The current message mode, used to determine if the default "Polls" tab is enabled.
3234
*/
3335
internal fun filterAllowedFactories(
3436
factories: List<AttachmentsPickerTabFactory>,
3537
channel: Channel,
38+
messageMode: MessageMode,
3639
): List<AttachmentsPickerTabFactory> {
3740
return factories
38-
.filter { factory -> factory.isPickerTabEnabled(channel) }
41+
.filter { factory ->
42+
factory.isEnabledForMessageMode(messageMode) && factory.isPickerTabEnabled(channel)
43+
}
3944
.map { factory ->
4045
when (factory) {
41-
is AttachmentsPickerSystemTabFactory -> adjustSystemFactory(factory, channel)
46+
is AttachmentsPickerSystemTabFactory -> adjustSystemFactory(factory, channel, messageMode)
4247
else -> factory
4348
}
4449
}
@@ -47,11 +52,23 @@ internal class AttachmentsPickerTabFactoryFilter {
4752
private fun adjustSystemFactory(
4853
factory: AttachmentsPickerSystemTabFactory,
4954
channel: Channel,
55+
messageMode: MessageMode,
5056
): AttachmentsPickerSystemTabFactory {
51-
// Adjust pollEnabled based on the channel config
57+
// Adjust pollAllowed based on the:
58+
// 1. Current message mode (only in Normal mode)
59+
// 2. Channel config (are polls enabled for the channel and the user has rights)
60+
// 3. Factory config (is the polls tab enabled in the factory config)
5261
val config = factory.config.copy(
53-
pollAllowed = channel.isPollEnabled() && factory.config.pollAllowed,
62+
pollAllowed = messageMode is MessageMode.Normal && channel.isPollEnabled() && factory.config.pollAllowed,
5463
)
5564
return AttachmentsPickerSystemTabFactory(config)
5665
}
66+
67+
private fun AttachmentsPickerTabFactory.isEnabledForMessageMode(mode: MessageMode): Boolean {
68+
return when (this) {
69+
// The default "Polls" tab is only shown in Normal mode
70+
is AttachmentsPickerPollTabFactory -> mode is MessageMode.Normal
71+
else -> true
72+
}
73+
}
5774
}

0 commit comments

Comments
 (0)