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