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:
@@ -0,0 +1,31 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.DataTransferObjects.Attachments;
|
||||
using Ping.Api.Filters;
|
||||
using Ping.Application.Attachments;
|
||||
using Ping.Application.Attachments.Responses;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class AttachmentEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapAttachmentEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/attachments")
|
||||
.WithTags("Attachments")
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapPost("/upload-url", RequestUploadUrl)
|
||||
.WithName("RequestAttachmentUploadUrl")
|
||||
.WithSummary("Request A Presigned Url To Upload A File Or Voice Message Directly To Storage.")
|
||||
.AddEndpointFilter<ValidationFilter<RequestAttachmentUploadUrlRequest>>()
|
||||
.Produces<AttachmentUploadResponse>()
|
||||
.ProducesValidationProblem();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<AttachmentUploadResponse> RequestUploadUrl(
|
||||
IMediator mediator, CurrentUser currentUser, RequestAttachmentUploadUrlRequest request, CancellationToken cancellationToken) =>
|
||||
await mediator.Send(new RequestAttachmentUploadUrlCommand(currentUser.RequiredId, request.FileName, request.ContentType), cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.Extensions;
|
||||
using Ping.Application.Calls;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class CallEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapCallEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/channels/{channelId:guid}/call-token")
|
||||
.WithTags("Calls")
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapPost("/", RequestCallToken)
|
||||
.WithName("RequestCallToken")
|
||||
.WithSummary("Request A Livekit Access Token To Join The Voice/Video Call For A Channel.")
|
||||
.Produces<Application.Calls.Responses.CallTokenResponse>()
|
||||
.ProducesValidationProblem()
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> RequestCallToken(
|
||||
IMediator mediator, CurrentUser currentUser, Guid channelId, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new RequestCallTokenCommand(currentUser.RequiredId, channelId), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.DataTransferObjects.Channels;
|
||||
using Ping.Api.Extensions;
|
||||
using Ping.Api.Filters;
|
||||
using Ping.Application.Channels;
|
||||
using Ping.Application.Channels.Responses;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class ChannelEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapChannelEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var servers = app.MapGroup("/servers/{serverId:guid}/channels")
|
||||
.WithTags("Channels")
|
||||
.RequireAuthorization();
|
||||
|
||||
servers.MapGet("/", GetServerChannels)
|
||||
.WithName("GetServerChannels")
|
||||
.WithSummary("Get A Server'S Channels.")
|
||||
.Produces<IEnumerable<ChannelResponse>>()
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
servers.MapPost("/", CreateChannel)
|
||||
.WithName("CreateChannel")
|
||||
.WithSummary("Create A Text Or Voice Channel In A Server.")
|
||||
.AddEndpointFilter<ValidationFilter<CreateChannelRequest>>()
|
||||
.Produces<ChannelResponse>(StatusCodes.Status201Created)
|
||||
.ProducesValidationProblem()
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
var directMessages = app.MapGroup("/channels/dm")
|
||||
.WithTags("Channels")
|
||||
.RequireAuthorization();
|
||||
|
||||
directMessages.MapPost("/{otherUserId:long}", GetOrCreateDirectMessageChannel)
|
||||
.WithName("GetOrCreateDirectMessageChannel")
|
||||
.WithSummary("Get Or Create A Direct Message Channel With A Friend.")
|
||||
.Produces<ChannelResponse>()
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.ProducesValidationProblem();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetServerChannels(
|
||||
IMediator mediator, CurrentUser currentUser, Guid serverId, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new GetServerChannelsQuery(currentUser.RequiredId, serverId), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> CreateChannel(
|
||||
IMediator mediator, CurrentUser currentUser, Guid serverId, CreateChannelRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(
|
||||
new CreateChannelCommand(currentUser.RequiredId, serverId, request.Name, request.Type), cancellationToken);
|
||||
return result.ToCreatedResult(channel => $"/servers/{serverId}/channels/{channel.Id}");
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetOrCreateDirectMessageChannel(
|
||||
IMediator mediator, CurrentUser currentUser, long otherUserId, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(
|
||||
new GetOrCreateDirectMessageChannelCommand(currentUser.RequiredId, otherUserId), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.DataTransferObjects.Friends;
|
||||
using Ping.Api.Extensions;
|
||||
using Ping.Api.Filters;
|
||||
using Ping.Application.Friends;
|
||||
using Ping.Application.Friends.Responses;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class FriendEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapFriendEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/friends")
|
||||
.WithTags("Friends")
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapGet("/", GetFriends)
|
||||
.WithName("GetFriends")
|
||||
.WithSummary("Get The Current User'S Accepted Friends.")
|
||||
.Produces<IEnumerable<FriendshipResponse>>();
|
||||
|
||||
group.MapGet("/requests", GetPendingFriendRequests)
|
||||
.WithName("GetPendingFriendRequests")
|
||||
.WithSummary("Get Incoming And Outgoing Pending Friend Requests.")
|
||||
.Produces<IEnumerable<FriendshipResponse>>();
|
||||
|
||||
group.MapPost("/requests", SendFriendRequest)
|
||||
.WithName("SendFriendRequest")
|
||||
.WithSummary("Send A Friend Request.")
|
||||
.AddEndpointFilter<ValidationFilter<SendFriendRequestRequest>>()
|
||||
.Produces<FriendshipResponse>(StatusCodes.Status201Created)
|
||||
.ProducesValidationProblem()
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
.Produces(StatusCodes.Status409Conflict);
|
||||
|
||||
group.MapPost("/requests/{id:guid}/accept", AcceptFriendRequest)
|
||||
.WithName("AcceptFriendRequest")
|
||||
.WithSummary("Accept A Pending Friend Request.")
|
||||
.Produces<FriendshipResponse>()
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
group.MapPost("/requests/{id:guid}/reject", RejectFriendRequest)
|
||||
.WithName("RejectFriendRequest")
|
||||
.WithSummary("Reject A Pending Friend Request.")
|
||||
.Produces(StatusCodes.Status204NoContent)
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
group.MapDelete("/{id:guid}", RemoveFriend)
|
||||
.WithName("RemoveFriend")
|
||||
.WithSummary("Remove An Existing Friend.")
|
||||
.Produces(StatusCodes.Status204NoContent)
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
group.MapPost("/block/{userId:long}", BlockUser)
|
||||
.WithName("BlockUser")
|
||||
.WithSummary("Block A User.")
|
||||
.Produces(StatusCodes.Status204NoContent)
|
||||
.ProducesValidationProblem();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<FriendshipResponse>> GetFriends(
|
||||
IMediator mediator, CurrentUser currentUser, CancellationToken cancellationToken) =>
|
||||
await mediator.Send(new GetFriendsQuery(currentUser.RequiredId), cancellationToken);
|
||||
|
||||
private static async Task<IEnumerable<FriendshipResponse>> GetPendingFriendRequests(
|
||||
IMediator mediator, CurrentUser currentUser, CancellationToken cancellationToken) =>
|
||||
await mediator.Send(new GetPendingFriendRequestsQuery(currentUser.RequiredId), cancellationToken);
|
||||
|
||||
private static async Task<IResult> SendFriendRequest(
|
||||
IMediator mediator, CurrentUser currentUser, SendFriendRequestRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new SendFriendRequestCommand(currentUser.RequiredId, request.AddresseeId), cancellationToken);
|
||||
return result.ToCreatedResult(friendship => $"/friends/requests/{friendship.Id}");
|
||||
}
|
||||
|
||||
private static async Task<IResult> AcceptFriendRequest(
|
||||
IMediator mediator, CurrentUser currentUser, Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new AcceptFriendRequestCommand(currentUser.RequiredId, id), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> RejectFriendRequest(
|
||||
IMediator mediator, CurrentUser currentUser, Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new RejectFriendRequestCommand(currentUser.RequiredId, id), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> RemoveFriend(
|
||||
IMediator mediator, CurrentUser currentUser, Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new RemoveFriendCommand(currentUser.RequiredId, id), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> BlockUser(
|
||||
IMediator mediator, CurrentUser currentUser, long userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new BlockUserCommand(currentUser.RequiredId, userId), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.DataTransferObjects.Messages;
|
||||
using Ping.Api.Extensions;
|
||||
using Ping.Api.Filters;
|
||||
using Ping.Application.Messages;
|
||||
using Ping.Application.Messages.Responses;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class MessageEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapMessageEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var channelMessages = app.MapGroup("/channels/{channelId:guid}/messages")
|
||||
.WithTags("Messages")
|
||||
.RequireAuthorization();
|
||||
|
||||
channelMessages.MapGet("/", GetChannelMessages)
|
||||
.WithName("GetChannelMessages")
|
||||
.WithSummary("Get A Channel'S Messages, Newest First, Optionally Before A Given Message.")
|
||||
.Produces<IEnumerable<MessageResponse>>()
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
channelMessages.MapPost("/", SendMessage)
|
||||
.WithName("SendMessage")
|
||||
.WithSummary("Send A Message To A Channel.")
|
||||
.AddEndpointFilter<ValidationFilter<SendMessageRequest>>()
|
||||
.Produces<MessageResponse>(StatusCodes.Status201Created)
|
||||
.ProducesValidationProblem()
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
var messages = app.MapGroup("/messages")
|
||||
.WithTags("Messages")
|
||||
.RequireAuthorization();
|
||||
|
||||
messages.MapPatch("/{id:guid}", EditMessage)
|
||||
.WithName("EditMessage")
|
||||
.WithSummary("Edit A Message'S Content.")
|
||||
.AddEndpointFilter<ValidationFilter<EditMessageRequest>>()
|
||||
.Produces<MessageResponse>()
|
||||
.ProducesValidationProblem()
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
messages.MapDelete("/{id:guid}", DeleteMessage)
|
||||
.WithName("DeleteMessage")
|
||||
.WithSummary("Delete A Message (Soft Delete).")
|
||||
.Produces(StatusCodes.Status204NoContent)
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetChannelMessages(
|
||||
IMediator mediator, CurrentUser currentUser, Guid channelId, Guid? before, int take, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(
|
||||
new GetChannelMessagesQuery(currentUser.RequiredId, channelId, before, take == 0 ? 50 : take), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> SendMessage(
|
||||
IMediator mediator, CurrentUser currentUser, Guid channelId, SendMessageRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var attachments = (request.Attachments ?? [])
|
||||
.Select(a => new PendingAttachment(a.Key, a.FileName, a.ContentType, a.Kind))
|
||||
.ToList();
|
||||
|
||||
var result = await mediator.Send(
|
||||
new SendMessageCommand(currentUser.RequiredId, channelId, request.Content ?? string.Empty, request.ReplyToMessageId, attachments),
|
||||
cancellationToken);
|
||||
|
||||
return result.ToCreatedResult(message => $"/channels/{channelId}/messages/{message.Id}");
|
||||
}
|
||||
|
||||
private static async Task<IResult> EditMessage(
|
||||
IMediator mediator, CurrentUser currentUser, Guid id, EditMessageRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new EditMessageCommand(currentUser.RequiredId, id, request.Content), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> DeleteMessage(
|
||||
IMediator mediator, CurrentUser currentUser, Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new DeleteMessageCommand(currentUser.RequiredId, id), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.DataTransferObjects.Servers;
|
||||
using Ping.Api.Filters;
|
||||
using Ping.Application.Servers;
|
||||
using Ping.Application.Servers.Responses;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class ServerEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapServerEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/servers")
|
||||
.WithTags("Servers")
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapGet("/", GetUserServers)
|
||||
.WithName("GetUserServers")
|
||||
.WithSummary("Get The Servers The Current User Is A Member Of.")
|
||||
.Produces<IEnumerable<ServerResponse>>();
|
||||
|
||||
group.MapPost("/", CreateServer)
|
||||
.WithName("CreateServer")
|
||||
.WithSummary("Create A New Server, With The Current User As Owner.")
|
||||
.AddEndpointFilter<ValidationFilter<CreateServerRequest>>()
|
||||
.Produces<ServerResponse>(StatusCodes.Status201Created)
|
||||
.ProducesValidationProblem();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<ServerResponse>> GetUserServers(
|
||||
IMediator mediator, CurrentUser currentUser, CancellationToken cancellationToken) =>
|
||||
await mediator.Send(new GetUserServersQuery(currentUser.RequiredId), cancellationToken);
|
||||
|
||||
private static async Task<IResult> CreateServer(
|
||||
IMediator mediator, CurrentUser currentUser, CreateServerRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var server = await mediator.Send(new CreateServerCommand(currentUser.RequiredId, request.Name), cancellationToken);
|
||||
return Results.Created($"/servers/{server.Id}", server);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Mediator;
|
||||
using Ping.Api.Authentication;
|
||||
using Ping.Api.Extensions;
|
||||
using Ping.Application.Users;
|
||||
|
||||
namespace Ping.Api.Endpoints;
|
||||
|
||||
public static class UserEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapUserEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/users")
|
||||
.WithTags("Users")
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapGet("/me", GetCurrentUser)
|
||||
.WithName("GetCurrentUser")
|
||||
.WithSummary("Get The Currently Authenticated User.")
|
||||
.Produces<Application.Users.Responses.UserResponse>()
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
group.MapGet("/{id:long}", GetUserById)
|
||||
.WithName("GetUserById")
|
||||
.WithSummary("Get A User By Id.")
|
||||
.Produces<Application.Users.Responses.UserResponse>()
|
||||
.Produces(StatusCodes.Status404NotFound);
|
||||
|
||||
group.MapGet("/search", SearchUsers)
|
||||
.WithName("SearchUsers")
|
||||
.WithSummary("Search Users By Username, Excluding The Current User.")
|
||||
.Produces<IEnumerable<Application.Users.Responses.UserResponse>>();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetCurrentUser(IMediator mediator, CurrentUser currentUser, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new GetUserByIdQuery(currentUser.RequiredId), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetUserById(IMediator mediator, long id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await mediator.Send(new GetUserByIdQuery(id), cancellationToken);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<Application.Users.Responses.UserResponse>> SearchUsers(
|
||||
IMediator mediator, CurrentUser currentUser, string query, CancellationToken cancellationToken) =>
|
||||
await mediator.Send(new SearchUsersQuery(query, currentUser.RequiredId), cancellationToken);
|
||||
}
|
||||
Reference in New Issue
Block a user