Files
team/server/channels/app/scheduled_post.go
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

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/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/shared/mlog"
"github.com/hanzoteam/server/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(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(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(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(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(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)
}