Adopt b2b.next's client patterns: services, page state, feedback
Ported from the owner's b2b.next and adapted from MudBlazor to the daisyUI
components:
- LocalizableComponentBase provides CascadingState, Navigation, Toast,
IsLoading/IsDefaultDisabled, WhenOnline/StartOnlinePolling, and after-render
jobs that actually run.
- CascadingState starts with initialised state and polls the online status
with a PeriodicTimer. It reports errors through ILoggingService plus a toast,
and disposes what it holds.
- New pieces:
- ToastService + ToastHost, the snackbar, with AddResponseError/Success/
Warning/Exception in SUtility;
- ErrorViewer, which shows the validation messages plus AdditionalError(s);
- LoggingService, which writes to the console and the IndexedDB client log.
- HttpService is rebuilt and registered:
- auth levels Get/Post/Delete, *Anon and *TotallyAnon;
- retry with backoff;
- classified failures (FailureCodes) instead of exceptions;
- HTTP 410 on cancel;
- the "default" client.
ReadWebResult and DefaultReadFromJsonAsync read its responses.
- ClientJsonContext is source-generated, and SUtility.DefaultSerializer and
Blazored.LocalStorage use it.
- WebResult gains a C# 14 IsInvalid extension property.
The code follows the imported rules:
- Every service and async helper takes a CancellationToken last.
- Primary constructors replace constructor-plus-fields.
- MessagesService and IStorage return Task<WebResult> / Task<List<T>>.
MessagesService is still mock, building its results in memory; the seed
feed and thread moved to Faker.
- Index and ExpandMessage follow the page pattern: an offline guard with
WhenOnline, LoadData, and early returns with toasts on the results they used
to ignore. ExpandMessage reloads on route change and shows "not found"
instead of a null message.
- Model and state collections are [], and the fake identity defaults moved
out of the models.
- Components:
- no ?. chains over initialised state;
- [Inject] instead of @inject;
- IsDefaultDisabled on the actions;
- autocomplete on every text input;
- Localizer[key, args] instead of string.Format;
- the localizer no longer leaks its inner _pLocalizer.
- Settings → General gains a language picker (English/Italiano), and
SetDefaultCulture limits the culture to those two.
Bugs fixed along the way:
- A compose with untouched (null) content passed the empty check.
- File errors were joined with <br/> and rendered as markup.
- ClientLogs.ErrorMessage() threw on warnings.
- CoalescingStringLocalizer[key, args] showed a raw "{0}" for missing keys.
- AppStatusService exposed a call to a JS function that doesn't exist.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
1 parent
ca060f25fb
commit
95428430c0
55 files changed
+1844
-1337
No files matched your search
+79
-65
@@ -1,74 +1,88 @@
|
||||
using decePubClient.Models;
|
||||
using collAnon.Client.Services;
|
||||
|
||||
using decePubClient.Extensions;
|
||||
using decePubClient.Models;
|
||||
using decePubClient.Models.Types;
|
||||
|
||||
using PrivaPub.ClientModels;
|
||||
|
||||
namespace decePubClient.Services
|
||||
{
|
||||
public class MessagesService
|
||||
/// <summary>
|
||||
/// Message actions. Still a mock: the app isn't wired to PrivaPub yet, so each method builds its result in memory.
|
||||
/// The signatures are the real ones (a Task<WebResult> whose Data is the updated entity), so wiring means
|
||||
/// calling httpService against PrivaPub's /clientapi/post/* routes inside the same try/catch, reading failures with
|
||||
/// ReadWebResult and the body with DefaultReadFromJsonAsync, as b2b.next's StoreService does.
|
||||
/// </summary>
|
||||
public class MessagesService(ILoggingService logger, CoalescingStringLocalizer localizer)
|
||||
{
|
||||
readonly IStorage storage;
|
||||
readonly HttpClient http;
|
||||
/// <summary>Posts a message or, with RootMessageId set, a reply; Data is the new Message.</summary>
|
||||
public Task<WebResult> SubmitMessage(MessageForm messageForm, User author, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(SubmitMessage), () => (object)new Message
|
||||
{
|
||||
RootMessageId = messageForm.RootMessageId,
|
||||
MessageId = Guid.NewGuid().ToString(),
|
||||
Content = messageForm.Content?.ParseContent(messageForm.ContentType),
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
MessageType = messageForm.MessageType,
|
||||
Medias = [.. messageForm.Media.Select(m => new Media
|
||||
{
|
||||
FileName = m.FileName,
|
||||
AltText = m.AltText,
|
||||
ContentType = m.ContentType,
|
||||
Url = m.Base64Preview,
|
||||
Blob = m.Blob
|
||||
})],
|
||||
Title = messageForm.Title,
|
||||
User = author.ToMessageUser()
|
||||
}, cancellationToken);
|
||||
|
||||
public MessagesService(IHttpClientFactory clientFactory, IStorage storage)
|
||||
/// <summary>Opens a reply of the given scope to a message; Data is the message.</summary>
|
||||
public Task<WebResult> ReplyMessage(Message messageToReply, MessageType messageType = MessageType.Public, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(ReplyMessage), () => messageToReply, cancellationToken);
|
||||
|
||||
/// <summary>Boosts the message, or takes the boost back; Data is the message with its counter updated.</summary>
|
||||
public Task<WebResult> BoostUnboostMessage(Message message, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(BoostUnboostMessage), () =>
|
||||
{
|
||||
message.BoostsCounter += message.IsBoostedByCurrentUser ? -1 : 1;
|
||||
message.IsBoostedByCurrentUser = !message.IsBoostedByCurrentUser;
|
||||
return message;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>Adds the message to the favourites, or removes it; Data is the updated message.</summary>
|
||||
public Task<WebResult> FavouriteUnfavouriteMessage(Message message, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(FavouriteUnfavouriteMessage), () =>
|
||||
{
|
||||
message.IsFavourite = !message.IsFavourite;
|
||||
return message;
|
||||
}, cancellationToken);
|
||||
|
||||
public Task<WebResult> DeleteMessage(Message message, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(DeleteMessage), () => message, cancellationToken);
|
||||
|
||||
public Task<WebResult> BlockUserFromMessage(MessageUser messageUser, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(BlockUserFromMessage), () => messageUser, cancellationToken);
|
||||
|
||||
public Task<WebResult> SilenceUserFromMessage(MessageUser messageUser, CancellationToken cancellationToken = default) =>
|
||||
Run(nameof(SilenceUserFromMessage), () => messageUser, cancellationToken);
|
||||
|
||||
/// <summary>The mock's stand-in for an API call: the same success/failure handling, with the outcome built locally.</summary>
|
||||
async Task<WebResult> Run(string caller, Func<object> action, CancellationToken cancellationToken)
|
||||
{
|
||||
this.storage = storage;
|
||||
http = clientFactory.CreateClient("default");
|
||||
}
|
||||
|
||||
//Covers also direct message
|
||||
public async Task<Message> SubmitMessage(MessageForm messageForm)
|
||||
{
|
||||
//TODO
|
||||
|
||||
await Task.Run(() => {});
|
||||
return new();
|
||||
}
|
||||
|
||||
//Covers also direct message
|
||||
public async Task<Message> ReplyMessage(Message messageToReply, MessageType messageType = MessageType.Public)
|
||||
{
|
||||
//TODO
|
||||
|
||||
await Task.Run(() => {});
|
||||
return messageToReply;
|
||||
}
|
||||
|
||||
public async Task<Message> BoostUnboostMessage(Message messageToReply)
|
||||
{
|
||||
//TODO
|
||||
|
||||
await Task.Run(() => {});
|
||||
return messageToReply;
|
||||
}
|
||||
|
||||
public async Task<Message> FavouriteUnfavouriteMessage(Message messageToReply)
|
||||
{
|
||||
//TODO
|
||||
|
||||
await Task.Run(() => {});
|
||||
return messageToReply;
|
||||
}
|
||||
|
||||
public async Task<Message> DeleteMessage(Message messageToReply)
|
||||
{
|
||||
//TODO
|
||||
|
||||
await Task.Run(() => {});
|
||||
return messageToReply;
|
||||
}
|
||||
|
||||
public async Task<MessageUser> BlockUserFromMessage(MessageUser messageUser)
|
||||
{
|
||||
//TODO
|
||||
await Task.Run(() => {});
|
||||
return messageUser;
|
||||
}
|
||||
|
||||
public async Task<MessageUser> SilenceUserFromMessage(MessageUser messageUser)
|
||||
{
|
||||
//TODO
|
||||
|
||||
await Task.Run(() => {});
|
||||
return messageUser;
|
||||
var result = new WebResult();
|
||||
try
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await Task.Yield();
|
||||
result.Data = action();
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await logger.ProcessError(ex, $"{nameof(MessagesService)}.{caller}", cancellationToken);
|
||||
return result.Invalidate(localizer["The action could not be completed."], exception: ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user