Return 404 when thread membership not found (#21960)

We were incorrectly returning by checking
for `err != nil` when a 404 error was included
in that condition.

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2023-01-03 21:30:51 +05:30
коммит произвёл GitHub
родитель 356188de28
Коммит e4b050693d
3 изменённых файлов: 29 добавлений и 12 удалений

Просмотреть файл

@@ -2408,6 +2408,11 @@ func TestFollowThreadSkipsParticipants(t *testing.T) {
for _, p := range thread.Participants { for _, p := range thread.Participants {
require.True(t, p.Id == sysadmin.Id || p.Id == user.Id) require.True(t, p.Id == sysadmin.Id || p.Id == user.Id)
} }
threadMembership.PostId = "notfound"
_, err = th.App.GetThreadForUser(threadMembership, false)
require.NotNil(t, err)
assert.Equal(t, http.StatusNotFound, err.StatusCode)
} }
func TestAutofollowBasedOnRootPost(t *testing.T) { func TestAutofollowBasedOnRootPost(t *testing.T) {

Просмотреть файл

@@ -2484,24 +2484,31 @@ func (a *App) GetThreadsForUser(userID, teamID string, options model.GetUserThre
} }
func (a *App) GetThreadMembershipForUser(userId, threadId string) (*model.ThreadMembership, *model.AppError) { func (a *App) GetThreadMembershipForUser(userId, threadId string) (*model.ThreadMembership, *model.AppError) {
threadMembership, err := a.Srv().Store().Thread().GetMembershipForUser(userId, threadId) threadMembership, nErr := a.Srv().Store().Thread().GetMembershipForUser(userId, threadId)
if err != nil { if nErr != nil {
return nil, model.NewAppError("GetThreadMembershipForUser", "app.user.get_thread_membership_for_user.app_error", nil, "", http.StatusInternalServerError).Wrap(err) var nfErr *store.ErrNotFound
} switch {
if threadMembership == nil { case errors.As(nErr, &nfErr):
return nil, model.NewAppError("GetThreadMembershipForUser", "app.user.get_thread_membership_for_user.not_found", nil, "thread membership not found/followed", http.StatusNotFound) return nil, model.NewAppError("GetThreadMembershipForUser", "app.user.get_thread_membership_for_user.not_found", nil, "", http.StatusNotFound).Wrap(nErr)
default:
return nil, model.NewAppError("GetThreadMembershipForUser", "app.user.get_thread_membership_for_user.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
}
} }
return threadMembership, nil return threadMembership, nil
} }
func (a *App) GetThreadForUser(threadMembership *model.ThreadMembership, extended bool) (*model.ThreadResponse, *model.AppError) { func (a *App) GetThreadForUser(threadMembership *model.ThreadMembership, extended bool) (*model.ThreadResponse, *model.AppError) {
thread, err := a.Srv().Store().Thread().GetThreadForUser(threadMembership, extended, a.isPostPriorityEnabled()) thread, nErr := a.Srv().Store().Thread().GetThreadForUser(threadMembership, extended, a.isPostPriorityEnabled())
if err != nil { if nErr != nil {
return nil, model.NewAppError("GetThreadForUser", "app.user.get_threads_for_user.app_error", nil, "", http.StatusInternalServerError).Wrap(err) var nfErr *store.ErrNotFound
} switch {
if thread == nil { case errors.As(nErr, &nfErr):
return nil, model.NewAppError("GetThreadForUser", "app.user.get_threads_for_user.not_found", nil, "thread not found/followed", http.StatusNotFound) return nil, model.NewAppError("GetThreadForUser", "app.user.get_threads_for_user.not_found", nil, "thread not found/followed", http.StatusNotFound)
default:
return nil, model.NewAppError("GetThreadForUser", "app.user.get_threads_for_user.app_error", nil, "", http.StatusInternalServerError).Wrap(nErr)
}
} }
a.sanitizeProfiles(thread.Participants, false) a.sanitizeProfiles(thread.Participants, false)
thread.Post.SanitizeProps() thread.Post.SanitizeProps()
return thread, nil return thread, nil

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"net/http"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@@ -1700,6 +1701,10 @@ func TestUpdateThreadReadForUser(t *testing.T) {
require.Nil(t, appErr) require.Nil(t, appErr)
require.NotNil(t, threadMembership) require.NotNil(t, threadMembership)
assert.True(t, threadMembership.Following) assert.True(t, threadMembership.Following)
_, appErr = th.App.GetThreadMembershipForUser(th.BasicUser.Id, "notfound")
require.NotNil(t, appErr)
assert.Equal(t, http.StatusNotFound, appErr.StatusCode)
}) })
t.Run("Ensure no panic on error", func(t *testing.T) { t.Run("Ensure no panic on error", func(t *testing.T) {