|
| 1 | +# Content Flagging System Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | +The application now includes a comprehensive content flagging system that allows users to flag inappropriate, incorrect, or problematic content for admin review. This system helps maintain content quality and community standards. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +### For All Users |
| 9 | +Users can flag any of the following content types: |
| 10 | +- **Notes**: Full articles and study notes |
| 11 | +- **Comments**: Comments on notes, Bible verses, or cross-references |
| 12 | +- **Cross-References**: Biblical cross-reference connections |
| 13 | + |
| 14 | +#### How to Flag Content |
| 15 | +1. Navigate to any note, comment, or cross-reference |
| 16 | +2. Look for the "Flag for Review" button or link (displayed next to content actions) |
| 17 | +3. Click the flag button and enter a reason for flagging |
| 18 | +4. The content will be immediately flagged and sent to admins for review |
| 19 | + |
| 20 | +**Note**: Users cannot flag content they can edit/delete (their own content or if they're admin). |
| 21 | + |
| 22 | +### For Contributors & Content Authors |
| 23 | + |
| 24 | +#### Viewing Your Flagged Content |
| 25 | +- URL: `/my-flagged-content` |
| 26 | +- Accessible via "My Content" in the main navigation |
| 27 | +- A notification badge shows the number of items needing your review |
| 28 | +- View all flags on your content across all types (notes, comments, cross-references) |
| 29 | + |
| 30 | +#### What You'll See |
| 31 | +When admins request a review on your content, you'll see: |
| 32 | +- **Content Details**: Which of your content was flagged |
| 33 | +- **Reason**: Why it was flagged by the user |
| 34 | +- **Admin Feedback**: Specific guidance from the admin on what needs to be changed |
| 35 | +- **Status**: Current status (pending, review requested, approved, edited, deleted) |
| 36 | +- **Action Buttons**: Quick access to edit or view the flagged content |
| 37 | + |
| 38 | +#### Status Filters |
| 39 | +- **Review Requested**: Content that needs your attention (default view) |
| 40 | +- **Under Review**: Content flagged but not yet reviewed by admins |
| 41 | +- **Approved**: Content that was flagged but deemed acceptable |
| 42 | +- **Edited**: Content that has been revised |
| 43 | +- **Deleted**: Content that was removed |
| 44 | + |
| 45 | +### For Admins |
| 46 | + |
| 47 | +#### Accessing Flagged Content |
| 48 | +- URL: `/admin/content_flags` |
| 49 | +- Accessible via the Admin navigation in the top menu |
| 50 | +- A notification badge shows the number of pending flags |
| 51 | + |
| 52 | +#### Admin Dashboard Features |
| 53 | + |
| 54 | +##### 1. Status Overview Cards |
| 55 | +View statistics for all flag statuses: |
| 56 | +- **Pending Review**: Flags awaiting admin action |
| 57 | +- **Approved**: Content approved despite being flagged |
| 58 | +- **Review Requested**: Admin requested author to review/revise |
| 59 | +- **Edited**: Content was edited by admin or author |
| 60 | +- **Deleted**: Content was removed |
| 61 | + |
| 62 | +##### 2. Status Filter Tabs |
| 63 | +- Filter flags by status (Pending, Approved, Review Requested, Edited, Deleted) |
| 64 | +- Default view shows pending flags requiring action |
| 65 | + |
| 66 | +##### 3. Flag Details |
| 67 | +Each flag displays: |
| 68 | +- Content type (Note, Comment, or Cross-Reference) |
| 69 | +- Content title/preview |
| 70 | +- Flagged by (user who flagged it) |
| 71 | +- Content author |
| 72 | +- Reason for flagging |
| 73 | +- Preview of the flagged content |
| 74 | +- Timestamp of when it was flagged |
| 75 | + |
| 76 | +##### 4. Admin Actions (for Pending Flags) |
| 77 | + |
| 78 | +**Approve** |
| 79 | +- Marks the content as reviewed and acceptable |
| 80 | +- Content remains published |
| 81 | +- Flag status changes to "approved" |
| 82 | + |
| 83 | +**Request Review** |
| 84 | +- Notifies content author via their "My Content" page |
| 85 | +- Optional: Add a note for the author explaining what needs to be changed |
| 86 | +- Flag status changes to "review_requested" |
| 87 | +- Badge notification appears in author's navigation |
| 88 | +- *Future enhancement: Email notification to author* |
| 89 | + |
| 90 | +**Edit Content** |
| 91 | +- Redirects admin to the content edit page |
| 92 | +- Admin can directly fix issues with the content |
| 93 | +- After editing, flag can be marked as resolved |
| 94 | + |
| 95 | +**Delete Content** |
| 96 | +- Permanently removes the flagged content |
| 97 | +- Optional: Add a note explaining why |
| 98 | +- Requires confirmation |
| 99 | +- Flag status changes to "deleted" |
| 100 | + |
| 101 | +**View Content** |
| 102 | +- Opens the content in a new tab for full review |
| 103 | +- Allows admin to see the content in context |
| 104 | + |
| 105 | +## Implementation Details |
| 106 | + |
| 107 | +### Database Schema |
| 108 | + |
| 109 | +#### ContentFlags Table |
| 110 | +```ruby |
| 111 | +create_table :content_flags do |t| |
| 112 | + t.references :flaggable, polymorphic: true, null: false # The flagged content |
| 113 | + t.references :user, null: false, foreign_key: true # User who flagged |
| 114 | + t.text :reason # Why it was flagged |
| 115 | + t.string :status, default: 'pending', null: false # Flag status |
| 116 | + t.references :resolved_by, foreign_key: { to_table: :users } # Admin who resolved |
| 117 | + t.datetime :resolved_at # When it was resolved |
| 118 | + t.text :admin_note # Admin's note/explanation |
| 119 | + t.timestamps |
| 120 | +end |
| 121 | +``` |
| 122 | + |
| 123 | +**Indexes:** |
| 124 | +- `status` - for filtering by status |
| 125 | +- `[flaggable_type, flaggable_id]` - for finding flags for specific content |
| 126 | + |
| 127 | +### Models |
| 128 | + |
| 129 | +#### ContentFlag Model |
| 130 | +- **Belongs to**: `flaggable` (polymorphic), `user`, `resolved_by` |
| 131 | +- **Enum**: `status` (pending, approved, review_requested, edited, deleted) |
| 132 | +- **Validations**: Presence of user, flaggable, and status |
| 133 | +- **Scopes**: |
| 134 | + - `pending` - only pending flags |
| 135 | + - `resolved` - all resolved flags |
| 136 | + - `recent` - ordered by creation date (newest first) |
| 137 | + |
| 138 | +#### Updated Models |
| 139 | +All flaggable models now include: |
| 140 | +```ruby |
| 141 | +has_many :content_flags, as: :flaggable, dependent: :destroy |
| 142 | +``` |
| 143 | +- **Note** |
| 144 | +- **Comment** |
| 145 | +- **CrossReference** |
| 146 | + |
| 147 | +### Controllers |
| 148 | + |
| 149 | +#### ContentFlagsController |
| 150 | +- **Action**: `create` |
| 151 | +- **Purpose**: Handles flag submissions from users |
| 152 | +- **Validation**: |
| 153 | + - Prevents duplicate flags (same user + same content + pending status) |
| 154 | + - Requires reason for flagging |
| 155 | +- **Authorization**: Requires authenticated user |
| 156 | + |
| 157 | +#### Admin::ContentFlagsController |
| 158 | +- **Actions**: `index`, `approve`, `request_review`, `edit_content`, `destroy_content` |
| 159 | +- **Authorization**: Admin only (via `authorize_admin!`) |
| 160 | +- **Features**: |
| 161 | + - Lists all flags with filtering |
| 162 | + - Provides all resolution actions |
| 163 | + - Tracks who resolved each flag and when |
| 164 | + |
| 165 | +### Routes |
| 166 | + |
| 167 | +```ruby |
| 168 | +# User-facing flag creation |
| 169 | +POST /content_flags |
| 170 | + |
| 171 | +# Contributor's flagged content dashboard |
| 172 | +GET /my-flagged-content # View your flagged content |
| 173 | + |
| 174 | +# Admin flag management |
| 175 | +GET /admin/content_flags # List all flags |
| 176 | +PATCH /admin/content_flags/:id/approve # Approve flagged content |
| 177 | +PATCH /admin/content_flags/:id/request_review # Request author review |
| 178 | +GET /admin/content_flags/:id/edit_content # Redirect to edit page |
| 179 | +DELETE /admin/content_flags/:id/destroy_content # Delete flagged content |
| 180 | +``` |
| 181 | + |
| 182 | +### Views |
| 183 | + |
| 184 | +#### User-Facing Flag Buttons |
| 185 | +Flag functionality is added to: |
| 186 | +- **Notes**: Flag button appears for users who cannot edit the note |
| 187 | +- **Comments**: "Flag" link in comment header for users who cannot edit |
| 188 | +- **Cross-References**: "Flag" link in actions column for users who cannot delete |
| 189 | + |
| 190 | +#### Admin Interface |
| 191 | +- **Navigation**: Shared admin navigation with User Management and Flagged Content tabs |
| 192 | +- **Index Page**: Comprehensive flag listing with status cards, filters, and actions |
| 193 | +- **Styling**: Consistent with existing admin design system |
| 194 | + |
| 195 | +### JavaScript |
| 196 | +Global `flagContent(contentType, contentId)` function: |
| 197 | +- Prompts user for flag reason |
| 198 | +- Creates and submits a form with CSRF token |
| 199 | +- Handles all three content types (Note, Comment, CrossReference) |
| 200 | +- Located in application layout for global access |
| 201 | + |
| 202 | +### Styling |
| 203 | +Flag-specific styles in `app/assets/stylesheets/components/_admin.scss`: |
| 204 | +- `.flag-card` - Individual flag container |
| 205 | +- `.flag-header`, `.flag-content`, `.flag-actions` - Flag sections |
| 206 | +- `.flag-meta` - Metadata grid |
| 207 | +- `.flag-reason` - Highlighted reason display |
| 208 | +- Badge status colors for all flag statuses |
| 209 | +- Responsive stat cards for the admin dashboard |
| 210 | + |
| 211 | +## Security & Permissions |
| 212 | + |
| 213 | +### User Permissions |
| 214 | +- All authenticated users can flag content |
| 215 | +- Users cannot flag their own content |
| 216 | +- Admins can flag content but primarily use direct edit/delete |
| 217 | + |
| 218 | +### Admin Permissions |
| 219 | +- Only users with `role_admin?` can access admin flag management |
| 220 | +- All flag resolution actions require admin authentication |
| 221 | +- Authorization checked via `Authorizable` concern |
| 222 | + |
| 223 | +## Future Enhancements |
| 224 | + |
| 225 | +### Planned Features |
| 226 | +1. **Email Notifications** |
| 227 | + - Notify content authors when their content is flagged |
| 228 | + - Alert authors when review is requested |
| 229 | + - Confirm to flaggers when action is taken |
| 230 | + |
| 231 | +2. **Flag Analytics** |
| 232 | + - Track which users frequently flag content |
| 233 | + - Identify content that receives multiple flags |
| 234 | + - Generate reports on flag resolution times |
| 235 | + |
| 236 | +3. **Auto-Moderation** |
| 237 | + - Automatic actions based on flag count |
| 238 | + - Hide content with multiple pending flags |
| 239 | + - Pattern detection for problematic content |
| 240 | + |
| 241 | +4. **Enhanced Workflow** |
| 242 | + - Assign flags to specific admin reviewers |
| 243 | + - Flag priority levels (low, medium, high, urgent) |
| 244 | + - Bulk flag actions for efficient moderation |
| 245 | + |
| 246 | +5. **User Flag History** |
| 247 | + - View all flags submitted by a user |
| 248 | + - Track flag accuracy (approved vs. rejected) |
| 249 | + - Prevent flag abuse |
| 250 | + |
| 251 | +## Usage Examples |
| 252 | + |
| 253 | +### Example 1: User Flags Inappropriate Comment |
| 254 | +1. User sees inappropriate language in a comment |
| 255 | +2. Clicks "Flag" link next to the comment |
| 256 | +3. Enters reason: "Contains offensive language" |
| 257 | +4. Admin receives notification |
| 258 | +5. Admin reviews content and deletes comment |
| 259 | +6. Flag marked as "deleted" with admin note |
| 260 | + |
| 261 | +### Example 2: Admin Requests Review (Complete Workflow) |
| 262 | +1. User flags a note for factual inaccuracy |
| 263 | +2. Admin reviews and identifies the issue |
| 264 | +3. Admin clicks "Request Review" |
| 265 | +4. Adds note: "Please verify the dates mentioned in paragraph 3" |
| 266 | +5. Flag status changes to "review_requested" |
| 267 | +6. Author sees notification badge (🔔 1) in "My Content" navigation |
| 268 | +7. Author visits "My Content" page and sees: |
| 269 | + - The flag reason |
| 270 | + - The admin's specific feedback |
| 271 | + - "Edit Content" button |
| 272 | +8. Author clicks "Edit Content" and makes revisions |
| 273 | +9. **Upon saving**, the system automatically: |
| 274 | + - Changes flag status back to "pending" for admin re-review |
| 275 | + - Adds update timestamp to the flag |
| 276 | + - Shows success message: "Content updated and submitted back to admins for review" |
| 277 | + - Removes the notification badge for the author |
| 278 | +10. Admin sees the flag back in "Pending" view with: |
| 279 | + - Green "Content has been updated by the author and is ready for re-review!" notice |
| 280 | + - Updated timestamp showing when author made changes |
| 281 | + - Original admin note plus update history |
| 282 | +11. Admin reviews the updated content and can then: |
| 283 | + - Approve it (content was fixed) |
| 284 | + - Request another review (needs more work) |
| 285 | + - Delete it (still problematic) |
| 286 | + |
| 287 | +### Example 3: Admin Approves Content |
| 288 | +1. User flags a controversial but valid theological perspective |
| 289 | +2. Admin reviews and determines content is appropriate |
| 290 | +3. Admin clicks "Approve" |
| 291 | +4. Content remains published |
| 292 | +5. Flag marked as "approved" |
| 293 | +6. Creates record of administrative review |
| 294 | + |
| 295 | +## Testing |
| 296 | + |
| 297 | +### Manual Testing Checklist |
| 298 | + |
| 299 | +**User Flagging:** |
| 300 | +- [ ] User can flag a note they didn't create |
| 301 | +- [ ] User can flag a comment they didn't create |
| 302 | +- [ ] User can flag a cross-reference they didn't create |
| 303 | +- [ ] User cannot flag their own content |
| 304 | +- [ ] User cannot flag same content twice while pending |
| 305 | + |
| 306 | +**Contributor Dashboard:** |
| 307 | +- [ ] Contributors can access "My Content" page |
| 308 | +- [ ] Badge shows count of review-requested items |
| 309 | +- [ ] Can see all flags on their content |
| 310 | +- [ ] Can filter by status |
| 311 | +- [ ] Can edit flagged content from the page |
| 312 | +- [ ] Admin notes/feedback are visible |
| 313 | +- [ ] Can view deleted content details (even if content is gone) |
| 314 | + |
| 315 | +**Admin Actions:** |
| 316 | +- [ ] Admin can see all pending flags |
| 317 | +- [ ] Admin can filter flags by status |
| 318 | +- [ ] Admin can approve a flag |
| 319 | +- [ ] Admin can request review with note |
| 320 | +- [ ] Admin can edit flagged content |
| 321 | +- [ ] Admin can delete flagged content with confirmation |
| 322 | +- [ ] Flag counts display correctly in admin nav |
| 323 | +- [ ] Flag status changes are tracked correctly |
| 324 | + |
| 325 | +## Troubleshooting |
| 326 | + |
| 327 | +### Common Issues |
| 328 | + |
| 329 | +**Issue**: Flag button not appearing |
| 330 | +- **Check**: User must be authenticated |
| 331 | +- **Check**: User must not be able to edit/delete the content |
| 332 | +- **Solution**: Verify user permissions and content ownership |
| 333 | + |
| 334 | +**Issue**: Admin actions not working |
| 335 | +- **Check**: User has admin role |
| 336 | +- **Check**: CSRF token is present in form |
| 337 | +- **Solution**: Verify admin authentication and Rails CSRF protection |
| 338 | + |
| 339 | +**Issue**: Flagging returns "already flagged" error |
| 340 | +- **Check**: User has pending flag for this content |
| 341 | +- **Solution**: Resolve existing flag before allowing new flag |
| 342 | + |
| 343 | +## Database Queries |
| 344 | + |
| 345 | +### Useful Queries for Analytics |
| 346 | + |
| 347 | +```ruby |
| 348 | +# Total flags by status |
| 349 | +ContentFlag.group(:status).count |
| 350 | + |
| 351 | +# Most flagged content |
| 352 | +ContentFlag.group(:flaggable_type).count |
| 353 | + |
| 354 | +# Flags resolved by each admin |
| 355 | +ContentFlag.where.not(resolved_by: nil).group(:resolved_by_id).count |
| 356 | + |
| 357 | +# Average time to resolution |
| 358 | +ContentFlag.where.not(resolved_at: nil) |
| 359 | + .average("EXTRACT(EPOCH FROM (resolved_at - created_at))") |
| 360 | + |
| 361 | +# Flags for a specific user's content |
| 362 | +ContentFlag.joins("INNER JOIN notes ON notes.id = content_flags.flaggable_id AND content_flags.flaggable_type = 'Note'") |
| 363 | + .where(notes: { user_id: user_id }) |
| 364 | +``` |
| 365 | + |
| 366 | +## Maintenance |
| 367 | + |
| 368 | +### Regular Tasks |
| 369 | +1. **Weekly**: Review resolved flags for patterns |
| 370 | +2. **Monthly**: Archive old resolved flags (optional) |
| 371 | +3. **Quarterly**: Analyze flag trends and user behavior |
| 372 | +4. **As Needed**: Update flag categories and reasons |
| 373 | + |
| 374 | +### Performance Considerations |
| 375 | +- Indexes on `status` and `flaggable` optimize queries |
| 376 | +- Scopes use database-level filtering |
| 377 | +- Eager loading used for admin interface (`includes(:user, :flaggable, :resolved_by)`) |
| 378 | + |
| 379 | +## Support |
| 380 | + |
| 381 | +For questions or issues with the flagging system: |
| 382 | +1. Check this documentation |
| 383 | +2. Review the code comments in models and controllers |
| 384 | +3. Check Rails logs for error messages |
| 385 | +4. Test in development environment first |
| 386 | + |
| 387 | +--- |
| 388 | + |
| 389 | +**Version**: 1.0 |
| 390 | +**Last Updated**: November 13, 2025 |
| 391 | +**Author**: AI Assistant |
| 392 | +**Status**: Implemented and Ready for Production |
| 393 | + |
0 commit comments