Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
172 lines
7.9 KiB
Go
172 lines
7.9 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
"github.com/mattermost/mattermost/server/public/shared/request"
|
|
)
|
|
|
|
// recurringScheduledPostsEnabled gates turning recurrence on. The api4 routes already enforce
|
|
// the ScheduledPosts setting and license for every scheduled post request, and the job keeps
|
|
// sending existing recurring series regardless of the flag.
|
|
func (a *App) recurringScheduledPostsEnabled() bool {
|
|
return a.Config().FeatureFlags.RecurringScheduledPosts
|
|
}
|
|
|
|
func recurringScheduledPostsDisabledError(where string) *model.AppError {
|
|
return model.NewAppError(where, "app.scheduled_post.recurring_disabled.app_error", nil, "", http.StatusBadRequest)
|
|
}
|
|
|
|
func (a *App) SaveScheduledPost(rctx request.CTX, scheduledPost *model.ScheduledPost, connectionId string) (*model.ScheduledPost, *model.AppError) {
|
|
maxMessageLength := a.Srv().Store().ScheduledPost().GetMaxMessageSize()
|
|
scheduledPost.PreSave()
|
|
if validationErr := scheduledPost.IsValid(maxMessageLength); validationErr != nil {
|
|
return nil, validationErr
|
|
}
|
|
|
|
if scheduledPost.RepeatType != model.ScheduledPostRepeatTypeNone && !a.recurringScheduledPostsEnabled() {
|
|
return nil, recurringScheduledPostsDisabledError("App.SaveScheduledPost")
|
|
}
|
|
|
|
// validate the channel is not archived
|
|
channel, appErr := a.GetChannel(rctx, scheduledPost.ChannelId)
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
|
|
restrictDM, appErr := a.CheckIfChannelIsRestrictedDM(rctx, channel)
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
|
|
if restrictDM {
|
|
err := model.NewAppError("App.scheduledPostPreSaveChecks", "app.save_scheduled_post.restricted_dm.error", nil, "", http.StatusBadRequest)
|
|
return nil, err
|
|
}
|
|
|
|
if channel.DeleteAt > 0 {
|
|
return nil, model.NewAppError("App.scheduledPostPreSaveChecks", "app.save_scheduled_post.channel_deleted.app_error", map[string]any{"user_id": scheduledPost.UserId, "channel_id": scheduledPost.ChannelId}, "", http.StatusBadRequest)
|
|
}
|
|
|
|
scheduledPost, appErr = a.runGuardedScheduledPostWillBeCreated(rctx, scheduledPost, "SaveScheduledPost", func(reason string) *model.AppError {
|
|
return model.NewAppError("SaveScheduledPost", "app.scheduled_post.save.rejected_by_plugin", map[string]any{"Reason": reason}, "", http.StatusBadRequest)
|
|
})
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
|
|
savedScheduledPost, err := a.Srv().Store().ScheduledPost().CreateScheduledPost(rctx, scheduledPost)
|
|
if err != nil {
|
|
return nil, model.NewAppError("App.ScheduledPost", "app.save_scheduled_post.save.app_error", map[string]any{"user_id": scheduledPost.UserId, "channel_id": scheduledPost.ChannelId}, "", http.StatusBadRequest).Wrap(err)
|
|
}
|
|
|
|
a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostCreated, savedScheduledPost, connectionId)
|
|
|
|
return savedScheduledPost, nil
|
|
}
|
|
|
|
func (a *App) GetUserTeamScheduledPosts(rctx request.CTX, userId, teamId string) ([]*model.ScheduledPost, *model.AppError) {
|
|
scheduledPosts, err := a.Srv().Store().ScheduledPost().GetScheduledPostsForUser(rctx, userId, teamId)
|
|
if err != nil {
|
|
return nil, model.NewAppError("App.GetUserTeamScheduledPosts", "app.get_user_team_scheduled_posts.error", map[string]any{"user_id": userId, "team_id": teamId}, "", http.StatusInternalServerError).Wrap(err)
|
|
}
|
|
|
|
if scheduledPosts == nil {
|
|
scheduledPosts = []*model.ScheduledPost{}
|
|
}
|
|
|
|
for _, scheduledPost := range scheduledPosts {
|
|
a.prepareDraftWithFileInfos(rctx, userId, &scheduledPost.Draft)
|
|
}
|
|
|
|
return scheduledPosts, nil
|
|
}
|
|
|
|
func (a *App) UpdateScheduledPost(rctx request.CTX, userId string, scheduledPost *model.ScheduledPost, connectionId string) (*model.ScheduledPost, *model.AppError) {
|
|
maxMessageLength := a.Srv().Store().ScheduledPost().GetMaxMessageSize()
|
|
scheduledPost.PreUpdate()
|
|
if validationErr := scheduledPost.IsValid(maxMessageLength); validationErr != nil {
|
|
return nil, validationErr
|
|
}
|
|
|
|
existingScheduledPost, err := a.Srv().Store().ScheduledPost().Get(rctx, scheduledPost.Id)
|
|
if err != nil {
|
|
return nil, model.NewAppError("app.UpdateScheduledPost", "app.update_scheduled_post.get_scheduled_post.error", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPost.Id}, "", http.StatusInternalServerError).Wrap(err)
|
|
}
|
|
|
|
if existingScheduledPost == nil {
|
|
return nil, model.NewAppError("app.UpdateScheduledPost", "app.update_scheduled_post.existing_scheduled_post.not_exist", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPost.Id}, "", http.StatusNotFound)
|
|
}
|
|
|
|
// Only turning recurrence ON is blocked while the flag is off; editing or rescheduling an
|
|
// already-recurring post and turning recurrence off must stay possible so users can manage
|
|
// series created while the flag was on.
|
|
if scheduledPost.RepeatType != model.ScheduledPostRepeatTypeNone &&
|
|
existingScheduledPost.RepeatType == model.ScheduledPostRepeatTypeNone &&
|
|
!a.recurringScheduledPostsEnabled() {
|
|
return nil, recurringScheduledPostsDisabledError("App.UpdateScheduledPost")
|
|
}
|
|
|
|
// This step is not required for update but is useful as we want to return the
|
|
// updated scheduled post. It's better to do this before calling update than after.
|
|
scheduledPost.RestoreNonUpdatableFields(existingScheduledPost)
|
|
scheduledPost.ErrorCode = ""
|
|
scheduledPost.ProcessedAt = 0
|
|
|
|
var appErr *model.AppError
|
|
scheduledPost, appErr = a.runGuardedScheduledPostWillBeCreated(rctx, scheduledPost, "UpdateScheduledPost", func(reason string) *model.AppError {
|
|
return model.NewAppError("UpdateScheduledPost", "app.scheduled_post.update.rejected_by_plugin", map[string]any{"Reason": reason}, "", http.StatusBadRequest)
|
|
})
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
|
|
if err := a.Srv().Store().ScheduledPost().UpdatedScheduledPost(rctx, scheduledPost); err != nil {
|
|
return nil, model.NewAppError("app.UpdateScheduledPost", "app.update_scheduled_post.update.error", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPost.Id}, "", http.StatusInternalServerError).Wrap(err)
|
|
}
|
|
|
|
a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostUpdated, scheduledPost, connectionId)
|
|
|
|
return scheduledPost, nil
|
|
}
|
|
|
|
func (a *App) DeleteScheduledPost(rctx request.CTX, userId, scheduledPostId, connectionId string) (*model.ScheduledPost, *model.AppError) {
|
|
scheduledPost, err := a.Srv().Store().ScheduledPost().Get(rctx, scheduledPostId)
|
|
if err != nil {
|
|
return nil, model.NewAppError("app.DeleteScheduledPost", "app.delete_scheduled_post.get_scheduled_post.error", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPostId}, "", http.StatusInternalServerError).Wrap(err)
|
|
}
|
|
|
|
if scheduledPost == nil {
|
|
return nil, model.NewAppError("app.DeleteScheduledPost", "app.delete_scheduled_post.existing_scheduled_post.not_exist", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPostId}, "", http.StatusNotFound)
|
|
}
|
|
|
|
if err := a.Srv().Store().ScheduledPost().PermanentlyDeleteScheduledPosts([]string{scheduledPostId}); err != nil {
|
|
return nil, model.NewAppError("app.DeleteScheduledPost", "app.delete_scheduled_post.delete_error", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPostId}, "", http.StatusInternalServerError).Wrap(err)
|
|
}
|
|
|
|
a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostDeleted, scheduledPost, connectionId)
|
|
|
|
return scheduledPost, nil
|
|
}
|
|
|
|
func (a *App) PublishScheduledPostEvent(rctx request.CTX, eventType model.WebsocketEventType, scheduledPost *model.ScheduledPost, connectionId string) {
|
|
if scheduledPost == nil {
|
|
rctx.Logger().Warn("publishScheduledPostEvent called with nil scheduledPost")
|
|
return
|
|
}
|
|
message := model.NewWebSocketEvent(eventType, "", "", scheduledPost.UserId, nil, connectionId)
|
|
scheduledPostJSON, jsonErr := json.Marshal(scheduledPost)
|
|
if jsonErr != nil {
|
|
rctx.Logger().Warn("publishScheduledPostEvent - Failed to Marshal", mlog.Err(jsonErr))
|
|
return
|
|
}
|
|
message.Add("scheduledPost", string(scheduledPostJSON))
|
|
a.Publish(message)
|
|
}
|