mattermost/server/public/model/content_flagging_settings.go
Harshil Sharma 3265054ad5
Some checks failed
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Waiting to run
Web App CI / check-types (push) Waiting to run
Web App CI / test (push) Waiting to run
Web App CI / build (push) Waiting to run
Migration-assist Sync / Check if migration-assist have been synced (push) Has been cancelled
Migrate content flagging settings to database (#33989)
* lint fix

* CI

* added new migration mocks

* Used setup for tests

* some comment

* Removed unnecesseery nil check

* Form validation

* WIP tests

* WIP tests

* WIP tests

* fix: mock content flagging config selector with correct reasons format

Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>

* fix: add mock for getContentFlaggingConfig in flag post modal test

Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>

* Updated error code order in API docs

* removed empty files

* Added tests

* lint fixes

* minor tweak

* lint fix

* type fix

* fixed test

* nit

* test enhancements

* API WIP

* API WIP

* creating values

* creating content flagging channel and properties

* Able to save properties

* Added another property field

* WIP

* WIP

* Added validations

* Added data validations and hidden post if confifgured to

* lint fixes

* Added API spec

* Added some tests

* Added tests for getContentReviewBot

* test: add comprehensive tests for getContentReviewChannels function

* Added more app layer tests

* Added TestCanFlagPost

* test: Add comprehensive tests for FlagPost function

* Added all app layer tests

* Removed a file that was reamoved downstream

* test: add content flagging test file

* test: add comprehensive tests for FlagContentRequest.IsValid method

* Added model tests

* test: add comprehensive tests for SqlPropertyValueStore.CreateMany

* test: add comprehensive tests for flagPost() API function

* Added API tests

* linter fix

* WIP

* sent post flagging confirmation message

* fixed i18n nissues

* fixed i18n nissues

* CI

* WIP

* WIP

* Added API call

* test: add test for Client4.flagPost API call in FlagPostModal

* fix: remove userEvent.setup() from flag post modal test

* test: wrap submit button click in act for proper state updates

* Updated tests

* lint fix

* Updated test

* fix: reset contentFlaggingGroupId for test isolation in content flagging tests

* removed cached group ID

* removed debug log

* CI

* Updated to allow special characters in comments

* Handled empty comment

* Created getContentFlaggingFields API

* created getPostPropertyValues API

* Used finally

* WIP

* Created useContentFlaggingFields hook

* WIP

* WIP

* Added option to retain data for reviewers

* Displayed deleted post's preview

* DIsplayed all properties

* Adding field name i18n

* WIP - managing i18n able texts

* Finished displaying all fields

* Manual cleanup

* lint fixes

* team role filter logic fix

* Fixed tests

* created new API to fetch flagged posts

* lint fix

* Added new client methods

* test: add comprehensive tests for content flagging APIs

* Added new API tests

* fixed openapi spec

* Fixed DataSpillageReport tests

* Fixed PostMarkdown test

* Fixed PostPreviewPropertyRenderer test

* Added metadata to card renderer

* test fixes

* Added no comment placeholder

* Added view detail button

* Created RemoveFlaggedMessageConfirmationModal modal

* Added key and remove flag request modal

* IMplemented delete flagged post

* Handled edge cases of deleting flagged post

* keep message

* UI integration

* Added WS event for post report update and handled deleted files of flagged post

* Added error handling in keep/remove forms

* i18n fixes

* Fixed test

* Updated OpenAPI specs

* fixed types

* fixed types

* refactoring

* refactor: improve test mocking for data spillage report component

* test mock updates

* Fixed tests

* Updated reducer

* not resetting mocks

* Added migrations for content flagging tables

* Created new structure

* review fixes

* Used correct ot name

* WIP

* review fixes

* review fixes

* Added new property translations

* CI

* CI

* CI

* Improved test

* fixed test

* CI

* New UI component

* WIP

* Updated settings APIs

* cached DB data

* used cached reviewer data

* Updated tests

* Lint fixes

* test: add tests for saveContentFlaggingSettings and getContentFlaggingSettings APIs

* test fix

* test: add tests for SaveContentFlaggingConfig and GetContentFlaggingConfigReviewerIDs

* Updated tests

* test: add content flagging test for local cache layer

* test: add comprehensive tests for content flagging store cache

* Updated tests

* lint fix

* Updated mobile text

* Added content flagging SQL store mocks

* Added API specs for new APIs

* fixed tests

* feat: add TestContentFlaggingStore function for content flagging store testing

* feat: add comprehensive tests for content flagging store

* Added SQL store tests

* test: add content flagging test for local cache layer

* test: add tests for content flagging store caching

* Added cache layer tests

* Updated tests

* Fixed

* Handled JSON error

* fixes

* fixes

* Fixed retry layer test

* fixerdf i18n

* Fixed test

* CI

* building index concurrently

* CI

* fixed a test

* CI

* cleanup

* Integrate flag post api (#33798)

* WIP

* WIP

* Added API call

* test: add test for Client4.flagPost API call in FlagPostModal

* fix: remove userEvent.setup() from flag post modal test

* test: wrap submit button click in act for proper state updates

* Updated tests

* lint fix

* CI

* Updated to allow special characters in comments

* Handled empty comment

* Used finally

* CI

* Fixed test

* Spillage card integration (#33832)

* Created getContentFlaggingFields API

* created getPostPropertyValues API

* WIP

* Created useContentFlaggingFields hook

* WIP

* WIP

* Added option to retain data for reviewers

* Displayed deleted post's preview

* DIsplayed all properties

* Adding field name i18n

* WIP - managing i18n able texts

* Finished displaying all fields

* Manual cleanup

* lint fixes

* team role filter logic fix

* Fixed tests

* created new API to fetch flagged posts

* lint fix

* Added new client methods

* test: add comprehensive tests for content flagging APIs

* Added new API tests

* fixed openapi spec

* Fixed DataSpillageReport tests

* Fixed PostMarkdown test

* Fixed PostPreviewPropertyRenderer test

* Added metadata to card renderer

* test fixes

* Added no comment placeholder

* Fixed test

* refactor: improve test mocking for data spillage report component

* test mock updates

* Updated reducer

* not resetting mocks

* WIP

* review fixes

* CI

* Fixed

* fixes

* Content flagging actions implementation (#33852)

* Added view detail button

* Created RemoveFlaggedMessageConfirmationModal modal

* Added key and remove flag request modal

* IMplemented delete flagged post

* Handled edge cases of deleting flagged post

* keep message

* UI integration

* Added WS event for post report update and handled deleted files of flagged post

* Added error handling in keep/remove forms

* i18n fixes

* Updated OpenAPI specs

* fixed types

* fixed types

* refactoring

* Fixed tests

* review fixes

* Added new property translations

* Improved test

* fixed test

* CI

* fixes

* CI

* fixed a test

* CI

* Review fixes

---------

Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>
2025-10-13 12:24:01 +05:30

233 lines
6.8 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"fmt"
"net/http"
"slices"
)
type ContentFlaggingEvent string
const (
EventFlagged ContentFlaggingEvent = "flagged"
EventAssigned ContentFlaggingEvent = "assigned"
EventContentRemoved ContentFlaggingEvent = "removed"
EventContentDismissed ContentFlaggingEvent = "dismissed"
)
type NotificationTarget string
const (
TargetReviewers NotificationTarget = "reviewers"
TargetAuthor NotificationTarget = "author"
TargetReporter NotificationTarget = "reporter"
)
var ContentFlaggingDefaultReasons = []string{
"Inappropriate content",
"Sensitive data",
"Security concern",
"Harassment or abuse",
"Spam or phishing",
}
type ContentFlaggingNotificationSettings struct {
EventTargetMapping map[ContentFlaggingEvent][]NotificationTarget
}
func (cfs *ContentFlaggingNotificationSettings) SetDefaults() {
if cfs.EventTargetMapping == nil {
cfs.EventTargetMapping = make(map[ContentFlaggingEvent][]NotificationTarget)
}
if _, exists := cfs.EventTargetMapping[EventFlagged]; !exists {
cfs.EventTargetMapping[EventFlagged] = []NotificationTarget{TargetReviewers}
} else {
// Ensure TargetReviewers is always included for EventFlagged
if !slices.Contains(cfs.EventTargetMapping[EventFlagged], TargetReviewers) {
cfs.EventTargetMapping[EventFlagged] = append(cfs.EventTargetMapping[EventFlagged], TargetReviewers)
}
}
if _, exists := cfs.EventTargetMapping[EventAssigned]; !exists {
cfs.EventTargetMapping[EventAssigned] = []NotificationTarget{TargetReviewers}
}
if _, exists := cfs.EventTargetMapping[EventContentRemoved]; !exists {
cfs.EventTargetMapping[EventContentRemoved] = []NotificationTarget{TargetReviewers, TargetAuthor, TargetReporter}
}
if _, exists := cfs.EventTargetMapping[EventContentDismissed]; !exists {
cfs.EventTargetMapping[EventContentDismissed] = []NotificationTarget{TargetReviewers, TargetReporter}
}
}
func (cfs *ContentFlaggingNotificationSettings) IsValid() *AppError {
// Reviewers must be notified when content is flagged
// Disabling this option is not allowed in the UI, so this check is for safety and consistency.
// Only valid events and targets are allowed
for event, targets := range cfs.EventTargetMapping {
if event != EventFlagged && event != EventAssigned && event != EventContentRemoved && event != EventContentDismissed {
return NewAppError("Config.IsValid", "model.config.is_valid.notification_settings.invalid_event", nil, "", http.StatusBadRequest)
}
for _, target := range targets {
if target != TargetReviewers && target != TargetAuthor && target != TargetReporter {
return NewAppError("Config.IsValid", "model.config.is_valid.notification_settings.invalid_target", nil, fmt.Sprintf("target: %s", target), http.StatusBadRequest)
}
}
}
if len(cfs.EventTargetMapping[EventFlagged]) == 0 {
return NewAppError("Config.IsValid", "model.config.is_valid.notification_settings.reviewer_flagged_notification_disabled", nil, "", http.StatusBadRequest)
}
// Search for the TargetReviewers in the EventFlagged event
reviewerFound := slices.Contains(cfs.EventTargetMapping[EventFlagged], TargetReviewers)
if !reviewerFound {
return NewAppError("Config.IsValid", "model.config.is_valid.notification_settings.reviewer_flagged_notification_disabled", nil, "", http.StatusBadRequest)
}
return nil
}
type TeamReviewerSetting struct {
Enabled *bool
ReviewerIds []string
}
type ReviewerSettings struct {
CommonReviewers *bool
SystemAdminsAsReviewers *bool
TeamAdminsAsReviewers *bool
}
func (rs *ReviewerSettings) SetDefaults() {
if rs.CommonReviewers == nil {
rs.CommonReviewers = NewPointer(true)
}
if rs.SystemAdminsAsReviewers == nil {
rs.SystemAdminsAsReviewers = NewPointer(false)
}
if rs.TeamAdminsAsReviewers == nil {
rs.TeamAdminsAsReviewers = NewPointer(true)
}
}
type AdditionalContentFlaggingSettings struct {
Reasons *[]string
ReporterCommentRequired *bool
ReviewerCommentRequired *bool
HideFlaggedContent *bool
}
func (acfs *AdditionalContentFlaggingSettings) SetDefaults() {
if acfs.Reasons == nil {
acfs.Reasons = &ContentFlaggingDefaultReasons
}
if acfs.ReporterCommentRequired == nil {
acfs.ReporterCommentRequired = NewPointer(true)
}
if acfs.ReviewerCommentRequired == nil {
acfs.ReviewerCommentRequired = NewPointer(true)
}
if acfs.HideFlaggedContent == nil {
acfs.HideFlaggedContent = NewPointer(true)
}
}
func (acfs *AdditionalContentFlaggingSettings) IsValid() *AppError {
if acfs.Reasons == nil || len(*acfs.Reasons) == 0 {
return NewAppError("Config.IsValid", "model.config.is_valid.content_flagging.reasons_not_set.app_error", nil, "", http.StatusBadRequest)
}
return nil
}
type ContentFlaggingSettingsBase struct {
EnableContentFlagging *bool
NotificationSettings *ContentFlaggingNotificationSettings
AdditionalSettings *AdditionalContentFlaggingSettings
}
func (cfs *ContentFlaggingSettingsBase) SetDefaults() {
if cfs.EnableContentFlagging == nil {
cfs.EnableContentFlagging = NewPointer(false)
}
if cfs.NotificationSettings == nil {
cfs.NotificationSettings = &ContentFlaggingNotificationSettings{
EventTargetMapping: make(map[ContentFlaggingEvent][]NotificationTarget),
}
}
if cfs.AdditionalSettings == nil {
cfs.AdditionalSettings = &AdditionalContentFlaggingSettings{}
}
cfs.NotificationSettings.SetDefaults()
cfs.AdditionalSettings.SetDefaults()
}
func (cfs *ContentFlaggingSettingsBase) IsValid() *AppError {
if err := cfs.NotificationSettings.IsValid(); err != nil {
return err
}
if err := cfs.AdditionalSettings.IsValid(); err != nil {
return err
}
return nil
}
type ContentFlaggingSettings struct {
ContentFlaggingSettingsBase
ReviewerSettings *ReviewerSettings
}
func (cfs *ContentFlaggingSettings) SetDefaults() {
cfs.ContentFlaggingSettingsBase.SetDefaults()
if cfs.ReviewerSettings == nil {
cfs.ReviewerSettings = &ReviewerSettings{}
}
cfs.ReviewerSettings.SetDefaults()
}
func (cfs *ContentFlaggingSettings) IsValid() *AppError {
return cfs.ContentFlaggingSettingsBase.IsValid()
}
type ContentFlaggingReportingConfig struct {
Reasons *[]string `json:"reasons"`
ReporterCommentRequired *bool `json:"reporter_comment_required"`
ReviewerCommentRequired *bool `json:"reviewer_comment_required"`
NotifyReporterOnDismissal *bool `json:"notify_reporter_on_dismissal,omitempty"`
NotifyReporterOnRemoval *bool `json:"notify_reporter_on_removal,omitempty"`
}
type ReviewerIDsSettings struct {
CommonReviewerIds []string
TeamReviewersSetting map[string]*TeamReviewerSetting
}
func (rs *ReviewerIDsSettings) SetDefaults() {
if rs.CommonReviewerIds == nil {
rs.CommonReviewerIds = []string{}
}
if rs.TeamReviewersSetting == nil {
rs.TeamReviewersSetting = map[string]*TeamReviewerSetting{}
}
}