From 2593860063525539252f28092a8c8845d64e14f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20V=C3=A9lez?= Date: Fri, 8 Nov 2024 10:48:23 +0100 Subject: [PATCH] MM-61068 - send ws event on scheduled message job send (#29153) Co-authored-by: Mattermost Build --- server/channels/app/app_iface.go | 1 + .../channels/app/opentracing/opentracing_layer.go | 15 +++++++++++++++ server/channels/app/scheduled_post.go | 8 ++++---- server/channels/app/scheduled_post_job.go | 5 +++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/server/channels/app/app_iface.go b/server/channels/app/app_iface.go index f573d82bfb..830689a372 100644 --- a/server/channels/app/app_iface.go +++ b/server/channels/app/app_iface.go @@ -1003,6 +1003,7 @@ type AppIface interface { ProcessScheduledPosts(rctx request.CTX) ProcessSlackText(text string) string Publish(message *model.WebSocketEvent) + PublishScheduledPostEvent(rctx request.CTX, eventType model.WebsocketEventType, scheduledPost *model.ScheduledPost, connectionId string) PublishUserTyping(userID, channelID, parentId string) *model.AppError PurgeBleveIndexes(c request.CTX) *model.AppError PurgeElasticsearchIndexes(c request.CTX, indexes []string) *model.AppError diff --git a/server/channels/app/opentracing/opentracing_layer.go b/server/channels/app/opentracing/opentracing_layer.go index 80e1088cec..1135303f68 100644 --- a/server/channels/app/opentracing/opentracing_layer.go +++ b/server/channels/app/opentracing/opentracing_layer.go @@ -14064,6 +14064,21 @@ func (a *OpenTracingAppLayer) Publish(message *model.WebSocketEvent) { a.app.Publish(message) } +func (a *OpenTracingAppLayer) PublishScheduledPostEvent(rctx request.CTX, eventType model.WebsocketEventType, scheduledPost *model.ScheduledPost, connectionId string) { + origCtx := a.ctx + span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.PublishScheduledPostEvent") + + a.ctx = newCtx + a.app.Srv().Store().SetContext(newCtx) + defer func() { + a.app.Srv().Store().SetContext(origCtx) + a.ctx = origCtx + }() + + defer span.Finish() + a.app.PublishScheduledPostEvent(rctx, eventType, scheduledPost, connectionId) +} + func (a *OpenTracingAppLayer) PublishUserTyping(userID string, channelID string, parentId string) *model.AppError { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.PublishUserTyping") diff --git a/server/channels/app/scheduled_post.go b/server/channels/app/scheduled_post.go index d7844710b9..3ddff6d326 100644 --- a/server/channels/app/scheduled_post.go +++ b/server/channels/app/scheduled_post.go @@ -34,7 +34,7 @@ func (a *App) SaveScheduledPost(rctx request.CTX, scheduledPost *model.Scheduled 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) } - publishScheduledPostEvent(a, rctx, model.WebsocketScheduledPostCreated, savedScheduledPost, connectionId) + a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostCreated, savedScheduledPost, connectionId) return savedScheduledPost, nil } @@ -85,7 +85,7 @@ func (a *App) UpdateScheduledPost(rctx request.CTX, userId string, scheduledPost 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) } - publishScheduledPostEvent(a, rctx, model.WebsocketScheduledPostUpdated, scheduledPost, connectionId) + a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostUpdated, scheduledPost, connectionId) return scheduledPost, nil } @@ -108,12 +108,12 @@ func (a *App) DeleteScheduledPost(rctx request.CTX, userId, scheduledPostId, con return nil, model.NewAppError("app.DeleteScheduledPost", "app.delete_scheduled_post.delete_error", map[string]any{"user_id": userId, "scheduled_post_id": scheduledPostId}, "", http.StatusInternalServerError) } - publishScheduledPostEvent(a, rctx, model.WebsocketScheduledPostDeleted, scheduledPost, connectionId) + a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostDeleted, scheduledPost, connectionId) return scheduledPost, nil } -func publishScheduledPostEvent(a *App, rctx request.CTX, eventType model.WebsocketEventType, scheduledPost *model.ScheduledPost, connectionId string) { +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 diff --git a/server/channels/app/scheduled_post_job.go b/server/channels/app/scheduled_post_job.go index 81a5ba8265..660386225f 100644 --- a/server/channels/app/scheduled_post_job.go +++ b/server/channels/app/scheduled_post_job.go @@ -227,6 +227,9 @@ func (a *App) postScheduledPost(rctx request.CTX, scheduledPost *model.Scheduled return scheduledPost, appErr } + // send the WS event to delete the just posted scheduledPost from list + a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostDeleted, scheduledPost, "") + return scheduledPost, nil } @@ -361,6 +364,8 @@ func (a *App) handleFailedScheduledPosts(rctx request.CTX, failedScheduledPosts mlog.Err(err), ) } + // send WS event for updating the scheduled post with the error code + a.PublishScheduledPostEvent(rctx, model.WebsocketScheduledPostUpdated, failedScheduledPost, "") } if len(failedScheduledPosts) > 0 {