Admin & Moderation
Relevant source files
The following files were used as context for generating this wiki page:
- apps/backend/src/admin/admin.module.ts
- apps/backend/src/admin/admin.controller.ts
- apps/backend/src/admin/providers/moderation.service.ts
- apps/backend/src/admin/audit-log/audit-log.service.ts
- apps/backend/src/admin/audit-log/audit-log.entity.ts
- apps/backend/src/forum-report/entities/forum-report.entity.ts
- apps/backend/src/users/entities/user.entity.ts
- apps/backend/src/auth/guards/roles.guard.ts
- apps/backend/src/auth/decorators/roles.decorator.ts
The Admin & Moderation module is a core component of the OnChainSage backend, responsible for providing administrative oversight and content moderation capabilities. It equips authorized users (Admins and Moderators) with the tools to manage user-generated content, handle reports, take punitive actions like banning posts or shadow-banning users, and view system statistics.
This module is built using NestJS and is designed to be secure and auditable. Access to its functionalities is strictly controlled through a role-based access control (RBAC) system, and all significant moderation actions are logged for accountability. It interacts with several other modules, including Users, Forum, and Stats, to perform its functions.
Architecture
The AdminModule encapsulates all administrative and moderation logic. It imports necessary services and TypeORM entity repositories, and exposes its functionality via the AdminController.
Sources: apps/backend/src/admin/admin.module.ts:1-25
Module Components
The diagram below illustrates the key components within the AdminModule and their primary dependencies.
This diagram shows the internal services of the Admin module and how it connects to external services and modules for authentication, data access, and statistics.
Sources: apps/backend/src/admin/admin.module.ts:1-25, apps/backend/src/admin/admin.controller.ts:1-49
Role-Based Access Control (RBAC)
All endpoints within the AdminController are protected to ensure that only authorized personnel can perform administrative actions. This is achieved using a combination of guards and decorators.
JwtAuthGuard: Ensures the user is authenticated via a valid JSON Web Token.RolesGuard: Checks if the authenticated user has the required role(s) to access the endpoint.@Roles()Decorator: Specifies which roles are permitted. For this module, the roles areUserRole.ADMINandUserRole.Moderator.
Sources: apps/backend/src/admin/admin.controller.ts:11-14, apps/backend/src/admin/admin.controller.ts:20, apps/backend/src/auth/decorators/roles.decorator.ts, apps/backend/src/auth/guards/roles.guard.ts
The user roles are defined in the User entity:
// apps/backend/src/users/entities/user.entity.ts
export enum UserRole {
USER = 'user',
Moderator = 'moderator',
ADMIN = 'admin',
}
Sources: apps/backend/src/users/entities/user.entity.ts (file referenced in admin.module.ts)
API Endpoints
The AdminController exposes several RESTful endpoints for moderation and administrative tasks.
| Method | Endpoint | Required Roles | Description |
|---|---|---|---|
GET | /admin/reports | ADMIN, Moderator | Fetches all open forum reports. |
GET | /admin/stats | ADMIN, Moderator | Retrieves system statistics based on query params. |
POST | /admin/post/:id/ban | ADMIN, Moderator | Bans a specific forum post. |
PATCH | /admin/user/:wallet/shadowban | ADMIN, Moderator | Shadow-bans a user by their wallet address. |
Sources: apps/backend/src/admin/admin.controller.ts:24-49
Core Services
Moderation Service
The ModerationService contains the business logic for taking action against users and their content. The implementation details are not fully available, but its interface can be inferred from the AdminController.
Key functions include:
banPost(postId: string, moderatorWallet: string): Marks a post as banned. This action is likely recorded by theAuditLogService.shadowbanUser(userWallet: string, moderatorWallet: string): Flags a user for shadow-banning, which might limit the visibility of their future content without their knowledge. This is also expected to be an audited action.
Sources: apps/backend/src/admin/admin.controller.ts:40-49, apps/backend/src/admin/providers/moderation.service.ts
The following sequence diagram shows the typical flow for banning a post.
This diagram illustrates how a moderator's request to ban a post is authenticated, processed by the moderation service, and logged for auditing before a success response is returned.
Sources: apps/backend/src/admin/admin.controller.ts:40-43, apps/backend/src/admin/admin.module.ts:22-23
Audit Log Service
The AuditLogService is crucial for maintaining a record of all administrative actions. It is responsible for creating and persisting AuditLog entities. While the service implementation is not provided, its purpose is clear from its inclusion in the AdminModule. It is injected into services like ModerationService to be called after a significant action is performed.
Sources: apps/backend/src/admin/admin.module.ts:9, apps/backend/src/admin/admin.module.ts:23, apps/backend/src/admin/audit-log/audit-log.service.ts
Data Models
The module relies on several database entities to store information related to moderation. The relationships between these entities are managed by TypeORM.
Entity Relationship Diagram
This ER diagram shows the database schema relationships between Users, Posts, Forum Reports, and Audit Logs.
Sources: apps/backend/src/admin/admin.module.ts:16
Key Entities
ForumReport: Represents a report made by a user against a piece of content. It links a reporter, the content, and a reason.
Sources: apps/backend/src/forum-report/entities/forum-report.entity.ts, apps/backend/src/admin/admin.module.ts:16AuditLog: Records an administrative action, including who performed it (moderatorWallet), what the action was (action), and what it targeted (targetId).
Sources: apps/backend/src/admin/audit-log/audit-log.entity.ts, apps/backend/src/admin/admin.module.ts:16
Summary
The Admin & Moderation module is a protected, backend-only feature set that grants trusted users the power to maintain the health and integrity of the OnChainSage platform. Through a combination of role-based access, dedicated services for moderation and reporting, and a comprehensive audit trail, it provides a robust framework for platform governance.