From a3479308de2d304382500dfda2fe5aa8d46b5a01 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 18 Jul 2023 17:45:32 +0200 Subject: [PATCH] Fix mentions for long text and fix some text --- backend/i18n/frontend_en.json | 2 + backend/i18n/frontend_fr.json | 2 + backend/i18n/frontend_it.json | 2 + backend/i18n/frontend_nl.json | 2 + backend/i18n/frontend_pt.json | 2 + backend/i18n/frontend_zh.json | 2 + backend/i18n/source/frontend_en.json | 2 + .../DomainObject/CommentsCommandMiddleware.cs | 40 ++++++++++--------- .../CommentsCommandMiddlewareTests.cs | 20 ++++++++++ .../shared/forms/chat-dialog.component.html | 2 +- .../shared/forms/chat-dialog.component.scss | 2 +- .../shared/forms/chat-dialog.component.ts | 4 +- 12 files changed, 60 insertions(+), 22 deletions(-) diff --git a/backend/i18n/frontend_en.json b/backend/i18n/frontend_en.json index a0b4ad69e..f4243976a 100644 --- a/backend/i18n/frontend_en.json +++ b/backend/i18n/frontend_en.json @@ -166,6 +166,8 @@ "backups.started": "Backup started, it can take several minutes to complete.", "backups.startedLabel": "Started", "backups.startFailed": "Failed to start backup.", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/i18n/frontend_fr.json b/backend/i18n/frontend_fr.json index aa458b532..4f0dfd112 100644 --- a/backend/i18n/frontend_fr.json +++ b/backend/i18n/frontend_fr.json @@ -166,6 +166,8 @@ "backups.started": "La sauvegarde a commencé, cela peut prendre plusieurs minutes.", "backups.startedLabel": "Commencé", "backups.startFailed": "Échec du démarrage de la sauvegarde.", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/i18n/frontend_it.json b/backend/i18n/frontend_it.json index 68a8e3d5c..c7df4de6f 100644 --- a/backend/i18n/frontend_it.json +++ b/backend/i18n/frontend_it.json @@ -166,6 +166,8 @@ "backups.started": "Backup avviato, il suo completamento potrebbe richiedere alcuni minuti.", "backups.startedLabel": "Avviato", "backups.startFailed": "Non è stato possibile avviare il backup.", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/i18n/frontend_nl.json b/backend/i18n/frontend_nl.json index db4c22ee4..1878c26cc 100644 --- a/backend/i18n/frontend_nl.json +++ b/backend/i18n/frontend_nl.json @@ -166,6 +166,8 @@ "backups.started": "Back-up gestart, het kan enkele minuten duren om te voltooien.", "backups.startedLabel": "Gestart", "backups.startFailed": "Starten van back-up is mislukt.", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/i18n/frontend_pt.json b/backend/i18n/frontend_pt.json index 55c1775f5..2ba7970b9 100644 --- a/backend/i18n/frontend_pt.json +++ b/backend/i18n/frontend_pt.json @@ -166,6 +166,8 @@ "backups.started": "O reforço começou, pode levar vários minutos para ser concluído.", "backups.startedLabel": "Começou", "backups.startFailed": "Falhou em começar o backup.", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/i18n/frontend_zh.json b/backend/i18n/frontend_zh.json index 9fd9c0339..bacd27ee6 100644 --- a/backend/i18n/frontend_zh.json +++ b/backend/i18n/frontend_zh.json @@ -166,6 +166,8 @@ "backups.started": "备份已开始,可能需要几分钟才能完成。", "backups.startedLabel": "开始", "backups.startFailed": "启动备份失败。", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/i18n/source/frontend_en.json b/backend/i18n/source/frontend_en.json index a0b4ad69e..f4243976a 100644 --- a/backend/i18n/source/frontend_en.json +++ b/backend/i18n/source/frontend_en.json @@ -166,6 +166,8 @@ "backups.started": "Backup started, it can take several minutes to complete.", "backups.startedLabel": "Started", "backups.startFailed": "Failed to start backup.", + "chat.answers": "Answers", + "chat.answersEmpty": "The ChatBot does not provide an answer or has not been configured yet.", "chat.ask": "Ask", "chat.description": "Use the ChatBot (usually OpenAI) to generate content. Just write a prompt and describe, what you need.\n\n\nAlso add the desired format (for example Markdown or HTML) to your prompt, dependending on the editor that you use.", "chat.prompt": "Describe the content you want to generate", diff --git a/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsCommandMiddleware.cs b/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsCommandMiddleware.cs index a82e00f49..3b2239dab 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsCommandMiddleware.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsCommandMiddleware.cs @@ -14,7 +14,7 @@ namespace Squidex.Domain.Apps.Entities.Comments.DomainObject; public sealed class CommentsCommandMiddleware : AggregateCommandMiddleware { - private static readonly Regex MentionRegex = new Regex(@"@(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*", RegexOptions.Compiled | RegexOptions.ExplicitCapture, TimeSpan.FromMilliseconds(100)); + private static readonly Regex MentionRegex = new Regex(@"@(?=.{1,64}@)[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*", RegexOptions.Compiled | RegexOptions.ExplicitCapture, TimeSpan.FromMilliseconds(100)); private readonly IUserResolver userResolver; public CommentsCommandMiddleware(IDomainObjectFactory domainObjectFactory, IUserResolver userResolver) @@ -44,29 +44,33 @@ public sealed class CommentsCommandMiddleware : AggregateCommandMiddleware x.Value[1..]).ToArray(); + return; + } - if (emails.Length > 0) - { - var mentions = new List(); + var emails = MentionRegex.Matches(command.Text).Select(x => x.Value[1..]).ToArray(); + + if (emails.Length == 0) + { + return; + } - foreach (var email in emails) - { - var user = await userResolver.FindByIdOrEmailAsync(email); + var mentions = new List(); - if (user != null) - { - mentions.Add(user.Id); - } - } + foreach (var email in emails) + { + var user = await userResolver.FindByIdOrEmailAsync(email); - if (mentions.Count > 0) - { - command.Mentions = mentions.ToArray(); - } + if (user != null) + { + mentions.Add(user.Id); } } + + if (mentions.Count > 0) + { + command.Mentions = mentions.ToArray(); + } } } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs index 0a609eab8..56355d997 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using LoremNET; using Squidex.Domain.Apps.Core.TestHelpers; using Squidex.Domain.Apps.Entities.Comments.Commands; using Squidex.Domain.Apps.Entities.TestHelpers; @@ -76,6 +77,25 @@ public class CommentsCommandMiddlewareTests : GivenContext Assert.Equal(command.Mentions, new[] { "id1", "id2" }); } + [Fact] + public async Task Should_enrich_with_mentioned_user_ids_if_found_and_long_text() + { + SetupUser("id1", "mail1@squidex.io"); + SetupUser("id2", "mail2@squidex.io"); + + var command = CreateCommentsCommand(new CreateComment + { + Text = $"Hi @mail1@squidex.io, @mail2@squidex.io and @notfound@squidex.io {Lorem.Paragraph(200, 10)}", + IsMention = false + }); + + var context = CrateCommandContext(command); + + await sut.HandleAsync(context, CancellationToken); + + Assert.Equal(command.Mentions, new[] { "id1", "id2" }); + } + [Fact] public async Task Should_not_invoke_commands_for_mentioned_users() { diff --git a/frontend/src/app/features/content/shared/forms/chat-dialog.component.html b/frontend/src/app/features/content/shared/forms/chat-dialog.component.html index 1a4309e04..bace71da3 100644 --- a/frontend/src/app/features/content/shared/forms/chat-dialog.component.html +++ b/frontend/src/app/features/content/shared/forms/chat-dialog.component.html @@ -31,7 +31,7 @@

{{ 'chat.answers' | sqxTranslate }}

- {{ 'chat.noAnswers' | sqxTranslate }} + {{ 'chat.answersEmpty' | sqxTranslate }}
diff --git a/frontend/src/app/features/content/shared/forms/chat-dialog.component.scss b/frontend/src/app/features/content/shared/forms/chat-dialog.component.scss index 2ad9ae3f0..573bc47d6 100644 --- a/frontend/src/app/features/content/shared/forms/chat-dialog.component.scss +++ b/frontend/src/app/features/content/shared/forms/chat-dialog.component.scss @@ -2,5 +2,5 @@ @import 'vars'; textarea { - height: 100px; + height: 300px; } \ No newline at end of file diff --git a/frontend/src/app/features/content/shared/forms/chat-dialog.component.ts b/frontend/src/app/features/content/shared/forms/chat-dialog.component.ts index bbae2db7e..3171a7cca 100644 --- a/frontend/src/app/features/content/shared/forms/chat-dialog.component.ts +++ b/frontend/src/app/features/content/shared/forms/chat-dialog.component.ts @@ -52,10 +52,10 @@ export class ChatDialogComponent extends StatefulComponent { this.translator.ask(this.appsState.appName, { prompt: this.snapshot.chatQuestion }) .subscribe({ next: chatAnswers => { - this.next({ chatAnswers }); + this.next({ chatAnswers, isRunning: false }); }, error: () => { - this.next({ chatAnswers: [] }); + this.next({ chatAnswers: [], isRunning: false }); }, complete: () => { this.next({ isRunning: false });