Skip to content

Commit

Permalink
Adds includeDeleted flag to get remote cluster app and store methods (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdelacroix authored Sep 13, 2024
1 parent 46ca774 commit cae456d
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 57 deletions.
14 changes: 7 additions & 7 deletions server/channels/api4/remote_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func remoteClusterPing(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

rc, appErr := c.App.GetRemoteCluster(frame.RemoteId)
rc, appErr := c.App.GetRemoteCluster(frame.RemoteId, false)
if appErr != nil {
c.SetInvalidRemoteIdError(frame.RemoteId)
return
Expand Down Expand Up @@ -110,7 +110,7 @@ func remoteClusterAcceptMessage(c *Context, w http.ResponseWriter, r *http.Reque
return
}

rc, appErr := c.App.GetRemoteCluster(frame.RemoteId)
rc, appErr := c.App.GetRemoteCluster(frame.RemoteId, false)
if appErr != nil {
c.SetInvalidRemoteIdError(frame.RemoteId)
return
Expand Down Expand Up @@ -158,7 +158,7 @@ func remoteClusterConfirmInvite(c *Context, w http.ResponseWriter, r *http.Reque
return
}

rc, err := c.App.GetRemoteCluster(frame.RemoteId)
rc, err := c.App.GetRemoteCluster(frame.RemoteId, false)
if err != nil {
c.SetInvalidRemoteIdError(frame.RemoteId)
return
Expand Down Expand Up @@ -527,7 +527,7 @@ func generateRemoteClusterInvite(c *Context, w http.ResponseWriter, r *http.Requ
return
}

rc, appErr := c.App.GetRemoteCluster(c.Params.RemoteId)
rc, appErr := c.App.GetRemoteCluster(c.Params.RemoteId, false)
if appErr != nil {
c.Err = appErr
return
Expand Down Expand Up @@ -561,7 +561,7 @@ func getRemoteCluster(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

rc, err := c.App.GetRemoteCluster(c.Params.RemoteId)
rc, err := c.App.GetRemoteCluster(c.Params.RemoteId, true)
if err != nil {
c.Err = err
return
Expand Down Expand Up @@ -601,7 +601,7 @@ func patchRemoteCluster(c *Context, w http.ResponseWriter, r *http.Request) {
audit.AddEventParameterAuditable(auditRec, "remotecluster_patch", &patch)
defer c.LogAuditRec(auditRec)

orc, err := c.App.GetRemoteCluster(c.Params.RemoteId)
orc, err := c.App.GetRemoteCluster(c.Params.RemoteId, false)
if err != nil {
c.Err = err
return
Expand Down Expand Up @@ -645,7 +645,7 @@ func deleteRemoteCluster(c *Context, w http.ResponseWriter, r *http.Request) {
audit.AddEventParameter(auditRec, "remote_id", c.Params.RemoteId)
defer c.LogAuditRec(auditRec)

orc, err := c.App.GetRemoteCluster(c.Params.RemoteId)
orc, err := c.App.GetRemoteCluster(c.Params.RemoteId, false)
if err != nil {
c.Err = err
return
Expand Down
8 changes: 4 additions & 4 deletions server/channels/api4/remote_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestCreateRemoteCluster(t *testing.T) {
require.NotZero(t, rcWithInvite.Password)
require.Len(t, rcWithInvite.Password, 16)

rc, appErr := th.App.GetRemoteCluster(rcWithInvite.RemoteCluster.RemoteId)
rc, appErr := th.App.GetRemoteCluster(rcWithInvite.RemoteCluster.RemoteId, false)
require.Nil(t, appErr)
require.Equal(t, rcWithTeamNoPassword.Name, rc.Name)

Expand All @@ -266,7 +266,7 @@ func TestCreateRemoteCluster(t *testing.T) {
// by the endpoint
require.Zero(t, rcWithInvite.Password)

rc, appErr := th.App.GetRemoteCluster(rcWithInvite.RemoteCluster.RemoteId)
rc, appErr := th.App.GetRemoteCluster(rcWithInvite.RemoteCluster.RemoteId, false)
require.Nil(t, appErr)
require.Equal(t, rcWithTeamAndPassword.Name, rc.Name)

Expand Down Expand Up @@ -599,7 +599,7 @@ func TestDeleteRemoteCluster(t *testing.T) {

t.Run("should correctly delete the remote cluster", func(t *testing.T) {
// ensure the remote cluster is not deleted
initialRC, appErr := th.App.GetRemoteCluster(rc.RemoteId)
initialRC, appErr := th.App.GetRemoteCluster(rc.RemoteId, false)
require.Nil(t, appErr)
require.NotEmpty(t, initialRC)
require.Zero(t, initialRC.DeleteAt)
Expand All @@ -608,7 +608,7 @@ func TestDeleteRemoteCluster(t *testing.T) {
CheckOKStatus(t, resp)
require.NoError(t, err)

deletedRC, appErr := th.App.GetRemoteCluster(rc.RemoteId)
deletedRC, appErr := th.App.GetRemoteCluster(rc.RemoteId, true)
require.Nil(t, appErr)
require.NotEmpty(t, deletedRC)
require.NotZero(t, deletedRC.DeleteAt)
Expand Down
6 changes: 3 additions & 3 deletions server/channels/api4/shared_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func getSharedChannelRemotesByRemoteCluster(c *Context, w http.ResponseWriter, r
return
}

if _, appErr := c.App.GetRemoteCluster(c.Params.RemoteId); appErr != nil {
if _, appErr := c.App.GetRemoteCluster(c.Params.RemoteId, true); appErr != nil {
c.Err = appErr
return
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func inviteRemoteClusterToChannel(c *Context, w http.ResponseWriter, r *http.Req
return
}

if rc, appErr := c.App.GetRemoteCluster(c.Params.RemoteId); appErr != nil || rc.DeleteAt != 0 {
if _, appErr := c.App.GetRemoteCluster(c.Params.RemoteId, false); appErr != nil {
c.SetInvalidRemoteIdError(c.Params.RemoteId)
return
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func uninviteRemoteClusterToChannel(c *Context, w http.ResponseWriter, r *http.R
return
}

if rc, appErr := c.App.GetRemoteCluster(c.Params.RemoteId); appErr != nil || rc.DeleteAt != 0 {
if _, appErr := c.App.GetRemoteCluster(c.Params.RemoteId, false); appErr != nil {
c.SetInvalidRemoteIdError(c.Params.RemoteId)
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/channels/app/app_iface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/channels/app/opentracing/opentracing_layer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/channels/app/platform/shared_channel_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func handleInvitation(ps *PlatformService, syncService SharedChannelServiceIFace
return nil
}

rc, err := ps.Store.RemoteCluster().Get(*participant.RemoteId)
rc, err := ps.Store.RemoteCluster().Get(*participant.RemoteId, false)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("couldn't find remote cluster %s, for creating shared channel invitation for a DM", *participant.RemoteId))
}
Expand Down
6 changes: 3 additions & 3 deletions server/channels/app/remote_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (a *App) AddRemoteCluster(rc *model.RemoteCluster) (*model.RemoteCluster, *
}

func (a *App) PatchRemoteCluster(rcId string, patch *model.RemoteClusterPatch) (*model.RemoteCluster, *model.AppError) {
rc, err := a.GetRemoteCluster(rcId)
rc, err := a.GetRemoteCluster(rcId, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -152,8 +152,8 @@ func (a *App) DeleteRemoteCluster(remoteClusterId string) (bool, *model.AppError
return deleted, nil
}

func (a *App) GetRemoteCluster(remoteClusterId string) (*model.RemoteCluster, *model.AppError) {
rc, err := a.Srv().Store().RemoteCluster().Get(remoteClusterId)
func (a *App) GetRemoteCluster(remoteClusterId string, includeDeleted bool) (*model.RemoteCluster, *model.AppError) {
rc, err := a.Srv().Store().RemoteCluster().Get(remoteClusterId, includeDeleted)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
Expand Down
4 changes: 2 additions & 2 deletions server/channels/app/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (a *App) GetCloudSession(token string) (*model.Session, *model.AppError) {
}

func (a *App) GetRemoteClusterSession(token string, remoteId string) (*model.Session, *model.AppError) {
rc, appErr := a.GetRemoteCluster(remoteId)
if appErr == nil && rc.DeleteAt == 0 && subtle.ConstantTimeCompare([]byte(rc.Token), []byte(token)) == 1 {
rc, appErr := a.GetRemoteCluster(remoteId, false)
if appErr == nil && subtle.ConstantTimeCompare([]byte(rc.Token), []byte(token)) == 1 {
// Need a bare-bones session object for later checks
session := &model.Session{
Token: token,
Expand Down
5 changes: 1 addition & 4 deletions server/channels/app/slashcommands/command_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,10 @@ func (sp *ShareProvider) doInviteRemote(a *app.App, c request.CTX, args *model.C
}()
}

rc, appErr := a.GetRemoteCluster(remoteID)
rc, appErr := a.GetRemoteCluster(remoteID, false)
if appErr != nil {
return responsef(args.T("api.command_share.remote_id_invalid.error", map[string]any{"Error": appErr.Error()}))
}
if rc.DeleteAt != 0 {
return responsef(args.T("api.command_share.remote_id_invalid.error", map[string]any{"Error": "entity is deleted"}))
}

if err = a.InviteRemoteToChannel(args.ChannelId, remoteID, args.UserId, true); err != nil {
return responsef(args.T("api.command_share.invite_remote_to_channel.error", map[string]any{"Error": err.Error()}))
Expand Down
4 changes: 2 additions & 2 deletions server/channels/store/opentracinglayer/opentracinglayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/channels/store/retrylayer/retrylayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions server/channels/store/sqlstore/remote_cluster_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,16 @@ func (s sqlRemoteClusterStore) Delete(remoteId string) (bool, error) {
return count > 0, nil
}

func (s sqlRemoteClusterStore) Get(remoteId string) (*model.RemoteCluster, error) {
func (s sqlRemoteClusterStore) Get(remoteId string, includeDeleted bool) (*model.RemoteCluster, error) {
query := s.getQueryBuilder().
Select(remoteClusterFields("")...).
From("RemoteClusters").
Where(sq.Eq{"RemoteId": remoteId})

if !includeDeleted {
query = query.Where(sq.Eq{"DeleteAt": 0})
}

queryString, args, err := query.ToSql()
if err != nil {
return nil, errors.Wrap(err, "remote_cluster_get_tosql")
Expand Down Expand Up @@ -272,7 +276,7 @@ func (s sqlRemoteClusterStore) GetAll(offset, limit int, filter model.RemoteClus
}

func (s sqlRemoteClusterStore) UpdateTopics(remoteClusterid string, topics string) (*model.RemoteCluster, error) {
rc, err := s.Get(remoteClusterid)
rc, err := s.Get(remoteClusterid, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion server/channels/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ type RemoteClusterStore interface {
Save(rc *model.RemoteCluster) (*model.RemoteCluster, error)
Update(rc *model.RemoteCluster) (*model.RemoteCluster, error)
Delete(remoteClusterId string) (bool, error)
Get(remoteClusterId string) (*model.RemoteCluster, error)
Get(remoteClusterId string, includeDeleted bool) (*model.RemoteCluster, error)
GetByPluginID(pluginID string) (*model.RemoteCluster, error)
GetAll(offset, limit int, filter model.RemoteClusterQueryFilter) ([]*model.RemoteCluster, error)
UpdateTopics(remoteClusterId string, topics string) (*model.RemoteCluster, error)
Expand Down
18 changes: 9 additions & 9 deletions server/channels/store/storetest/mocks/RemoteClusterStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions server/channels/store/storetest/remote_cluster_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func testRemoteClusterDelete(t *testing.T, rctx request.CTX, ss store.Store) {
require.NoError(t, err)
require.True(t, deleted)

deletedRC, err := ss.RemoteCluster().Get(rcSaved.RemoteId)
deletedRC, err := ss.RemoteCluster().Get(rcSaved.RemoteId, true)
require.NoError(t, err)
require.NotZero(t, deletedRC.DeleteAt)
})
Expand Down Expand Up @@ -202,7 +202,7 @@ func testRemoteClusterDelete(t *testing.T, rctx request.CTX, ss store.Store) {
require.NoError(t, err)
require.True(t, deleted)

deletedRC, err := ss.RemoteCluster().Get(rcSaved.RemoteId)
deletedRC, err := ss.RemoteCluster().Get(rcSaved.RemoteId, true)
require.NoError(t, err)
require.NotZero(t, deletedRC.DeleteAt)

Expand Down Expand Up @@ -230,15 +230,38 @@ func testRemoteClusterGet(t *testing.T, _ request.CTX, ss store.Store) {
rcSaved, err := ss.RemoteCluster().Save(rc)
require.NoError(t, err)

rcGet, err := ss.RemoteCluster().Get(rcSaved.RemoteId)
rcGet, err := ss.RemoteCluster().Get(rcSaved.RemoteId, false)
require.NoError(t, err)
require.Equal(t, rcSaved.RemoteId, rcGet.RemoteId)
require.Equal(t, rcSaved.PluginID, rcGet.PluginID)
require.True(t, rcGet.IsOptionFlagSet(model.BitflagOptionAutoShareDMs))
})

t.Run("Get deleted", func(t *testing.T) {
rc := &model.RemoteCluster{
Name: "shortlived_remote_3",
SiteURL: makeSiteURL(),
CreatorId: model.NewId(),
PluginID: model.NewId(),
DeleteAt: 123,
}
rc.SetOptionFlag(model.BitflagOptionAutoShareDMs)
rcSaved, err := ss.RemoteCluster().Save(rc)
require.NoError(t, err)

rcGet, err := ss.RemoteCluster().Get(rcSaved.RemoteId, false)
require.Error(t, err)
require.Empty(t, rcGet)

rcGetDeleted, err := ss.RemoteCluster().Get(rcSaved.RemoteId, true)
require.NoError(t, err)
require.Equal(t, rcSaved.RemoteId, rcGetDeleted.RemoteId)
require.Equal(t, rcSaved.PluginID, rcGetDeleted.PluginID)
require.True(t, rcGetDeleted.IsOptionFlagSet(model.BitflagOptionAutoShareDMs))
})

t.Run("Get not found", func(t *testing.T) {
_, err := ss.RemoteCluster().Get(model.NewId())
_, err := ss.RemoteCluster().Get(model.NewId(), false)
require.Error(t, err)
})
}
Expand All @@ -248,7 +271,7 @@ func testRemoteClusterGetByPluginID(t *testing.T, _ request.CTX, ss store.Store)

t.Run("GetByPluginID", func(t *testing.T) {
rc := &model.RemoteCluster{
Name: "shortlived_remote_3",
Name: "shortlived_remote_4",
SiteURL: makeSiteURL(),
CreatorId: model.NewId(),
PluginID: pluginID,
Expand Down Expand Up @@ -731,7 +754,7 @@ func testRemoteClusterUpdateTopics(t *testing.T, _ request.CTX, ss store.Store)
_, err = ss.RemoteCluster().UpdateTopics(remoteId, tt.topics)
require.NoError(t, err)

rcUpdated, err := ss.RemoteCluster().Get(remoteId)
rcUpdated, err := ss.RemoteCluster().Get(remoteId, false)
require.NoError(t, err)

require.Equal(t, tt.expected, rcUpdated.Topics)
Expand Down
Loading

0 comments on commit cae456d

Please sign in to comment.