Skip to content

Commit

Permalink
feat: generate notification of post's tag events
Browse files Browse the repository at this point in the history
  • Loading branch information
LeDuyNhan1201 committed Nov 13, 2024
1 parent 6b32ed2 commit 8b60a86
Show file tree
Hide file tree
Showing 59 changed files with 681 additions and 351 deletions.
4 changes: 4 additions & 0 deletions Application/Contracts/Commands/DomainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed record PostNotificationCommand : INotificationCommand

public Guid PostId { get; set; }

public DateTime CreatedAt { get; set; }

}

public sealed record CommentNotificationCommand : INotificationCommand
Expand All @@ -35,6 +37,8 @@ public sealed record CommentNotificationCommand : INotificationCommand

public Guid CommentId { get; set; }

public DateTime CreatedAt { get; set; }

}

}
Expand Down
2 changes: 2 additions & 0 deletions Application/Contracts/Messages/INotificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface INotificationCommand : IMessage

public string TriggeredBy { get; set; }

public DateTime CreatedAt { get; set; }

}
}
17 changes: 17 additions & 0 deletions Application/DTOs/Responses/Comment/PreviewCommentResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace InfinityNetServer.BuildingBlocks.Application.DTOs.Responses.Comment
{
public sealed record PreviewCommentResponse
{

public Guid Id { get; set; }

public Guid ProfileId { get; set; }

public Guid PostId { get; set; }

public string PreviewContent { get; set; }

}
}
2 changes: 1 addition & 1 deletion Application/DTOs/Responses/File/BaseMetadataResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace InfinityNetServer.BuildingBlocks.Application.DTOs.Responses.File
public abstract record BaseMetadataResponse
{

public string Id { get; set; }
public Guid Id { get; set; }

public string Name { get; set; }

Expand Down
15 changes: 15 additions & 0 deletions Application/DTOs/Responses/Post/PreviewPostResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace InfinityNetServer.BuildingBlocks.Application.DTOs.Responses.Post
{
public sealed record PreviewPostResponse
{

public Guid Id { get; set; }

public Guid OwnerId { get; set; }

public string PreviewContent { get; set; }

}
}
1 change: 1 addition & 0 deletions Application/Exceptions/BaseErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected BaseErrorCode(string code, string message)
public static readonly BaseErrorCode PROFILE_NOT_FOUND = new("common/profile-not-found", "profile_not_found");
public static readonly BaseErrorCode RELATIONSHIP_NOT_FOUND = new("common/relationship-not-found", "relationship_not_found");
public static readonly BaseErrorCode POST_NOT_FOUND = new("common/post-not-found", "post_not_found");
public static readonly BaseErrorCode COMMENT_NOT_FOUND = new("common/comment-not-found", "comment_not_found");
public static readonly BaseErrorCode FILE_NOT_FOUND = new("common/file-not-found", "file_not_found");

}
16 changes: 16 additions & 0 deletions Application/GrpcClients/CommonCommentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,21 @@ public async Task<List<string>> GetCommentIds()
}
}

public async Task<DTOs.Responses.Comment.PreviewCommentResponse> GetPreviewComment(string id)
{
try
{
logger.LogInformation("Starting get preview comment");
var response = await client.getPreviewCommentAsync(new PreviewCommentRequest { Id = id });
// Call the gRPC server to introspect the token
return mapper.Map<DTOs.Responses.Comment.PreviewCommentResponse>(response);
}
catch (Exception e)
{
logger.LogError(e.Message);
throw new CommonException(BaseErrorCode.COMMENT_NOT_FOUND, StatusCodes.Status404NotFound);
}
}

}
}
17 changes: 16 additions & 1 deletion Application/GrpcClients/CommonPostClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Google.Protobuf.WellKnownTypes;
using InfinityNetServer.BuildingBlocks.Application.Exceptions;
using InfinityNetServer.BuildingBlocks.Application.Protos;
using InfinityNetServer.BuildingBlocks.Domain.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
Expand Down Expand Up @@ -49,5 +48,21 @@ public async Task<List<string>> GetPostIds()
}
}

public async Task<DTOs.Responses.Post.PreviewPostResponse> GetPreviewPost(string id)
{
try
{
logger.LogInformation("Starting get preview post");
var response = await client.getPreviewPostAsync(new PreviewPostRequest { Id = id });
// Call the gRPC server to introspect the token
return mapper.Map<DTOs.Responses.Post.PreviewPostResponse>(response);
}
catch (Exception e)
{
logger.LogError(e.Message);
throw new CommonException(BaseErrorCode.POST_NOT_FOUND, StatusCodes.Status404NotFound);
}
}

}
}
24 changes: 22 additions & 2 deletions Application/Protos/comment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,33 @@ message FileMetadataIdWithOwnerId {
}

message GetFileMetadataIdsWithOwnerIdsResponse {

repeated FileMetadataIdWithOwnerId fileMetadataIdsWithOwnerIds = 1;

}

message PreviewCommentRequest {
string id = 1;
}

message PreviewCommentResponse {

string id = 1;

string profileId = 2;

string postId = 3;

string previewContent = 4;

}

service CommentService {

rpc getCommentIds (google.protobuf.Empty) returns (GetCommentIdsResponse) {}
rpc getCommentIds (google.protobuf.Empty) returns (GetCommentIdsResponse) {}

rpc getFileMetadataIdsWithOwnerIds (google.protobuf.Empty) returns (GetFileMetadataIdsWithOwnerIdsResponse) {}

rpc getFileMetadataIdsWithOwnerIds (google.protobuf.Empty) returns (GetFileMetadataIdsWithOwnerIdsResponse) {}
rpc getPreviewComment (PreviewCommentRequest) returns (PreviewCommentResponse) {}

}
16 changes: 16 additions & 0 deletions Application/Protos/post.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ message GetFileMetadataIdOfPostResponse {
string fileMetadataId = 1;
}

message PreviewPostRequest {
string id = 1;
}

message PreviewPostResponse {

string id = 1;

string ownerId = 2;

string previewContent = 3;

}

service PostService {

rpc getPostIds (google.protobuf.Empty) returns (GetPostIdsResponse) {}
Expand All @@ -46,4 +60,6 @@ service PostService {

rpc getFileMetadataIdOfPost (GetFileMetadataIdOfPostRequest) returns (GetFileMetadataIdOfPostResponse) {}

rpc getPreviewPost (PreviewPostRequest) returns (PreviewPostResponse) {}

}
14 changes: 13 additions & 1 deletion Comment.Application/GrpcServices/GrpcCommentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
using Google.Protobuf.WellKnownTypes;
using System.Linq;
using InfinityNetServer.Services.Comment.Domain.Repositories;
using System;
using AutoMapper;

namespace InfinityNetServer.Services.Comment.Application.GrpcServices
{
public class GrpcCommentService(ILogger<GrpcCommentService> logger, ICommentRepository commentRepository) : CommentService.CommentServiceBase
public class GrpcCommentService(
ILogger<GrpcCommentService> logger,
IMapper mapper,
ICommentRepository commentRepository) : CommentService.CommentServiceBase
{

public override async Task<GetCommentIdsResponse> getCommentIds(Empty request, ServerCallContext context)
Expand Down Expand Up @@ -36,5 +41,12 @@ public override async Task<GetFileMetadataIdsWithOwnerIdsResponse> getFileMetada
return await Task.FromResult(response);
}

public override async Task<PreviewCommentResponse> getPreviewComment(PreviewCommentRequest request, ServerCallContext context)
{
logger.LogInformation("Received getPreviewComment request");
Domain.Entities.Comment comment = await commentRepository.GetByIdAsync(Guid.Parse(request.Id));
return mapper.Map<PreviewCommentResponse>(comment);
}

}
}
68 changes: 35 additions & 33 deletions Comment.Infrastructure/Data/CommentDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,55 +46,57 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
comment.HasIndex(c => c.CreatedAt);
}

public override int SaveChanges()
{
return base.SaveChanges();
}

public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
var entries = ChangeTracker.Entries<Domain.Entities.Comment>();

foreach (var entry in entries)
{
Guid id = entry.Entity.Id;
Guid profileId = entry.Entity.ProfileId;
int result = await base.SaveChangesAsync(cancellationToken);

// tagged in comment
CommentContent content = entry.Entity.Content;
if (content.TagFacets.Count > 0)
if (result > 0)
{
foreach (var entry in entries)
{
foreach (var tag in content.TagFacets)
Guid id = entry.Entity.Id;
Guid profileId = entry.Entity.ProfileId;
DateTime createdAt = entry.Entity.CreatedAt;

// tagged in comment
CommentContent content = entry.Entity.Content;
if (content.TagFacets.Count > 0)
{
Guid taggedProfileId = tag.ProfileId;
foreach (var tag in content.TagFacets)
{
Guid taggedProfileId = tag.ProfileId;
await messageBus.Publish(new DomainCommand.CommentNotificationCommand
{
Id = Guid.NewGuid(),
TriggeredBy = profileId.ToString(),
RelatedProfileId = taggedProfileId,
CommentId = id,
Type = BuildingBlocks.Domain.Enums.NotificationType.TaggedInComment,
CreatedAt = createdAt
});
}
}

// reply to comment
if (entry.Entity.ParentId != null)
{
Guid parentCommentId = entry.Entity.ParentId.Value;
Domain.Entities.Comment parentComment = await Comments.FindAsync(parentCommentId);
await messageBus.Publish(new DomainCommand.CommentNotificationCommand
{
Id = Guid.NewGuid(),
TriggeredBy = profileId.ToString(),
RelatedProfileId = taggedProfileId,
RelatedProfileId = parentComment.ProfileId,
CommentId = id,
Type = BuildingBlocks.Domain.Enums.NotificationType.TaggedInComment
Type = BuildingBlocks.Domain.Enums.NotificationType.ReplyToComment,
CreatedAt = createdAt
});
}
}

// reply to comment
if (entry.Entity.ParentId != null)
{
Guid parentCommentId = entry.Entity.ParentId.Value;
Domain.Entities.Comment parentComment = await Comments.FindAsync(parentCommentId);
await messageBus.Publish(new DomainCommand.CommentNotificationCommand
{
Id = Guid.NewGuid(),
TriggeredBy = profileId.ToString(),
RelatedProfileId = parentComment.ProfileId,
CommentId = id,
Type = BuildingBlocks.Domain.Enums.NotificationType.ReplyToComment
});
}
}

return await base.SaveChangesAsync(cancellationToken);
return result;
}

}
Expand Down
7 changes: 4 additions & 3 deletions Comment.Presentation/Configurations/HostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using InfinityNetServer.Services.Comment.Infrastructure.DependencyInjection;
using InfinityNetServer.Services.Comment.Presentation.Services;
using InfinityNetServer.Services.Comment.Presentation.Exceptions;
using InfinityNetServer.Services.Comment.Presentation.Mappers;

namespace InfinityNetServer.Services.Comment.Presentation.Configurations;

Expand All @@ -32,7 +33,7 @@ internal static class HostingExtensions

public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
ArgumentNullException.ThrowIfNull(builder);

builder.AddSettings();

Expand All @@ -44,7 +45,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde

builder.Services.AddRepositories();

builder.Services.AddMappers();
builder.Services.AddMappers(typeof(CommentMappers));

builder.Services.AddLocalization(builder.Configuration);

Expand Down Expand Up @@ -94,7 +95,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde

public static WebApplication ConfigurePipeline(this WebApplication app, IConfiguration configuration)
{
if (app == null) throw new ArgumentNullException(nameof(app));
ArgumentNullException.ThrowIfNull(app);

app.UseSerilogRequestLogging();

Expand Down
23 changes: 23 additions & 0 deletions Comment.Presentation/Mappers/CommentMappers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AutoMapper;

namespace InfinityNetServer.Services.Comment.Presentation.Mappers;

public class CommentMappers : Profile
{
public CommentMappers()
{

CreateMap<Domain.Entities.Comment, BuildingBlocks.Application.Protos.PreviewCommentResponse>()
.AfterMap((src, dest) =>
{
dest.PreviewContent = src.Content.Text[..50];
});

CreateMap<Domain.Entities.Comment, BuildingBlocks.Application.DTOs.Responses.Comment.PreviewCommentResponse>()
.AfterMap((src, dest) =>
{
dest.PreviewContent = src.Content.Text[..50];
});

}
}
22 changes: 22 additions & 0 deletions Domain/Entities/MongoEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;

namespace InfinityNetServer.BuildingBlocks.Domain.Entities
{
public abstract class MongoEntity<TId>
{

[BsonId]
[BsonElement("id")]
[BsonRepresentation(BsonType.String)]
public TId Id { get; set; }

[BsonElement("created_at")]
public DateTime? CreatedAt { get; set; }

[BsonElement("updated_at")]
public DateTime? UpdatedAt { get; set; }

}
}
Loading

0 comments on commit 8b60a86

Please sign in to comment.