Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Put limit of two image attachments per message

laurenspriema256c66d410f
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/HomeView.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/HomeView.kt
index 3866f23cd1..21be364584 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/HomeView.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/HomeView.kt
@@ -65,6 +65,7 @@ import io.ente.ensu.domain.model.Attachment
import io.ente.ensu.domain.model.AttachmentType
import io.ente.ensu.domain.model.EnsuDefaults
import io.ente.ensu.domain.model.LogEntry
+import io.ente.ensu.domain.model.MaxImageAttachmentsPerMessage
import io.ente.ensu.domain.state.AppState
import io.ente.ensu.domain.store.AppStore
import io.ente.ensu.utils.EnsuFeatureFlags
@@ -217,9 +218,13 @@ fun HomeView(
val openDrawer: () -> Unit = { scope.launch { drawerState.open() } }
- val handleAttachmentSelected: (AttachmentType) -> Unit = { type ->
+ val handleAttachmentSelected: (AttachmentType) -> Unit = handle@{ type ->
when (type) {
AttachmentType.Image -> {
+ val imageCount = appState.chat.attachments.count {
+ it.type == AttachmentType.Image
+ }
+ if (imageCount >= MaxImageAttachmentsPerMessage) return@handle
imagePicker.launch("image/*")
}
AttachmentType.Document -> {
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
index 509778a4a4..e600441216 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
@@ -57,6 +57,7 @@ import io.ente.ensu.components.ImageAttachmentThumbnail
import io.ente.ensu.domain.model.Attachment
import io.ente.ensu.domain.model.AttachmentType
import io.ente.ensu.domain.model.ChatMessage
+import io.ente.ensu.domain.model.MaxImageAttachmentsPerMessage
import io.ente.ensu.domain.util.formattedFileSize
import io.ente.ensu.utils.EnsuFeatureFlags
import io.ente.ensu.utils.rememberEnsuHaptics
@@ -124,6 +125,8 @@ internal fun MessageInput(
val hasAttachmentContent = attachments.isNotEmpty() || isProcessingAttachments
val inputVerticalPadding = if (hasAttachmentContent) EnsuSpacing.xs.dp else 10.dp
val bottomPadding = if (hasAttachmentContent) EnsuSpacing.sm.dp else EnsuSpacing.md.dp
+ val isImageAttachmentLimitReached =
+ attachments.count { it.type == AttachmentType.Image } >= MaxImageAttachmentsPerMessage
Column(
modifier = modifier
@@ -266,12 +269,16 @@ internal fun MessageInput(
Spacer(modifier = Modifier.width(EnsuSpacing.sm.dp))
if (EnsuFeatureFlags.enableImageUploads && editingMessage == null) {
+ val canAddImageAttachment = !isGenerating &&
+ !isDownloading &&
+ !isAttachmentDownloadBlocked &&
+ !isImageAttachmentLimitReached
IconButton(
onClick = {
haptic.perform(HapticFeedbackType.TextHandleMove)
onAttachmentSelected(AttachmentType.Image)
},
- enabled = !isGenerating && !isDownloading && !isAttachmentDownloadBlocked,
+ enabled = canAddImageAttachment,
modifier = Modifier.size(36.dp)
) {
Icon(
diff --git a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt
index 3e7793fbab..eceb8f3673 100644
--- a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt
+++ b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt
@@ -33,6 +33,8 @@ enum class AttachmentType {
Document
}
+const val MaxImageAttachmentsPerMessage = 2
+
data class Attachment(
val id: String = UUID.randomUUID().toString(),
val name: String,
diff --git a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/AttachmentStoreActions.kt b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/AttachmentStoreActions.kt
index 314c44f4da..bf98ee979b 100644
--- a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/AttachmentStoreActions.kt
+++ b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/AttachmentStoreActions.kt
@@ -4,7 +4,9 @@ import io.ente.ensu.domain.chat.ChatSyncRepository
import io.ente.ensu.domain.model.Attachment
import io.ente.ensu.domain.model.AttachmentDownloadItem
import io.ente.ensu.domain.model.AttachmentDownloadStatus
+import io.ente.ensu.domain.model.AttachmentType
import io.ente.ensu.domain.model.ChatMessage
+import io.ente.ensu.domain.model.MaxImageAttachmentsPerMessage
import io.ente.ensu.domain.state.AppState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -39,18 +41,27 @@ internal class AttachmentStoreActions(
}
fun addAttachment(attachment: Attachment) {
- if (state.value.chat.isGenerating ||
- state.value.chat.isDownloading ||
- state.value.chat.isAttachmentDownloadBlocked
- ) return
-
state.update { appState ->
- appState.copy(
- chat = appState.chat.copy(
- attachments = appState.chat.attachments + attachment,
- isProcessingAttachments = false
+ val chat = appState.chat
+ val imageLimitReached = attachment.type == AttachmentType.Image &&
+ chat.attachments.count { it.type == AttachmentType.Image } >=
+ MaxImageAttachmentsPerMessage
+ if (chat.isGenerating ||
+ chat.isDownloading ||
+ chat.isAttachmentDownloadBlocked ||
+ imageLimitReached
+ ) {
+ appState.copy(
+ chat = chat.copy(isProcessingAttachments = false)
)
- )
+ } else {
+ appState.copy(
+ chat = chat.copy(
+ attachments = chat.attachments + attachment,
+ isProcessingAttachments = false
+ )
+ )
+ }
}
}
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift
index 44bce41097..00cb778e3f 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift
@@ -449,6 +449,10 @@ final class ChatViewModel: ObservableObject {
@Published var isProcessingAttachments: Bool = false
@Published var draftText: String = ""
@Published var draftAttachments: [ChatAttachment] = []
+ private var draftImageAttachmentCount: Int {
+ draftAttachments.filter { $0.kind == .image }.count
+ }
+
@Published var editingMessageId: UUID?
@Published var downloadToast: DownloadToastState?
@Published var isModelDownloaded: Bool = false
@@ -1096,7 +1100,10 @@ final class ChatViewModel: ObservableObject {
}
func addImageAttachment(data: Data, fileName: String?) {
- guard !isGenerating && !isDownloading && !isAttachmentDownloadBlocked else { return }
+ guard !isGenerating,
+ !isDownloading,
+ !isAttachmentDownloadBlocked,
+ draftImageAttachmentCount < ChatAttachmentLimits.maxImagesPerMessage else { return }
isProcessingAttachments = true
Task.detached { [weak self] in
@@ -1114,6 +1121,11 @@ final class ChatViewModel: ObservableObject {
isUploading: false
)
await MainActor.run {
+ if self.draftImageAttachmentCount >= ChatAttachmentLimits.maxImagesPerMessage {
+ try? FileManager.default.removeItem(at: url)
+ self.isProcessingAttachments = false
+ return
+ }
self.draftAttachments.append(attachment)
self.isProcessingAttachments = false
self.prewarmImageInferenceIfDownloaded()
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift
index 858aecb7e8..73525bef4a 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift
@@ -48,6 +48,10 @@ struct MessageInputView: View {
return hasContent && !isDownloading && !isAttachmentDownloadBlocked && !voiceInputState.blocksSend
}
+ private var isImageAttachmentLimitReached: Bool {
+ attachments.filter { $0.kind == .image }.count >= ChatAttachmentLimits.maxImagesPerMessage
+ }
+
var body: some View {
ZStack(alignment: .bottomTrailing) {
VStack(alignment: .leading, spacing: EnsuSpacing.sm) {
@@ -162,7 +166,10 @@ struct MessageInputView: View {
#endif
if EnsuFeatureFlags.enableImageUploads && editingMessage == nil {
- let canUseAttachment = !isGenerating && !isDownloading && !isAttachmentDownloadBlocked
+ let canUseAttachment = !isGenerating &&
+ !isDownloading &&
+ !isAttachmentDownloadBlocked &&
+ !isImageAttachmentLimitReached
PhotosPicker(selection: $selectedPhotoItem, matching: .images) {
Image("Upload01Icon")
.resizable()
@@ -173,7 +180,9 @@ struct MessageInputView: View {
.disabled(!canUseAttachment)
.foregroundStyle(canUseAttachment ? EnsuColor.textPrimary : EnsuColor.textMuted)
.simultaneousGesture(TapGesture().onEnded {
- hapticTap()
+ if canUseAttachment {
+ hapticTap()
+ }
})
#if os(macOS)
.buttonStyle(.plain)
@@ -181,6 +190,10 @@ struct MessageInputView: View {
#endif
.onChange(of: selectedPhotoItem) { newItem in
guard let newItem else { return }
+ guard !isImageAttachmentLimitReached else {
+ selectedPhotoItem = nil
+ return
+ }
Task {
let data = try? await newItem.loadTransferable(type: Data.self)
await MainActor.run {
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/Models/ChatModels.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/Models/ChatModels.swift
index f7fae6c2b8..c3172d2262 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/Models/ChatModels.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/Models/ChatModels.swift
@@ -41,6 +41,10 @@ struct ChatAttachment: Identifiable, Equatable {
}
}
+enum ChatAttachmentLimits {
+ static let maxImagesPerMessage = 2
+}
+
struct AttachmentDownloadItem: Identifiable, Equatable {
enum Status {
case queued
diff --git a/web/apps/ensu/src/components/chat/ChatComposer.tsx b/web/apps/ensu/src/components/chat/ChatComposer.tsx
index 2372a7d403..41e49b0ab2 100644
--- a/web/apps/ensu/src/components/chat/ChatComposer.tsx
+++ b/web/apps/ensu/src/components/chat/ChatComposer.tsx
@@ -71,6 +71,7 @@ export interface ChatComposerProps {
closeAttachmentMenu: () => void;
handleAttachmentChoice: (choice: "image" | "document") => void;
showImageAttachment: boolean;
+ isImageAttachmentLimitReached: boolean;
getDocumentInputProps: () => React.InputHTMLAttributes<HTMLInputElement>;
getImageInputProps: () => React.InputHTMLAttributes<HTMLInputElement>;
actionButtonSx: SxProps<Theme>;
@@ -112,6 +113,7 @@ export const ChatComposer = memo(
closeAttachmentMenu,
handleAttachmentChoice,
showImageAttachment,
+ isImageAttachmentLimitReached,
getDocumentInputProps,
getImageInputProps,
actionButtonSx,
@@ -127,6 +129,10 @@ export const ChatComposer = memo(
!input.trim() &&
pendingDocuments.length === 0 &&
pendingImages.length === 0);
+ const disableAttachmentButton =
+ isGenerating ||
+ isDownloading ||
+ (showImageAttachment && isImageAttachmentLimitReached);
const pendingImagePreviewRow =
pendingImages.length > 0 ? (
@@ -554,8 +560,7 @@ export const ChatComposer = memo(
aria-label="Add attachment"
sx={drawerIconButtonSx}
disabled={
- isGenerating ||
- isDownloading
+ disableAttachmentButton
}
onClick={openAttachmentMenu}
>
@@ -666,6 +671,7 @@ export const ChatComposer = memo(
>
{showImageAttachment && (
<MenuItem
+ disabled={isImageAttachmentLimitReached}
onClick={() =>
handleAttachmentChoice("image")
}
diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsx
index 7e0cdbb4db..6cc3cf7725 100644
--- a/web/apps/ensu/src/pages/chat.tsx
+++ b/web/apps/ensu/src/pages/chat.tsx
@@ -169,6 +169,7 @@ const IMAGE_SELECTOR_EXTENSIONS = [
const IMAGE_SELECTOR_ACCEPT = IMAGE_SELECTOR_EXTENSIONS.map(
(ext) => `.${ext}`,
).join(",");
+const MAX_IMAGE_ATTACHMENTS_PER_MESSAGE = 2;
const buildPromptWithImages = (text: string, imageCount: number) => {
if (imageCount <= 0) return text;
@@ -496,6 +497,7 @@ const Page: React.FC = () => {
bgcolor: "transparent",
color: "text.base",
"&:hover": { bgcolor: "fill.faint" },
+ "&.Mui-disabled": { color: "text.faint" },
} as const;
const smallIconProps = { size: 24, strokeWidth: 2 } as const;
const actionIconProps = { size: 24, strokeWidth: 2 } as const;
@@ -3499,10 +3501,16 @@ const Page: React.FC = () => {
onSelect: handleDocumentSelect,
onCancel: handleDocumentCancel,
});
+ const imageAttachmentSlotsRemaining = Math.max(
+ 0,
+ MAX_IMAGE_ATTACHMENTS_PER_MESSAGE - pendingImages.length,
+ );
+ const isImageAttachmentLimitReached = imageAttachmentSlotsRemaining === 0;
const handleImageSelect = useCallback(
(files: File[]) => {
closeAttachmentMenu();
+ if (imageAttachmentSlotsRemaining <= 0) return;
const images = files.map((file) => ({
id: createAttachmentId(),
name: file.name.replace(/\0/g, ""),
@@ -3510,10 +3518,17 @@ const Page: React.FC = () => {
file,
}));
if (images.length) {
- setPendingImages((prev) => [...prev, ...images]);
+ setPendingImages((prev) => {
+ const slotsRemaining = Math.max(
+ 0,
+ MAX_IMAGE_ATTACHMENTS_PER_MESSAGE - prev.length,
+ );
+ if (slotsRemaining === 0) return prev;
+ return [...prev, ...images.slice(0, slotsRemaining)];
+ });
}
},
- [closeAttachmentMenu],
+ [closeAttachmentMenu, imageAttachmentSlotsRemaining],
);
const handleImageCancel = useCallback(() => {
@@ -3532,6 +3547,7 @@ const Page: React.FC = () => {
const openTauriImageSelector = useCallback(async () => {
closeAttachmentMenu();
+ if (imageAttachmentSlotsRemaining <= 0) return;
try {
const selection = await openFileDialog({
directory: false,
@@ -3554,10 +3570,14 @@ const Page: React.FC = () => {
handleImageCancel();
return;
}
+ const pathsToProcess = selectedPaths.slice(
+ 0,
+ imageAttachmentSlotsRemaining,
+ );
const { invoke } = await import("@tauri-apps/api/tauri");
const files = await Promise.all(
- selectedPaths.map(async (selectedPath) => {
+ pathsToProcess.map(async (selectedPath) => {
const normalized = selectedPath.replace(/\\/g, "/");
const name =
normalized.split("/").pop()?.replace(/\0/g, "") ||
@@ -3597,6 +3617,7 @@ const Page: React.FC = () => {
closeAttachmentMenu,
handleImageCancel,
handleImageSelect,
+ imageAttachmentSlotsRemaining,
prewarmSelectedImageInference,
showMiniDialog,
]);
@@ -3605,6 +3626,7 @@ const Page: React.FC = () => {
(_event: React.MouseEvent<HTMLElement>) => {
closeAttachmentMenu();
if (showImageAttachment) {
+ if (isImageAttachmentLimitReached) return;
if (isTauriRuntime) {
void openTauriImageSelector();
} else {
@@ -3617,6 +3639,7 @@ const Page: React.FC = () => {
[
closeAttachmentMenu,
isTauriRuntime,
+ isImageAttachmentLimitReached,
openDocumentSelector,
openImageSelector,
openTauriImageSelector,
@@ -3626,15 +3649,20 @@ const Page: React.FC = () => {
const handleAttachmentChoice = useCallback(
(choice: "image" | "document") => {
+ closeAttachmentMenu();
if (choice === "image") {
- closeAttachmentMenu();
+ if (isImageAttachmentLimitReached) return;
openImageSelector();
} else {
- closeAttachmentMenu();
openDocumentSelector();
}
},
- [closeAttachmentMenu, openDocumentSelector, openImageSelector],
+ [
+ closeAttachmentMenu,
+ isImageAttachmentLimitReached,
+ openDocumentSelector,
+ openImageSelector,
+ ],
);
const removePendingDocument = useCallback((id: string) => {
@@ -4189,6 +4217,9 @@ const Page: React.FC = () => {
closeAttachmentMenu={closeAttachmentMenu}
handleAttachmentChoice={handleAttachmentChoice}
showImageAttachment={showImageAttachment}
+ isImageAttachmentLimitReached={
+ isImageAttachmentLimitReached
+ }
getDocumentInputProps={getDocumentInputProps}
getImageInputProps={getImageInputProps}
actionButtonSx={actionButtonSx}