Report which dropped files were actually attached

This commit is contained in:
Thorsten Sommer committed 2026-09-13 17:07:38 +02:00
1 parent 8a735c2bbe
commit c69cc05d08
1 file changed
+45 -7
@@ -220,11 +220,31 @@ public partial class AttachDocuments : MSGComponentBase
/// Attaches what the user dropped on the zone of this component. /// Attaches what the user dropped on the zone of this component.
/// </summary> /// </summary>
/// <param name="paths">The dropped paths, in the order the runtime delivered them.</param> /// <param name="paths">The dropped paths, in the order the runtime delivered them.</param>
private async Task PathsDropped(List<string> paths) private async Task PathsDropped(List<string> paths) => await this.AttachDroppedPathsAsync(paths);
/// <summary>
/// Attaches dropped paths and reports which files it made of them.
/// </summary>
/// <remarks>
/// This is what the attachment dialogs call while they are open: a drop lands in the dialog the
/// user is looking at, yet only this component knows how to turn a path into an attachment. The
/// answer is what lets those dialogs show the result, see PathsDropped for our own zone.
/// </remarks>
/// <param name="paths">The dropped paths, in the order the runtime delivered them.</param>
/// <returns>The files this call attached, in the order they were dropped.</returns>
private async Task<IReadOnlyList<FileAttachment>> AttachDroppedPathsAsync(List<string> paths)
{ {
await this.AddFileBatchAsync(paths); var attached = await this.AddFileBatchAsync(paths);
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths); await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
await this.OnChange(this.DocumentPaths); await this.OnChange(this.DocumentPaths);
//
// A dialog reaches us through a delegate rather than through an event callback, so nothing
// renders this component afterwards. Without this, the number on the badge would stay at its
// old value until something else happens to render us.
//
this.StateHasChanged();
return attached;
} }
private async Task AddFilesManually() private async Task AddFilesManually()
@@ -307,8 +327,18 @@ public partial class AttachDocuments : MSGComponentBase
this.OwnerChat.PendingMediaTranscripts.RemoveAll(attachment => !retainedPaths.Contains(attachment.FilePath)); this.OwnerChat.PendingMediaTranscripts.RemoveAll(attachment => !retainedPaths.Contains(attachment.FilePath));
} }
private async Task AddFileBatchAsync(IEnumerable<string> paths) /// <summary>
/// Validates the given paths and attaches every file which passes.
/// </summary>
/// <param name="paths">The paths to attach, in the order they arrived.</param>
/// <returns>
/// The files this call attached, in the order they arrived. Media files are never among them:
/// they go to the transcription service and become attachments only once their transcript is
/// ready, which is long after this call has returned.
/// </returns>
private async Task<IReadOnlyList<FileAttachment>> AddFileBatchAsync(IEnumerable<string> paths)
{ {
var attached = new List<FileAttachment>();
var pathList = paths.ToList(); var pathList = paths.ToList();
if (this.AllowedFileTypes is { Length: > 0 }) if (this.AllowedFileTypes is { Length: > 0 })
{ {
@@ -356,18 +386,25 @@ public partial class AttachDocuments : MSGComponentBase
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(FileExtensionValidation.UseCase.ATTACHING_CONTENT, path, this.ValidateMediaFileTypes, this.Provider)) if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(FileExtensionValidation.UseCase.ATTACHING_CONTENT, path, this.ValidateMediaFileTypes, this.Provider))
continue; continue;
this.DocumentPaths.Add(FileAttachment.FromPath(path)); //
// This counts as attached even when the set already held the file: the user just
// dropped it, and whoever asked us wants to hear about the file they aimed at, not
// about whether it happened to be new to us.
//
var attachment = FileAttachment.FromPath(path);
this.DocumentPaths.Add(attachment);
attached.Add(attachment);
} }
if (mediaPaths.Count is 0) if (mediaPaths.Count is 0)
return; return attached;
if (string.IsNullOrWhiteSpace(this.SettingsManager.ConfigurationData.App.UseTranscriptionProvider)) if (string.IsNullOrWhiteSpace(this.SettingsManager.ConfigurationData.App.UseTranscriptionProvider))
{ {
await this.MessageBus.SendWarning(new( await this.MessageBus.SendWarning(new(
Icons.Material.Filled.VoiceChat, Icons.Material.Filled.VoiceChat,
this.T("Media files require a configured transcription provider. Configure one in the transcription settings."))); this.T("Media files require a configured transcription provider. Configure one in the transcription settings.")));
return; return attached;
} }
var names = string.Join('\n', mediaPaths.Select(path => $"- {Markdown.EscapeInlineText(Path.GetFileName(path))}")); var names = string.Join('\n', mediaPaths.Select(path => $"- {Markdown.EscapeInlineText(Path.GetFileName(path))}"));
@@ -391,7 +428,7 @@ public partial class AttachDocuments : MSGComponentBase
var dialogResult = await dialogReference.Result; var dialogResult = await dialogReference.Result;
if (dialogResult is null || dialogResult.Canceled) if (dialogResult is null || dialogResult.Canceled)
return; return attached;
if (this.OwnerChat is null) if (this.OwnerChat is null)
this.OwnerChat = await this.EnsureOwnerChatAsync(mediaPaths[0]); this.OwnerChat = await this.EnsureOwnerChatAsync(mediaPaths[0]);
@@ -408,6 +445,7 @@ public partial class AttachDocuments : MSGComponentBase
} }
this.MediaTranscriptionService.TryStartAttachmentBatch(mediaPaths, this.EffectiveMediaImportTarget, this.OwnerChat); this.MediaTranscriptionService.TryStartAttachmentBatch(mediaPaths, this.EffectiveMediaImportTarget, this.OwnerChat);
return attached;
} }
private static bool IsTranscribableMedia(string path) => FileTypes.IsAllowedPath(path, FileTypes.AUDIO) || FileTypes.IsAllowedPath(path, FileTypes.VIDEO); private static bool IsTranscribableMedia(string path) => FileTypes.IsAllowedPath(path, FileTypes.AUDIO) || FileTypes.IsAllowedPath(path, FileTypes.VIDEO);