Table of Contents

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
Access control: Regular users must be active project members to view/create topics. Admins, moderators, and team members have elevated access and can view deleted content.

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

forumRepository IForumRepository

Repository for forum data access operations.

projectRepository IProjectRepository

Repository for project data access operations.

membershipRepository IProjectMembershipRepository

Repository for project membership data access operations.

userRepository IUserRepository

Repository for user data access operations.

emailSender IEmailSender

Service for sending forum notification emails.

configuration IConfiguration

Application configuration for accessing settings (e.g., frontend base URL).

logger ILogger<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

topicId Guid

The unique identifier of the topic to add the message to.

currentUserId Guid

The unique identifier of the user posting the message.

body string

The body content of the message.

ct CancellationToken

Cancellation 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

projectId Guid

The unique identifier of the project where the topic should be created.

currentUserId Guid

The unique identifier of the user creating the topic.

title string

The title of the forum topic. Must be unique within the project.

body string

The body content of the initial message in the topic.

ct CancellationToken

Cancellation token to cancel the operation.

Returns

Task<ProjectForumTopicDto>

A ProjectForumTopicDto representing the created topic.

Remarks

This method performs the following operations:

  1. Validates project existence and user access (same rules as GetTopicsAsync(Guid, Guid, bool, CancellationToken))
  2. Creates the forum topic entity
  3. Creates the initial message with the provided body
  4. Sends email notifications to subscribed users
  5. 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

ct CancellationToken

Cancellation 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

projectId Guid

The unique identifier of the project.

ct CancellationToken

Cancellation 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

projectId Guid

The unique identifier of the project.

topicId Guid

The unique identifier of the forum topic.

userId Guid

The unique identifier of the user.

ct CancellationToken

Cancellation 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

topicId Guid

The unique identifier of the topic to retrieve.

currentUserId Guid

The unique identifier of the user requesting the topic.

includeDeletedForAdmins bool

Whether to include soft-deleted messages (only effective for admins/moderators/team members).

ct CancellationToken

Cancellation 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

projectId Guid

The unique identifier of the project whose topics should be retrieved.

currentUserId Guid

The unique identifier of the user requesting the topics.

includeDeletedForAdmins bool

Whether to include soft-deleted topics (only effective for admins/moderators/team members).

ct CancellationToken

Cancellation 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 includeDeletedForAdmins is 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

projectId Guid

The unique identifier of the project.

userId Guid

The unique identifier of the user.

receiveAll bool

Whether the user should receive all forum notifications for this project.

ct CancellationToken

Cancellation 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

topicId Guid

The unique identifier of the forum topic.

userId Guid

The unique identifier of the user.

isSubscribed bool

Whether the user should be subscribed to receive notifications for this topic.

ct CancellationToken

Cancellation 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

messageId Guid

The unique identifier of the message to delete.

performedByUserId Guid

The unique identifier of the user performing the deletion (must be admin/moderator or message creator).

asAdminOrModerator bool

Whether the deletion is being performed with admin/moderator privileges.

ct CancellationToken

Cancellation 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

topicId Guid

The unique identifier of the topic to delete.

performedByUserId Guid

The unique identifier of the user performing the deletion (must be admin/moderator).

asAdminOrModerator bool

Whether the deletion is being performed with admin/moderator privileges.

ct CancellationToken

Cancellation 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.