github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/store/storetest/channel_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"sort"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/mattermost/gorp"
    16  	"github.com/mattermost/mattermost-server/model"
    17  	"github.com/mattermost/mattermost-server/store"
    18  )
    19  
    20  type SqlSupplier interface {
    21  	GetMaster() *gorp.DbMap
    22  }
    23  
    24  func cleanupChannels(t *testing.T, ss store.Store) {
    25  	result := <-ss.Channel().GetAllChannels(0, 100000, true)
    26  	if result.Err != nil {
    27  		t.Fatal("error cleaning all channels")
    28  	}
    29  	list := result.Data.(*model.ChannelListWithTeamData)
    30  	for _, channel := range *list {
    31  		ss.Channel().PermanentDelete(channel.Id)
    32  	}
    33  }
    34  
    35  func TestChannelStore(t *testing.T, ss store.Store, s SqlSupplier) {
    36  	createDefaultRoles(t, ss)
    37  
    38  	t.Run("Save", func(t *testing.T) { testChannelStoreSave(t, ss) })
    39  	t.Run("SaveDirectChannel", func(t *testing.T) { testChannelStoreSaveDirectChannel(t, ss) })
    40  	t.Run("CreateDirectChannel", func(t *testing.T) { testChannelStoreCreateDirectChannel(t, ss) })
    41  	t.Run("Update", func(t *testing.T) { testChannelStoreUpdate(t, ss) })
    42  	t.Run("GetChannelUnread", func(t *testing.T) { testGetChannelUnread(t, ss) })
    43  	t.Run("Get", func(t *testing.T) { testChannelStoreGet(t, ss) })
    44  	t.Run("GetForPost", func(t *testing.T) { testChannelStoreGetForPost(t, ss) })
    45  	t.Run("Restore", func(t *testing.T) { testChannelStoreRestore(t, ss) })
    46  	t.Run("Delete", func(t *testing.T) { testChannelStoreDelete(t, ss) })
    47  	t.Run("GetByName", func(t *testing.T) { testChannelStoreGetByName(t, ss) })
    48  	t.Run("GetByNames", func(t *testing.T) { testChannelStoreGetByNames(t, ss) })
    49  	t.Run("GetDeletedByName", func(t *testing.T) { testChannelStoreGetDeletedByName(t, ss) })
    50  	t.Run("GetDeleted", func(t *testing.T) { testChannelStoreGetDeleted(t, ss) })
    51  	t.Run("ChannelMemberStore", func(t *testing.T) { testChannelMemberStore(t, ss) })
    52  	t.Run("ChannelDeleteMemberStore", func(t *testing.T) { testChannelDeleteMemberStore(t, ss) })
    53  	t.Run("GetChannels", func(t *testing.T) { testChannelStoreGetChannels(t, ss) })
    54  	t.Run("GetAllChannels", func(t *testing.T) { testChannelStoreGetAllChannels(t, ss) })
    55  	t.Run("GetMoreChannels", func(t *testing.T) { testChannelStoreGetMoreChannels(t, ss) })
    56  	t.Run("GetPublicChannelsForTeam", func(t *testing.T) { testChannelStoreGetPublicChannelsForTeam(t, ss) })
    57  	t.Run("GetPublicChannelsByIdsForTeam", func(t *testing.T) { testChannelStoreGetPublicChannelsByIdsForTeam(t, ss) })
    58  	t.Run("GetChannelCounts", func(t *testing.T) { testChannelStoreGetChannelCounts(t, ss) })
    59  	t.Run("GetMembersForUser", func(t *testing.T) { testChannelStoreGetMembersForUser(t, ss) })
    60  	t.Run("UpdateLastViewedAt", func(t *testing.T) { testChannelStoreUpdateLastViewedAt(t, ss) })
    61  	t.Run("IncrementMentionCount", func(t *testing.T) { testChannelStoreIncrementMentionCount(t, ss) })
    62  	t.Run("UpdateChannelMember", func(t *testing.T) { testUpdateChannelMember(t, ss) })
    63  	t.Run("GetMember", func(t *testing.T) { testGetMember(t, ss) })
    64  	t.Run("GetMemberForPost", func(t *testing.T) { testChannelStoreGetMemberForPost(t, ss) })
    65  	t.Run("GetMemberCount", func(t *testing.T) { testGetMemberCount(t, ss) })
    66  	t.Run("SearchMore", func(t *testing.T) { testChannelStoreSearchMore(t, ss) })
    67  	t.Run("SearchInTeam", func(t *testing.T) { testChannelStoreSearchInTeam(t, ss) })
    68  	t.Run("SearchAllChannels", func(t *testing.T) { testChannelStoreSearchAllChannels(t, ss) })
    69  	t.Run("AutocompleteInTeamForSearch", func(t *testing.T) { testChannelStoreAutocompleteInTeamForSearch(t, ss) })
    70  	t.Run("GetMembersByIds", func(t *testing.T) { testChannelStoreGetMembersByIds(t, ss) })
    71  	t.Run("AnalyticsDeletedTypeCount", func(t *testing.T) { testChannelStoreAnalyticsDeletedTypeCount(t, ss) })
    72  	t.Run("GetPinnedPosts", func(t *testing.T) { testChannelStoreGetPinnedPosts(t, ss) })
    73  	t.Run("MaxChannelsPerTeam", func(t *testing.T) { testChannelStoreMaxChannelsPerTeam(t, ss) })
    74  	t.Run("GetChannelsByScheme", func(t *testing.T) { testChannelStoreGetChannelsByScheme(t, ss) })
    75  	t.Run("MigrateChannelMembers", func(t *testing.T) { testChannelStoreMigrateChannelMembers(t, ss) })
    76  	t.Run("ResetAllChannelSchemes", func(t *testing.T) { testResetAllChannelSchemes(t, ss) })
    77  	t.Run("ClearAllCustomRoleAssignments", func(t *testing.T) { testChannelStoreClearAllCustomRoleAssignments(t, ss) })
    78  	t.Run("MaterializedPublicChannels", func(t *testing.T) { testMaterializedPublicChannels(t, ss, s) })
    79  	t.Run("GetAllChannelsForExportAfter", func(t *testing.T) { testChannelStoreGetAllChannelsForExportAfter(t, ss) })
    80  	t.Run("GetChannelMembersForExport", func(t *testing.T) { testChannelStoreGetChannelMembersForExport(t, ss) })
    81  	t.Run("RemoveAllDeactivatedMembers", func(t *testing.T) { testChannelStoreRemoveAllDeactivatedMembers(t, ss) })
    82  }
    83  
    84  func testChannelStoreSave(t *testing.T, ss store.Store) {
    85  	teamId := model.NewId()
    86  
    87  	o1 := model.Channel{}
    88  	o1.TeamId = teamId
    89  	o1.DisplayName = "Name"
    90  	o1.Name = "zz" + model.NewId() + "b"
    91  	o1.Type = model.CHANNEL_OPEN
    92  
    93  	if err := (<-ss.Channel().Save(&o1, -1)).Err; err != nil {
    94  		t.Fatal("couldn't save item", err)
    95  	}
    96  
    97  	if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil {
    98  		t.Fatal("shouldn't be able to update from save")
    99  	}
   100  
   101  	o1.Id = ""
   102  	if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil {
   103  		t.Fatal("should be unique name")
   104  	}
   105  
   106  	o1.Id = ""
   107  	o1.Name = "zz" + model.NewId() + "b"
   108  	o1.Type = model.CHANNEL_DIRECT
   109  	if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil {
   110  		t.Fatal("Should not be able to save direct channel")
   111  	}
   112  }
   113  
   114  func testChannelStoreSaveDirectChannel(t *testing.T, ss store.Store) {
   115  	teamId := model.NewId()
   116  
   117  	o1 := model.Channel{}
   118  	o1.TeamId = teamId
   119  	o1.DisplayName = "Name"
   120  	o1.Name = "zz" + model.NewId() + "b"
   121  	o1.Type = model.CHANNEL_DIRECT
   122  
   123  	u1 := &model.User{}
   124  	u1.Email = MakeEmail()
   125  	u1.Nickname = model.NewId()
   126  	store.Must(ss.User().Save(u1))
   127  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1))
   128  
   129  	u2 := &model.User{}
   130  	u2.Email = MakeEmail()
   131  	u2.Nickname = model.NewId()
   132  	store.Must(ss.User().Save(u2))
   133  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1))
   134  
   135  	m1 := model.ChannelMember{}
   136  	m1.ChannelId = o1.Id
   137  	m1.UserId = u1.Id
   138  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
   139  
   140  	m2 := model.ChannelMember{}
   141  	m2.ChannelId = o1.Id
   142  	m2.UserId = u2.Id
   143  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
   144  
   145  	if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err != nil {
   146  		t.Fatal("couldn't save direct channel", err)
   147  	}
   148  
   149  	members := (<-ss.Channel().GetMembers(o1.Id, 0, 100)).Data.(*model.ChannelMembers)
   150  	if len(*members) != 2 {
   151  		t.Fatal("should have saved 2 members")
   152  	}
   153  
   154  	if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil {
   155  		t.Fatal("shouldn't be able to update from save")
   156  	}
   157  
   158  	// Attempt to save a direct channel that already exists
   159  	o1a := model.Channel{
   160  		TeamId:      o1.TeamId,
   161  		DisplayName: o1.DisplayName,
   162  		Name:        o1.Name,
   163  		Type:        o1.Type,
   164  	}
   165  
   166  	if result := <-ss.Channel().SaveDirectChannel(&o1a, &m1, &m2); result.Err == nil {
   167  		t.Fatal("should've failed to save a duplicate direct channel")
   168  	} else if result.Err.Id != store.CHANNEL_EXISTS_ERROR {
   169  		t.Fatal("should've returned CHANNEL_EXISTS_ERROR")
   170  	} else if returned := result.Data.(*model.Channel); returned.Id != o1.Id {
   171  		t.Fatal("should've returned original channel when saving a duplicate direct channel")
   172  	}
   173  
   174  	// Attempt to save a non-direct channel
   175  	o1.Id = ""
   176  	o1.Name = "zz" + model.NewId() + "b"
   177  	o1.Type = model.CHANNEL_OPEN
   178  	if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil {
   179  		t.Fatal("Should not be able to save non-direct channel")
   180  	}
   181  
   182  	// Save yourself Direct Message
   183  	o1.Id = ""
   184  	o1.DisplayName = "Myself"
   185  	o1.Name = "zz" + model.NewId() + "b"
   186  	o1.Type = model.CHANNEL_DIRECT
   187  	if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m1)).Err; err != nil {
   188  		t.Fatal("couldn't save direct channel", err)
   189  	}
   190  
   191  	members = (<-ss.Channel().GetMembers(o1.Id, 0, 100)).Data.(*model.ChannelMembers)
   192  	if len(*members) != 1 {
   193  		t.Fatal("should have saved just 1 member")
   194  	}
   195  
   196  }
   197  
   198  func testChannelStoreCreateDirectChannel(t *testing.T, ss store.Store) {
   199  	u1 := &model.User{}
   200  	u1.Email = MakeEmail()
   201  	u1.Nickname = model.NewId()
   202  	store.Must(ss.User().Save(u1))
   203  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1))
   204  
   205  	u2 := &model.User{}
   206  	u2.Email = MakeEmail()
   207  	u2.Nickname = model.NewId()
   208  	store.Must(ss.User().Save(u2))
   209  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1))
   210  
   211  	res := <-ss.Channel().CreateDirectChannel(u1.Id, u2.Id)
   212  	if res.Err != nil {
   213  		t.Fatal("couldn't create direct channel", res.Err)
   214  	}
   215  	c1 := res.Data.(*model.Channel)
   216  	defer func() {
   217  		<-ss.Channel().PermanentDeleteMembersByChannel(c1.Id)
   218  		<-ss.Channel().PermanentDelete(c1.Id)
   219  	}()
   220  
   221  	members := (<-ss.Channel().GetMembers(c1.Id, 0, 100)).Data.(*model.ChannelMembers)
   222  	if len(*members) != 2 {
   223  		t.Fatal("should have saved 2 members")
   224  	}
   225  }
   226  
   227  func testChannelStoreUpdate(t *testing.T, ss store.Store) {
   228  	o1 := model.Channel{}
   229  	o1.TeamId = model.NewId()
   230  	o1.DisplayName = "Name"
   231  	o1.Name = "zz" + model.NewId() + "b"
   232  	o1.Type = model.CHANNEL_OPEN
   233  	store.Must(ss.Channel().Save(&o1, -1))
   234  
   235  	o2 := model.Channel{}
   236  	o2.TeamId = o1.TeamId
   237  	o2.DisplayName = "Name"
   238  	o2.Name = "zz" + model.NewId() + "b"
   239  	o2.Type = model.CHANNEL_OPEN
   240  	store.Must(ss.Channel().Save(&o2, -1))
   241  
   242  	time.Sleep(100 * time.Millisecond)
   243  
   244  	if err := (<-ss.Channel().Update(&o1)).Err; err != nil {
   245  		t.Fatal(err)
   246  	}
   247  
   248  	o1.DeleteAt = 100
   249  	if err := (<-ss.Channel().Update(&o1)).Err; err == nil {
   250  		t.Fatal("Update should have failed because channel is archived")
   251  	}
   252  
   253  	o1.DeleteAt = 0
   254  	o1.Id = "missing"
   255  	if err := (<-ss.Channel().Update(&o1)).Err; err == nil {
   256  		t.Fatal("Update should have failed because of missing key")
   257  	}
   258  
   259  	o1.Id = model.NewId()
   260  	if err := (<-ss.Channel().Update(&o1)).Err; err == nil {
   261  		t.Fatal("Update should have faile because id change")
   262  	}
   263  
   264  	o2.Name = o1.Name
   265  	if err := (<-ss.Channel().Update(&o2)).Err; err == nil {
   266  		t.Fatal("Update should have failed because of existing name")
   267  	}
   268  }
   269  
   270  func testGetChannelUnread(t *testing.T, ss store.Store) {
   271  	teamId1 := model.NewId()
   272  	teamId2 := model.NewId()
   273  
   274  	uid := model.NewId()
   275  	m1 := &model.TeamMember{TeamId: teamId1, UserId: uid}
   276  	m2 := &model.TeamMember{TeamId: teamId2, UserId: uid}
   277  	store.Must(ss.Team().SaveMember(m1, -1))
   278  	store.Must(ss.Team().SaveMember(m2, -1))
   279  	notifyPropsModel := model.GetDefaultChannelNotifyProps()
   280  
   281  	// Setup Channel 1
   282  	c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Downtown", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
   283  	store.Must(ss.Channel().Save(c1, -1))
   284  	cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: notifyPropsModel, MsgCount: 90}
   285  	store.Must(ss.Channel().SaveMember(cm1))
   286  
   287  	// Setup Channel 2
   288  	c2 := &model.Channel{TeamId: m2.TeamId, Name: model.NewId(), DisplayName: "Cultural", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
   289  	store.Must(ss.Channel().Save(c2, -1))
   290  	cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m2.UserId, NotifyProps: notifyPropsModel, MsgCount: 90, MentionCount: 5}
   291  	store.Must(ss.Channel().SaveMember(cm2))
   292  
   293  	// Check for Channel 1
   294  	if resp := <-ss.Channel().GetChannelUnread(c1.Id, uid); resp.Err != nil {
   295  		t.Fatal(resp.Err)
   296  	} else {
   297  		ch := resp.Data.(*model.ChannelUnread)
   298  		if c1.Id != ch.ChannelId {
   299  			t.Fatal("wrong channel id")
   300  		}
   301  
   302  		if teamId1 != ch.TeamId {
   303  			t.Fatal("wrong team id for channel 1")
   304  		}
   305  
   306  		if ch.NotifyProps == nil {
   307  			t.Fatal("wrong props for channel 1")
   308  		}
   309  
   310  		if ch.MentionCount != 0 {
   311  			t.Fatal("wrong MentionCount for channel 1")
   312  		}
   313  
   314  		if ch.MsgCount != 10 {
   315  			t.Fatal("wrong MsgCount for channel 1")
   316  		}
   317  	}
   318  
   319  	// Check for Channel 2
   320  	if resp2 := <-ss.Channel().GetChannelUnread(c2.Id, uid); resp2.Err != nil {
   321  		t.Fatal(resp2.Err)
   322  	} else {
   323  		ch2 := resp2.Data.(*model.ChannelUnread)
   324  		if c2.Id != ch2.ChannelId {
   325  			t.Fatal("wrong channel id")
   326  		}
   327  
   328  		if teamId2 != ch2.TeamId {
   329  			t.Fatal("wrong team id")
   330  		}
   331  
   332  		if ch2.MentionCount != 5 {
   333  			t.Fatal("wrong MentionCount for channel 2")
   334  		}
   335  
   336  		if ch2.MsgCount != 10 {
   337  			t.Fatal("wrong MsgCount for channel 2")
   338  		}
   339  	}
   340  }
   341  
   342  func testChannelStoreGet(t *testing.T, ss store.Store) {
   343  	o1 := model.Channel{}
   344  	o1.TeamId = model.NewId()
   345  	o1.DisplayName = "Name"
   346  	o1.Name = "zz" + model.NewId() + "b"
   347  	o1.Type = model.CHANNEL_OPEN
   348  	store.Must(ss.Channel().Save(&o1, -1))
   349  
   350  	if r1 := <-ss.Channel().Get(o1.Id, false); r1.Err != nil {
   351  		t.Fatal(r1.Err)
   352  	} else {
   353  		if r1.Data.(*model.Channel).ToJson() != o1.ToJson() {
   354  			t.Fatal("invalid returned channel")
   355  		}
   356  	}
   357  
   358  	if err := (<-ss.Channel().Get("", false)).Err; err == nil {
   359  		t.Fatal("Missing id should have failed")
   360  	}
   361  
   362  	u1 := &model.User{}
   363  	u1.Email = MakeEmail()
   364  	u1.Nickname = model.NewId()
   365  	store.Must(ss.User().Save(u1))
   366  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1))
   367  
   368  	u2 := model.User{}
   369  	u2.Email = MakeEmail()
   370  	u2.Nickname = model.NewId()
   371  	store.Must(ss.User().Save(&u2))
   372  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1))
   373  
   374  	o2 := model.Channel{}
   375  	o2.TeamId = model.NewId()
   376  	o2.DisplayName = "Direct Name"
   377  	o2.Name = "zz" + model.NewId() + "b"
   378  	o2.Type = model.CHANNEL_DIRECT
   379  
   380  	m1 := model.ChannelMember{}
   381  	m1.ChannelId = o2.Id
   382  	m1.UserId = u1.Id
   383  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
   384  
   385  	m2 := model.ChannelMember{}
   386  	m2.ChannelId = o2.Id
   387  	m2.UserId = u2.Id
   388  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
   389  
   390  	store.Must(ss.Channel().SaveDirectChannel(&o2, &m1, &m2))
   391  
   392  	if r2 := <-ss.Channel().Get(o2.Id, false); r2.Err != nil {
   393  		t.Fatal(r2.Err)
   394  	} else {
   395  		if r2.Data.(*model.Channel).ToJson() != o2.ToJson() {
   396  			t.Fatal("invalid returned channel")
   397  		}
   398  	}
   399  
   400  	if r4 := <-ss.Channel().Get(o2.Id, true); r4.Err != nil {
   401  		t.Fatal(r4.Err)
   402  	} else {
   403  		if r4.Data.(*model.Channel).ToJson() != o2.ToJson() {
   404  			t.Fatal("invalid returned channel")
   405  		}
   406  	}
   407  
   408  	if r3 := <-ss.Channel().GetAll(o1.TeamId); r3.Err != nil {
   409  		t.Fatal(r3.Err)
   410  	} else {
   411  		channels := r3.Data.([]*model.Channel)
   412  		if len(channels) == 0 {
   413  			t.Fatal("too little")
   414  		}
   415  	}
   416  
   417  	if r3 := <-ss.Channel().GetTeamChannels(o1.TeamId); r3.Err != nil {
   418  		t.Fatal(r3.Err)
   419  	} else {
   420  		channels := r3.Data.(*model.ChannelList)
   421  		if len(*channels) == 0 {
   422  			t.Fatal("too little")
   423  		}
   424  	}
   425  }
   426  
   427  func testChannelStoreGetForPost(t *testing.T, ss store.Store) {
   428  	o1 := store.Must(ss.Channel().Save(&model.Channel{
   429  		TeamId:      model.NewId(),
   430  		DisplayName: "Name",
   431  		Name:        "zz" + model.NewId() + "b",
   432  		Type:        model.CHANNEL_OPEN,
   433  	}, -1)).(*model.Channel)
   434  
   435  	p1 := store.Must(ss.Post().Save(&model.Post{
   436  		UserId:    model.NewId(),
   437  		ChannelId: o1.Id,
   438  		Message:   "test",
   439  	})).(*model.Post)
   440  
   441  	if r1 := <-ss.Channel().GetForPost(p1.Id); r1.Err != nil {
   442  		t.Fatal(r1.Err)
   443  	} else if r1.Data.(*model.Channel).Id != o1.Id {
   444  		t.Fatal("incorrect channel returned")
   445  	}
   446  }
   447  
   448  func testChannelStoreRestore(t *testing.T, ss store.Store) {
   449  	o1 := model.Channel{}
   450  	o1.TeamId = model.NewId()
   451  	o1.DisplayName = "Channel1"
   452  	o1.Name = "zz" + model.NewId() + "b"
   453  	o1.Type = model.CHANNEL_OPEN
   454  	store.Must(ss.Channel().Save(&o1, -1))
   455  
   456  	if r := <-ss.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil {
   457  		t.Fatal(r.Err)
   458  	}
   459  
   460  	if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt == 0 {
   461  		t.Fatal("should have been deleted")
   462  	}
   463  
   464  	if r := <-ss.Channel().Restore(o1.Id, model.GetMillis()); r.Err != nil {
   465  		t.Fatal(r.Err)
   466  	}
   467  
   468  	if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt != 0 {
   469  		t.Fatal("should have been restored")
   470  	}
   471  
   472  }
   473  
   474  func testChannelStoreDelete(t *testing.T, ss store.Store) {
   475  	o1 := model.Channel{}
   476  	o1.TeamId = model.NewId()
   477  	o1.DisplayName = "Channel1"
   478  	o1.Name = "zz" + model.NewId() + "b"
   479  	o1.Type = model.CHANNEL_OPEN
   480  	store.Must(ss.Channel().Save(&o1, -1))
   481  
   482  	o2 := model.Channel{}
   483  	o2.TeamId = o1.TeamId
   484  	o2.DisplayName = "Channel2"
   485  	o2.Name = "zz" + model.NewId() + "b"
   486  	o2.Type = model.CHANNEL_OPEN
   487  	store.Must(ss.Channel().Save(&o2, -1))
   488  
   489  	o3 := model.Channel{}
   490  	o3.TeamId = o1.TeamId
   491  	o3.DisplayName = "Channel3"
   492  	o3.Name = "zz" + model.NewId() + "b"
   493  	o3.Type = model.CHANNEL_OPEN
   494  	store.Must(ss.Channel().Save(&o3, -1))
   495  
   496  	o4 := model.Channel{}
   497  	o4.TeamId = o1.TeamId
   498  	o4.DisplayName = "Channel4"
   499  	o4.Name = "zz" + model.NewId() + "b"
   500  	o4.Type = model.CHANNEL_OPEN
   501  	store.Must(ss.Channel().Save(&o4, -1))
   502  
   503  	m1 := model.ChannelMember{}
   504  	m1.ChannelId = o1.Id
   505  	m1.UserId = model.NewId()
   506  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
   507  	store.Must(ss.Channel().SaveMember(&m1))
   508  
   509  	m2 := model.ChannelMember{}
   510  	m2.ChannelId = o2.Id
   511  	m2.UserId = m1.UserId
   512  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
   513  	store.Must(ss.Channel().SaveMember(&m2))
   514  
   515  	if r := <-ss.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil {
   516  		t.Fatal(r.Err)
   517  	}
   518  
   519  	if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt == 0 {
   520  		t.Fatal("should have been deleted")
   521  	}
   522  
   523  	if r := <-ss.Channel().Delete(o3.Id, model.GetMillis()); r.Err != nil {
   524  		t.Fatal(r.Err)
   525  	}
   526  
   527  	cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false)
   528  	require.Nil(t, cresult.Err)
   529  	list := cresult.Data.(*model.ChannelList)
   530  
   531  	if len(*list) != 1 {
   532  		t.Fatal("invalid number of channels")
   533  	}
   534  
   535  	cresult = <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 0, 100)
   536  	require.Nil(t, cresult.Err)
   537  	list = cresult.Data.(*model.ChannelList)
   538  
   539  	if len(*list) != 1 {
   540  		t.Fatal("invalid number of channels")
   541  	}
   542  
   543  	cresult = <-ss.Channel().PermanentDelete(o2.Id)
   544  	require.Nil(t, cresult.Err)
   545  
   546  	cresult = <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false)
   547  	if assert.NotNil(t, cresult.Err) {
   548  		require.Equal(t, "store.sql_channel.get_channels.not_found.app_error", cresult.Err.Id)
   549  	} else {
   550  		require.Equal(t, &model.ChannelList{}, cresult.Data.(*model.ChannelList))
   551  	}
   552  
   553  	if r := <-ss.Channel().PermanentDeleteByTeam(o1.TeamId); r.Err != nil {
   554  		t.Fatal(r.Err)
   555  	}
   556  }
   557  
   558  func testChannelStoreGetByName(t *testing.T, ss store.Store) {
   559  	o1 := model.Channel{}
   560  	o1.TeamId = model.NewId()
   561  	o1.DisplayName = "Name"
   562  	o1.Name = "zz" + model.NewId() + "b"
   563  	o1.Type = model.CHANNEL_OPEN
   564  	store.Must(ss.Channel().Save(&o1, -1))
   565  
   566  	r1 := <-ss.Channel().GetByName(o1.TeamId, o1.Name, true)
   567  	if r1.Err != nil {
   568  		t.Fatal(r1.Err)
   569  	} else {
   570  		if r1.Data.(*model.Channel).ToJson() != o1.ToJson() {
   571  			t.Fatal("invalid returned channel")
   572  		}
   573  	}
   574  
   575  	if err := (<-ss.Channel().GetByName(o1.TeamId, "", true)).Err; err == nil {
   576  		t.Fatal("Missing id should have failed")
   577  	}
   578  
   579  	if r1 := <-ss.Channel().GetByName(o1.TeamId, o1.Name, false); r1.Err != nil {
   580  		t.Fatal(r1.Err)
   581  	} else {
   582  		if r1.Data.(*model.Channel).ToJson() != o1.ToJson() {
   583  			t.Fatal("invalid returned channel")
   584  		}
   585  	}
   586  
   587  	if err := (<-ss.Channel().GetByName(o1.TeamId, "", false)).Err; err == nil {
   588  		t.Fatal("Missing id should have failed")
   589  	}
   590  
   591  	store.Must(ss.Channel().Delete(r1.Data.(*model.Channel).Id, model.GetMillis()))
   592  
   593  	if err := (<-ss.Channel().GetByName(o1.TeamId, r1.Data.(*model.Channel).Name, false)).Err; err == nil {
   594  		t.Fatal("Deleted channel should not be returned by GetByName()")
   595  	}
   596  }
   597  
   598  func testChannelStoreGetByNames(t *testing.T, ss store.Store) {
   599  	o1 := model.Channel{
   600  		TeamId:      model.NewId(),
   601  		DisplayName: "Name",
   602  		Name:        "zz" + model.NewId() + "b",
   603  		Type:        model.CHANNEL_OPEN,
   604  	}
   605  	store.Must(ss.Channel().Save(&o1, -1))
   606  
   607  	o2 := model.Channel{
   608  		TeamId:      o1.TeamId,
   609  		DisplayName: "Name",
   610  		Name:        "zz" + model.NewId() + "b",
   611  		Type:        model.CHANNEL_OPEN,
   612  	}
   613  	store.Must(ss.Channel().Save(&o2, -1))
   614  
   615  	for index, tc := range []struct {
   616  		TeamId      string
   617  		Names       []string
   618  		ExpectedIds []string
   619  	}{
   620  		{o1.TeamId, []string{o1.Name}, []string{o1.Id}},
   621  		{o1.TeamId, []string{o1.Name, o2.Name}, []string{o1.Id, o2.Id}},
   622  		{o1.TeamId, nil, nil},
   623  		{o1.TeamId, []string{"foo"}, nil},
   624  		{o1.TeamId, []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}},
   625  		{"", []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}},
   626  		{"asd", []string{o1.Name, "foo", o2.Name, o2.Name}, nil},
   627  	} {
   628  		r := <-ss.Channel().GetByNames(tc.TeamId, tc.Names, true)
   629  		require.Nil(t, r.Err)
   630  		channels := r.Data.([]*model.Channel)
   631  		var ids []string
   632  		for _, channel := range channels {
   633  			ids = append(ids, channel.Id)
   634  		}
   635  		sort.Strings(ids)
   636  		sort.Strings(tc.ExpectedIds)
   637  		assert.Equal(t, tc.ExpectedIds, ids, "tc %v", index)
   638  	}
   639  
   640  	store.Must(ss.Channel().Delete(o1.Id, model.GetMillis()))
   641  	store.Must(ss.Channel().Delete(o2.Id, model.GetMillis()))
   642  
   643  	r := <-ss.Channel().GetByNames(o1.TeamId, []string{o1.Name}, false)
   644  	require.Nil(t, r.Err)
   645  	channels := r.Data.([]*model.Channel)
   646  	assert.Len(t, channels, 0)
   647  }
   648  
   649  func testChannelStoreGetDeletedByName(t *testing.T, ss store.Store) {
   650  	o1 := model.Channel{}
   651  	o1.TeamId = model.NewId()
   652  	o1.DisplayName = "Name"
   653  	o1.Name = "zz" + model.NewId() + "b"
   654  	o1.Type = model.CHANNEL_OPEN
   655  	store.Must(ss.Channel().Save(&o1, -1))
   656  	now := model.GetMillis()
   657  	store.Must(ss.Channel().Delete(o1.Id, now))
   658  	o1.DeleteAt = now
   659  	o1.UpdateAt = now
   660  
   661  	if r1 := <-ss.Channel().GetDeletedByName(o1.TeamId, o1.Name); r1.Err != nil {
   662  		t.Fatal(r1.Err)
   663  	} else {
   664  		if r1.Data.(*model.Channel).ToJson() != o1.ToJson() {
   665  			t.Fatal("invalid returned channel")
   666  		}
   667  	}
   668  
   669  	if err := (<-ss.Channel().GetDeletedByName(o1.TeamId, "")).Err; err == nil {
   670  		t.Fatal("Missing id should have failed")
   671  	}
   672  }
   673  
   674  func testChannelStoreGetDeleted(t *testing.T, ss store.Store) {
   675  	o1 := model.Channel{}
   676  	o1.TeamId = model.NewId()
   677  	o1.DisplayName = "Channel1"
   678  	o1.Name = "zz" + model.NewId() + "b"
   679  	o1.Type = model.CHANNEL_OPEN
   680  	store.Must(ss.Channel().Save(&o1, -1))
   681  	store.Must(ss.Channel().Delete(o1.Id, model.GetMillis()))
   682  
   683  	cresult := <-ss.Channel().GetDeleted(o1.TeamId, 0, 100)
   684  	if cresult.Err != nil {
   685  		t.Fatal(cresult.Err)
   686  	}
   687  	list := cresult.Data.(*model.ChannelList)
   688  
   689  	if len(*list) != 1 {
   690  		t.Fatal("wrong list")
   691  	}
   692  
   693  	if (*list)[0].Name != o1.Name {
   694  		t.Fatal("missing channel")
   695  	}
   696  
   697  	o2 := model.Channel{}
   698  	o2.TeamId = o1.TeamId
   699  	o2.DisplayName = "Channel2"
   700  	o2.Name = "zz" + model.NewId() + "b"
   701  	o2.Type = model.CHANNEL_OPEN
   702  	store.Must(ss.Channel().Save(&o2, -1))
   703  
   704  	cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 100)
   705  	if cresult.Err != nil {
   706  		t.Fatal(cresult.Err)
   707  	}
   708  	list = cresult.Data.(*model.ChannelList)
   709  
   710  	if len(*list) != 1 {
   711  		t.Fatal("wrong list")
   712  	}
   713  
   714  	o3 := model.Channel{}
   715  	o3.TeamId = o1.TeamId
   716  	o3.DisplayName = "Channel3"
   717  	o3.Name = "zz" + model.NewId() + "b"
   718  	o3.Type = model.CHANNEL_OPEN
   719  	store.Must(ss.Channel().Save(&o3, -1))
   720  	store.Must(ss.Channel().SetDeleteAt(o3.Id, model.GetMillis(), model.GetMillis()))
   721  
   722  	cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 100)
   723  	if cresult.Err != nil {
   724  		t.Fatal(cresult.Err)
   725  	}
   726  	list = cresult.Data.(*model.ChannelList)
   727  
   728  	if len(*list) != 2 {
   729  		t.Fatal("wrong list length")
   730  	}
   731  
   732  	cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 1)
   733  	if cresult.Err != nil {
   734  		t.Fatal(cresult.Err)
   735  	}
   736  	list = cresult.Data.(*model.ChannelList)
   737  
   738  	if len(*list) != 1 {
   739  		t.Fatal("wrong list length")
   740  	}
   741  
   742  	cresult = <-ss.Channel().GetDeleted(o1.TeamId, 1, 1)
   743  	if cresult.Err != nil {
   744  		t.Fatal(cresult.Err)
   745  	}
   746  	list = cresult.Data.(*model.ChannelList)
   747  
   748  	if len(*list) != 1 {
   749  		t.Fatal("wrong list length")
   750  	}
   751  
   752  }
   753  
   754  func testChannelMemberStore(t *testing.T, ss store.Store) {
   755  	c1 := model.Channel{}
   756  	c1.TeamId = model.NewId()
   757  	c1.DisplayName = "NameName"
   758  	c1.Name = "zz" + model.NewId() + "b"
   759  	c1.Type = model.CHANNEL_OPEN
   760  	c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel)
   761  
   762  	c1t1 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel)
   763  	assert.EqualValues(t, 0, c1t1.ExtraUpdateAt, "ExtraUpdateAt should be 0")
   764  
   765  	u1 := model.User{}
   766  	u1.Email = MakeEmail()
   767  	u1.Nickname = model.NewId()
   768  	store.Must(ss.User().Save(&u1))
   769  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1))
   770  
   771  	u2 := model.User{}
   772  	u2.Email = MakeEmail()
   773  	u2.Nickname = model.NewId()
   774  	store.Must(ss.User().Save(&u2))
   775  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1))
   776  
   777  	o1 := model.ChannelMember{}
   778  	o1.ChannelId = c1.Id
   779  	o1.UserId = u1.Id
   780  	o1.NotifyProps = model.GetDefaultChannelNotifyProps()
   781  	store.Must(ss.Channel().SaveMember(&o1))
   782  
   783  	o2 := model.ChannelMember{}
   784  	o2.ChannelId = c1.Id
   785  	o2.UserId = u2.Id
   786  	o2.NotifyProps = model.GetDefaultChannelNotifyProps()
   787  	store.Must(ss.Channel().SaveMember(&o2))
   788  
   789  	c1t2 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel)
   790  	assert.EqualValues(t, 0, c1t2.ExtraUpdateAt, "ExtraUpdateAt should be 0")
   791  
   792  	count := (<-ss.Channel().GetMemberCount(o1.ChannelId, true)).Data.(int64)
   793  	if count != 2 {
   794  		t.Fatal("should have saved 2 members")
   795  	}
   796  
   797  	count = (<-ss.Channel().GetMemberCount(o1.ChannelId, true)).Data.(int64)
   798  	if count != 2 {
   799  		t.Fatal("should have saved 2 members")
   800  	}
   801  
   802  	if ss.Channel().GetMemberCountFromCache(o1.ChannelId) != 2 {
   803  		t.Fatal("should have saved 2 members")
   804  	}
   805  
   806  	if ss.Channel().GetMemberCountFromCache("junk") != 0 {
   807  		t.Fatal("should have saved 0 members")
   808  	}
   809  
   810  	count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64)
   811  	if count != 2 {
   812  		t.Fatal("should have saved 2 members")
   813  	}
   814  
   815  	store.Must(ss.Channel().RemoveMember(o2.ChannelId, o2.UserId))
   816  
   817  	count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64)
   818  	if count != 1 {
   819  		t.Fatal("should have removed 1 member")
   820  	}
   821  
   822  	c1t3 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel)
   823  	assert.EqualValues(t, 0, c1t3.ExtraUpdateAt, "ExtraUpdateAt should be 0")
   824  
   825  	member := (<-ss.Channel().GetMember(o1.ChannelId, o1.UserId)).Data.(*model.ChannelMember)
   826  	if member.ChannelId != o1.ChannelId {
   827  		t.Fatal("should have go member")
   828  	}
   829  
   830  	if err := (<-ss.Channel().SaveMember(&o1)).Err; err == nil {
   831  		t.Fatal("Should have been a duplicate")
   832  	}
   833  
   834  	c1t4 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel)
   835  	assert.EqualValues(t, 0, c1t4.ExtraUpdateAt, "ExtraUpdateAt should be 0")
   836  }
   837  
   838  func testChannelDeleteMemberStore(t *testing.T, ss store.Store) {
   839  	c1 := model.Channel{}
   840  	c1.TeamId = model.NewId()
   841  	c1.DisplayName = "NameName"
   842  	c1.Name = "zz" + model.NewId() + "b"
   843  	c1.Type = model.CHANNEL_OPEN
   844  	c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel)
   845  
   846  	c1t1 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel)
   847  	assert.EqualValues(t, 0, c1t1.ExtraUpdateAt, "ExtraUpdateAt should be 0")
   848  
   849  	u1 := model.User{}
   850  	u1.Email = MakeEmail()
   851  	u1.Nickname = model.NewId()
   852  	store.Must(ss.User().Save(&u1))
   853  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1))
   854  
   855  	u2 := model.User{}
   856  	u2.Email = MakeEmail()
   857  	u2.Nickname = model.NewId()
   858  	store.Must(ss.User().Save(&u2))
   859  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1))
   860  
   861  	o1 := model.ChannelMember{}
   862  	o1.ChannelId = c1.Id
   863  	o1.UserId = u1.Id
   864  	o1.NotifyProps = model.GetDefaultChannelNotifyProps()
   865  	store.Must(ss.Channel().SaveMember(&o1))
   866  
   867  	o2 := model.ChannelMember{}
   868  	o2.ChannelId = c1.Id
   869  	o2.UserId = u2.Id
   870  	o2.NotifyProps = model.GetDefaultChannelNotifyProps()
   871  	store.Must(ss.Channel().SaveMember(&o2))
   872  
   873  	c1t2 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel)
   874  	assert.EqualValues(t, 0, c1t2.ExtraUpdateAt, "ExtraUpdateAt should be 0")
   875  
   876  	count := (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64)
   877  	if count != 2 {
   878  		t.Fatal("should have saved 2 members")
   879  	}
   880  
   881  	store.Must(ss.Channel().PermanentDeleteMembersByUser(o2.UserId))
   882  
   883  	count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64)
   884  	if count != 1 {
   885  		t.Fatal("should have removed 1 member")
   886  	}
   887  
   888  	if r1 := <-ss.Channel().PermanentDeleteMembersByChannel(o1.ChannelId); r1.Err != nil {
   889  		t.Fatal(r1.Err)
   890  	}
   891  
   892  	count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64)
   893  	if count != 0 {
   894  		t.Fatal("should have removed all members")
   895  	}
   896  }
   897  
   898  func testChannelStoreGetChannels(t *testing.T, ss store.Store) {
   899  	o2 := model.Channel{}
   900  	o2.TeamId = model.NewId()
   901  	o2.DisplayName = "Channel2"
   902  	o2.Name = "zz" + model.NewId() + "b"
   903  	o2.Type = model.CHANNEL_OPEN
   904  	store.Must(ss.Channel().Save(&o2, -1))
   905  
   906  	o1 := model.Channel{}
   907  	o1.TeamId = model.NewId()
   908  	o1.DisplayName = "Channel1"
   909  	o1.Name = "zz" + model.NewId() + "b"
   910  	o1.Type = model.CHANNEL_OPEN
   911  	store.Must(ss.Channel().Save(&o1, -1))
   912  
   913  	m1 := model.ChannelMember{}
   914  	m1.ChannelId = o1.Id
   915  	m1.UserId = model.NewId()
   916  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
   917  	store.Must(ss.Channel().SaveMember(&m1))
   918  
   919  	m2 := model.ChannelMember{}
   920  	m2.ChannelId = o1.Id
   921  	m2.UserId = model.NewId()
   922  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
   923  	store.Must(ss.Channel().SaveMember(&m2))
   924  
   925  	m3 := model.ChannelMember{}
   926  	m3.ChannelId = o2.Id
   927  	m3.UserId = model.NewId()
   928  	m3.NotifyProps = model.GetDefaultChannelNotifyProps()
   929  	store.Must(ss.Channel().SaveMember(&m3))
   930  
   931  	cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false)
   932  	list := cresult.Data.(*model.ChannelList)
   933  
   934  	if (*list)[0].Id != o1.Id {
   935  		t.Fatal("missing channel")
   936  	}
   937  
   938  	acresult := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, false, false)
   939  	ids := acresult.Data.(map[string]string)
   940  	if _, ok := ids[o1.Id]; !ok {
   941  		t.Fatal("missing channel")
   942  	}
   943  
   944  	acresult2 := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, true, false)
   945  	ids2 := acresult2.Data.(map[string]string)
   946  	if _, ok := ids2[o1.Id]; !ok {
   947  		t.Fatal("missing channel")
   948  	}
   949  
   950  	acresult3 := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, true, false)
   951  	ids3 := acresult3.Data.(map[string]string)
   952  	if _, ok := ids3[o1.Id]; !ok {
   953  		t.Fatal("missing channel")
   954  	}
   955  
   956  	if !ss.Channel().IsUserInChannelUseCache(m1.UserId, o1.Id) {
   957  		t.Fatal("missing channel")
   958  	}
   959  
   960  	if ss.Channel().IsUserInChannelUseCache(m1.UserId, o2.Id) {
   961  		t.Fatal("missing channel")
   962  	}
   963  
   964  	if ss.Channel().IsUserInChannelUseCache(m1.UserId, "blahblah") {
   965  		t.Fatal("missing channel")
   966  	}
   967  
   968  	if ss.Channel().IsUserInChannelUseCache("blahblah", "blahblah") {
   969  		t.Fatal("missing channel")
   970  	}
   971  
   972  	ss.Channel().InvalidateAllChannelMembersForUser(m1.UserId)
   973  }
   974  
   975  func testChannelStoreGetAllChannels(t *testing.T, ss store.Store) {
   976  	cleanupChannels(t, ss)
   977  
   978  	t1 := model.Team{}
   979  	t1.DisplayName = "Name"
   980  	t1.Name = model.NewId()
   981  	t1.Email = MakeEmail()
   982  	t1.Type = model.TEAM_OPEN
   983  	store.Must(ss.Team().Save(&t1))
   984  
   985  	t2 := model.Team{}
   986  	t2.DisplayName = "Name2"
   987  	t2.Name = model.NewId()
   988  	t2.Email = MakeEmail()
   989  	t2.Type = model.TEAM_OPEN
   990  	store.Must(ss.Team().Save(&t2))
   991  
   992  	c1 := model.Channel{}
   993  	c1.TeamId = t1.Id
   994  	c1.DisplayName = "Channel1" + model.NewId()
   995  	c1.Name = "zz" + model.NewId() + "b"
   996  	c1.Type = model.CHANNEL_OPEN
   997  	store.Must(ss.Channel().Save(&c1, -1))
   998  
   999  	c2 := model.Channel{}
  1000  	c2.TeamId = t1.Id
  1001  	c2.DisplayName = "Channel2" + model.NewId()
  1002  	c2.Name = "zz" + model.NewId() + "b"
  1003  	c2.Type = model.CHANNEL_OPEN
  1004  	store.Must(ss.Channel().Save(&c2, -1))
  1005  	c2.DeleteAt = model.GetMillis()
  1006  	c2.UpdateAt = c2.DeleteAt
  1007  	store.Must(ss.Channel().Delete(c2.Id, c2.DeleteAt))
  1008  
  1009  	c3 := model.Channel{}
  1010  	c3.TeamId = t2.Id
  1011  	c3.DisplayName = "Channel3" + model.NewId()
  1012  	c3.Name = "zz" + model.NewId() + "b"
  1013  	c3.Type = model.CHANNEL_PRIVATE
  1014  	store.Must(ss.Channel().Save(&c3, -1))
  1015  
  1016  	store.Must(ss.Channel().CreateDirectChannel(model.NewId(), model.NewId()))
  1017  
  1018  	userIds := []string{model.NewId(), model.NewId(), model.NewId()}
  1019  
  1020  	c5 := model.Channel{}
  1021  	c5.Name = model.GetGroupNameFromUserIds(userIds)
  1022  	c5.DisplayName = "GroupChannel" + model.NewId()
  1023  	c5.Name = "zz" + model.NewId() + "b"
  1024  	c5.Type = model.CHANNEL_GROUP
  1025  	store.Must(ss.Channel().Save(&c5, -1))
  1026  
  1027  	cresult := <-ss.Channel().GetAllChannels(0, 10, false)
  1028  	list := cresult.Data.(*model.ChannelListWithTeamData)
  1029  	assert.Len(t, *list, 2)
  1030  	assert.Equal(t, (*list)[0].Id, c1.Id)
  1031  	assert.Equal(t, (*list)[0].TeamDisplayName, "Name")
  1032  	assert.Equal(t, (*list)[1].Id, c3.Id)
  1033  	assert.Equal(t, (*list)[1].TeamDisplayName, "Name2")
  1034  
  1035  	cresult = <-ss.Channel().GetAllChannels(0, 10, true)
  1036  	list = cresult.Data.(*model.ChannelListWithTeamData)
  1037  	assert.Len(t, *list, 3)
  1038  	assert.Equal(t, (*list)[0].Id, c1.Id)
  1039  	assert.Equal(t, (*list)[0].TeamDisplayName, "Name")
  1040  	assert.Equal(t, (*list)[1].Id, c2.Id)
  1041  	assert.Equal(t, (*list)[2].Id, c3.Id)
  1042  
  1043  	cresult = <-ss.Channel().GetAllChannels(0, 1, true)
  1044  	list = cresult.Data.(*model.ChannelListWithTeamData)
  1045  	assert.Len(t, *list, 1)
  1046  	assert.Equal(t, (*list)[0].Id, c1.Id)
  1047  	assert.Equal(t, (*list)[0].TeamDisplayName, "Name")
  1048  }
  1049  
  1050  func testChannelStoreGetMoreChannels(t *testing.T, ss store.Store) {
  1051  	teamId := model.NewId()
  1052  	otherTeamId := model.NewId()
  1053  	userId := model.NewId()
  1054  	otherUserId1 := model.NewId()
  1055  	otherUserId2 := model.NewId()
  1056  
  1057  	// o1 is a channel on the team to which the user (and the other user 1) belongs
  1058  	o1 := model.Channel{
  1059  		TeamId:      teamId,
  1060  		DisplayName: "Channel1",
  1061  		Name:        "zz" + model.NewId() + "b",
  1062  		Type:        model.CHANNEL_OPEN,
  1063  	}
  1064  	store.Must(ss.Channel().Save(&o1, -1))
  1065  
  1066  	store.Must(ss.Channel().SaveMember(&model.ChannelMember{
  1067  		ChannelId:   o1.Id,
  1068  		UserId:      userId,
  1069  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1070  	}))
  1071  
  1072  	store.Must(ss.Channel().SaveMember(&model.ChannelMember{
  1073  		ChannelId:   o1.Id,
  1074  		UserId:      otherUserId1,
  1075  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1076  	}))
  1077  
  1078  	// o2 is a channel on the other team to which the user belongs
  1079  	o2 := model.Channel{
  1080  		TeamId:      otherTeamId,
  1081  		DisplayName: "Channel2",
  1082  		Name:        "zz" + model.NewId() + "b",
  1083  		Type:        model.CHANNEL_OPEN,
  1084  	}
  1085  	store.Must(ss.Channel().Save(&o2, -1))
  1086  
  1087  	store.Must(ss.Channel().SaveMember(&model.ChannelMember{
  1088  		ChannelId:   o2.Id,
  1089  		UserId:      otherUserId2,
  1090  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1091  	}))
  1092  
  1093  	// o3 is a channel on the team to which the user does not belong, and thus should show up
  1094  	// in "more channels"
  1095  	o3 := model.Channel{
  1096  		TeamId:      teamId,
  1097  		DisplayName: "ChannelA",
  1098  		Name:        "zz" + model.NewId() + "b",
  1099  		Type:        model.CHANNEL_OPEN,
  1100  	}
  1101  	store.Must(ss.Channel().Save(&o3, -1))
  1102  
  1103  	// o4 is a private channel on the team to which the user does not belong
  1104  	o4 := model.Channel{
  1105  		TeamId:      teamId,
  1106  		DisplayName: "ChannelB",
  1107  		Name:        "zz" + model.NewId() + "b",
  1108  		Type:        model.CHANNEL_PRIVATE,
  1109  	}
  1110  	store.Must(ss.Channel().Save(&o4, -1))
  1111  
  1112  	// o5 is another private channel on the team to which the user does belong
  1113  	o5 := model.Channel{
  1114  		TeamId:      teamId,
  1115  		DisplayName: "ChannelC",
  1116  		Name:        "zz" + model.NewId() + "b",
  1117  		Type:        model.CHANNEL_PRIVATE,
  1118  	}
  1119  	store.Must(ss.Channel().Save(&o5, -1))
  1120  
  1121  	store.Must(ss.Channel().SaveMember(&model.ChannelMember{
  1122  		ChannelId:   o5.Id,
  1123  		UserId:      userId,
  1124  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1125  	}))
  1126  
  1127  	t.Run("only o3 listed in more channels", func(t *testing.T) {
  1128  		result := <-ss.Channel().GetMoreChannels(teamId, userId, 0, 100)
  1129  		require.Nil(t, result.Err)
  1130  		require.Equal(t, &model.ChannelList{&o3}, result.Data.(*model.ChannelList))
  1131  	})
  1132  
  1133  	// o6 is another channel on the team to which the user does not belong, and would thus
  1134  	// start showing up in "more channels".
  1135  	o6 := model.Channel{
  1136  		TeamId:      teamId,
  1137  		DisplayName: "ChannelD",
  1138  		Name:        "zz" + model.NewId() + "b",
  1139  		Type:        model.CHANNEL_OPEN,
  1140  	}
  1141  	store.Must(ss.Channel().Save(&o6, -1))
  1142  
  1143  	// o7 is another channel on the team to which the user does not belong, but is deleted,
  1144  	// and thus would not start showing up in "more channels"
  1145  	o7 := model.Channel{
  1146  		TeamId:      teamId,
  1147  		DisplayName: "ChannelD",
  1148  		Name:        "zz" + model.NewId() + "b",
  1149  		Type:        model.CHANNEL_OPEN,
  1150  	}
  1151  	store.Must(ss.Channel().Save(&o7, -1))
  1152  	store.Must(ss.Channel().Delete(o7.Id, model.GetMillis()))
  1153  
  1154  	t.Run("both o3 and o6 listed in more channels", func(t *testing.T) {
  1155  		result := <-ss.Channel().GetMoreChannels(teamId, userId, 0, 100)
  1156  		require.Nil(t, result.Err)
  1157  		require.Equal(t, &model.ChannelList{&o3, &o6}, result.Data.(*model.ChannelList))
  1158  	})
  1159  
  1160  	t.Run("only o3 listed in more channels with offset 0, limit 1", func(t *testing.T) {
  1161  		result := <-ss.Channel().GetMoreChannels(teamId, userId, 0, 1)
  1162  		require.Nil(t, result.Err)
  1163  		require.Equal(t, &model.ChannelList{&o3}, result.Data.(*model.ChannelList))
  1164  	})
  1165  
  1166  	t.Run("only o6 listed in more channels with offset 1, limit 1", func(t *testing.T) {
  1167  		result := <-ss.Channel().GetMoreChannels(teamId, userId, 1, 1)
  1168  		require.Nil(t, result.Err)
  1169  		require.Equal(t, &model.ChannelList{&o6}, result.Data.(*model.ChannelList))
  1170  	})
  1171  
  1172  	t.Run("verify analytics for open channels", func(t *testing.T) {
  1173  		result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN)
  1174  		require.Nil(t, result.Err)
  1175  		require.EqualValues(t, 4, result.Data.(int64))
  1176  	})
  1177  
  1178  	t.Run("verify analytics for private channels", func(t *testing.T) {
  1179  		result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE)
  1180  		require.Nil(t, result.Err)
  1181  		require.EqualValues(t, 2, result.Data.(int64))
  1182  	})
  1183  }
  1184  
  1185  func testChannelStoreGetPublicChannelsForTeam(t *testing.T, ss store.Store) {
  1186  	teamId := model.NewId()
  1187  
  1188  	// o1 is a public channel on the team
  1189  	o1 := model.Channel{
  1190  		TeamId:      teamId,
  1191  		DisplayName: "OpenChannel1Team1",
  1192  		Name:        "zz" + model.NewId() + "b",
  1193  		Type:        model.CHANNEL_OPEN,
  1194  	}
  1195  	store.Must(ss.Channel().Save(&o1, -1))
  1196  
  1197  	// o2 is a public channel on another team
  1198  	o2 := model.Channel{
  1199  		TeamId:      model.NewId(),
  1200  		DisplayName: "OpenChannel1Team2",
  1201  		Name:        "zz" + model.NewId() + "b",
  1202  		Type:        model.CHANNEL_OPEN,
  1203  	}
  1204  	store.Must(ss.Channel().Save(&o2, -1))
  1205  
  1206  	// o3 is a private channel on the team
  1207  	o3 := model.Channel{
  1208  		TeamId:      teamId,
  1209  		DisplayName: "PrivateChannel1Team1",
  1210  		Name:        "zz" + model.NewId() + "b",
  1211  		Type:        model.CHANNEL_PRIVATE,
  1212  	}
  1213  	store.Must(ss.Channel().Save(&o3, -1))
  1214  
  1215  	t.Run("only o1 initially listed in public channels", func(t *testing.T) {
  1216  		result := <-ss.Channel().GetPublicChannelsForTeam(teamId, 0, 100)
  1217  		require.Nil(t, result.Err)
  1218  		require.Equal(t, &model.ChannelList{&o1}, result.Data.(*model.ChannelList))
  1219  	})
  1220  
  1221  	// o4 is another public channel on the team
  1222  	o4 := model.Channel{
  1223  		TeamId:      teamId,
  1224  		DisplayName: "OpenChannel2Team1",
  1225  		Name:        "zz" + model.NewId() + "b",
  1226  		Type:        model.CHANNEL_OPEN,
  1227  	}
  1228  	store.Must(ss.Channel().Save(&o4, -1))
  1229  
  1230  	// o5 is another public, but deleted channel on the team
  1231  	o5 := model.Channel{
  1232  		TeamId:      teamId,
  1233  		DisplayName: "OpenChannel3Team1",
  1234  		Name:        "zz" + model.NewId() + "b",
  1235  		Type:        model.CHANNEL_OPEN,
  1236  	}
  1237  	store.Must(ss.Channel().Save(&o5, -1))
  1238  	store.Must(ss.Channel().Delete(o5.Id, model.GetMillis()))
  1239  
  1240  	t.Run("both o1 and o4 listed in public channels", func(t *testing.T) {
  1241  		cresult := <-ss.Channel().GetPublicChannelsForTeam(teamId, 0, 100)
  1242  		require.Nil(t, cresult.Err)
  1243  		require.Equal(t, &model.ChannelList{&o1, &o4}, cresult.Data.(*model.ChannelList))
  1244  	})
  1245  
  1246  	t.Run("only o1 listed in public channels with offset 0, limit 1", func(t *testing.T) {
  1247  		result := <-ss.Channel().GetPublicChannelsForTeam(teamId, 0, 1)
  1248  		require.Nil(t, result.Err)
  1249  		require.Equal(t, &model.ChannelList{&o1}, result.Data.(*model.ChannelList))
  1250  	})
  1251  
  1252  	t.Run("only o4 listed in public channels with offset 1, limit 1", func(t *testing.T) {
  1253  		result := <-ss.Channel().GetPublicChannelsForTeam(teamId, 1, 1)
  1254  		require.Nil(t, result.Err)
  1255  		require.Equal(t, &model.ChannelList{&o4}, result.Data.(*model.ChannelList))
  1256  	})
  1257  
  1258  	t.Run("verify analytics for open channels", func(t *testing.T) {
  1259  		result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN)
  1260  		require.Nil(t, result.Err)
  1261  		require.EqualValues(t, 3, result.Data.(int64))
  1262  	})
  1263  
  1264  	t.Run("verify analytics for private channels", func(t *testing.T) {
  1265  		result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE)
  1266  		require.Nil(t, result.Err)
  1267  		require.EqualValues(t, 1, result.Data.(int64))
  1268  	})
  1269  }
  1270  
  1271  func testChannelStoreGetPublicChannelsByIdsForTeam(t *testing.T, ss store.Store) {
  1272  	teamId := model.NewId()
  1273  
  1274  	// oc1 is a public channel on the team
  1275  	oc1 := model.Channel{
  1276  		TeamId:      teamId,
  1277  		DisplayName: "OpenChannel1Team1",
  1278  		Name:        "zz" + model.NewId() + "b",
  1279  		Type:        model.CHANNEL_OPEN,
  1280  	}
  1281  	store.Must(ss.Channel().Save(&oc1, -1))
  1282  
  1283  	// oc2 is a public channel on another team
  1284  	oc2 := model.Channel{
  1285  		TeamId:      model.NewId(),
  1286  		DisplayName: "OpenChannel2TeamOther",
  1287  		Name:        "zz" + model.NewId() + "b",
  1288  		Type:        model.CHANNEL_OPEN,
  1289  	}
  1290  	store.Must(ss.Channel().Save(&oc2, -1))
  1291  
  1292  	// pc3 is a private channel on the team
  1293  	pc3 := model.Channel{
  1294  		TeamId:      teamId,
  1295  		DisplayName: "PrivateChannel3Team1",
  1296  		Name:        "zz" + model.NewId() + "b",
  1297  		Type:        model.CHANNEL_PRIVATE,
  1298  	}
  1299  	store.Must(ss.Channel().Save(&pc3, -1))
  1300  
  1301  	t.Run("oc1 by itself should be found as a public channel in the team", func(t *testing.T) {
  1302  		result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id})
  1303  		require.Nil(t, result.Err)
  1304  		require.Equal(t, &model.ChannelList{&oc1}, result.Data.(*model.ChannelList))
  1305  	})
  1306  
  1307  	t.Run("only oc1, among others, should be found as a public channel in the team", func(t *testing.T) {
  1308  		result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id})
  1309  		require.Nil(t, result.Err)
  1310  		require.Equal(t, &model.ChannelList{&oc1}, result.Data.(*model.ChannelList))
  1311  	})
  1312  
  1313  	// oc4 is another public channel on the team
  1314  	oc4 := model.Channel{
  1315  		TeamId:      teamId,
  1316  		DisplayName: "OpenChannel4Team1",
  1317  		Name:        "zz" + model.NewId() + "b",
  1318  		Type:        model.CHANNEL_OPEN,
  1319  	}
  1320  	store.Must(ss.Channel().Save(&oc4, -1))
  1321  
  1322  	// oc4 is another public, but deleted channel on the team
  1323  	oc5 := model.Channel{
  1324  		TeamId:      teamId,
  1325  		DisplayName: "OpenChannel4Team1",
  1326  		Name:        "zz" + model.NewId() + "b",
  1327  		Type:        model.CHANNEL_OPEN,
  1328  	}
  1329  	store.Must(ss.Channel().Save(&oc5, -1))
  1330  	store.Must(ss.Channel().Delete(oc5.Id, model.GetMillis()))
  1331  
  1332  	t.Run("only oc1 and oc4, among others, should be found as a public channel in the team", func(t *testing.T) {
  1333  		result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id, oc4.Id})
  1334  		require.Nil(t, result.Err)
  1335  		require.Equal(t, &model.ChannelList{&oc1, &oc4}, result.Data.(*model.ChannelList))
  1336  	})
  1337  
  1338  	t.Run("random channel id should not be found as a public channel in the team", func(t *testing.T) {
  1339  		result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{model.NewId()})
  1340  		require.NotNil(t, result.Err)
  1341  		require.Equal(t, result.Err.Id, "store.sql_channel.get_channels_by_ids.not_found.app_error")
  1342  	})
  1343  }
  1344  
  1345  func testChannelStoreGetChannelCounts(t *testing.T, ss store.Store) {
  1346  	o2 := model.Channel{}
  1347  	o2.TeamId = model.NewId()
  1348  	o2.DisplayName = "Channel2"
  1349  	o2.Name = "zz" + model.NewId() + "b"
  1350  	o2.Type = model.CHANNEL_OPEN
  1351  	store.Must(ss.Channel().Save(&o2, -1))
  1352  
  1353  	o1 := model.Channel{}
  1354  	o1.TeamId = model.NewId()
  1355  	o1.DisplayName = "Channel1"
  1356  	o1.Name = "zz" + model.NewId() + "b"
  1357  	o1.Type = model.CHANNEL_OPEN
  1358  	store.Must(ss.Channel().Save(&o1, -1))
  1359  
  1360  	m1 := model.ChannelMember{}
  1361  	m1.ChannelId = o1.Id
  1362  	m1.UserId = model.NewId()
  1363  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  1364  	store.Must(ss.Channel().SaveMember(&m1))
  1365  
  1366  	m2 := model.ChannelMember{}
  1367  	m2.ChannelId = o1.Id
  1368  	m2.UserId = model.NewId()
  1369  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  1370  	store.Must(ss.Channel().SaveMember(&m2))
  1371  
  1372  	m3 := model.ChannelMember{}
  1373  	m3.ChannelId = o2.Id
  1374  	m3.UserId = model.NewId()
  1375  	m3.NotifyProps = model.GetDefaultChannelNotifyProps()
  1376  	store.Must(ss.Channel().SaveMember(&m3))
  1377  
  1378  	cresult := <-ss.Channel().GetChannelCounts(o1.TeamId, m1.UserId)
  1379  	counts := cresult.Data.(*model.ChannelCounts)
  1380  
  1381  	if len(counts.Counts) != 1 {
  1382  		t.Fatal("wrong number of counts")
  1383  	}
  1384  
  1385  	if len(counts.UpdateTimes) != 1 {
  1386  		t.Fatal("wrong number of update times")
  1387  	}
  1388  }
  1389  
  1390  func testChannelStoreGetMembersForUser(t *testing.T, ss store.Store) {
  1391  	t1 := model.Team{}
  1392  	t1.DisplayName = "Name"
  1393  	t1.Name = model.NewId()
  1394  	t1.Email = MakeEmail()
  1395  	t1.Type = model.TEAM_OPEN
  1396  	store.Must(ss.Team().Save(&t1))
  1397  
  1398  	o1 := model.Channel{}
  1399  	o1.TeamId = t1.Id
  1400  	o1.DisplayName = "Channel1"
  1401  	o1.Name = "zz" + model.NewId() + "b"
  1402  	o1.Type = model.CHANNEL_OPEN
  1403  	store.Must(ss.Channel().Save(&o1, -1))
  1404  
  1405  	o2 := model.Channel{}
  1406  	o2.TeamId = o1.TeamId
  1407  	o2.DisplayName = "Channel2"
  1408  	o2.Name = "zz" + model.NewId() + "b"
  1409  	o2.Type = model.CHANNEL_OPEN
  1410  	store.Must(ss.Channel().Save(&o2, -1))
  1411  
  1412  	m1 := model.ChannelMember{}
  1413  	m1.ChannelId = o1.Id
  1414  	m1.UserId = model.NewId()
  1415  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  1416  	store.Must(ss.Channel().SaveMember(&m1))
  1417  
  1418  	m2 := model.ChannelMember{}
  1419  	m2.ChannelId = o2.Id
  1420  	m2.UserId = m1.UserId
  1421  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  1422  	store.Must(ss.Channel().SaveMember(&m2))
  1423  
  1424  	cresult := <-ss.Channel().GetMembersForUser(o1.TeamId, m1.UserId)
  1425  	members := cresult.Data.(*model.ChannelMembers)
  1426  
  1427  	// no unread messages
  1428  	if len(*members) != 2 {
  1429  		t.Fatal("wrong number of members")
  1430  	}
  1431  }
  1432  
  1433  func testChannelStoreUpdateLastViewedAt(t *testing.T, ss store.Store) {
  1434  	o1 := model.Channel{}
  1435  	o1.TeamId = model.NewId()
  1436  	o1.DisplayName = "Channel1"
  1437  	o1.Name = "zz" + model.NewId() + "b"
  1438  	o1.Type = model.CHANNEL_OPEN
  1439  	o1.TotalMsgCount = 25
  1440  	o1.LastPostAt = 12345
  1441  	store.Must(ss.Channel().Save(&o1, -1))
  1442  
  1443  	m1 := model.ChannelMember{}
  1444  	m1.ChannelId = o1.Id
  1445  	m1.UserId = model.NewId()
  1446  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  1447  	store.Must(ss.Channel().SaveMember(&m1))
  1448  
  1449  	o2 := model.Channel{}
  1450  	o2.TeamId = model.NewId()
  1451  	o2.DisplayName = "Channel1"
  1452  	o2.Name = "zz" + model.NewId() + "c"
  1453  	o2.Type = model.CHANNEL_OPEN
  1454  	o2.TotalMsgCount = 26
  1455  	o2.LastPostAt = 123456
  1456  	store.Must(ss.Channel().Save(&o2, -1))
  1457  
  1458  	m2 := model.ChannelMember{}
  1459  	m2.ChannelId = o2.Id
  1460  	m2.UserId = m1.UserId
  1461  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  1462  	store.Must(ss.Channel().SaveMember(&m2))
  1463  
  1464  	if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId}, m1.UserId); result.Err != nil {
  1465  		t.Fatal("failed to update", result.Err)
  1466  	} else if result.Data.(map[string]int64)[o1.Id] != o1.LastPostAt {
  1467  		t.Fatal("last viewed at time incorrect")
  1468  	}
  1469  
  1470  	if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId, m2.ChannelId}, m1.UserId); result.Err != nil {
  1471  		t.Fatal("failed to update", result.Err)
  1472  	} else if result.Data.(map[string]int64)[o2.Id] != o2.LastPostAt {
  1473  		t.Fatal("last viewed at time incorrect")
  1474  	}
  1475  
  1476  	rm1 := store.Must(ss.Channel().GetMember(m1.ChannelId, m1.UserId)).(*model.ChannelMember)
  1477  	assert.Equal(t, rm1.LastViewedAt, o1.LastPostAt)
  1478  	assert.Equal(t, rm1.LastUpdateAt, o1.LastPostAt)
  1479  	assert.Equal(t, rm1.MsgCount, o1.TotalMsgCount)
  1480  
  1481  	rm2 := store.Must(ss.Channel().GetMember(m2.ChannelId, m2.UserId)).(*model.ChannelMember)
  1482  	assert.Equal(t, rm2.LastViewedAt, o2.LastPostAt)
  1483  	assert.Equal(t, rm2.LastUpdateAt, o2.LastPostAt)
  1484  	assert.Equal(t, rm2.MsgCount, o2.TotalMsgCount)
  1485  
  1486  	if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId}, "missing id"); result.Err != nil {
  1487  		t.Fatal("failed to update")
  1488  	}
  1489  }
  1490  
  1491  func testChannelStoreIncrementMentionCount(t *testing.T, ss store.Store) {
  1492  	o1 := model.Channel{}
  1493  	o1.TeamId = model.NewId()
  1494  	o1.DisplayName = "Channel1"
  1495  	o1.Name = "zz" + model.NewId() + "b"
  1496  	o1.Type = model.CHANNEL_OPEN
  1497  	o1.TotalMsgCount = 25
  1498  	store.Must(ss.Channel().Save(&o1, -1))
  1499  
  1500  	m1 := model.ChannelMember{}
  1501  	m1.ChannelId = o1.Id
  1502  	m1.UserId = model.NewId()
  1503  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  1504  	store.Must(ss.Channel().SaveMember(&m1))
  1505  
  1506  	err := (<-ss.Channel().IncrementMentionCount(m1.ChannelId, m1.UserId)).Err
  1507  	if err != nil {
  1508  		t.Fatal("failed to update")
  1509  	}
  1510  
  1511  	err = (<-ss.Channel().IncrementMentionCount(m1.ChannelId, "missing id")).Err
  1512  	if err != nil {
  1513  		t.Fatal("failed to update")
  1514  	}
  1515  
  1516  	err = (<-ss.Channel().IncrementMentionCount("missing id", m1.UserId)).Err
  1517  	if err != nil {
  1518  		t.Fatal("failed to update")
  1519  	}
  1520  
  1521  	err = (<-ss.Channel().IncrementMentionCount("missing id", "missing id")).Err
  1522  	if err != nil {
  1523  		t.Fatal("failed to update")
  1524  	}
  1525  }
  1526  
  1527  func testUpdateChannelMember(t *testing.T, ss store.Store) {
  1528  	userId := model.NewId()
  1529  
  1530  	c1 := &model.Channel{
  1531  		TeamId:      model.NewId(),
  1532  		DisplayName: model.NewId(),
  1533  		Name:        model.NewId(),
  1534  		Type:        model.CHANNEL_OPEN,
  1535  	}
  1536  	store.Must(ss.Channel().Save(c1, -1))
  1537  
  1538  	m1 := &model.ChannelMember{
  1539  		ChannelId:   c1.Id,
  1540  		UserId:      userId,
  1541  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1542  	}
  1543  	store.Must(ss.Channel().SaveMember(m1))
  1544  
  1545  	m1.NotifyProps["test"] = "sometext"
  1546  	if result := <-ss.Channel().UpdateMember(m1); result.Err != nil {
  1547  		t.Fatal(result.Err)
  1548  	}
  1549  
  1550  	m1.UserId = ""
  1551  	if result := <-ss.Channel().UpdateMember(m1); result.Err == nil {
  1552  		t.Fatal("bad user id - should fail")
  1553  	}
  1554  }
  1555  
  1556  func testGetMember(t *testing.T, ss store.Store) {
  1557  	userId := model.NewId()
  1558  
  1559  	c1 := &model.Channel{
  1560  		TeamId:      model.NewId(),
  1561  		DisplayName: model.NewId(),
  1562  		Name:        model.NewId(),
  1563  		Type:        model.CHANNEL_OPEN,
  1564  	}
  1565  	store.Must(ss.Channel().Save(c1, -1))
  1566  
  1567  	c2 := &model.Channel{
  1568  		TeamId:      c1.TeamId,
  1569  		DisplayName: model.NewId(),
  1570  		Name:        model.NewId(),
  1571  		Type:        model.CHANNEL_OPEN,
  1572  	}
  1573  	store.Must(ss.Channel().Save(c2, -1))
  1574  
  1575  	m1 := &model.ChannelMember{
  1576  		ChannelId:   c1.Id,
  1577  		UserId:      userId,
  1578  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1579  	}
  1580  	store.Must(ss.Channel().SaveMember(m1))
  1581  
  1582  	m2 := &model.ChannelMember{
  1583  		ChannelId:   c2.Id,
  1584  		UserId:      userId,
  1585  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1586  	}
  1587  	store.Must(ss.Channel().SaveMember(m2))
  1588  
  1589  	if result := <-ss.Channel().GetMember(model.NewId(), userId); result.Err == nil {
  1590  		t.Fatal("should've failed to get member for non-existent channel")
  1591  	}
  1592  
  1593  	if result := <-ss.Channel().GetMember(c1.Id, model.NewId()); result.Err == nil {
  1594  		t.Fatal("should've failed to get member for non-existent user")
  1595  	}
  1596  
  1597  	if result := <-ss.Channel().GetMember(c1.Id, userId); result.Err != nil {
  1598  		t.Fatal("shouldn't have errored when getting member", result.Err)
  1599  	} else if member := result.Data.(*model.ChannelMember); member.ChannelId != c1.Id {
  1600  		t.Fatal("should've gotten member of channel 1")
  1601  	} else if member.UserId != userId {
  1602  		t.Fatal("should've gotten member for user")
  1603  	}
  1604  
  1605  	if result := <-ss.Channel().GetMember(c2.Id, userId); result.Err != nil {
  1606  		t.Fatal("shouldn't have errored when getting member", result.Err)
  1607  	} else if member := result.Data.(*model.ChannelMember); member.ChannelId != c2.Id {
  1608  		t.Fatal("should've gotten member of channel 2")
  1609  	} else if member.UserId != userId {
  1610  		t.Fatal("should've gotten member for user")
  1611  	}
  1612  
  1613  	if result := <-ss.Channel().GetAllChannelMembersNotifyPropsForChannel(c2.Id, false); result.Err != nil {
  1614  		t.Fatal(result.Err)
  1615  	} else {
  1616  		props := result.Data.(map[string]model.StringMap)
  1617  		if len(props) == 0 {
  1618  			t.Fatal("should not be empty")
  1619  		}
  1620  	}
  1621  
  1622  	if result := <-ss.Channel().GetAllChannelMembersNotifyPropsForChannel(c2.Id, true); result.Err != nil {
  1623  		t.Fatal(result.Err)
  1624  	} else {
  1625  		props := result.Data.(map[string]model.StringMap)
  1626  		if len(props) == 0 {
  1627  			t.Fatal("should not be empty")
  1628  		}
  1629  	}
  1630  
  1631  	ss.Channel().InvalidateCacheForChannelMembersNotifyProps(c2.Id)
  1632  }
  1633  
  1634  func testChannelStoreGetMemberForPost(t *testing.T, ss store.Store) {
  1635  	o1 := store.Must(ss.Channel().Save(&model.Channel{
  1636  		TeamId:      model.NewId(),
  1637  		DisplayName: "Name",
  1638  		Name:        "zz" + model.NewId() + "b",
  1639  		Type:        model.CHANNEL_OPEN,
  1640  	}, -1)).(*model.Channel)
  1641  
  1642  	m1 := store.Must(ss.Channel().SaveMember(&model.ChannelMember{
  1643  		ChannelId:   o1.Id,
  1644  		UserId:      model.NewId(),
  1645  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1646  	})).(*model.ChannelMember)
  1647  
  1648  	p1 := store.Must(ss.Post().Save(&model.Post{
  1649  		UserId:    model.NewId(),
  1650  		ChannelId: o1.Id,
  1651  		Message:   "test",
  1652  	})).(*model.Post)
  1653  
  1654  	if r1 := <-ss.Channel().GetMemberForPost(p1.Id, m1.UserId); r1.Err != nil {
  1655  		t.Fatal(r1.Err)
  1656  	} else if r1.Data.(*model.ChannelMember).ToJson() != m1.ToJson() {
  1657  		t.Fatal("invalid returned channel member")
  1658  	}
  1659  
  1660  	if r2 := <-ss.Channel().GetMemberForPost(p1.Id, model.NewId()); r2.Err == nil {
  1661  		t.Fatal("shouldn't have returned a member")
  1662  	}
  1663  }
  1664  
  1665  func testGetMemberCount(t *testing.T, ss store.Store) {
  1666  	teamId := model.NewId()
  1667  
  1668  	c1 := model.Channel{
  1669  		TeamId:      teamId,
  1670  		DisplayName: "Channel1",
  1671  		Name:        "zz" + model.NewId() + "b",
  1672  		Type:        model.CHANNEL_OPEN,
  1673  	}
  1674  	store.Must(ss.Channel().Save(&c1, -1))
  1675  
  1676  	c2 := model.Channel{
  1677  		TeamId:      teamId,
  1678  		DisplayName: "Channel2",
  1679  		Name:        "zz" + model.NewId() + "b",
  1680  		Type:        model.CHANNEL_OPEN,
  1681  	}
  1682  	store.Must(ss.Channel().Save(&c2, -1))
  1683  
  1684  	u1 := &model.User{
  1685  		Email:    MakeEmail(),
  1686  		DeleteAt: 0,
  1687  	}
  1688  	store.Must(ss.User().Save(u1))
  1689  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1))
  1690  
  1691  	m1 := model.ChannelMember{
  1692  		ChannelId:   c1.Id,
  1693  		UserId:      u1.Id,
  1694  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1695  	}
  1696  	store.Must(ss.Channel().SaveMember(&m1))
  1697  
  1698  	if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil {
  1699  		t.Fatalf("failed to get member count: %v", result.Err)
  1700  	} else if result.Data.(int64) != 1 {
  1701  		t.Fatalf("got incorrect member count %v", result.Data)
  1702  	}
  1703  
  1704  	u2 := model.User{
  1705  		Email:    MakeEmail(),
  1706  		DeleteAt: 0,
  1707  	}
  1708  	store.Must(ss.User().Save(&u2))
  1709  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1))
  1710  
  1711  	m2 := model.ChannelMember{
  1712  		ChannelId:   c1.Id,
  1713  		UserId:      u2.Id,
  1714  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1715  	}
  1716  	store.Must(ss.Channel().SaveMember(&m2))
  1717  
  1718  	if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil {
  1719  		t.Fatalf("failed to get member count: %v", result.Err)
  1720  	} else if result.Data.(int64) != 2 {
  1721  		t.Fatalf("got incorrect member count %v", result.Data)
  1722  	}
  1723  
  1724  	// make sure members of other channels aren't counted
  1725  	u3 := model.User{
  1726  		Email:    MakeEmail(),
  1727  		DeleteAt: 0,
  1728  	}
  1729  	store.Must(ss.User().Save(&u3))
  1730  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u3.Id}, -1))
  1731  
  1732  	m3 := model.ChannelMember{
  1733  		ChannelId:   c2.Id,
  1734  		UserId:      u3.Id,
  1735  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1736  	}
  1737  	store.Must(ss.Channel().SaveMember(&m3))
  1738  
  1739  	if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil {
  1740  		t.Fatalf("failed to get member count: %v", result.Err)
  1741  	} else if result.Data.(int64) != 2 {
  1742  		t.Fatalf("got incorrect member count %v", result.Data)
  1743  	}
  1744  
  1745  	// make sure inactive users aren't counted
  1746  	u4 := &model.User{
  1747  		Email:    MakeEmail(),
  1748  		DeleteAt: 10000,
  1749  	}
  1750  	store.Must(ss.User().Save(u4))
  1751  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u4.Id}, -1))
  1752  
  1753  	m4 := model.ChannelMember{
  1754  		ChannelId:   c1.Id,
  1755  		UserId:      u4.Id,
  1756  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1757  	}
  1758  	store.Must(ss.Channel().SaveMember(&m4))
  1759  
  1760  	if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil {
  1761  		t.Fatalf("failed to get member count: %v", result.Err)
  1762  	} else if result.Data.(int64) != 2 {
  1763  		t.Fatalf("got incorrect member count %v", result.Data)
  1764  	}
  1765  }
  1766  
  1767  func testChannelStoreSearchMore(t *testing.T, ss store.Store) {
  1768  	teamId := model.NewId()
  1769  	otherTeamId := model.NewId()
  1770  
  1771  	o1 := model.Channel{
  1772  		TeamId:      teamId,
  1773  		DisplayName: "ChannelA",
  1774  		Name:        "zz" + model.NewId() + "b",
  1775  		Type:        model.CHANNEL_OPEN,
  1776  	}
  1777  	store.Must(ss.Channel().Save(&o1, -1))
  1778  
  1779  	m1 := model.ChannelMember{
  1780  		ChannelId:   o1.Id,
  1781  		UserId:      model.NewId(),
  1782  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1783  	}
  1784  	store.Must(ss.Channel().SaveMember(&m1))
  1785  
  1786  	m2 := model.ChannelMember{
  1787  		ChannelId:   o1.Id,
  1788  		UserId:      model.NewId(),
  1789  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1790  	}
  1791  	store.Must(ss.Channel().SaveMember(&m2))
  1792  
  1793  	o2 := model.Channel{
  1794  		TeamId:      otherTeamId,
  1795  		DisplayName: "Channel2",
  1796  		Name:        "zz" + model.NewId() + "b",
  1797  		Type:        model.CHANNEL_OPEN,
  1798  	}
  1799  	store.Must(ss.Channel().Save(&o2, -1))
  1800  
  1801  	m3 := model.ChannelMember{
  1802  		ChannelId:   o2.Id,
  1803  		UserId:      model.NewId(),
  1804  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1805  	}
  1806  	store.Must(ss.Channel().SaveMember(&m3))
  1807  
  1808  	o3 := model.Channel{
  1809  		TeamId:      teamId,
  1810  		DisplayName: "ChannelA",
  1811  		Name:        "zz" + model.NewId() + "b",
  1812  		Type:        model.CHANNEL_OPEN,
  1813  	}
  1814  	store.Must(ss.Channel().Save(&o3, -1))
  1815  
  1816  	o4 := model.Channel{
  1817  		TeamId:      teamId,
  1818  		DisplayName: "ChannelB",
  1819  		Name:        "zz" + model.NewId() + "b",
  1820  		Type:        model.CHANNEL_PRIVATE,
  1821  	}
  1822  	store.Must(ss.Channel().Save(&o4, -1))
  1823  
  1824  	o5 := model.Channel{
  1825  		TeamId:      teamId,
  1826  		DisplayName: "ChannelC",
  1827  		Name:        "zz" + model.NewId() + "b",
  1828  		Type:        model.CHANNEL_PRIVATE,
  1829  	}
  1830  	store.Must(ss.Channel().Save(&o5, -1))
  1831  
  1832  	o6 := model.Channel{
  1833  		TeamId:      teamId,
  1834  		DisplayName: "Off-Topic",
  1835  		Name:        "off-topic",
  1836  		Type:        model.CHANNEL_OPEN,
  1837  	}
  1838  	store.Must(ss.Channel().Save(&o6, -1))
  1839  
  1840  	o7 := model.Channel{
  1841  		TeamId:      teamId,
  1842  		DisplayName: "Off-Set",
  1843  		Name:        "off-set",
  1844  		Type:        model.CHANNEL_OPEN,
  1845  	}
  1846  	store.Must(ss.Channel().Save(&o7, -1))
  1847  
  1848  	o8 := model.Channel{
  1849  		TeamId:      teamId,
  1850  		DisplayName: "Off-Limit",
  1851  		Name:        "off-limit",
  1852  		Type:        model.CHANNEL_PRIVATE,
  1853  	}
  1854  	store.Must(ss.Channel().Save(&o8, -1))
  1855  
  1856  	o9 := model.Channel{
  1857  		TeamId:      teamId,
  1858  		DisplayName: "Channel With Purpose",
  1859  		Purpose:     "This can now be searchable!",
  1860  		Name:        "with-purpose",
  1861  		Type:        model.CHANNEL_OPEN,
  1862  	}
  1863  	store.Must(ss.Channel().Save(&o9, -1))
  1864  
  1865  	o10 := model.Channel{
  1866  		TeamId:      teamId,
  1867  		DisplayName: "ChannelA",
  1868  		Name:        "channel-a-deleted",
  1869  		Type:        model.CHANNEL_OPEN,
  1870  	}
  1871  	store.Must(ss.Channel().Save(&o10, -1))
  1872  	o10.DeleteAt = model.GetMillis()
  1873  	o10.UpdateAt = o10.DeleteAt
  1874  	store.Must(ss.Channel().Delete(o10.Id, o10.DeleteAt))
  1875  
  1876  	t.Run("three public channels matching 'ChannelA', but already a member of one and one deleted", func(t *testing.T) {
  1877  		result := <-ss.Channel().SearchMore(m1.UserId, teamId, "ChannelA")
  1878  		require.Nil(t, result.Err)
  1879  		require.Equal(t, &model.ChannelList{&o3}, result.Data.(*model.ChannelList))
  1880  	})
  1881  
  1882  	t.Run("one public channels, but already a member", func(t *testing.T) {
  1883  		result := <-ss.Channel().SearchMore(m1.UserId, teamId, o4.Name)
  1884  		require.Nil(t, result.Err)
  1885  		require.Equal(t, &model.ChannelList{}, result.Data.(*model.ChannelList))
  1886  	})
  1887  
  1888  	t.Run("three matching channels, but only two public", func(t *testing.T) {
  1889  		result := <-ss.Channel().SearchMore(m1.UserId, teamId, "off-")
  1890  		require.Nil(t, result.Err)
  1891  		require.Equal(t, &model.ChannelList{&o7, &o6}, result.Data.(*model.ChannelList))
  1892  	})
  1893  
  1894  	t.Run("one channel matching 'off-topic'", func(t *testing.T) {
  1895  		result := <-ss.Channel().SearchMore(m1.UserId, teamId, "off-topic")
  1896  		require.Nil(t, result.Err)
  1897  		require.Equal(t, &model.ChannelList{&o6}, result.Data.(*model.ChannelList))
  1898  	})
  1899  
  1900  	t.Run("search purpose", func(t *testing.T) {
  1901  		result := <-ss.Channel().SearchMore(m1.UserId, teamId, "now searchable")
  1902  		require.Nil(t, result.Err)
  1903  		require.Equal(t, &model.ChannelList{&o9}, result.Data.(*model.ChannelList))
  1904  	})
  1905  }
  1906  
  1907  type ByChannelDisplayName model.ChannelList
  1908  
  1909  func (s ByChannelDisplayName) Len() int { return len(s) }
  1910  func (s ByChannelDisplayName) Swap(i, j int) {
  1911  	s[i], s[j] = s[j], s[i]
  1912  }
  1913  func (s ByChannelDisplayName) Less(i, j int) bool {
  1914  	if s[i].DisplayName != s[j].DisplayName {
  1915  		return s[i].DisplayName < s[j].DisplayName
  1916  	}
  1917  
  1918  	return s[i].Id < s[j].Id
  1919  }
  1920  
  1921  func testChannelStoreSearchInTeam(t *testing.T, ss store.Store) {
  1922  	teamId := model.NewId()
  1923  	otherTeamId := model.NewId()
  1924  
  1925  	o1 := model.Channel{
  1926  		TeamId:      teamId,
  1927  		DisplayName: "ChannelA",
  1928  		Name:        "zz" + model.NewId() + "b",
  1929  		Type:        model.CHANNEL_OPEN,
  1930  	}
  1931  	store.Must(ss.Channel().Save(&o1, -1))
  1932  
  1933  	o2 := model.Channel{
  1934  		TeamId:      otherTeamId,
  1935  		DisplayName: "ChannelA",
  1936  		Name:        "zz" + model.NewId() + "b",
  1937  		Type:        model.CHANNEL_OPEN,
  1938  	}
  1939  	store.Must(ss.Channel().Save(&o2, -1))
  1940  
  1941  	m1 := model.ChannelMember{
  1942  		ChannelId:   o1.Id,
  1943  		UserId:      model.NewId(),
  1944  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1945  	}
  1946  	store.Must(ss.Channel().SaveMember(&m1))
  1947  
  1948  	m2 := model.ChannelMember{
  1949  		ChannelId:   o1.Id,
  1950  		UserId:      model.NewId(),
  1951  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1952  	}
  1953  	store.Must(ss.Channel().SaveMember(&m2))
  1954  
  1955  	m3 := model.ChannelMember{
  1956  		ChannelId:   o2.Id,
  1957  		UserId:      model.NewId(),
  1958  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  1959  	}
  1960  	store.Must(ss.Channel().SaveMember(&m3))
  1961  
  1962  	o3 := model.Channel{
  1963  		TeamId:      teamId,
  1964  		DisplayName: "ChannelA (alternate)",
  1965  		Name:        "zz" + model.NewId() + "b",
  1966  		Type:        model.CHANNEL_OPEN,
  1967  	}
  1968  	store.Must(ss.Channel().Save(&o3, -1))
  1969  
  1970  	o4 := model.Channel{
  1971  		TeamId:      teamId,
  1972  		DisplayName: "Channel B",
  1973  		Name:        "zz" + model.NewId() + "b",
  1974  		Type:        model.CHANNEL_PRIVATE,
  1975  	}
  1976  	store.Must(ss.Channel().Save(&o4, -1))
  1977  
  1978  	o5 := model.Channel{
  1979  		TeamId:      teamId,
  1980  		DisplayName: "Channel C",
  1981  		Name:        "zz" + model.NewId() + "b",
  1982  		Type:        model.CHANNEL_PRIVATE,
  1983  	}
  1984  	store.Must(ss.Channel().Save(&o5, -1))
  1985  
  1986  	o6 := model.Channel{
  1987  		TeamId:      teamId,
  1988  		DisplayName: "Off-Topic",
  1989  		Name:        "off-topic",
  1990  		Type:        model.CHANNEL_OPEN,
  1991  	}
  1992  	store.Must(ss.Channel().Save(&o6, -1))
  1993  
  1994  	o7 := model.Channel{
  1995  		TeamId:      teamId,
  1996  		DisplayName: "Off-Set",
  1997  		Name:        "off-set",
  1998  		Type:        model.CHANNEL_OPEN,
  1999  	}
  2000  	store.Must(ss.Channel().Save(&o7, -1))
  2001  
  2002  	o8 := model.Channel{
  2003  		TeamId:      teamId,
  2004  		DisplayName: "Off-Limit",
  2005  		Name:        "off-limit",
  2006  		Type:        model.CHANNEL_PRIVATE,
  2007  	}
  2008  	store.Must(ss.Channel().Save(&o8, -1))
  2009  
  2010  	o9 := model.Channel{
  2011  		TeamId:      teamId,
  2012  		DisplayName: "Town Square",
  2013  		Name:        "town-square",
  2014  		Type:        model.CHANNEL_OPEN,
  2015  	}
  2016  	store.Must(ss.Channel().Save(&o9, -1))
  2017  
  2018  	o10 := model.Channel{
  2019  		TeamId:      teamId,
  2020  		DisplayName: "The",
  2021  		Name:        "the",
  2022  		Type:        model.CHANNEL_OPEN,
  2023  	}
  2024  	store.Must(ss.Channel().Save(&o10, -1))
  2025  
  2026  	o11 := model.Channel{
  2027  		TeamId:      teamId,
  2028  		DisplayName: "Native Mobile Apps",
  2029  		Name:        "native-mobile-apps",
  2030  		Type:        model.CHANNEL_OPEN,
  2031  	}
  2032  	store.Must(ss.Channel().Save(&o11, -1))
  2033  
  2034  	o12 := model.Channel{
  2035  		TeamId:      teamId,
  2036  		DisplayName: "ChannelZ",
  2037  		Purpose:     "This can now be searchable!",
  2038  		Name:        "with-purpose",
  2039  		Type:        model.CHANNEL_OPEN,
  2040  	}
  2041  	store.Must(ss.Channel().Save(&o12, -1))
  2042  
  2043  	o13 := model.Channel{
  2044  		TeamId:      teamId,
  2045  		DisplayName: "ChannelA (deleted)",
  2046  		Name:        model.NewId(),
  2047  		Type:        model.CHANNEL_OPEN,
  2048  	}
  2049  	store.Must(ss.Channel().Save(&o13, -1))
  2050  	o13.DeleteAt = model.GetMillis()
  2051  	o13.UpdateAt = o13.DeleteAt
  2052  	store.Must(ss.Channel().Delete(o13.Id, o13.DeleteAt))
  2053  
  2054  	testCases := []struct {
  2055  		Description     string
  2056  		TeamId          string
  2057  		Term            string
  2058  		IncludeDeleted  bool
  2059  		ExpectedResults *model.ChannelList
  2060  	}{
  2061  		{"ChannelA", teamId, "ChannelA", false, &model.ChannelList{&o1, &o3}},
  2062  		{"ChannelA, include deleted", teamId, "ChannelA", true, &model.ChannelList{&o1, &o3, &o13}},
  2063  		{"ChannelA, other team", otherTeamId, "ChannelA", false, &model.ChannelList{&o2}},
  2064  		{"empty string", teamId, "", false, &model.ChannelList{&o1, &o3, &o12, &o11, &o7, &o6, &o10, &o9}},
  2065  		{"no matches", teamId, "blargh", false, &model.ChannelList{}},
  2066  		{"prefix", teamId, "off-", false, &model.ChannelList{&o7, &o6}},
  2067  		{"full match with dash", teamId, "off-topic", false, &model.ChannelList{&o6}},
  2068  		{"town square", teamId, "town square", false, &model.ChannelList{&o9}},
  2069  		{"the in name", teamId, "the", false, &model.ChannelList{&o10}},
  2070  		{"Mobile", teamId, "Mobile", false, &model.ChannelList{&o11}},
  2071  		{"search purpose", teamId, "now searchable", false, &model.ChannelList{&o12}},
  2072  		{"pipe ignored", teamId, "town square |", false, &model.ChannelList{&o9}},
  2073  	}
  2074  
  2075  	for name, search := range map[string]func(teamId string, term string, includeDeleted bool) store.StoreChannel{
  2076  		"AutocompleteInTeam": ss.Channel().AutocompleteInTeam,
  2077  		"SearchInTeam":       ss.Channel().SearchInTeam,
  2078  	} {
  2079  		for _, testCase := range testCases {
  2080  			t.Run(testCase.Description, func(t *testing.T) {
  2081  				result := <-search(testCase.TeamId, testCase.Term, testCase.IncludeDeleted)
  2082  				require.Nil(t, result.Err)
  2083  
  2084  				channels := result.Data.(*model.ChannelList)
  2085  
  2086  				// AutoCompleteInTeam doesn't currently sort its output results.
  2087  				if name == "AutocompleteInTeam" {
  2088  					sort.Sort(ByChannelDisplayName(*channels))
  2089  				}
  2090  
  2091  				require.Equal(t, testCase.ExpectedResults, channels)
  2092  			})
  2093  		}
  2094  	}
  2095  }
  2096  
  2097  func testChannelStoreSearchAllChannels(t *testing.T, ss store.Store) {
  2098  	cleanupChannels(t, ss)
  2099  
  2100  	t1 := model.Team{}
  2101  	t1.DisplayName = "Name"
  2102  	t1.Name = model.NewId()
  2103  	t1.Email = MakeEmail()
  2104  	t1.Type = model.TEAM_OPEN
  2105  	store.Must(ss.Team().Save(&t1))
  2106  
  2107  	t2 := model.Team{}
  2108  	t2.DisplayName = "Name2"
  2109  	t2.Name = model.NewId()
  2110  	t2.Email = MakeEmail()
  2111  	t2.Type = model.TEAM_OPEN
  2112  	store.Must(ss.Team().Save(&t2))
  2113  
  2114  	o1 := model.Channel{
  2115  		TeamId:      t1.Id,
  2116  		DisplayName: "ChannelA",
  2117  		Name:        "zz" + model.NewId() + "b",
  2118  		Type:        model.CHANNEL_OPEN,
  2119  	}
  2120  	store.Must(ss.Channel().Save(&o1, -1))
  2121  
  2122  	o2 := model.Channel{
  2123  		TeamId:      t2.Id,
  2124  		DisplayName: "ChannelA",
  2125  		Name:        "zz" + model.NewId() + "b",
  2126  		Type:        model.CHANNEL_OPEN,
  2127  	}
  2128  	store.Must(ss.Channel().Save(&o2, -1))
  2129  
  2130  	m1 := model.ChannelMember{
  2131  		ChannelId:   o1.Id,
  2132  		UserId:      model.NewId(),
  2133  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  2134  	}
  2135  	store.Must(ss.Channel().SaveMember(&m1))
  2136  
  2137  	m2 := model.ChannelMember{
  2138  		ChannelId:   o1.Id,
  2139  		UserId:      model.NewId(),
  2140  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  2141  	}
  2142  	store.Must(ss.Channel().SaveMember(&m2))
  2143  
  2144  	m3 := model.ChannelMember{
  2145  		ChannelId:   o2.Id,
  2146  		UserId:      model.NewId(),
  2147  		NotifyProps: model.GetDefaultChannelNotifyProps(),
  2148  	}
  2149  	store.Must(ss.Channel().SaveMember(&m3))
  2150  
  2151  	o3 := model.Channel{
  2152  		TeamId:      t1.Id,
  2153  		DisplayName: "ChannelA (alternate)",
  2154  		Name:        "zz" + model.NewId() + "b",
  2155  		Type:        model.CHANNEL_OPEN,
  2156  	}
  2157  	store.Must(ss.Channel().Save(&o3, -1))
  2158  
  2159  	o4 := model.Channel{
  2160  		TeamId:      t1.Id,
  2161  		DisplayName: "ChannelB",
  2162  		Name:        "zz" + model.NewId() + "b",
  2163  		Type:        model.CHANNEL_PRIVATE,
  2164  	}
  2165  	store.Must(ss.Channel().Save(&o4, -1))
  2166  
  2167  	o5 := model.Channel{
  2168  		TeamId:      t1.Id,
  2169  		DisplayName: "ChannelC",
  2170  		Name:        "zz" + model.NewId() + "b",
  2171  		Type:        model.CHANNEL_PRIVATE,
  2172  	}
  2173  	store.Must(ss.Channel().Save(&o5, -1))
  2174  
  2175  	o6 := model.Channel{
  2176  		TeamId:      t1.Id,
  2177  		DisplayName: "Off-Topic",
  2178  		Name:        "off-topic",
  2179  		Type:        model.CHANNEL_OPEN,
  2180  	}
  2181  	store.Must(ss.Channel().Save(&o6, -1))
  2182  
  2183  	o7 := model.Channel{
  2184  		TeamId:      t1.Id,
  2185  		DisplayName: "Off-Set",
  2186  		Name:        "off-set",
  2187  		Type:        model.CHANNEL_OPEN,
  2188  	}
  2189  	store.Must(ss.Channel().Save(&o7, -1))
  2190  
  2191  	o8 := model.Channel{
  2192  		TeamId:      t1.Id,
  2193  		DisplayName: "Off-Limit",
  2194  		Name:        "off-limit",
  2195  		Type:        model.CHANNEL_PRIVATE,
  2196  	}
  2197  	store.Must(ss.Channel().Save(&o8, -1))
  2198  
  2199  	o9 := model.Channel{
  2200  		TeamId:      t1.Id,
  2201  		DisplayName: "Town Square",
  2202  		Name:        "town-square",
  2203  		Type:        model.CHANNEL_OPEN,
  2204  	}
  2205  	store.Must(ss.Channel().Save(&o9, -1))
  2206  
  2207  	o10 := model.Channel{
  2208  		TeamId:      t1.Id,
  2209  		DisplayName: "The",
  2210  		Name:        "the",
  2211  		Type:        model.CHANNEL_OPEN,
  2212  	}
  2213  	store.Must(ss.Channel().Save(&o10, -1))
  2214  
  2215  	o11 := model.Channel{
  2216  		TeamId:      t1.Id,
  2217  		DisplayName: "Native Mobile Apps",
  2218  		Name:        "native-mobile-apps",
  2219  		Type:        model.CHANNEL_OPEN,
  2220  	}
  2221  	store.Must(ss.Channel().Save(&o11, -1))
  2222  
  2223  	o12 := model.Channel{
  2224  		TeamId:      t1.Id,
  2225  		DisplayName: "ChannelZ",
  2226  		Purpose:     "This can now be searchable!",
  2227  		Name:        "with-purpose",
  2228  		Type:        model.CHANNEL_OPEN,
  2229  	}
  2230  	store.Must(ss.Channel().Save(&o12, -1))
  2231  
  2232  	o13 := model.Channel{
  2233  		TeamId:      t1.Id,
  2234  		DisplayName: "ChannelA (deleted)",
  2235  		Name:        model.NewId(),
  2236  		Type:        model.CHANNEL_OPEN,
  2237  	}
  2238  	store.Must(ss.Channel().Save(&o13, -1))
  2239  	o13.DeleteAt = model.GetMillis()
  2240  	o13.UpdateAt = o13.DeleteAt
  2241  	store.Must(ss.Channel().Delete(o13.Id, o13.DeleteAt))
  2242  
  2243  	testCases := []struct {
  2244  		Description     string
  2245  		Term            string
  2246  		IncludeDeleted  bool
  2247  		ExpectedResults *model.ChannelList
  2248  	}{
  2249  		{"ChannelA", "ChannelA", false, &model.ChannelList{&o1, &o2, &o3}},
  2250  		{"ChannelA, include deleted", "ChannelA", true, &model.ChannelList{&o1, &o2, &o3, &o13}},
  2251  		{"empty string", "", false, &model.ChannelList{&o1, &o2, &o3, &o4, &o5, &o12, &o11, &o8, &o7, &o6, &o10, &o9}},
  2252  		{"no matches", "blargh", false, &model.ChannelList{}},
  2253  		{"prefix", "off-", false, &model.ChannelList{&o8, &o7, &o6}},
  2254  		{"full match with dash", "off-topic", false, &model.ChannelList{&o6}},
  2255  		{"town square", "town square", false, &model.ChannelList{&o9}},
  2256  		{"the in name", "the", false, &model.ChannelList{&o10}},
  2257  		{"Mobile", "Mobile", false, &model.ChannelList{&o11}},
  2258  		{"search purpose", "now searchable", false, &model.ChannelList{&o12}},
  2259  		{"pipe ignored", "town square |", false, &model.ChannelList{&o9}},
  2260  	}
  2261  
  2262  	for _, testCase := range testCases {
  2263  		t.Run(testCase.Description, func(t *testing.T) {
  2264  			result := <-ss.Channel().SearchAllChannels(testCase.Term, testCase.IncludeDeleted)
  2265  			require.Nil(t, result.Err)
  2266  			channels := result.Data.(*model.ChannelListWithTeamData)
  2267  			require.Equal(t, len(*channels), len(*testCase.ExpectedResults))
  2268  			for i, expected := range *testCase.ExpectedResults {
  2269  				require.Equal(t, (*channels)[i].Id, expected.Id)
  2270  			}
  2271  		})
  2272  	}
  2273  }
  2274  
  2275  func testChannelStoreAutocompleteInTeamForSearch(t *testing.T, ss store.Store) {
  2276  	u1 := &model.User{}
  2277  	u1.Email = MakeEmail()
  2278  	u1.Username = "user1" + model.NewId()
  2279  	u1.Nickname = model.NewId()
  2280  	store.Must(ss.User().Save(u1))
  2281  
  2282  	u2 := &model.User{}
  2283  	u2.Email = MakeEmail()
  2284  	u2.Username = "user2" + model.NewId()
  2285  	u2.Nickname = model.NewId()
  2286  	store.Must(ss.User().Save(u2))
  2287  
  2288  	u3 := &model.User{}
  2289  	u3.Email = MakeEmail()
  2290  	u3.Username = "user3" + model.NewId()
  2291  	u3.Nickname = model.NewId()
  2292  	store.Must(ss.User().Save(u3))
  2293  
  2294  	u4 := &model.User{}
  2295  	u4.Email = MakeEmail()
  2296  	u4.Username = "user4" + model.NewId()
  2297  	u4.Nickname = model.NewId()
  2298  	store.Must(ss.User().Save(u4))
  2299  
  2300  	o1 := model.Channel{}
  2301  	o1.TeamId = model.NewId()
  2302  	o1.DisplayName = "ChannelA"
  2303  	o1.Name = "zz" + model.NewId() + "b"
  2304  	o1.Type = model.CHANNEL_OPEN
  2305  	store.Must(ss.Channel().Save(&o1, -1))
  2306  
  2307  	m1 := model.ChannelMember{}
  2308  	m1.ChannelId = o1.Id
  2309  	m1.UserId = u1.Id
  2310  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  2311  	store.Must(ss.Channel().SaveMember(&m1))
  2312  
  2313  	o2 := model.Channel{}
  2314  	o2.TeamId = model.NewId()
  2315  	o2.DisplayName = "Channel2"
  2316  	o2.Name = "zz" + model.NewId() + "b"
  2317  	o2.Type = model.CHANNEL_OPEN
  2318  	store.Must(ss.Channel().Save(&o2, -1))
  2319  
  2320  	m2 := model.ChannelMember{}
  2321  	m2.ChannelId = o2.Id
  2322  	m2.UserId = m1.UserId
  2323  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  2324  	store.Must(ss.Channel().SaveMember(&m2))
  2325  
  2326  	o3 := model.Channel{}
  2327  	o3.TeamId = o1.TeamId
  2328  	o3.DisplayName = "ChannelA"
  2329  	o3.Name = "zz" + model.NewId() + "b"
  2330  	o3.Type = model.CHANNEL_OPEN
  2331  	store.Must(ss.Channel().Save(&o3, -1))
  2332  
  2333  	m3 := model.ChannelMember{}
  2334  	m3.ChannelId = o3.Id
  2335  	m3.UserId = m1.UserId
  2336  	m3.NotifyProps = model.GetDefaultChannelNotifyProps()
  2337  	store.Must(ss.Channel().SaveMember(&m3))
  2338  
  2339  	store.Must(ss.Channel().SetDeleteAt(o3.Id, 100, 100))
  2340  
  2341  	o4 := model.Channel{}
  2342  	o4.TeamId = o1.TeamId
  2343  	o4.DisplayName = "ChannelA"
  2344  	o4.Name = "zz" + model.NewId() + "b"
  2345  	o4.Type = model.CHANNEL_PRIVATE
  2346  	store.Must(ss.Channel().Save(&o4, -1))
  2347  
  2348  	m4 := model.ChannelMember{}
  2349  	m4.ChannelId = o4.Id
  2350  	m4.UserId = m1.UserId
  2351  	m4.NotifyProps = model.GetDefaultChannelNotifyProps()
  2352  	store.Must(ss.Channel().SaveMember(&m4))
  2353  
  2354  	o5 := model.Channel{}
  2355  	o5.TeamId = o1.TeamId
  2356  	o5.DisplayName = "ChannelC"
  2357  	o5.Name = "zz" + model.NewId() + "b"
  2358  	o5.Type = model.CHANNEL_PRIVATE
  2359  	store.Must(ss.Channel().Save(&o5, -1))
  2360  
  2361  	store.Must(ss.Channel().CreateDirectChannel(u1.Id, u2.Id))
  2362  	store.Must(ss.Channel().CreateDirectChannel(u2.Id, u3.Id))
  2363  
  2364  	tt := []struct {
  2365  		name            string
  2366  		term            string
  2367  		includeDeleted  bool
  2368  		expectedMatches int
  2369  	}{
  2370  		{"Empty search (list all)", "", false, 4},
  2371  		{"Narrow search", "ChannelA", false, 2},
  2372  		{"Wide search", "Cha", false, 3},
  2373  		{"Direct messages", "user", false, 1},
  2374  		{"Wide search with archived channels", "Cha", true, 4},
  2375  		{"Narrow with archived channels", "ChannelA", true, 3},
  2376  		{"Direct messages with archived channels", "user", true, 1},
  2377  		{"Search without results", "blarg", true, 0},
  2378  	}
  2379  
  2380  	for _, tc := range tt {
  2381  		t.Run(tc.name, func(t *testing.T) {
  2382  			result := <-ss.Channel().AutocompleteInTeamForSearch(o1.TeamId, m1.UserId, "ChannelA", false)
  2383  			require.Nil(t, result.Err)
  2384  			channels := result.Data.(*model.ChannelList)
  2385  			require.Len(t, *channels, 2)
  2386  		})
  2387  	}
  2388  }
  2389  
  2390  func testChannelStoreGetMembersByIds(t *testing.T, ss store.Store) {
  2391  	o1 := model.Channel{}
  2392  	o1.TeamId = model.NewId()
  2393  	o1.DisplayName = "ChannelA"
  2394  	o1.Name = "zz" + model.NewId() + "b"
  2395  	o1.Type = model.CHANNEL_OPEN
  2396  	store.Must(ss.Channel().Save(&o1, -1))
  2397  
  2398  	m1 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()}
  2399  	store.Must(ss.Channel().SaveMember(m1))
  2400  
  2401  	if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId}); r.Err != nil {
  2402  		t.Fatal(r.Err)
  2403  	} else {
  2404  		rm1 := (*r.Data.(*model.ChannelMembers))[0]
  2405  
  2406  		if rm1.ChannelId != m1.ChannelId {
  2407  			t.Fatal("bad team id")
  2408  		}
  2409  
  2410  		if rm1.UserId != m1.UserId {
  2411  			t.Fatal("bad user id")
  2412  		}
  2413  	}
  2414  
  2415  	m2 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()}
  2416  	store.Must(ss.Channel().SaveMember(m2))
  2417  
  2418  	if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId, m2.UserId, model.NewId()}); r.Err != nil {
  2419  		t.Fatal(r.Err)
  2420  	} else {
  2421  		rm := (*r.Data.(*model.ChannelMembers))
  2422  
  2423  		if len(rm) != 2 {
  2424  			t.Fatal("return wrong number of results")
  2425  		}
  2426  	}
  2427  
  2428  	if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{}); r.Err == nil {
  2429  		t.Fatal("empty user ids - should have failed")
  2430  	}
  2431  }
  2432  
  2433  func testChannelStoreAnalyticsDeletedTypeCount(t *testing.T, ss store.Store) {
  2434  	o1 := model.Channel{}
  2435  	o1.TeamId = model.NewId()
  2436  	o1.DisplayName = "ChannelA"
  2437  	o1.Name = "zz" + model.NewId() + "b"
  2438  	o1.Type = model.CHANNEL_OPEN
  2439  	store.Must(ss.Channel().Save(&o1, -1))
  2440  
  2441  	o2 := model.Channel{}
  2442  	o2.TeamId = model.NewId()
  2443  	o2.DisplayName = "Channel2"
  2444  	o2.Name = "zz" + model.NewId() + "b"
  2445  	o2.Type = model.CHANNEL_OPEN
  2446  	store.Must(ss.Channel().Save(&o2, -1))
  2447  
  2448  	p3 := model.Channel{}
  2449  	p3.TeamId = model.NewId()
  2450  	p3.DisplayName = "Channel3"
  2451  	p3.Name = "zz" + model.NewId() + "b"
  2452  	p3.Type = model.CHANNEL_PRIVATE
  2453  	store.Must(ss.Channel().Save(&p3, -1))
  2454  
  2455  	u1 := &model.User{}
  2456  	u1.Email = MakeEmail()
  2457  	u1.Nickname = model.NewId()
  2458  	store.Must(ss.User().Save(u1))
  2459  
  2460  	u2 := &model.User{}
  2461  	u2.Email = MakeEmail()
  2462  	u2.Nickname = model.NewId()
  2463  	store.Must(ss.User().Save(u2))
  2464  
  2465  	var d4 *model.Channel
  2466  	if result := <-ss.Channel().CreateDirectChannel(u1.Id, u2.Id); result.Err != nil {
  2467  		t.Fatalf(result.Err.Error())
  2468  	} else {
  2469  		d4 = result.Data.(*model.Channel)
  2470  	}
  2471  	defer func() {
  2472  		<-ss.Channel().PermanentDeleteMembersByChannel(d4.Id)
  2473  		<-ss.Channel().PermanentDelete(d4.Id)
  2474  	}()
  2475  
  2476  	var openStartCount int64
  2477  	if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil {
  2478  		t.Fatal(result.Err.Error())
  2479  	} else {
  2480  		openStartCount = result.Data.(int64)
  2481  	}
  2482  
  2483  	var privateStartCount int64
  2484  	if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil {
  2485  		t.Fatal(result.Err.Error())
  2486  	} else {
  2487  		privateStartCount = result.Data.(int64)
  2488  	}
  2489  
  2490  	var directStartCount int64
  2491  	if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil {
  2492  		t.Fatal(result.Err.Error())
  2493  	} else {
  2494  		directStartCount = result.Data.(int64)
  2495  	}
  2496  
  2497  	store.Must(ss.Channel().Delete(o1.Id, model.GetMillis()))
  2498  	store.Must(ss.Channel().Delete(o2.Id, model.GetMillis()))
  2499  	store.Must(ss.Channel().Delete(p3.Id, model.GetMillis()))
  2500  	store.Must(ss.Channel().Delete(d4.Id, model.GetMillis()))
  2501  
  2502  	if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil {
  2503  		t.Fatal(result.Err.Error())
  2504  	} else {
  2505  		if result.Data.(int64) != openStartCount+2 {
  2506  			t.Fatalf("Wrong open channel deleted count.")
  2507  		}
  2508  	}
  2509  
  2510  	if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil {
  2511  		t.Fatal(result.Err.Error())
  2512  	} else {
  2513  		if result.Data.(int64) != privateStartCount+1 {
  2514  			t.Fatalf("Wrong private channel deleted count.")
  2515  		}
  2516  	}
  2517  
  2518  	if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil {
  2519  		t.Fatal(result.Err.Error())
  2520  	} else {
  2521  		if result.Data.(int64) != directStartCount+1 {
  2522  			t.Fatalf("Wrong direct channel deleted count.")
  2523  		}
  2524  	}
  2525  }
  2526  
  2527  func testChannelStoreGetPinnedPosts(t *testing.T, ss store.Store) {
  2528  	o1 := store.Must(ss.Channel().Save(&model.Channel{
  2529  		TeamId:      model.NewId(),
  2530  		DisplayName: "Name",
  2531  		Name:        "zz" + model.NewId() + "b",
  2532  		Type:        model.CHANNEL_OPEN,
  2533  	}, -1)).(*model.Channel)
  2534  
  2535  	p1 := store.Must(ss.Post().Save(&model.Post{
  2536  		UserId:    model.NewId(),
  2537  		ChannelId: o1.Id,
  2538  		Message:   "test",
  2539  		IsPinned:  true,
  2540  	})).(*model.Post)
  2541  
  2542  	if r1 := <-ss.Channel().GetPinnedPosts(o1.Id); r1.Err != nil {
  2543  		t.Fatal(r1.Err)
  2544  	} else if r1.Data.(*model.PostList).Posts[p1.Id] == nil {
  2545  		t.Fatal("didn't return relevant pinned posts")
  2546  	}
  2547  
  2548  	o2 := store.Must(ss.Channel().Save(&model.Channel{
  2549  		TeamId:      model.NewId(),
  2550  		DisplayName: "Name",
  2551  		Name:        "zz" + model.NewId() + "b",
  2552  		Type:        model.CHANNEL_OPEN,
  2553  	}, -1)).(*model.Channel)
  2554  
  2555  	store.Must(ss.Post().Save(&model.Post{
  2556  		UserId:    model.NewId(),
  2557  		ChannelId: o2.Id,
  2558  		Message:   "test",
  2559  	}))
  2560  
  2561  	if r2 := <-ss.Channel().GetPinnedPosts(o2.Id); r2.Err != nil {
  2562  		t.Fatal(r2.Err)
  2563  	} else if len(r2.Data.(*model.PostList).Posts) != 0 {
  2564  		t.Fatal("wasn't supposed to return posts")
  2565  	}
  2566  }
  2567  
  2568  func testChannelStoreMaxChannelsPerTeam(t *testing.T, ss store.Store) {
  2569  	channel := &model.Channel{
  2570  		TeamId:      model.NewId(),
  2571  		DisplayName: "Channel",
  2572  		Name:        model.NewId(),
  2573  		Type:        model.CHANNEL_OPEN,
  2574  	}
  2575  	result := <-ss.Channel().Save(channel, 0)
  2576  	assert.NotEqual(t, nil, result.Err)
  2577  	assert.Equal(t, result.Err.Id, "store.sql_channel.save_channel.limit.app_error")
  2578  
  2579  	channel.Id = ""
  2580  	result = <-ss.Channel().Save(channel, 1)
  2581  	assert.Nil(t, result.Err)
  2582  }
  2583  
  2584  func testChannelStoreGetChannelsByScheme(t *testing.T, ss store.Store) {
  2585  	// Create some schemes.
  2586  	s1 := &model.Scheme{
  2587  		DisplayName: model.NewId(),
  2588  		Name:        model.NewId(),
  2589  		Description: model.NewId(),
  2590  		Scope:       model.SCHEME_SCOPE_CHANNEL,
  2591  	}
  2592  
  2593  	s2 := &model.Scheme{
  2594  		DisplayName: model.NewId(),
  2595  		Name:        model.NewId(),
  2596  		Description: model.NewId(),
  2597  		Scope:       model.SCHEME_SCOPE_CHANNEL,
  2598  	}
  2599  
  2600  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  2601  	s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme)
  2602  
  2603  	// Create and save some teams.
  2604  	c1 := &model.Channel{
  2605  		TeamId:      model.NewId(),
  2606  		DisplayName: "Name",
  2607  		Name:        model.NewId(),
  2608  		Type:        model.CHANNEL_OPEN,
  2609  		SchemeId:    &s1.Id,
  2610  	}
  2611  
  2612  	c2 := &model.Channel{
  2613  		TeamId:      model.NewId(),
  2614  		DisplayName: "Name",
  2615  		Name:        model.NewId(),
  2616  		Type:        model.CHANNEL_OPEN,
  2617  		SchemeId:    &s1.Id,
  2618  	}
  2619  
  2620  	c3 := &model.Channel{
  2621  		TeamId:      model.NewId(),
  2622  		DisplayName: "Name",
  2623  		Name:        model.NewId(),
  2624  		Type:        model.CHANNEL_OPEN,
  2625  	}
  2626  
  2627  	_ = (<-ss.Channel().Save(c1, 100)).Data.(*model.Channel)
  2628  	_ = (<-ss.Channel().Save(c2, 100)).Data.(*model.Channel)
  2629  	_ = (<-ss.Channel().Save(c3, 100)).Data.(*model.Channel)
  2630  
  2631  	// Get the channels by a valid Scheme ID.
  2632  	res1 := <-ss.Channel().GetChannelsByScheme(s1.Id, 0, 100)
  2633  	assert.Nil(t, res1.Err)
  2634  	d1 := res1.Data.(model.ChannelList)
  2635  	assert.Len(t, d1, 2)
  2636  
  2637  	// Get the channels by a valid Scheme ID where there aren't any matching Channel.
  2638  	res2 := <-ss.Channel().GetChannelsByScheme(s2.Id, 0, 100)
  2639  	assert.Nil(t, res2.Err)
  2640  	d2 := res2.Data.(model.ChannelList)
  2641  	assert.Len(t, d2, 0)
  2642  
  2643  	// Get the channels by an invalid Scheme ID.
  2644  	res3 := <-ss.Channel().GetChannelsByScheme(model.NewId(), 0, 100)
  2645  	assert.Nil(t, res3.Err)
  2646  	d3 := res3.Data.(model.ChannelList)
  2647  	assert.Len(t, d3, 0)
  2648  }
  2649  
  2650  func testChannelStoreMigrateChannelMembers(t *testing.T, ss store.Store) {
  2651  	s1 := model.NewId()
  2652  	c1 := &model.Channel{
  2653  		TeamId:      model.NewId(),
  2654  		DisplayName: "Name",
  2655  		Name:        model.NewId(),
  2656  		Type:        model.CHANNEL_OPEN,
  2657  		SchemeId:    &s1,
  2658  	}
  2659  	c1 = (<-ss.Channel().Save(c1, 100)).Data.(*model.Channel)
  2660  
  2661  	cm1 := &model.ChannelMember{
  2662  		ChannelId:     c1.Id,
  2663  		UserId:        model.NewId(),
  2664  		ExplicitRoles: "channel_admin channel_user",
  2665  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2666  	}
  2667  	cm2 := &model.ChannelMember{
  2668  		ChannelId:     c1.Id,
  2669  		UserId:        model.NewId(),
  2670  		ExplicitRoles: "channel_user",
  2671  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2672  	}
  2673  	cm3 := &model.ChannelMember{
  2674  		ChannelId:     c1.Id,
  2675  		UserId:        model.NewId(),
  2676  		ExplicitRoles: "something_else",
  2677  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2678  	}
  2679  
  2680  	cm1 = (<-ss.Channel().SaveMember(cm1)).Data.(*model.ChannelMember)
  2681  	cm2 = (<-ss.Channel().SaveMember(cm2)).Data.(*model.ChannelMember)
  2682  	cm3 = (<-ss.Channel().SaveMember(cm3)).Data.(*model.ChannelMember)
  2683  
  2684  	lastDoneChannelId := strings.Repeat("0", 26)
  2685  	lastDoneUserId := strings.Repeat("0", 26)
  2686  
  2687  	for {
  2688  		res := <-ss.Channel().MigrateChannelMembers(lastDoneChannelId, lastDoneUserId)
  2689  		if assert.Nil(t, res.Err) {
  2690  			if res.Data == nil {
  2691  				break
  2692  			}
  2693  			data := res.Data.(map[string]string)
  2694  			lastDoneChannelId = data["ChannelId"]
  2695  			lastDoneUserId = data["UserId"]
  2696  		}
  2697  	}
  2698  
  2699  	ss.Channel().ClearCaches()
  2700  
  2701  	res1 := <-ss.Channel().GetMember(cm1.ChannelId, cm1.UserId)
  2702  	assert.Nil(t, res1.Err)
  2703  	cm1b := res1.Data.(*model.ChannelMember)
  2704  	assert.Equal(t, "", cm1b.ExplicitRoles)
  2705  	assert.True(t, cm1b.SchemeUser)
  2706  	assert.True(t, cm1b.SchemeAdmin)
  2707  
  2708  	res2 := <-ss.Channel().GetMember(cm2.ChannelId, cm2.UserId)
  2709  	assert.Nil(t, res2.Err)
  2710  	cm2b := res2.Data.(*model.ChannelMember)
  2711  	assert.Equal(t, "", cm2b.ExplicitRoles)
  2712  	assert.True(t, cm2b.SchemeUser)
  2713  	assert.False(t, cm2b.SchemeAdmin)
  2714  
  2715  	res3 := <-ss.Channel().GetMember(cm3.ChannelId, cm3.UserId)
  2716  	assert.Nil(t, res3.Err)
  2717  	cm3b := res3.Data.(*model.ChannelMember)
  2718  	assert.Equal(t, "something_else", cm3b.ExplicitRoles)
  2719  	assert.False(t, cm3b.SchemeUser)
  2720  	assert.False(t, cm3b.SchemeAdmin)
  2721  }
  2722  
  2723  func testResetAllChannelSchemes(t *testing.T, ss store.Store) {
  2724  	s1 := &model.Scheme{
  2725  		Name:        model.NewId(),
  2726  		DisplayName: model.NewId(),
  2727  		Description: model.NewId(),
  2728  		Scope:       model.SCHEME_SCOPE_CHANNEL,
  2729  	}
  2730  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  2731  
  2732  	c1 := &model.Channel{
  2733  		TeamId:      model.NewId(),
  2734  		DisplayName: "Name",
  2735  		Name:        model.NewId(),
  2736  		Type:        model.CHANNEL_OPEN,
  2737  		SchemeId:    &s1.Id,
  2738  	}
  2739  
  2740  	c2 := &model.Channel{
  2741  		TeamId:      model.NewId(),
  2742  		DisplayName: "Name",
  2743  		Name:        model.NewId(),
  2744  		Type:        model.CHANNEL_OPEN,
  2745  		SchemeId:    &s1.Id,
  2746  	}
  2747  
  2748  	c1 = (<-ss.Channel().Save(c1, 100)).Data.(*model.Channel)
  2749  	c2 = (<-ss.Channel().Save(c2, 100)).Data.(*model.Channel)
  2750  
  2751  	assert.Equal(t, s1.Id, *c1.SchemeId)
  2752  	assert.Equal(t, s1.Id, *c2.SchemeId)
  2753  
  2754  	res := <-ss.Channel().ResetAllChannelSchemes()
  2755  	assert.Nil(t, res.Err)
  2756  
  2757  	c1 = (<-ss.Channel().Get(c1.Id, true)).Data.(*model.Channel)
  2758  	c2 = (<-ss.Channel().Get(c2.Id, true)).Data.(*model.Channel)
  2759  
  2760  	assert.Equal(t, "", *c1.SchemeId)
  2761  	assert.Equal(t, "", *c2.SchemeId)
  2762  }
  2763  
  2764  func testChannelStoreClearAllCustomRoleAssignments(t *testing.T, ss store.Store) {
  2765  	c := &model.Channel{
  2766  		TeamId:      model.NewId(),
  2767  		DisplayName: "Name",
  2768  		Name:        model.NewId(),
  2769  		Type:        model.CHANNEL_OPEN,
  2770  	}
  2771  
  2772  	c = (<-ss.Channel().Save(c, 100)).Data.(*model.Channel)
  2773  
  2774  	m1 := &model.ChannelMember{
  2775  		ChannelId:     c.Id,
  2776  		UserId:        model.NewId(),
  2777  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2778  		ExplicitRoles: "channel_user channel_admin system_user_access_token",
  2779  	}
  2780  	m2 := &model.ChannelMember{
  2781  		ChannelId:     c.Id,
  2782  		UserId:        model.NewId(),
  2783  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2784  		ExplicitRoles: "channel_user custom_role channel_admin another_custom_role",
  2785  	}
  2786  	m3 := &model.ChannelMember{
  2787  		ChannelId:     c.Id,
  2788  		UserId:        model.NewId(),
  2789  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2790  		ExplicitRoles: "channel_user",
  2791  	}
  2792  	m4 := &model.ChannelMember{
  2793  		ChannelId:     c.Id,
  2794  		UserId:        model.NewId(),
  2795  		NotifyProps:   model.GetDefaultChannelNotifyProps(),
  2796  		ExplicitRoles: "custom_only",
  2797  	}
  2798  
  2799  	store.Must(ss.Channel().SaveMember(m1))
  2800  	store.Must(ss.Channel().SaveMember(m2))
  2801  	store.Must(ss.Channel().SaveMember(m3))
  2802  	store.Must(ss.Channel().SaveMember(m4))
  2803  
  2804  	require.Nil(t, (<-ss.Channel().ClearAllCustomRoleAssignments()).Err)
  2805  
  2806  	r1 := <-ss.Channel().GetMember(m1.ChannelId, m1.UserId)
  2807  	require.Nil(t, r1.Err)
  2808  	assert.Equal(t, m1.ExplicitRoles, r1.Data.(*model.ChannelMember).Roles)
  2809  
  2810  	r2 := <-ss.Channel().GetMember(m2.ChannelId, m2.UserId)
  2811  	require.Nil(t, r2.Err)
  2812  	assert.Equal(t, "channel_user channel_admin", r2.Data.(*model.ChannelMember).Roles)
  2813  
  2814  	r3 := <-ss.Channel().GetMember(m3.ChannelId, m3.UserId)
  2815  	require.Nil(t, r3.Err)
  2816  	assert.Equal(t, m3.ExplicitRoles, r3.Data.(*model.ChannelMember).Roles)
  2817  
  2818  	r4 := <-ss.Channel().GetMember(m4.ChannelId, m4.UserId)
  2819  	require.Nil(t, r4.Err)
  2820  	assert.Equal(t, "", r4.Data.(*model.ChannelMember).Roles)
  2821  }
  2822  
  2823  // testMaterializedPublicChannels tests edge cases involving the triggers and stored procedures
  2824  // that materialize the PublicChannels table.
  2825  func testMaterializedPublicChannels(t *testing.T, ss store.Store, s SqlSupplier) {
  2826  	teamId := model.NewId()
  2827  
  2828  	// o1 is a public channel on the team
  2829  	o1 := model.Channel{
  2830  		TeamId:      teamId,
  2831  		DisplayName: "Open Channel",
  2832  		Name:        model.NewId(),
  2833  		Type:        model.CHANNEL_OPEN,
  2834  	}
  2835  	store.Must(ss.Channel().Save(&o1, -1))
  2836  
  2837  	// o2 is another public channel on the team
  2838  	o2 := model.Channel{
  2839  		TeamId:      teamId,
  2840  		DisplayName: "Open Channel 2",
  2841  		Name:        model.NewId(),
  2842  		Type:        model.CHANNEL_OPEN,
  2843  	}
  2844  	store.Must(ss.Channel().Save(&o2, -1))
  2845  
  2846  	t.Run("o1 and o2 initially listed in public channels", func(t *testing.T) {
  2847  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2848  		require.Nil(t, result.Err)
  2849  		require.Equal(t, &model.ChannelList{&o1, &o2}, result.Data.(*model.ChannelList))
  2850  	})
  2851  
  2852  	o1.DeleteAt = model.GetMillis()
  2853  	o1.UpdateAt = model.GetMillis()
  2854  	store.Must(ss.Channel().Delete(o1.Id, o1.DeleteAt))
  2855  
  2856  	t.Run("o1 still listed in public channels when marked as deleted", func(t *testing.T) {
  2857  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2858  		require.Nil(t, result.Err)
  2859  		require.Equal(t, &model.ChannelList{&o1, &o2}, result.Data.(*model.ChannelList))
  2860  	})
  2861  
  2862  	<-ss.Channel().PermanentDelete(o1.Id)
  2863  
  2864  	t.Run("o1 no longer listed in public channels when permanently deleted", func(t *testing.T) {
  2865  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2866  		require.Nil(t, result.Err)
  2867  		require.Equal(t, &model.ChannelList{&o2}, result.Data.(*model.ChannelList))
  2868  	})
  2869  
  2870  	o2.Type = model.CHANNEL_PRIVATE
  2871  	require.Nil(t, (<-ss.Channel().Update(&o2)).Err)
  2872  
  2873  	t.Run("o2 no longer listed since now private", func(t *testing.T) {
  2874  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2875  		require.Nil(t, result.Err)
  2876  		require.Equal(t, &model.ChannelList{}, result.Data.(*model.ChannelList))
  2877  	})
  2878  
  2879  	o2.Type = model.CHANNEL_OPEN
  2880  	require.Nil(t, (<-ss.Channel().Update(&o2)).Err)
  2881  
  2882  	t.Run("o2 listed once again since now public", func(t *testing.T) {
  2883  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2884  		require.Nil(t, result.Err)
  2885  		require.Equal(t, &model.ChannelList{&o2}, result.Data.(*model.ChannelList))
  2886  	})
  2887  
  2888  	// o3 is a public channel on the team that already existed in the PublicChannels table.
  2889  	o3 := model.Channel{
  2890  		Id:          model.NewId(),
  2891  		TeamId:      teamId,
  2892  		DisplayName: "Open Channel 3",
  2893  		Name:        model.NewId(),
  2894  		Type:        model.CHANNEL_OPEN,
  2895  	}
  2896  
  2897  	_, err := s.GetMaster().ExecNoTimeout(`
  2898  		INSERT INTO
  2899  		    PublicChannels(Id, DeleteAt, TeamId, DisplayName, Name, Header, Purpose)
  2900  		VALUES
  2901  		    (:Id, :DeleteAt, :TeamId, :DisplayName, :Name, :Header, :Purpose);
  2902  	`, map[string]interface{}{
  2903  		"Id":          o3.Id,
  2904  		"DeleteAt":    o3.DeleteAt,
  2905  		"TeamId":      o3.TeamId,
  2906  		"DisplayName": o3.DisplayName,
  2907  		"Name":        o3.Name,
  2908  		"Header":      o3.Header,
  2909  		"Purpose":     o3.Purpose,
  2910  	})
  2911  	require.Nil(t, err)
  2912  
  2913  	o3.DisplayName = "Open Channel 3 - Modified"
  2914  
  2915  	_, err = s.GetMaster().ExecNoTimeout(`
  2916  		INSERT INTO
  2917  		    Channels(Id, CreateAt, UpdateAt, DeleteAt, TeamId, Type, DisplayName, Name, Header, Purpose, LastPostAt, TotalMsgCount, ExtraUpdateAt, CreatorId)
  2918  		VALUES
  2919  		    (:Id, :CreateAt, :UpdateAt, :DeleteAt, :TeamId, :Type, :DisplayName, :Name, :Header, :Purpose, :LastPostAt, :TotalMsgCount, :ExtraUpdateAt, :CreatorId);
  2920  	`, map[string]interface{}{
  2921  		"Id":            o3.Id,
  2922  		"CreateAt":      o3.CreateAt,
  2923  		"UpdateAt":      o3.UpdateAt,
  2924  		"DeleteAt":      o3.DeleteAt,
  2925  		"TeamId":        o3.TeamId,
  2926  		"Type":          o3.Type,
  2927  		"DisplayName":   o3.DisplayName,
  2928  		"Name":          o3.Name,
  2929  		"Header":        o3.Header,
  2930  		"Purpose":       o3.Purpose,
  2931  		"LastPostAt":    o3.LastPostAt,
  2932  		"TotalMsgCount": o3.TotalMsgCount,
  2933  		"ExtraUpdateAt": o3.ExtraUpdateAt,
  2934  		"CreatorId":     o3.CreatorId,
  2935  	})
  2936  	require.Nil(t, err)
  2937  
  2938  	t.Run("verify o3 INSERT converted to UPDATE", func(t *testing.T) {
  2939  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2940  		require.Nil(t, result.Err)
  2941  		require.Equal(t, &model.ChannelList{&o2, &o3}, result.Data.(*model.ChannelList))
  2942  	})
  2943  
  2944  	// o4 is a public channel on the team that existed in the Channels table but was omitted from the PublicChannels table.
  2945  	o4 := model.Channel{
  2946  		TeamId:      teamId,
  2947  		DisplayName: "Open Channel 4",
  2948  		Name:        model.NewId(),
  2949  		Type:        model.CHANNEL_OPEN,
  2950  	}
  2951  
  2952  	store.Must(ss.Channel().Save(&o4, -1))
  2953  
  2954  	_, err = s.GetMaster().ExecNoTimeout(`
  2955  		DELETE FROM
  2956  		    PublicChannels
  2957  		WHERE
  2958  		    Id = :Id
  2959  	`, map[string]interface{}{
  2960  		"Id": o4.Id,
  2961  	})
  2962  	require.Nil(t, err)
  2963  
  2964  	o4.DisplayName += " - Modified"
  2965  	require.Nil(t, (<-ss.Channel().Update(&o4)).Err)
  2966  
  2967  	t.Run("verify o4 UPDATE converted to INSERT", func(t *testing.T) {
  2968  		result := <-ss.Channel().SearchInTeam(teamId, "", true)
  2969  		require.Nil(t, result.Err)
  2970  		require.Equal(t, &model.ChannelList{&o2, &o3, &o4}, result.Data.(*model.ChannelList))
  2971  	})
  2972  }
  2973  
  2974  func testChannelStoreGetAllChannelsForExportAfter(t *testing.T, ss store.Store) {
  2975  	t1 := model.Team{}
  2976  	t1.DisplayName = "Name"
  2977  	t1.Name = model.NewId()
  2978  	t1.Email = MakeEmail()
  2979  	t1.Type = model.TEAM_OPEN
  2980  	store.Must(ss.Team().Save(&t1))
  2981  
  2982  	c1 := model.Channel{}
  2983  	c1.TeamId = t1.Id
  2984  	c1.DisplayName = "Channel1"
  2985  	c1.Name = "zz" + model.NewId() + "b"
  2986  	c1.Type = model.CHANNEL_OPEN
  2987  	store.Must(ss.Channel().Save(&c1, -1))
  2988  
  2989  	r1 := <-ss.Channel().GetAllChannelsForExportAfter(10000, strings.Repeat("0", 26))
  2990  	assert.Nil(t, r1.Err)
  2991  	d1 := r1.Data.([]*model.ChannelForExport)
  2992  
  2993  	found := false
  2994  	for _, c := range d1 {
  2995  		if c.Id == c1.Id {
  2996  			found = true
  2997  			assert.Equal(t, t1.Id, c.TeamId)
  2998  			assert.Nil(t, c.SchemeId)
  2999  			assert.Equal(t, t1.Name, c.TeamName)
  3000  		}
  3001  	}
  3002  	assert.True(t, found)
  3003  }
  3004  
  3005  func testChannelStoreGetChannelMembersForExport(t *testing.T, ss store.Store) {
  3006  	t1 := model.Team{}
  3007  	t1.DisplayName = "Name"
  3008  	t1.Name = model.NewId()
  3009  	t1.Email = MakeEmail()
  3010  	t1.Type = model.TEAM_OPEN
  3011  	store.Must(ss.Team().Save(&t1))
  3012  
  3013  	c1 := model.Channel{}
  3014  	c1.TeamId = t1.Id
  3015  	c1.DisplayName = "Channel1"
  3016  	c1.Name = "zz" + model.NewId() + "b"
  3017  	c1.Type = model.CHANNEL_OPEN
  3018  	store.Must(ss.Channel().Save(&c1, -1))
  3019  
  3020  	c2 := model.Channel{}
  3021  	c2.TeamId = model.NewId()
  3022  	c2.DisplayName = "Channel2"
  3023  	c2.Name = "zz" + model.NewId() + "b"
  3024  	c2.Type = model.CHANNEL_OPEN
  3025  	store.Must(ss.Channel().Save(&c2, -1))
  3026  
  3027  	u1 := model.User{}
  3028  	u1.Email = MakeEmail()
  3029  	u1.Nickname = model.NewId()
  3030  	store.Must(ss.User().Save(&u1))
  3031  
  3032  	m1 := model.ChannelMember{}
  3033  	m1.ChannelId = c1.Id
  3034  	m1.UserId = u1.Id
  3035  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  3036  	store.Must(ss.Channel().SaveMember(&m1))
  3037  
  3038  	m2 := model.ChannelMember{}
  3039  	m2.ChannelId = c2.Id
  3040  	m2.UserId = u1.Id
  3041  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  3042  	store.Must(ss.Channel().SaveMember(&m2))
  3043  
  3044  	r1 := <-ss.Channel().GetChannelMembersForExport(u1.Id, t1.Id)
  3045  	assert.Nil(t, r1.Err)
  3046  
  3047  	d1 := r1.Data.([]*model.ChannelMemberForExport)
  3048  	assert.Len(t, d1, 1)
  3049  
  3050  	cmfe1 := d1[0]
  3051  	assert.Equal(t, c1.Name, cmfe1.ChannelName)
  3052  	assert.Equal(t, c1.Id, cmfe1.ChannelId)
  3053  	assert.Equal(t, u1.Id, cmfe1.UserId)
  3054  }
  3055  
  3056  func testChannelStoreRemoveAllDeactivatedMembers(t *testing.T, ss store.Store) {
  3057  	// Set up all the objects needed in the store.
  3058  	t1 := model.Team{}
  3059  	t1.DisplayName = "Name"
  3060  	t1.Name = model.NewId()
  3061  	t1.Email = MakeEmail()
  3062  	t1.Type = model.TEAM_OPEN
  3063  	store.Must(ss.Team().Save(&t1))
  3064  
  3065  	c1 := model.Channel{}
  3066  	c1.TeamId = t1.Id
  3067  	c1.DisplayName = "Channel1"
  3068  	c1.Name = "zz" + model.NewId() + "b"
  3069  	c1.Type = model.CHANNEL_OPEN
  3070  	store.Must(ss.Channel().Save(&c1, -1))
  3071  
  3072  	u1 := model.User{}
  3073  	u1.Email = MakeEmail()
  3074  	u1.Nickname = model.NewId()
  3075  	store.Must(ss.User().Save(&u1))
  3076  
  3077  	u2 := model.User{}
  3078  	u2.Email = MakeEmail()
  3079  	u2.Nickname = model.NewId()
  3080  	store.Must(ss.User().Save(&u2))
  3081  
  3082  	u3 := model.User{}
  3083  	u3.Email = MakeEmail()
  3084  	u3.Nickname = model.NewId()
  3085  	store.Must(ss.User().Save(&u3))
  3086  
  3087  	m1 := model.ChannelMember{}
  3088  	m1.ChannelId = c1.Id
  3089  	m1.UserId = u1.Id
  3090  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  3091  	store.Must(ss.Channel().SaveMember(&m1))
  3092  
  3093  	m2 := model.ChannelMember{}
  3094  	m2.ChannelId = c1.Id
  3095  	m2.UserId = u2.Id
  3096  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  3097  	store.Must(ss.Channel().SaveMember(&m2))
  3098  
  3099  	m3 := model.ChannelMember{}
  3100  	m3.ChannelId = c1.Id
  3101  	m3.UserId = u3.Id
  3102  	m3.NotifyProps = model.GetDefaultChannelNotifyProps()
  3103  	store.Must(ss.Channel().SaveMember(&m3))
  3104  
  3105  	// Get all the channel members. Check there are 3.
  3106  	r1 := <-ss.Channel().GetMembers(c1.Id, 0, 1000)
  3107  	assert.Nil(t, r1.Err)
  3108  	d1 := r1.Data.(*model.ChannelMembers)
  3109  	assert.Len(t, *d1, 3)
  3110  
  3111  	// Deactivate users 1 & 2.
  3112  	u1.DeleteAt = model.GetMillis()
  3113  	u2.DeleteAt = model.GetMillis()
  3114  	require.Nil(t, (<-ss.User().Update(&u1, true)).Err)
  3115  	require.Nil(t, (<-ss.User().Update(&u2, true)).Err)
  3116  
  3117  	// Remove all deactivated users from the channel.
  3118  	assert.Nil(t, (<-ss.Channel().RemoveAllDeactivatedMembers(c1.Id)).Err)
  3119  
  3120  	// Get all the channel members. Check there is now only 1: m3.
  3121  	r2 := <-ss.Channel().GetMembers(c1.Id, 0, 1000)
  3122  	assert.Nil(t, r1.Err)
  3123  	d2 := r2.Data.(*model.ChannelMembers)
  3124  	assert.Len(t, *d2, 1)
  3125  	assert.Equal(t, (*d2)[0].UserId, u3.Id)
  3126  }