This commit is contained in:
john 2025-05-27 00:28:10 +02:00
parent def7767b94
commit 8ad4302ec8
13 changed files with 3902 additions and 4 deletions

View file

@ -3,4 +3,4 @@ using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record GetAllPublicPostsResponse(IEnumerable<PostDto> Posts, Guid? Next);
public record LoadPostsResponse(IEnumerable<PostDto> Posts, Guid? Next);

View file

@ -8,5 +8,6 @@ public record PostDto(
Guid PostId,
string Content,
IEnumerable<PostMediaDto> Media,
IEnumerable<PostReactionDto> Reactions,
DateTimeOffset CreatedAt
);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Posts.Dto;
public record PostReactionDto(Guid ReactionId, string Emoji, int Count, bool DidReact);

View file

@ -14,7 +14,7 @@ namespace Femto.Api.Controllers.Posts;
public class PostsController(IBlogModule blogModule, ICurrentUserContext currentUserContext) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<GetAllPublicPostsResponse>> LoadPosts(
public async Task<ActionResult<LoadPostsResponse>> LoadPosts(
[FromQuery] GetPublicPostsSearchParams searchParams,
CancellationToken cancellationToken
)
@ -30,12 +30,13 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
cancellationToken
);
return new GetAllPublicPostsResponse(
return new LoadPostsResponse(
res.Posts.Select(p => new PostDto(
new PostAuthorDto(p.Author.AuthorId, p.Author.Username),
p.PostId,
p.Text,
p.Media.Select(m => new PostMediaDto(m.Url, m.Width, m.Height)),
p.Reactions?.Select(r => new PostReactionDto(r.ReactionId, r.Emoji, r.Count, r.DidReact)),
p.CreatedAt
)),
res.Next

View file

@ -0,0 +1,12 @@
-- Migration: AddReactions
-- Created at: 26/05/2025 22:00:32
ALTER TABLE blog.post
ADD COLUMN possible_reactions TEXT;
CREATE TABLE blog.post_reaction
(
post_id uuid REFERENCES blog.post(id),
author_id uuid REFERENCES blog.author(id),
emoji text not null
);

View file

@ -10,5 +10,6 @@ internal class PostConfiguration : IEntityTypeConfiguration<Post>
{
table.ToTable("post");
table.OwnsMany(post => post.Media).WithOwner();
table.OwnsMany(post => post.Reactions).WithOwner();
}
}

View file

@ -1,3 +1,9 @@
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
public record PostDto(Guid PostId, string Text, IList<PostMediaDto> Media, DateTimeOffset CreatedAt, PostAuthorDto Author);
public record PostDto(
Guid PostId,
string Text,
IList<PostMediaDto> Media,
DateTimeOffset CreatedAt,
PostAuthorDto Author,
IList<PostReactionDto> Reactions);

View file

@ -0,0 +1,3 @@
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
public record PostReactionDto(string Emoji, int Count, bool DidReact);

View file

@ -1,5 +1,6 @@
using Femto.Common.Domain;
using Femto.Modules.Blog.Domain.Posts.Events;
using Femto.Modules.Blog.Emoji;
namespace Femto.Modules.Blog.Domain.Posts;
@ -9,6 +10,8 @@ internal class Post : Entity
public Guid AuthorId { get; private set; }
public string Content { get; private set; } = null!;
public IList<PostMedia> Media { get; private set; }
public IList<PostReaction> Reactions { get; private set; } = [];
public bool IsPublic { get; set; }
private Post() { }
@ -19,7 +22,13 @@ internal class Post : Entity
this.AuthorId = authorId;
this.Content = content;
this.Media = media;
this.Reactions = AllEmoji
.GetRandomEmoji(5)
.Select(emoji => new PostReaction(emoji, 0))
.ToList();
this.AddDomainEvent(new PostCreated(this));
}
}

View file

@ -0,0 +1,17 @@
namespace Femto.Modules.Blog.Domain.Posts;
public class PostReaction
{
public Guid Id { get; private set; }
public string Emoji { get; private set; } = null!;
public int Count { get; private set; }
public PostReaction(string emoji, int count)
{
this.Id = Guid.CreateVersion7();
this.Emoji = emoji;
this.Count = count;
}
private PostReaction() { }
}

View file

@ -0,0 +1,18 @@
namespace Femto.Modules.Blog.Emoji;
internal static partial class AllEmoji
{
public static IList<string> GetRandomEmoji(int count) => new Random().TakeRandomly(Emojis).Distinct().Take(count).ToList();
}
internal static class RandomExtensions
{
public static IEnumerable<T> TakeRandomly<T>(this Random rand, ICollection<T> collection)
{
while (true)
{
var idx = rand.Next(collection.Count);
yield return collection.ElementAt(idx);
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,38 @@
try {
console.error('Downloading emoji-test.txt...');
const response = await fetch('https://unicode.org/Public/emoji/latest/emoji-test.txt');
const text = await response.text();
const emojis = extractEmojis(text);
console.error(`Extracted ${emojis.length} fully-qualified emojis.`);
console.log(`
namespace Femto.Modules.Blog.Emoji;
internal static partial class AllEmoji
{
public static readonly string[] Emojis = [\n${emojis.map(e => `"${e}"`).join(',\n')}\n];
}
`)
} catch (err) {
console.error('Error:', err);
}
function extractEmojis(text) {
const lines = text.split('\n');
const emojis = [];
for (const line of lines) {
if (line.startsWith('#') || line.trim() === '') continue;
const [codePart, descPart] = line.split(';');
if (!descPart || !descPart.includes('fully-qualified')) continue;
const match = line.match(/#\s+(.+?)\s+E\d+\.\d+/);
if (match) {
emojis.push(match[1]);
}
}
return emojis;
}