Add Ping backend

.NET 10 solution for Ping, a Discord style chat app: Domain, Application
and Infrastructure projects plus the Api host. SignalR for chat and
presence, LiveKit for call tokens, EF Core on Postgres, S3 compatible
storage for attachments, and OIDC auth shared with the Musify Zitadel
instance. Includes the Compose stack for local development.
This commit is contained in:
2026-09-21 19:32:55 +02:00
commit 0623875dfa
126 changed files with 5048 additions and 0 deletions
@@ -0,0 +1,41 @@
using ErrorOr;
using Mediator;
using Microsoft.EntityFrameworkCore;
using Ping.Application.Contracts;
namespace Ping.Application.Messages
{
public record DeleteMessageCommand(long CurrentUserId, Guid MessageId) : ICommand<ErrorOr<Success>>;
public class DeleteMessageCommandHandler(IDatabase database, IRealtimeNotifier notifier)
: ICommandHandler<DeleteMessageCommand, ErrorOr<Success>>
{
public async ValueTask<ErrorOr<Success>> Handle(DeleteMessageCommand request, CancellationToken cancellationToken)
{
var message = await database.Messages
.FirstOrDefaultAsync(m => m.Id == request.MessageId && m.DeletedAt == null, cancellationToken);
if (message is null)
return Error.NotFound(description: $"Message {request.MessageId} not found");
if (message.AuthorId != request.CurrentUserId)
{
var isServerOwner = await database.Channels
.Where(c => c.Id == message.ChannelId && c.ServerId != null)
.Select(c => c.ServerId)
.Join(database.Servers, serverId => serverId, server => server.Id, (_, server) => server.OwnerId)
.AnyAsync(ownerId => ownerId == request.CurrentUserId, cancellationToken);
if (!isServerOwner)
return Error.Forbidden(description: "Only the author or the server owner can delete this message");
}
message.DeletedAt = DateTime.UtcNow;
message.Content = string.Empty;
await database.SaveChangesAsync(cancellationToken);
await notifier.MessageDeletedAsync(message.ChannelId, message.Id, cancellationToken);
return new Success();
}
}
}
@@ -0,0 +1,39 @@
using ErrorOr;
using Mediator;
using Microsoft.EntityFrameworkCore;
using Ping.Application.Contracts;
using Ping.Application.Messages.Responses;
namespace Ping.Application.Messages
{
public record EditMessageCommand(long CurrentUserId, Guid MessageId, string Content) : ICommand<ErrorOr<MessageResponse>>;
public class EditMessageCommandHandler(IDatabase database, IRealtimeNotifier notifier)
: ICommandHandler<EditMessageCommand, ErrorOr<MessageResponse>>
{
public async ValueTask<ErrorOr<MessageResponse>> Handle(EditMessageCommand request, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(request.Content))
return Error.Validation(description: "Message content cannot be empty");
var message = await database.Messages
.Include(m => m.Author)
.Include(m => m.Attachments)
.FirstOrDefaultAsync(m => m.Id == request.MessageId && m.DeletedAt == null, cancellationToken);
if (message is null)
return Error.NotFound(description: $"Message {request.MessageId} not found");
if (message.AuthorId != request.CurrentUserId)
return Error.Forbidden(description: "Only the author can edit this message");
message.Content = request.Content;
message.EditedAt = DateTime.UtcNow;
await database.SaveChangesAsync(cancellationToken);
var response = MessageResponse.FromEntity(message);
await notifier.MessageUpdatedAsync(message.ChannelId, response, cancellationToken);
return response;
}
}
}
@@ -0,0 +1,49 @@
using ErrorOr;
using Mediator;
using Microsoft.EntityFrameworkCore;
using Ping.Application.Contracts;
using Ping.Application.Messages.Responses;
using Ping.Application.Shared;
namespace Ping.Application.Messages
{
public record GetChannelMessagesQuery(long CurrentUserId, Guid ChannelId, Guid? Before, int Take)
: IQuery<ErrorOr<List<MessageResponse>>>;
public class GetChannelMessagesQueryHandler(IDatabase database)
: IQueryHandler<GetChannelMessagesQuery, ErrorOr<List<MessageResponse>>>
{
private const int MaxTake = 100;
public async ValueTask<ErrorOr<List<MessageResponse>>> Handle(GetChannelMessagesQuery request, CancellationToken cancellationToken)
{
var hasAccess = await database.HasAccessAsync(request.ChannelId, request.CurrentUserId, cancellationToken);
if (!hasAccess)
return Error.NotFound(description: $"Channel {request.ChannelId} not found");
var take = Math.Clamp(request.Take, 1, MaxTake);
var query = database.Messages.AsNoTracking()
.Include(m => m.Author)
.Include(m => m.Attachments)
.Where(m => m.ChannelId == request.ChannelId);
if (request.Before is { } beforeId)
{
var cursorCreatedAt = await database.Messages
.Where(m => m.Id == beforeId)
.Select(m => m.CreatedAt)
.FirstOrDefaultAsync(cancellationToken);
query = query.Where(m => m.CreatedAt < cursorCreatedAt);
}
var messages = await query
.OrderByDescending(m => m.CreatedAt)
.Take(take)
.ToListAsync(cancellationToken);
return messages.Select(MessageResponse.FromEntity).ToList();
}
}
}
@@ -0,0 +1,11 @@
using Ping.Domain.Entities;
using Ping.Domain.ValueObjects;
namespace Ping.Application.Messages.Responses
{
public record AttachmentResponse(Guid Id, string FileName, string ContentType, long SizeBytes, AttachmentKind Kind)
{
public static AttachmentResponse FromEntity(Attachment attachment) =>
new(attachment.Id, attachment.FileName, attachment.ContentType, attachment.SizeBytes, attachment.Kind);
}
}
@@ -0,0 +1,27 @@
using Ping.Application.Users.Responses;
using Ping.Domain.Entities;
namespace Ping.Application.Messages.Responses
{
public record MessageResponse(
Guid Id,
Guid ChannelId,
UserResponse Author,
string Content,
Guid? ReplyToMessageId,
DateTime CreatedAt,
DateTime? EditedAt,
IReadOnlyList<AttachmentResponse> Attachments)
{
public static MessageResponse FromEntity(Message message) =>
new(
message.Id,
message.ChannelId,
UserResponse.FromEntity(message.Author!),
message.Content,
message.ReplyToMessageId,
message.CreatedAt,
message.EditedAt,
message.Attachments.Select(AttachmentResponse.FromEntity).ToList());
}
}
@@ -0,0 +1,77 @@
using ErrorOr;
using Mediator;
using Microsoft.EntityFrameworkCore;
using Ping.Application.Configuration;
using Ping.Application.Contracts;
using Ping.Application.Messages.Responses;
using Ping.Application.Shared;
using Ping.Domain.Entities;
using Ping.Domain.ValueObjects;
namespace Ping.Application.Messages
{
public record PendingAttachment(string Key, string FileName, string ContentType, AttachmentKind Kind);
public record SendMessageCommand(
long AuthorId,
Guid ChannelId,
string Content,
Guid? ReplyToMessageId,
IReadOnlyList<PendingAttachment> Attachments)
: ICommand<ErrorOr<MessageResponse>>;
public class SendMessageCommandHandler(
IDatabase database,
IStorageService storageService,
ApplicationStorageConfiguration storageConfiguration,
IRealtimeNotifier notifier)
: ICommandHandler<SendMessageCommand, ErrorOr<MessageResponse>>
{
public async ValueTask<ErrorOr<MessageResponse>> Handle(SendMessageCommand request, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(request.Content) && request.Attachments.Count == 0)
return Error.Validation(description: "A message needs content or at least one attachment");
var hasAccess = await database.HasAccessAsync(request.ChannelId, request.AuthorId, cancellationToken);
if (!hasAccess)
return Error.NotFound(description: $"Channel {request.ChannelId} not found");
var message = new Message
{
Id = Guid.NewGuid(),
ChannelId = request.ChannelId,
AuthorId = request.AuthorId,
Content = request.Content,
ReplyToMessageId = request.ReplyToMessageId,
};
foreach (var pending in request.Attachments)
{
var metadata = await storageService.HeadObjectAsync(storageConfiguration.Bucket, pending.Key, cancellationToken);
if (metadata is null)
return Error.Validation(description: $"Attachment '{pending.FileName}' was not found in storage");
message.Attachments.Add(new Attachment
{
Id = Guid.NewGuid(),
MessageId = message.Id,
Bucket = storageConfiguration.Bucket,
Key = pending.Key,
FileName = pending.FileName,
ContentType = pending.ContentType,
SizeBytes = metadata.ContentLength,
Kind = pending.Kind,
});
}
await database.Messages.AddAsync(message, cancellationToken);
await database.SaveChangesAsync(cancellationToken);
message.Author = await database.Users.FirstAsync(u => u.Id == request.AuthorId, cancellationToken);
var response = MessageResponse.FromEntity(message);
await notifier.MessageCreatedAsync(request.ChannelId, response, cancellationToken);
return response;
}
}
}