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
354 lines
13 KiB
Go
354 lines
13 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"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"
|
|
"github.com/mattermost/mattermost/server/v8/channels/store"
|
|
)
|
|
|
|
func (a *App) SaveAcknowledgementForPost(rctx request.CTX, postID, userID string) (*model.PostAcknowledgement, *model.AppError) {
|
|
return a.saveAcknowledgementForPostWithPost(rctx, nil, userID, postID)
|
|
}
|
|
|
|
func (a *App) saveAcknowledgementForPostWithPost(rctx request.CTX, post *model.Post, userID string, postID ...string) (*model.PostAcknowledgement, *model.AppError) {
|
|
if post == nil {
|
|
if len(postID) == 0 {
|
|
return nil, model.NewAppError("SaveAcknowledgementForPost", "app.acknowledgement.save.missing_post.app_error", nil, "", http.StatusBadRequest)
|
|
}
|
|
var err *model.AppError
|
|
post, err = a.GetSinglePost(rctx, postID[0], false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
channel, err := a.GetChannel(rctx, post.ChannelId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if channel.DeleteAt > 0 {
|
|
return nil, model.NewAppError("SaveAcknowledgementForPost", "api.acknowledgement.save.archived_channel.app_error", nil, "", http.StatusForbidden)
|
|
}
|
|
|
|
// Pre-populate the ChannelId to save a DB call in store
|
|
acknowledgement := &model.PostAcknowledgement{
|
|
PostId: post.Id,
|
|
UserId: userID,
|
|
ChannelId: post.ChannelId,
|
|
}
|
|
|
|
savedAck, nErr := a.Srv().Store().PostAcknowledgement().SaveWithModel(acknowledgement)
|
|
if nErr != nil {
|
|
var appErr *model.AppError
|
|
switch {
|
|
case errors.As(nErr, &appErr):
|
|
return nil, appErr
|
|
default:
|
|
return nil, model.NewAppError("SaveAcknowledgementForPost", "app.acknowledgement.save.save.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
}
|
|
|
|
if appErr := a.ResolvePersistentNotification(rctx, post, userID); appErr != nil {
|
|
a.CountNotificationReason(model.NotificationStatusError, model.NotificationTypeWebsocket, model.NotificationReasonResolvePersistentNotificationError, model.NotificationNoPlatform)
|
|
a.Log().LogM(mlog.MlvlNotificationError, "Error resolving persistent notification",
|
|
mlog.String("sender_id", userID),
|
|
mlog.String("post_id", post.RootId),
|
|
mlog.String("status", model.NotificationStatusError),
|
|
mlog.String("reason", model.NotificationReasonResolvePersistentNotificationError),
|
|
mlog.Err(appErr),
|
|
)
|
|
return nil, appErr
|
|
}
|
|
|
|
// The post is always modified since the UpdateAt always changes
|
|
a.Srv().Store().Post().InvalidateLastPostTimeCache(channel.Id)
|
|
|
|
a.sendAcknowledgementEvent(rctx, model.WebsocketEventAcknowledgementAdded, savedAck, post)
|
|
|
|
// Trigger post updated event to ensure shared channel sync
|
|
a.sendPostUpdateEvent(rctx, post)
|
|
|
|
return savedAck, nil
|
|
}
|
|
|
|
func (a *App) DeleteAcknowledgementForPost(rctx request.CTX, postID, userID string) *model.AppError {
|
|
return a.deleteAcknowledgementForPostWithPost(rctx, nil, userID, postID)
|
|
}
|
|
|
|
func (a *App) deleteAcknowledgementForPostWithPost(rctx request.CTX, post *model.Post, userID string, postID ...string) *model.AppError {
|
|
if post == nil {
|
|
if len(postID) == 0 {
|
|
return model.NewAppError("DeleteAcknowledgementForPost", "app.acknowledgement.delete.missing_post.app_error", nil, "", http.StatusBadRequest)
|
|
}
|
|
var err *model.AppError
|
|
post, err = a.GetSinglePost(rctx, postID[0], false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
channel, err := a.GetChannel(rctx, post.ChannelId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if channel.DeleteAt > 0 {
|
|
return model.NewAppError("DeleteAcknowledgementForPost", "api.acknowledgement.delete.archived_channel.app_error", nil, "", http.StatusForbidden)
|
|
}
|
|
|
|
oldAck, nErr := a.Srv().Store().PostAcknowledgement().Get(post.Id, userID)
|
|
|
|
if nErr != nil {
|
|
var nfErr *store.ErrNotFound
|
|
switch {
|
|
case errors.As(nErr, &nfErr):
|
|
return model.NewAppError("GetPostAcknowledgement", "app.acknowledgement.get.app_error", nil, "", http.StatusNotFound).Wrap(nErr)
|
|
default:
|
|
return model.NewAppError("GetPostAcknowledgement", "app.acknowledgement.get.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
}
|
|
|
|
if model.GetMillis()-oldAck.AcknowledgedAt > 5*60*1000 {
|
|
return model.NewAppError("DeleteAcknowledgementForPost", "api.acknowledgement.delete.deadline.app_error", nil, "", http.StatusForbidden)
|
|
}
|
|
|
|
nErr = a.Srv().Store().PostAcknowledgement().Delete(oldAck)
|
|
if nErr != nil {
|
|
return model.NewAppError("DeleteAcknowledgementForPost", "app.acknowledgement.delete.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
|
|
// The post is always modified since the UpdateAt always changes
|
|
a.Srv().Store().Post().InvalidateLastPostTimeCache(channel.Id)
|
|
|
|
a.sendAcknowledgementEvent(rctx, model.WebsocketEventAcknowledgementRemoved, oldAck, post)
|
|
|
|
// Trigger post updated event to ensure shared channel sync
|
|
a.sendPostUpdateEvent(rctx, post)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) GetAcknowledgementsForPost(postID string) ([]*model.PostAcknowledgement, *model.AppError) {
|
|
acknowledgements, nErr := a.Srv().Store().PostAcknowledgement().GetForPost(postID)
|
|
if nErr != nil {
|
|
return nil, model.NewAppError("GetAcknowledgementsForPost", "app.acknowledgement.getforpost.get.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
|
|
return acknowledgements, nil
|
|
}
|
|
|
|
func (a *App) GetAcknowledgementsForPostList(postList *model.PostList) (map[string][]*model.PostAcknowledgement, *model.AppError) {
|
|
acknowledgements, err := a.Srv().Store().PostAcknowledgement().GetForPosts(postList.Order)
|
|
|
|
if err != nil {
|
|
return nil, model.NewAppError("GetPostAcknowledgementsForPostList", "app.acknowledgement.get.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
|
}
|
|
|
|
acknowledgementsMap := make(map[string][]*model.PostAcknowledgement)
|
|
|
|
for _, ack := range acknowledgements {
|
|
acknowledgementsMap[ack.PostId] = append(acknowledgementsMap[ack.PostId], ack)
|
|
}
|
|
|
|
return acknowledgementsMap, nil
|
|
}
|
|
|
|
// SaveAcknowledgementsForPost saves multiple acknowledgements for a post in a single operation.
|
|
func (a *App) SaveAcknowledgementsForPost(rctx request.CTX, postID string, userIDs []string) ([]*model.PostAcknowledgement, *model.AppError) {
|
|
if len(userIDs) == 0 {
|
|
return []*model.PostAcknowledgement{}, nil
|
|
}
|
|
|
|
post, err := a.GetSinglePost(rctx, postID, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channel, err := a.GetChannel(rctx, post.ChannelId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if channel.DeleteAt > 0 {
|
|
return nil, model.NewAppError("SaveAcknowledgementsForPost", "api.acknowledgement.save.archived_channel.app_error", nil, "", http.StatusForbidden)
|
|
}
|
|
|
|
// Create acknowledgements with current timestamp
|
|
acknowledgedAt := model.GetMillis()
|
|
var acknowledgements []*model.PostAcknowledgement
|
|
|
|
for _, userID := range userIDs {
|
|
acknowledgements = append(acknowledgements, &model.PostAcknowledgement{
|
|
PostId: post.Id,
|
|
UserId: userID,
|
|
ChannelId: post.ChannelId,
|
|
AcknowledgedAt: acknowledgedAt,
|
|
})
|
|
}
|
|
|
|
// Save all acknowledgements
|
|
savedAcks, nErr := a.Srv().Store().PostAcknowledgement().BatchSave(acknowledgements)
|
|
if nErr != nil {
|
|
var appErr *model.AppError
|
|
switch {
|
|
case errors.As(nErr, &appErr):
|
|
return nil, appErr
|
|
default:
|
|
return nil, model.NewAppError("SaveAcknowledgementsForPost", "app.acknowledgement.batch_save.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
}
|
|
|
|
// Resolve persistent notifications for each user
|
|
for _, userID := range userIDs {
|
|
if appErr := a.ResolvePersistentNotification(rctx, post, userID); appErr != nil {
|
|
a.CountNotificationReason(model.NotificationStatusError, model.NotificationTypeWebsocket, model.NotificationReasonResolvePersistentNotificationError, model.NotificationNoPlatform)
|
|
a.Log().LogM(mlog.MlvlNotificationError, "Error resolving persistent notification",
|
|
mlog.String("sender_id", userID),
|
|
mlog.String("post_id", post.RootId),
|
|
mlog.String("status", model.NotificationStatusError),
|
|
mlog.String("reason", model.NotificationReasonResolvePersistentNotificationError),
|
|
mlog.Err(appErr),
|
|
)
|
|
// We continue processing other acknowledgements even if one fails
|
|
}
|
|
}
|
|
|
|
// The post is always modified since the UpdateAt always changes
|
|
a.Srv().Store().Post().InvalidateLastPostTimeCache(channel.Id)
|
|
|
|
// Send WebSocket events for each acknowledgement
|
|
for _, ack := range savedAcks {
|
|
a.sendAcknowledgementEvent(rctx, model.WebsocketEventAcknowledgementAdded, ack, post)
|
|
}
|
|
|
|
// Trigger post updated event to ensure shared channel sync
|
|
a.sendPostUpdateEvent(rctx, post)
|
|
|
|
return savedAcks, nil
|
|
}
|
|
|
|
func (a *App) sendAcknowledgementEvent(rctx request.CTX, event model.WebsocketEventType, acknowledgement *model.PostAcknowledgement, post *model.Post) {
|
|
// send out that a acknowledgement has been added/removed
|
|
message := model.NewWebSocketEvent(event, "", post.ChannelId, "", nil, "")
|
|
|
|
acknowledgementJSON, err := json.Marshal(acknowledgement)
|
|
if err != nil {
|
|
rctx.Logger().Warn("Failed to encode acknowledgement to JSON", mlog.Err(err))
|
|
}
|
|
message.Add("acknowledgement", string(acknowledgementJSON))
|
|
a.Publish(message)
|
|
}
|
|
|
|
func (a *App) SaveAcknowledgementForPostWithModel(rctx request.CTX, acknowledgement *model.PostAcknowledgement) (*model.PostAcknowledgement, *model.AppError) {
|
|
// Get the post to verify it exists and get the channel
|
|
post, err := a.GetSinglePost(rctx, acknowledgement.PostId, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channel, err := a.GetChannel(rctx, post.ChannelId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if channel.DeleteAt > 0 {
|
|
return nil, model.NewAppError("SaveAcknowledgementForPostWithModel", "api.acknowledgement.save.archived_channel.app_error", nil, "", http.StatusForbidden)
|
|
}
|
|
|
|
// Make sure ChannelId is set
|
|
if acknowledgement.ChannelId == "" {
|
|
acknowledgement.ChannelId = post.ChannelId
|
|
}
|
|
|
|
savedAck, nErr := a.Srv().Store().PostAcknowledgement().SaveWithModel(acknowledgement)
|
|
if nErr != nil {
|
|
var appErr *model.AppError
|
|
switch {
|
|
case errors.As(nErr, &appErr):
|
|
return nil, appErr
|
|
default:
|
|
return nil, model.NewAppError("SaveAcknowledgementForPostWithModel", "app.acknowledgement.save.save.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
}
|
|
|
|
if appErr := a.ResolvePersistentNotification(rctx, post, acknowledgement.UserId); appErr != nil {
|
|
a.CountNotificationReason(model.NotificationStatusError, model.NotificationTypeWebsocket, model.NotificationReasonResolvePersistentNotificationError, model.NotificationNoPlatform)
|
|
a.Log().LogM(mlog.MlvlNotificationError, "Error resolving persistent notification",
|
|
mlog.String("sender_id", acknowledgement.UserId),
|
|
mlog.String("post_id", post.RootId),
|
|
mlog.String("status", model.NotificationStatusError),
|
|
mlog.String("reason", model.NotificationReasonResolvePersistentNotificationError),
|
|
mlog.Err(appErr),
|
|
)
|
|
return nil, appErr
|
|
}
|
|
|
|
// The post is always modified since the UpdateAt always changes
|
|
a.Srv().Store().Post().InvalidateLastPostTimeCache(channel.Id)
|
|
|
|
a.sendAcknowledgementEvent(rctx, model.WebsocketEventAcknowledgementAdded, savedAck, post)
|
|
|
|
// Trigger post updated event to ensure shared channel sync
|
|
a.sendPostUpdateEvent(rctx, post)
|
|
|
|
return savedAck, nil
|
|
}
|
|
|
|
func (a *App) DeleteAcknowledgementForPostWithModel(rctx request.CTX, acknowledgement *model.PostAcknowledgement) *model.AppError {
|
|
// Get the post to verify it exists and get the channel
|
|
post, err := a.GetSinglePost(rctx, acknowledgement.PostId, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
channel, err := a.GetChannel(rctx, post.ChannelId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if channel.DeleteAt > 0 {
|
|
return model.NewAppError("DeleteAcknowledgementForPostWithModel", "api.acknowledgement.delete.archived_channel.app_error", nil, "", http.StatusForbidden)
|
|
}
|
|
|
|
nErr := a.Srv().Store().PostAcknowledgement().Delete(acknowledgement)
|
|
if nErr != nil {
|
|
return model.NewAppError("DeleteAcknowledgementForPostWithModel", "app.acknowledgement.delete.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
|
|
}
|
|
|
|
// The post is always modified since the UpdateAt always changes
|
|
a.Srv().Store().Post().InvalidateLastPostTimeCache(channel.Id)
|
|
|
|
a.sendAcknowledgementEvent(rctx, model.WebsocketEventAcknowledgementRemoved, acknowledgement, post)
|
|
|
|
// Trigger post updated event to ensure shared channel sync
|
|
a.sendPostUpdateEvent(rctx, post)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) sendPostUpdateEvent(rctx request.CTX, post *model.Post) {
|
|
if post == nil {
|
|
rctx.Logger().Warn("sendPostUpdateEvent called with nil post")
|
|
return
|
|
}
|
|
|
|
// Send a post edited event to trigger shared channel sync
|
|
message := model.NewWebSocketEvent(model.WebsocketEventPostEdited, "", post.ChannelId, "", nil, "")
|
|
|
|
// Prepare the post with metadata for the event
|
|
preparedPost := a.PreparePostForClient(rctx, post, &model.PreparePostForClientOpts{IsEditPost: true, IncludePriority: true})
|
|
|
|
if appErr := a.publishWebsocketEventForPost(rctx, preparedPost, message); appErr != nil {
|
|
rctx.Logger().Warn("Failed to send post update event for acknowledgement sync", mlog.String("post_id", post.Id), mlog.Err(appErr))
|
|
}
|
|
}
|