Class ForumService
- Namespace
- Builvero.Application.Services
- Assembly
- Builvero.Application.dll
Provides business logic for forum operations including topic creation, message posting, subscriptions, and notifications.
public class ForumService : IForumService
- Inheritance
-
ForumService
- Implements
- Inherited Members
Remarks
This service handles all forum-related operations:
- Topic creation and retrieval with access control
- Message posting and retrieval
- Topic subscriptions for notification management
- Email notifications for new topics and messages
- Soft deletion support for moderators and admins
Constructors
ForumService(IForumRepository, IProjectRepository, IProjectMembershipRepository, IUserRepository, IEmailSender, IConfiguration, ILogger<ForumService>)
Initializes a new instance of the ForumService class.
public ForumService(IForumRepository forumRepository, IProjectRepository projectRepository, IProjectMembershipRepository membershipRepository, IUserRepository userRepository, IEmailSender emailSender, IConfiguration configuration, ILogger<ForumService> logger)
Parameters
forumRepositoryIForumRepositoryRepository for forum data access operations.
projectRepositoryIProjectRepositoryRepository for project data access operations.
membershipRepositoryIProjectMembershipRepositoryRepository for project membership data access operations.
userRepositoryIUserRepositoryRepository for user data access operations.
emailSenderIEmailSenderService for sending forum notification emails.
configurationIConfigurationApplication configuration for accessing settings (e.g., frontend base URL).
loggerILogger<ForumService>Logger for recording service operations and errors.
Methods
AddMessageAsync(Guid, Guid, string, CancellationToken)
Adds a new message to an existing forum topic.
public Task<ProjectForumMessageDto> AddMessageAsync(Guid topicId, Guid currentUserId, string body, CancellationToken ct)
Parameters
topicIdGuidThe unique identifier of the topic to add the message to.
currentUserIdGuidThe unique identifier of the user posting the message.
bodystringThe body content of the message.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<ProjectForumMessageDto>
A ProjectForumMessageDto representing the created message.
CreateTopicAsync(Guid, Guid, string, string, CancellationToken)
Creates a new forum topic in a project with an initial message.
public Task<ProjectForumTopicDto> CreateTopicAsync(Guid projectId, Guid currentUserId, string title, string body, CancellationToken ct)
Parameters
projectIdGuidThe unique identifier of the project where the topic should be created.
currentUserIdGuidThe unique identifier of the user creating the topic.
titlestringThe title of the forum topic. Must be unique within the project.
bodystringThe body content of the initial message in the topic.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<ProjectForumTopicDto>
A ProjectForumTopicDto representing the created topic.
Remarks
This method performs the following operations:
- Validates project existence and user access (same rules as GetTopicsAsync(Guid, Guid, bool, CancellationToken))
- Creates the forum topic entity
- Creates the initial message with the provided body
- Sends email notifications to subscribed users
- Auto-subscribes the topic creator to receive notifications
Topic titles must be unique within a project. Email notifications are sent to all users subscribed to forum notifications for the project, excluding the topic creator.
Exceptions
- Exception
Thrown when the project or user is not found, or the topic title is not unique within the project.
- UnauthorizedAccessException
Thrown when the user is not an admin/moderator/team member and is not an active project member.
GetAllProjectsForAdminAsync(CancellationToken)
Retrieves a summary list of all projects for admin/moderation purposes.
public Task<IReadOnlyList<ProjectSummaryDto>> GetAllProjectsForAdminAsync(CancellationToken ct)
Parameters
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<IReadOnlyList<ProjectSummaryDto>>
A read-only list of project summaries.
Remarks
This method is intended for admin/moderation UI to browse all projects and their forums. Requires admin/moderator/team member privileges.
GetAllTopicsForProjectAsAdminAsync(Guid, CancellationToken)
Retrieves all forum topics for a project, including deleted topics, for admin/moderation purposes.
public Task<IReadOnlyList<ProjectForumTopicDto>> GetAllTopicsForProjectAsAdminAsync(Guid projectId, CancellationToken ct)
Parameters
projectIdGuidThe unique identifier of the project.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<IReadOnlyList<ProjectForumTopicDto>>
A read-only list of all forum topics, including deleted ones.
Remarks
This method is intended for admin/moderation UI to view all topics, including soft-deleted ones. Requires admin/moderator/team member privileges.
GetSubscriptionStateAsync(Guid, Guid, Guid, CancellationToken)
Retrieves the current subscription state for a user regarding a project and topic.
public Task<ForumSubscriptionStateDto> GetSubscriptionStateAsync(Guid projectId, Guid topicId, Guid userId, CancellationToken ct)
Parameters
projectIdGuidThe unique identifier of the project.
topicIdGuidThe unique identifier of the forum topic.
userIdGuidThe unique identifier of the user.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<ForumSubscriptionStateDto>
A ForumSubscriptionStateDto containing global and topic-level subscription status.
GetTopicWithMessagesAsync(Guid, Guid, bool, CancellationToken)
Retrieves a forum topic with all its messages.
public Task<ProjectForumTopicDto> GetTopicWithMessagesAsync(Guid topicId, Guid currentUserId, bool includeDeletedForAdmins, CancellationToken ct)
Parameters
topicIdGuidThe unique identifier of the topic to retrieve.
currentUserIdGuidThe unique identifier of the user requesting the topic.
includeDeletedForAdminsboolWhether to include soft-deleted messages (only effective for admins/moderators/team members).
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<ProjectForumTopicDto>
A ProjectForumTopicDto containing the topic and all its messages.
GetTopicsAsync(Guid, Guid, bool, CancellationToken)
Retrieves all forum topics for a project, with access control based on user role and project membership.
public Task<IReadOnlyList<ProjectForumTopicDto>> GetTopicsAsync(Guid projectId, Guid currentUserId, bool includeDeletedForAdmins, CancellationToken ct)
Parameters
projectIdGuidThe unique identifier of the project whose topics should be retrieved.
currentUserIdGuidThe unique identifier of the user requesting the topics.
includeDeletedForAdminsboolWhether to include soft-deleted topics (only effective for admins/moderators/team members).
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task<IReadOnlyList<ProjectForumTopicDto>>
A read-only list of forum topics for the project.
Remarks
Access control rules:
- Admins, moderators, and team members can view all topics (including deleted if requested)
- Regular users must be active project members to view topics
- Regular users never see deleted topics, even if
includeDeletedForAdminsis true
Exceptions
- Exception
Thrown when the user is not found.
- UnauthorizedAccessException
Thrown when the user is not an admin/moderator/team member and is not an active project member.
SetGlobalSubscriptionAsync(Guid, Guid, bool, CancellationToken)
Sets the global forum notification preference for a user in a project.
public Task SetGlobalSubscriptionAsync(Guid projectId, Guid userId, bool receiveAll, CancellationToken ct)
Parameters
projectIdGuidThe unique identifier of the project.
userIdGuidThe unique identifier of the user.
receiveAllboolWhether the user should receive all forum notifications for this project.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task
- Provides business logic for forum operations including topic creation, message posting, subscriptions, and notifications.
Remarks
When receiveAll is false, the user will not receive any forum notifications for the project,
regardless of individual topic subscriptions.
SetTopicSubscriptionAsync(Guid, Guid, bool, CancellationToken)
Sets the subscription status for a user to a specific forum topic.
public Task SetTopicSubscriptionAsync(Guid topicId, Guid userId, bool isSubscribed, CancellationToken ct)
Parameters
topicIdGuidThe unique identifier of the forum topic.
userIdGuidThe unique identifier of the user.
isSubscribedboolWhether the user should be subscribed to receive notifications for this topic.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task
- Provides business logic for forum operations including topic creation, message posting, subscriptions, and notifications.
Remarks
Topic-level subscriptions only take effect if the user's global subscription for the project allows notifications.
SoftDeleteMessageAsync(Guid, Guid, bool, CancellationToken)
Soft-deletes a forum message, hiding it from normal view while preserving it in the database.
public Task SoftDeleteMessageAsync(Guid messageId, Guid performedByUserId, bool asAdminOrModerator, CancellationToken ct)
Parameters
messageIdGuidThe unique identifier of the message to delete.
performedByUserIdGuidThe unique identifier of the user performing the deletion (must be admin/moderator or message creator).
asAdminOrModeratorboolWhether the deletion is being performed with admin/moderator privileges.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task
- Provides business logic for forum operations including topic creation, message posting, subscriptions, and notifications.
Remarks
Admins, moderators, and team members can delete any message. Regular users can only delete their own messages.
Exceptions
- UnauthorizedAccessException
Thrown when the user does not have permission to delete the message.
SoftDeleteTopicAsync(Guid, Guid, bool, CancellationToken)
Soft-deletes a forum topic, hiding it from normal view while preserving it in the database.
public Task SoftDeleteTopicAsync(Guid topicId, Guid performedByUserId, bool asAdminOrModerator, CancellationToken ct)
Parameters
topicIdGuidThe unique identifier of the topic to delete.
performedByUserIdGuidThe unique identifier of the user performing the deletion (must be admin/moderator).
asAdminOrModeratorboolWhether the deletion is being performed with admin/moderator privileges.
ctCancellationTokenCancellation token to cancel the operation.
Returns
- Task
- Provides business logic for forum operations including topic creation, message posting, subscriptions, and notifications.
Remarks
Only admins, moderators, and team members can soft-delete topics. The topic and all its messages are marked as deleted.
Exceptions
- UnauthorizedAccessException
Thrown when the user does not have permission to delete topics.