github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/store/storetest/post_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  	"fmt"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/store"
    14  	"github.com/mattermost/mattermost-server/utils"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestPostStore(t *testing.T, ss store.Store) {
    20  	t.Run("Save", func(t *testing.T) { testPostStoreSave(t, ss) })
    21  	t.Run("SaveAndUpdateChannelMsgCounts", func(t *testing.T) { testPostStoreSaveChannelMsgCounts(t, ss) })
    22  	t.Run("Get", func(t *testing.T) { testPostStoreGet(t, ss) })
    23  	t.Run("GetSingle", func(t *testing.T) { testPostStoreGetSingle(t, ss) })
    24  	t.Run("GetEtagCache", func(t *testing.T) { testGetEtagCache(t, ss) })
    25  	t.Run("Update", func(t *testing.T) { testPostStoreUpdate(t, ss) })
    26  	t.Run("Delete", func(t *testing.T) { testPostStoreDelete(t, ss) })
    27  	t.Run("Delete1Level", func(t *testing.T) { testPostStoreDelete1Level(t, ss) })
    28  	t.Run("Delete2Level", func(t *testing.T) { testPostStoreDelete2Level(t, ss) })
    29  	t.Run("PermDelete1Level", func(t *testing.T) { testPostStorePermDelete1Level(t, ss) })
    30  	t.Run("PermDelete1Level2", func(t *testing.T) { testPostStorePermDelete1Level2(t, ss) })
    31  	t.Run("GetWithChildren", func(t *testing.T) { testPostStoreGetWithChildren(t, ss) })
    32  	t.Run("GetPostsWithDetails", func(t *testing.T) { testPostStoreGetPostsWithDetails(t, ss) })
    33  	t.Run("GetPostsBeforeAfter", func(t *testing.T) { testPostStoreGetPostsBeforeAfter(t, ss) })
    34  	t.Run("GetPostsSince", func(t *testing.T) { testPostStoreGetPostsSince(t, ss) })
    35  	t.Run("Search", func(t *testing.T) { testPostStoreSearch(t, ss) })
    36  	t.Run("UserCountsWithPostsByDay", func(t *testing.T) { testUserCountsWithPostsByDay(t, ss) })
    37  	t.Run("PostCountsByDay", func(t *testing.T) { testPostCountsByDay(t, ss) })
    38  	t.Run("GetFlaggedPostsForTeam", func(t *testing.T) { testPostStoreGetFlaggedPostsForTeam(t, ss) })
    39  	t.Run("GetFlaggedPosts", func(t *testing.T) { testPostStoreGetFlaggedPosts(t, ss) })
    40  	t.Run("GetFlaggedPostsForChannel", func(t *testing.T) { testPostStoreGetFlaggedPostsForChannel(t, ss) })
    41  	t.Run("GetPostsCreatedAt", func(t *testing.T) { testPostStoreGetPostsCreatedAt(t, ss) })
    42  	t.Run("Overwrite", func(t *testing.T) { testPostStoreOverwrite(t, ss) })
    43  	t.Run("GetPostsByIds", func(t *testing.T) { testPostStoreGetPostsByIds(t, ss) })
    44  	t.Run("GetPostsBatchForIndexing", func(t *testing.T) { testPostStoreGetPostsBatchForIndexing(t, ss) })
    45  	t.Run("PermanentDeleteBatch", func(t *testing.T) { testPostStorePermanentDeleteBatch(t, ss) })
    46  	t.Run("GetOldest", func(t *testing.T) { testPostStoreGetOldest(t, ss) })
    47  	t.Run("TestGetMaxPostSize", func(t *testing.T) { testGetMaxPostSize(t, ss) })
    48  	t.Run("GetParentsForExportAfter", func(t *testing.T) { testPostStoreGetParentsForExportAfter(t, ss) })
    49  	t.Run("GetRepliesForExport", func(t *testing.T) { testPostStoreGetRepliesForExport(t, ss) })
    50  }
    51  
    52  func testPostStoreSave(t *testing.T, ss store.Store) {
    53  	o1 := model.Post{}
    54  	o1.ChannelId = model.NewId()
    55  	o1.UserId = model.NewId()
    56  	o1.Message = "zz" + model.NewId() + "b"
    57  
    58  	if err := (<-ss.Post().Save(&o1)).Err; err != nil {
    59  		t.Fatal("couldn't save item", err)
    60  	}
    61  
    62  	if err := (<-ss.Post().Save(&o1)).Err; err == nil {
    63  		t.Fatal("shouldn't be able to update from save")
    64  	}
    65  }
    66  
    67  func testPostStoreSaveChannelMsgCounts(t *testing.T, ss store.Store) {
    68  	c1 := &model.Channel{Name: model.NewId(), DisplayName: "posttestchannel", Type: model.CHANNEL_OPEN}
    69  	res := <-ss.Channel().Save(c1, 1000000)
    70  	require.Nil(t, res.Err)
    71  
    72  	o1 := model.Post{}
    73  	o1.ChannelId = c1.Id
    74  	o1.UserId = model.NewId()
    75  	o1.Message = "zz" + model.NewId() + "b"
    76  
    77  	require.Nil(t, (<-ss.Post().Save(&o1)).Err)
    78  
    79  	res = <-ss.Channel().Get(c1.Id, false)
    80  	require.Nil(t, res.Err)
    81  	c1 = res.Data.(*model.Channel)
    82  	assert.Equal(t, int64(1), c1.TotalMsgCount, "Message count should update by 1")
    83  
    84  	o1.Id = ""
    85  	o1.Type = model.POST_ADD_TO_TEAM
    86  	require.Nil(t, (<-ss.Post().Save(&o1)).Err)
    87  
    88  	o1.Id = ""
    89  	o1.Type = model.POST_REMOVE_FROM_TEAM
    90  	require.Nil(t, (<-ss.Post().Save(&o1)).Err)
    91  
    92  	res = <-ss.Channel().Get(c1.Id, false)
    93  	require.Nil(t, res.Err)
    94  	c1 = res.Data.(*model.Channel)
    95  	assert.Equal(t, int64(1), c1.TotalMsgCount, "Message count should not update for team add/removed message")
    96  }
    97  
    98  func testPostStoreGet(t *testing.T, ss store.Store) {
    99  	o1 := &model.Post{}
   100  	o1.ChannelId = model.NewId()
   101  	o1.UserId = model.NewId()
   102  	o1.Message = "zz" + model.NewId() + "b"
   103  
   104  	etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
   105  	if strings.Index(etag1, model.CurrentVersion+".") != 0 {
   106  		t.Fatal("Invalid Etag")
   107  	}
   108  
   109  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   110  
   111  	etag2 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
   112  	if strings.Index(etag2, fmt.Sprintf("%v.%v", model.CurrentVersion, o1.UpdateAt)) != 0 {
   113  		t.Fatal("Invalid Etag")
   114  	}
   115  
   116  	if r1 := <-ss.Post().Get(o1.Id); r1.Err != nil {
   117  		t.Fatal(r1.Err)
   118  	} else {
   119  		if r1.Data.(*model.PostList).Posts[o1.Id].CreateAt != o1.CreateAt {
   120  			t.Fatal("invalid returned post")
   121  		}
   122  	}
   123  
   124  	if err := (<-ss.Post().Get("123")).Err; err == nil {
   125  		t.Fatal("Missing id should have failed")
   126  	}
   127  
   128  	if err := (<-ss.Post().Get("")).Err; err == nil {
   129  		t.Fatal("should fail for blank post ids")
   130  	}
   131  }
   132  
   133  func testPostStoreGetSingle(t *testing.T, ss store.Store) {
   134  	o1 := &model.Post{}
   135  	o1.ChannelId = model.NewId()
   136  	o1.UserId = model.NewId()
   137  	o1.Message = "zz" + model.NewId() + "b"
   138  
   139  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   140  
   141  	if r1 := <-ss.Post().GetSingle(o1.Id); r1.Err != nil {
   142  		t.Fatal(r1.Err)
   143  	} else {
   144  		if r1.Data.(*model.Post).CreateAt != o1.CreateAt {
   145  			t.Fatal("invalid returned post")
   146  		}
   147  	}
   148  
   149  	if err := (<-ss.Post().GetSingle("123")).Err; err == nil {
   150  		t.Fatal("Missing id should have failed")
   151  	}
   152  }
   153  
   154  func testGetEtagCache(t *testing.T, ss store.Store) {
   155  	o1 := &model.Post{}
   156  	o1.ChannelId = model.NewId()
   157  	o1.UserId = model.NewId()
   158  	o1.Message = "zz" + model.NewId() + "b"
   159  
   160  	etag1 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   161  	if strings.Index(etag1, model.CurrentVersion+".") != 0 {
   162  		t.Fatal("Invalid Etag")
   163  	}
   164  
   165  	// This one should come from the cache
   166  	etag2 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   167  	if strings.Index(etag2, model.CurrentVersion+".") != 0 {
   168  		t.Fatal("Invalid Etag")
   169  	}
   170  
   171  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   172  
   173  	// We have not invalidated the cache so this should be the same as above
   174  	etag3 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   175  	if strings.Index(etag3, etag2) != 0 {
   176  		t.Fatal("Invalid Etag")
   177  	}
   178  
   179  	ss.Post().InvalidateLastPostTimeCache(o1.ChannelId)
   180  
   181  	// Invalidated cache so we should get a good result
   182  	etag4 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   183  	if strings.Index(etag4, fmt.Sprintf("%v.%v", model.CurrentVersion, o1.UpdateAt)) != 0 {
   184  		t.Fatal("Invalid Etag")
   185  	}
   186  }
   187  
   188  func testPostStoreUpdate(t *testing.T, ss store.Store) {
   189  	o1 := &model.Post{}
   190  	o1.ChannelId = model.NewId()
   191  	o1.UserId = model.NewId()
   192  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
   193  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   194  
   195  	o2 := &model.Post{}
   196  	o2.ChannelId = o1.ChannelId
   197  	o2.UserId = model.NewId()
   198  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
   199  	o2.ParentId = o1.Id
   200  	o2.RootId = o1.Id
   201  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   202  
   203  	o3 := &model.Post{}
   204  	o3.ChannelId = o1.ChannelId
   205  	o3.UserId = model.NewId()
   206  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
   207  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   208  
   209  	ro1 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
   210  	ro2 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
   211  	ro3 := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
   212  
   213  	if ro1.Message != o1.Message {
   214  		t.Fatal("Failed to save/get")
   215  	}
   216  
   217  	o1a := &model.Post{}
   218  	*o1a = *ro1
   219  	o1a.Message = ro1.Message + "BBBBBBBBBB"
   220  	if result := <-ss.Post().Update(o1a, ro1); result.Err != nil {
   221  		t.Fatal(result.Err)
   222  	}
   223  
   224  	ro1a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
   225  
   226  	if ro1a.Message != o1a.Message {
   227  		t.Fatal("Failed to update/get")
   228  	}
   229  
   230  	o2a := &model.Post{}
   231  	*o2a = *ro2
   232  	o2a.Message = ro2.Message + "DDDDDDD"
   233  	if result := <-ss.Post().Update(o2a, ro2); result.Err != nil {
   234  		t.Fatal(result.Err)
   235  	}
   236  
   237  	ro2a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
   238  
   239  	if ro2a.Message != o2a.Message {
   240  		t.Fatal("Failed to update/get")
   241  	}
   242  
   243  	o3a := &model.Post{}
   244  	*o3a = *ro3
   245  	o3a.Message = ro3.Message + "WWWWWWW"
   246  	if result := <-ss.Post().Update(o3a, ro3); result.Err != nil {
   247  		t.Fatal(result.Err)
   248  	}
   249  
   250  	ro3a := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
   251  
   252  	if ro3a.Message != o3a.Message && ro3a.Hashtags != o3a.Hashtags {
   253  		t.Fatal("Failed to update/get")
   254  	}
   255  
   256  	o4 := store.Must(ss.Post().Save(&model.Post{
   257  		ChannelId: model.NewId(),
   258  		UserId:    model.NewId(),
   259  		Message:   model.NewId(),
   260  		Filenames: []string{"test"},
   261  	})).(*model.Post)
   262  
   263  	ro4 := (<-ss.Post().Get(o4.Id)).Data.(*model.PostList).Posts[o4.Id]
   264  
   265  	o4a := &model.Post{}
   266  	*o4a = *ro4
   267  	o4a.Filenames = []string{}
   268  	o4a.FileIds = []string{model.NewId()}
   269  	if result := <-ss.Post().Update(o4a, ro4); result.Err != nil {
   270  		t.Fatal(result.Err)
   271  	}
   272  
   273  	if ro4a := store.Must(ss.Post().Get(o4.Id)).(*model.PostList).Posts[o4.Id]; len(ro4a.Filenames) != 0 {
   274  		t.Fatal("Failed to clear Filenames")
   275  	} else if len(ro4a.FileIds) != 1 {
   276  		t.Fatal("Failed to set FileIds")
   277  	}
   278  }
   279  
   280  func testPostStoreDelete(t *testing.T, ss store.Store) {
   281  	o1 := &model.Post{}
   282  	o1.ChannelId = model.NewId()
   283  	o1.UserId = model.NewId()
   284  	o1.Message = "zz" + model.NewId() + "b"
   285  	deleteByID := model.NewId()
   286  
   287  	etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
   288  	if strings.Index(etag1, model.CurrentVersion+".") != 0 {
   289  		t.Fatal("Invalid Etag")
   290  	}
   291  
   292  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   293  
   294  	if r1 := <-ss.Post().Get(o1.Id); r1.Err != nil {
   295  		t.Fatal(r1.Err)
   296  	} else {
   297  		if r1.Data.(*model.PostList).Posts[o1.Id].CreateAt != o1.CreateAt {
   298  			t.Fatal("invalid returned post")
   299  		}
   300  	}
   301  
   302  	if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), deleteByID); r2.Err != nil {
   303  		t.Fatal(r2.Err)
   304  	}
   305  
   306  	r5 := <-ss.Post().GetPostsCreatedAt(o1.ChannelId, o1.CreateAt)
   307  	post := r5.Data.([]*model.Post)[0]
   308  	actual := post.Props[model.POST_PROPS_DELETE_BY]
   309  	if actual != deleteByID {
   310  		t.Errorf("Expected (*Post).Props[model.POST_PROPS_DELETE_BY] to be %v but got %v.", deleteByID, actual)
   311  	}
   312  
   313  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   314  		t.Log(r3.Data)
   315  		t.Fatal("Missing id should have failed")
   316  	}
   317  
   318  	etag2 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
   319  	if strings.Index(etag2, model.CurrentVersion+".") != 0 {
   320  		t.Fatal("Invalid Etag")
   321  	}
   322  }
   323  
   324  func testPostStoreDelete1Level(t *testing.T, ss store.Store) {
   325  	o1 := &model.Post{}
   326  	o1.ChannelId = model.NewId()
   327  	o1.UserId = model.NewId()
   328  	o1.Message = "zz" + model.NewId() + "b"
   329  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   330  
   331  	o2 := &model.Post{}
   332  	o2.ChannelId = o1.ChannelId
   333  	o2.UserId = model.NewId()
   334  	o2.Message = "zz" + model.NewId() + "b"
   335  	o2.ParentId = o1.Id
   336  	o2.RootId = o1.Id
   337  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   338  
   339  	if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), ""); r2.Err != nil {
   340  		t.Fatal(r2.Err)
   341  	}
   342  
   343  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   344  		t.Fatal("Deleted id should have failed")
   345  	}
   346  
   347  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   348  		t.Fatal("Deleted id should have failed")
   349  	}
   350  }
   351  
   352  func testPostStoreDelete2Level(t *testing.T, ss store.Store) {
   353  	o1 := &model.Post{}
   354  	o1.ChannelId = model.NewId()
   355  	o1.UserId = model.NewId()
   356  	o1.Message = "zz" + model.NewId() + "b"
   357  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   358  
   359  	o2 := &model.Post{}
   360  	o2.ChannelId = o1.ChannelId
   361  	o2.UserId = model.NewId()
   362  	o2.Message = "zz" + model.NewId() + "b"
   363  	o2.ParentId = o1.Id
   364  	o2.RootId = o1.Id
   365  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   366  
   367  	o3 := &model.Post{}
   368  	o3.ChannelId = o1.ChannelId
   369  	o3.UserId = model.NewId()
   370  	o3.Message = "zz" + model.NewId() + "b"
   371  	o3.ParentId = o2.Id
   372  	o3.RootId = o1.Id
   373  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   374  
   375  	o4 := &model.Post{}
   376  	o4.ChannelId = model.NewId()
   377  	o4.UserId = model.NewId()
   378  	o4.Message = "zz" + model.NewId() + "b"
   379  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   380  
   381  	if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), ""); r2.Err != nil {
   382  		t.Fatal(r2.Err)
   383  	}
   384  
   385  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   386  		t.Fatal("Deleted id should have failed")
   387  	}
   388  
   389  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   390  		t.Fatal("Deleted id should have failed")
   391  	}
   392  
   393  	if r5 := (<-ss.Post().Get(o3.Id)); r5.Err == nil {
   394  		t.Fatal("Deleted id should have failed")
   395  	}
   396  
   397  	if r6 := <-ss.Post().Get(o4.Id); r6.Err != nil {
   398  		t.Fatal(r6.Err)
   399  	}
   400  }
   401  
   402  func testPostStorePermDelete1Level(t *testing.T, ss store.Store) {
   403  	o1 := &model.Post{}
   404  	o1.ChannelId = model.NewId()
   405  	o1.UserId = model.NewId()
   406  	o1.Message = "zz" + model.NewId() + "b"
   407  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   408  
   409  	o2 := &model.Post{}
   410  	o2.ChannelId = o1.ChannelId
   411  	o2.UserId = model.NewId()
   412  	o2.Message = "zz" + model.NewId() + "b"
   413  	o2.ParentId = o1.Id
   414  	o2.RootId = o1.Id
   415  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   416  
   417  	o3 := &model.Post{}
   418  	o3.ChannelId = model.NewId()
   419  	o3.UserId = model.NewId()
   420  	o3.Message = "zz" + model.NewId() + "b"
   421  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   422  
   423  	if r2 := <-ss.Post().PermanentDeleteByUser(o2.UserId); r2.Err != nil {
   424  		t.Fatal(r2.Err)
   425  	}
   426  
   427  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err != nil {
   428  		t.Fatal("Deleted id shouldn't have failed")
   429  	}
   430  
   431  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   432  		t.Fatal("Deleted id should have failed")
   433  	}
   434  
   435  	if r2 := <-ss.Post().PermanentDeleteByChannel(o3.ChannelId); r2.Err != nil {
   436  		t.Fatal(r2.Err)
   437  	}
   438  
   439  	if r3 := (<-ss.Post().Get(o3.Id)); r3.Err == nil {
   440  		t.Fatal("Deleted id should have failed")
   441  	}
   442  }
   443  
   444  func testPostStorePermDelete1Level2(t *testing.T, ss store.Store) {
   445  	o1 := &model.Post{}
   446  	o1.ChannelId = model.NewId()
   447  	o1.UserId = model.NewId()
   448  	o1.Message = "zz" + model.NewId() + "b"
   449  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   450  
   451  	o2 := &model.Post{}
   452  	o2.ChannelId = o1.ChannelId
   453  	o2.UserId = model.NewId()
   454  	o2.Message = "zz" + model.NewId() + "b"
   455  	o2.ParentId = o1.Id
   456  	o2.RootId = o1.Id
   457  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   458  
   459  	o3 := &model.Post{}
   460  	o3.ChannelId = model.NewId()
   461  	o3.UserId = model.NewId()
   462  	o3.Message = "zz" + model.NewId() + "b"
   463  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   464  
   465  	if r2 := <-ss.Post().PermanentDeleteByUser(o1.UserId); r2.Err != nil {
   466  		t.Fatal(r2.Err)
   467  	}
   468  
   469  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   470  		t.Fatal("Deleted id should have failed")
   471  	}
   472  
   473  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   474  		t.Fatal("Deleted id should have failed")
   475  	}
   476  
   477  	if r5 := (<-ss.Post().Get(o3.Id)); r5.Err != nil {
   478  		t.Fatal("Deleted id shouldn't have failed")
   479  	}
   480  }
   481  
   482  func testPostStoreGetWithChildren(t *testing.T, ss store.Store) {
   483  	o1 := &model.Post{}
   484  	o1.ChannelId = model.NewId()
   485  	o1.UserId = model.NewId()
   486  	o1.Message = "zz" + model.NewId() + "b"
   487  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   488  
   489  	o2 := &model.Post{}
   490  	o2.ChannelId = o1.ChannelId
   491  	o2.UserId = model.NewId()
   492  	o2.Message = "zz" + model.NewId() + "b"
   493  	o2.ParentId = o1.Id
   494  	o2.RootId = o1.Id
   495  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   496  
   497  	o3 := &model.Post{}
   498  	o3.ChannelId = o1.ChannelId
   499  	o3.UserId = model.NewId()
   500  	o3.Message = "zz" + model.NewId() + "b"
   501  	o3.ParentId = o2.Id
   502  	o3.RootId = o1.Id
   503  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   504  
   505  	if r1 := <-ss.Post().Get(o1.Id); r1.Err != nil {
   506  		t.Fatal(r1.Err)
   507  	} else {
   508  		pl := r1.Data.(*model.PostList)
   509  		if len(pl.Posts) != 3 {
   510  			t.Fatal("invalid returned post")
   511  		}
   512  	}
   513  
   514  	store.Must(ss.Post().Delete(o3.Id, model.GetMillis(), ""))
   515  
   516  	if r2 := <-ss.Post().Get(o1.Id); r2.Err != nil {
   517  		t.Fatal(r2.Err)
   518  	} else {
   519  		pl := r2.Data.(*model.PostList)
   520  		if len(pl.Posts) != 2 {
   521  			t.Fatal("invalid returned post")
   522  		}
   523  	}
   524  
   525  	store.Must(ss.Post().Delete(o2.Id, model.GetMillis(), ""))
   526  
   527  	if r3 := <-ss.Post().Get(o1.Id); r3.Err != nil {
   528  		t.Fatal(r3.Err)
   529  	} else {
   530  		pl := r3.Data.(*model.PostList)
   531  		if len(pl.Posts) != 1 {
   532  			t.Fatal("invalid returned post")
   533  		}
   534  	}
   535  }
   536  
   537  func testPostStoreGetPostsWithDetails(t *testing.T, ss store.Store) {
   538  	o1 := &model.Post{}
   539  	o1.ChannelId = model.NewId()
   540  	o1.UserId = model.NewId()
   541  	o1.Message = "zz" + model.NewId() + "b"
   542  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   543  	time.Sleep(2 * time.Millisecond)
   544  
   545  	o2 := &model.Post{}
   546  	o2.ChannelId = o1.ChannelId
   547  	o2.UserId = model.NewId()
   548  	o2.Message = "zz" + model.NewId() + "b"
   549  	o2.ParentId = o1.Id
   550  	o2.RootId = o1.Id
   551  	_ = (<-ss.Post().Save(o2)).Data.(*model.Post)
   552  	time.Sleep(2 * time.Millisecond)
   553  
   554  	o2a := &model.Post{}
   555  	o2a.ChannelId = o1.ChannelId
   556  	o2a.UserId = model.NewId()
   557  	o2a.Message = "zz" + model.NewId() + "b"
   558  	o2a.ParentId = o1.Id
   559  	o2a.RootId = o1.Id
   560  	o2a = (<-ss.Post().Save(o2a)).Data.(*model.Post)
   561  	time.Sleep(2 * time.Millisecond)
   562  
   563  	o3 := &model.Post{}
   564  	o3.ChannelId = o1.ChannelId
   565  	o3.UserId = model.NewId()
   566  	o3.Message = "zz" + model.NewId() + "b"
   567  	o3.ParentId = o1.Id
   568  	o3.RootId = o1.Id
   569  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   570  	time.Sleep(2 * time.Millisecond)
   571  
   572  	o4 := &model.Post{}
   573  	o4.ChannelId = o1.ChannelId
   574  	o4.UserId = model.NewId()
   575  	o4.Message = "zz" + model.NewId() + "b"
   576  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   577  	time.Sleep(2 * time.Millisecond)
   578  
   579  	o5 := &model.Post{}
   580  	o5.ChannelId = o1.ChannelId
   581  	o5.UserId = model.NewId()
   582  	o5.Message = "zz" + model.NewId() + "b"
   583  	o5.ParentId = o4.Id
   584  	o5.RootId = o4.Id
   585  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   586  
   587  	r1 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 4, false)).Data.(*model.PostList)
   588  
   589  	if r1.Order[0] != o5.Id {
   590  		t.Fatal("invalid order")
   591  	}
   592  
   593  	if r1.Order[1] != o4.Id {
   594  		t.Fatal("invalid order")
   595  	}
   596  
   597  	if r1.Order[2] != o3.Id {
   598  		t.Fatal("invalid order")
   599  	}
   600  
   601  	if r1.Order[3] != o2a.Id {
   602  		t.Fatal("invalid order")
   603  	}
   604  
   605  	if len(r1.Posts) != 6 { //the last 4, + o1 (o2a and o3's parent) + o2 (in same thread as o2a and o3)
   606  		t.Fatal("wrong size")
   607  	}
   608  
   609  	if r1.Posts[o1.Id].Message != o1.Message {
   610  		t.Fatal("Missing parent")
   611  	}
   612  
   613  	r2 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 4, true)).Data.(*model.PostList)
   614  
   615  	if r2.Order[0] != o5.Id {
   616  		t.Fatal("invalid order")
   617  	}
   618  
   619  	if r2.Order[1] != o4.Id {
   620  		t.Fatal("invalid order")
   621  	}
   622  
   623  	if r2.Order[2] != o3.Id {
   624  		t.Fatal("invalid order")
   625  	}
   626  
   627  	if r2.Order[3] != o2a.Id {
   628  		t.Fatal("invalid order")
   629  	}
   630  
   631  	if len(r2.Posts) != 6 { //the last 4, + o1 (o2a and o3's parent) + o2 (in same thread as o2a and o3)
   632  		t.Fatal("wrong size")
   633  	}
   634  
   635  	if r2.Posts[o1.Id].Message != o1.Message {
   636  		t.Fatal("Missing parent")
   637  	}
   638  
   639  	// Run once to fill cache
   640  	<-ss.Post().GetPosts(o1.ChannelId, 0, 30, true)
   641  
   642  	o6 := &model.Post{}
   643  	o6.ChannelId = o1.ChannelId
   644  	o6.UserId = model.NewId()
   645  	o6.Message = "zz" + model.NewId() + "b"
   646  	_ = (<-ss.Post().Save(o6)).Data.(*model.Post)
   647  
   648  	// Should only be 6 since we hit the cache
   649  	r3 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 30, true)).Data.(*model.PostList)
   650  	assert.Equal(t, 6, len(r3.Order))
   651  
   652  	ss.Post().InvalidateLastPostTimeCache(o1.ChannelId)
   653  
   654  	// Cache was invalidated, we should get all the posts
   655  	r4 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 30, true)).Data.(*model.PostList)
   656  	assert.Equal(t, 7, len(r4.Order))
   657  }
   658  
   659  func testPostStoreGetPostsBeforeAfter(t *testing.T, ss store.Store) {
   660  	o0 := &model.Post{}
   661  	o0.ChannelId = model.NewId()
   662  	o0.UserId = model.NewId()
   663  	o0.Message = "zz" + model.NewId() + "b"
   664  	_ = (<-ss.Post().Save(o0)).Data.(*model.Post)
   665  	time.Sleep(2 * time.Millisecond)
   666  
   667  	o1 := &model.Post{}
   668  	o1.ChannelId = model.NewId()
   669  	o1.UserId = model.NewId()
   670  	o1.Message = "zz" + model.NewId() + "b"
   671  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   672  	time.Sleep(2 * time.Millisecond)
   673  
   674  	o2 := &model.Post{}
   675  	o2.ChannelId = o1.ChannelId
   676  	o2.UserId = model.NewId()
   677  	o2.Message = "zz" + model.NewId() + "b"
   678  	o2.ParentId = o1.Id
   679  	o2.RootId = o1.Id
   680  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   681  	time.Sleep(2 * time.Millisecond)
   682  
   683  	o2a := &model.Post{}
   684  	o2a.ChannelId = o1.ChannelId
   685  	o2a.UserId = model.NewId()
   686  	o2a.Message = "zz" + model.NewId() + "b"
   687  	o2a.ParentId = o1.Id
   688  	o2a.RootId = o1.Id
   689  	o2a = (<-ss.Post().Save(o2a)).Data.(*model.Post)
   690  	time.Sleep(2 * time.Millisecond)
   691  
   692  	o3 := &model.Post{}
   693  	o3.ChannelId = o1.ChannelId
   694  	o3.UserId = model.NewId()
   695  	o3.Message = "zz" + model.NewId() + "b"
   696  	o3.ParentId = o1.Id
   697  	o3.RootId = o1.Id
   698  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   699  	time.Sleep(2 * time.Millisecond)
   700  
   701  	o4 := &model.Post{}
   702  	o4.ChannelId = o1.ChannelId
   703  	o4.UserId = model.NewId()
   704  	o4.Message = "zz" + model.NewId() + "b"
   705  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   706  	time.Sleep(2 * time.Millisecond)
   707  
   708  	o5 := &model.Post{}
   709  	o5.ChannelId = o1.ChannelId
   710  	o5.UserId = model.NewId()
   711  	o5.Message = "zz" + model.NewId() + "b"
   712  	o5.ParentId = o4.Id
   713  	o5.RootId = o4.Id
   714  	_ = (<-ss.Post().Save(o5)).Data.(*model.Post)
   715  
   716  	r1 := (<-ss.Post().GetPostsBefore(o1.ChannelId, o1.Id, 4, 0)).Data.(*model.PostList)
   717  
   718  	if len(r1.Posts) != 0 {
   719  		t.Fatal("Wrong size")
   720  	}
   721  
   722  	r2 := (<-ss.Post().GetPostsAfter(o1.ChannelId, o1.Id, 4, 0)).Data.(*model.PostList)
   723  
   724  	if r2.Order[0] != o4.Id {
   725  		t.Fatal("invalid order")
   726  	}
   727  
   728  	if r2.Order[1] != o3.Id {
   729  		t.Fatal("invalid order")
   730  	}
   731  
   732  	if r2.Order[2] != o2a.Id {
   733  		t.Fatal("invalid order")
   734  	}
   735  
   736  	if r2.Order[3] != o2.Id {
   737  		t.Fatal("invalid order")
   738  	}
   739  
   740  	if len(r2.Posts) != 5 {
   741  		t.Fatal("wrong size")
   742  	}
   743  
   744  	r3 := (<-ss.Post().GetPostsBefore(o3.ChannelId, o3.Id, 2, 0)).Data.(*model.PostList)
   745  
   746  	if r3.Order[0] != o2a.Id {
   747  		t.Fatal("invalid order")
   748  	}
   749  
   750  	if r3.Order[1] != o2.Id {
   751  		t.Fatal("invalid order")
   752  	}
   753  
   754  	if len(r3.Posts) != 3 {
   755  		t.Fatal("wrong size")
   756  	}
   757  
   758  	if r3.Posts[o1.Id].Message != o1.Message {
   759  		t.Fatal("Missing parent")
   760  	}
   761  }
   762  
   763  func testPostStoreGetPostsSince(t *testing.T, ss store.Store) {
   764  	o0 := &model.Post{}
   765  	o0.ChannelId = model.NewId()
   766  	o0.UserId = model.NewId()
   767  	o0.Message = "zz" + model.NewId() + "b"
   768  	_ = (<-ss.Post().Save(o0)).Data.(*model.Post)
   769  	time.Sleep(2 * time.Millisecond)
   770  
   771  	o1 := &model.Post{}
   772  	o1.ChannelId = model.NewId()
   773  	o1.UserId = model.NewId()
   774  	o1.Message = "zz" + model.NewId() + "b"
   775  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   776  	time.Sleep(2 * time.Millisecond)
   777  
   778  	o2 := &model.Post{}
   779  	o2.ChannelId = o1.ChannelId
   780  	o2.UserId = model.NewId()
   781  	o2.Message = "zz" + model.NewId() + "b"
   782  	o2.ParentId = o1.Id
   783  	o2.RootId = o1.Id
   784  	_ = (<-ss.Post().Save(o2)).Data.(*model.Post)
   785  	time.Sleep(2 * time.Millisecond)
   786  
   787  	o2a := &model.Post{}
   788  	o2a.ChannelId = o1.ChannelId
   789  	o2a.UserId = model.NewId()
   790  	o2a.Message = "zz" + model.NewId() + "b"
   791  	o2a.ParentId = o1.Id
   792  	o2a.RootId = o1.Id
   793  	o2a = (<-ss.Post().Save(o2a)).Data.(*model.Post)
   794  	time.Sleep(2 * time.Millisecond)
   795  
   796  	o3 := &model.Post{}
   797  	o3.ChannelId = o1.ChannelId
   798  	o3.UserId = model.NewId()
   799  	o3.Message = "zz" + model.NewId() + "b"
   800  	o3.ParentId = o1.Id
   801  	o3.RootId = o1.Id
   802  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   803  	time.Sleep(2 * time.Millisecond)
   804  
   805  	o4 := &model.Post{}
   806  	o4.ChannelId = o1.ChannelId
   807  	o4.UserId = model.NewId()
   808  	o4.Message = "zz" + model.NewId() + "b"
   809  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   810  	time.Sleep(2 * time.Millisecond)
   811  
   812  	o5 := &model.Post{}
   813  	o5.ChannelId = o1.ChannelId
   814  	o5.UserId = model.NewId()
   815  	o5.Message = "zz" + model.NewId() + "b"
   816  	o5.ParentId = o4.Id
   817  	o5.RootId = o4.Id
   818  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   819  
   820  	r1 := (<-ss.Post().GetPostsSince(o1.ChannelId, o1.CreateAt, false)).Data.(*model.PostList)
   821  
   822  	if r1.Order[0] != o5.Id {
   823  		t.Fatal("invalid order")
   824  	}
   825  
   826  	if r1.Order[1] != o4.Id {
   827  		t.Fatal("invalid order")
   828  	}
   829  
   830  	if r1.Order[2] != o3.Id {
   831  		t.Fatal("invalid order")
   832  	}
   833  
   834  	if r1.Order[3] != o2a.Id {
   835  		t.Fatal("invalid order")
   836  	}
   837  
   838  	if len(r1.Posts) != 6 {
   839  		t.Fatal("wrong size")
   840  	}
   841  
   842  	if r1.Posts[o1.Id].Message != o1.Message {
   843  		t.Fatal("Missing parent")
   844  	}
   845  
   846  	r2 := (<-ss.Post().GetPostsSince(o1.ChannelId, o5.UpdateAt, true)).Data.(*model.PostList)
   847  
   848  	if len(r2.Order) != 0 {
   849  		t.Fatal("wrong size ", len(r2.Posts))
   850  	}
   851  }
   852  
   853  func testPostStoreSearch(t *testing.T, ss store.Store) {
   854  	teamId := model.NewId()
   855  	userId := model.NewId()
   856  
   857  	c1 := &model.Channel{}
   858  	c1.TeamId = teamId
   859  	c1.DisplayName = "Channel1"
   860  	c1.Name = "zz" + model.NewId() + "b"
   861  	c1.Type = model.CHANNEL_OPEN
   862  	c1 = (<-ss.Channel().Save(c1, -1)).Data.(*model.Channel)
   863  
   864  	m1 := model.ChannelMember{}
   865  	m1.ChannelId = c1.Id
   866  	m1.UserId = userId
   867  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
   868  	store.Must(ss.Channel().SaveMember(&m1))
   869  
   870  	c2 := &model.Channel{}
   871  	c2.TeamId = teamId
   872  	c2.DisplayName = "Channel1"
   873  	c2.Name = "zz" + model.NewId() + "b"
   874  	c2.Type = model.CHANNEL_OPEN
   875  	c2 = (<-ss.Channel().Save(c2, -1)).Data.(*model.Channel)
   876  
   877  	c3 := &model.Channel{}
   878  	c3.TeamId = teamId
   879  	c3.DisplayName = "Channel1"
   880  	c3.Name = "zz" + model.NewId() + "b"
   881  	c3.Type = model.CHANNEL_OPEN
   882  	c3 = (<-ss.Channel().Save(c3, -1)).Data.(*model.Channel)
   883  	<-ss.Channel().Delete(c3.Id, model.GetMillis())
   884  
   885  	m3 := model.ChannelMember{}
   886  	m3.ChannelId = c3.Id
   887  	m3.UserId = userId
   888  	m3.NotifyProps = model.GetDefaultChannelNotifyProps()
   889  	store.Must(ss.Channel().SaveMember(&m3))
   890  
   891  	o1 := &model.Post{}
   892  	o1.ChannelId = c1.Id
   893  	o1.UserId = model.NewId()
   894  	o1.Message = "corey mattermost new york"
   895  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   896  
   897  	o1a := &model.Post{}
   898  	o1a.ChannelId = c1.Id
   899  	o1a.UserId = model.NewId()
   900  	o1a.Message = "corey mattermost new york"
   901  	o1a.Type = model.POST_JOIN_CHANNEL
   902  	_ = (<-ss.Post().Save(o1a)).Data.(*model.Post)
   903  
   904  	o2 := &model.Post{}
   905  	o2.ChannelId = c1.Id
   906  	o2.UserId = model.NewId()
   907  	o2.Message = "New Jersey is where John is from"
   908  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   909  
   910  	o3 := &model.Post{}
   911  	o3.ChannelId = c2.Id
   912  	o3.UserId = model.NewId()
   913  	o3.Message = "New Jersey is where John is from corey new york"
   914  	_ = (<-ss.Post().Save(o3)).Data.(*model.Post)
   915  
   916  	o4 := &model.Post{}
   917  	o4.ChannelId = c1.Id
   918  	o4.UserId = model.NewId()
   919  	o4.Hashtags = "#hashtag"
   920  	o4.Message = "(message)blargh"
   921  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   922  
   923  	o5 := &model.Post{}
   924  	o5.ChannelId = c1.Id
   925  	o5.UserId = model.NewId()
   926  	o5.Hashtags = "#secret #howdy"
   927  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   928  
   929  	o6 := &model.Post{}
   930  	o6.ChannelId = c3.Id
   931  	o6.UserId = model.NewId()
   932  	o6.Hashtags = "#hashtag"
   933  	o6 = (<-ss.Post().Save(o6)).Data.(*model.Post)
   934  
   935  	o7 := &model.Post{}
   936  	o7.ChannelId = c3.Id
   937  	o7.UserId = model.NewId()
   938  	o7.Message = "New Jersey is where John is from corey new york"
   939  	o7 = (<-ss.Post().Save(o7)).Data.(*model.Post)
   940  
   941  	o8 := &model.Post{}
   942  	o8.ChannelId = c3.Id
   943  	o8.UserId = model.NewId()
   944  	o8.Message = "Deleted"
   945  	o8 = (<-ss.Post().Save(o8)).Data.(*model.Post)
   946  
   947  	tt := []struct {
   948  		name                     string
   949  		searchParams             *model.SearchParams
   950  		extectedResultsCount     int
   951  		expectedMessageResultIds []string
   952  	}{
   953  		{
   954  			"normal-search-1",
   955  			&model.SearchParams{Terms: "corey"},
   956  			1,
   957  			[]string{o1.Id},
   958  		},
   959  		{
   960  			"normal-search-2",
   961  			&model.SearchParams{Terms: "new"},
   962  			2,
   963  			[]string{o1.Id, o2.Id},
   964  		},
   965  		{
   966  			"normal-search-3",
   967  			&model.SearchParams{Terms: "john"},
   968  			1,
   969  			[]string{o2.Id},
   970  		},
   971  		{
   972  			"wildcard-search",
   973  			&model.SearchParams{Terms: "matter*"},
   974  			1,
   975  			[]string{o1.Id},
   976  		},
   977  		{
   978  			"hashtag-search",
   979  			&model.SearchParams{Terms: "#hashtag", IsHashtag: true},
   980  			1,
   981  			[]string{o4.Id},
   982  		},
   983  		{
   984  			"hashtag-search-2",
   985  			&model.SearchParams{Terms: "#secret", IsHashtag: true},
   986  			1,
   987  			[]string{o5.Id},
   988  		},
   989  		{
   990  			"no-match-mention",
   991  			&model.SearchParams{Terms: "@thisshouldmatchnothing", IsHashtag: true},
   992  			0,
   993  			[]string{},
   994  		},
   995  		{
   996  			"no-results-search",
   997  			&model.SearchParams{Terms: "mattermost jersey"},
   998  			0,
   999  			[]string{},
  1000  		},
  1001  		{
  1002  			"multiple-words-search",
  1003  			&model.SearchParams{Terms: "corey new york"},
  1004  			1,
  1005  			[]string{o1.Id},
  1006  		},
  1007  		{
  1008  			"multiple-wildcard-search",
  1009  			&model.SearchParams{Terms: "matter* jer*"},
  1010  			0,
  1011  			[]string{},
  1012  		},
  1013  		{
  1014  			"search-with-work-next-to-a-symbol",
  1015  			&model.SearchParams{Terms: "message blargh"},
  1016  			1,
  1017  			[]string{o4.Id},
  1018  		},
  1019  		{
  1020  			"search-with-or",
  1021  			&model.SearchParams{Terms: "Jersey corey", OrTerms: true},
  1022  			2,
  1023  			[]string{o1.Id, o2.Id},
  1024  		},
  1025  		{
  1026  			"search-with-or-and-deleted",
  1027  			&model.SearchParams{Terms: "Jersey corey", OrTerms: true, IncludeDeletedChannels: true},
  1028  			3,
  1029  			[]string{o1.Id, o2.Id, o7.Id},
  1030  		},
  1031  		{
  1032  			"search-hashtag-deleted",
  1033  			&model.SearchParams{Terms: "#hashtag", IsHashtag: true, IncludeDeletedChannels: true},
  1034  			2,
  1035  			[]string{o4.Id, o6.Id},
  1036  		},
  1037  		{
  1038  			"search-deleted-only",
  1039  			&model.SearchParams{Terms: "Deleted", IncludeDeletedChannels: true},
  1040  			1,
  1041  			[]string{o8.Id},
  1042  		},
  1043  	}
  1044  	for _, tc := range tt {
  1045  		t.Run(tc.name, func(t *testing.T) {
  1046  			result := (<-ss.Post().Search(teamId, userId, tc.searchParams)).Data.(*model.PostList)
  1047  			require.Len(t, result.Order, tc.extectedResultsCount)
  1048  			for _, expectedMessageResultId := range tc.expectedMessageResultIds {
  1049  				assert.Contains(t, result.Order, expectedMessageResultId)
  1050  			}
  1051  		})
  1052  	}
  1053  }
  1054  
  1055  func testUserCountsWithPostsByDay(t *testing.T, ss store.Store) {
  1056  	t1 := &model.Team{}
  1057  	t1.DisplayName = "DisplayName"
  1058  	t1.Name = "zz" + model.NewId() + "b"
  1059  	t1.Email = MakeEmail()
  1060  	t1.Type = model.TEAM_OPEN
  1061  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
  1062  
  1063  	c1 := &model.Channel{}
  1064  	c1.TeamId = t1.Id
  1065  	c1.DisplayName = "Channel2"
  1066  	c1.Name = "zz" + model.NewId() + "b"
  1067  	c1.Type = model.CHANNEL_OPEN
  1068  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
  1069  
  1070  	o1 := &model.Post{}
  1071  	o1.ChannelId = c1.Id
  1072  	o1.UserId = model.NewId()
  1073  	o1.CreateAt = utils.MillisFromTime(utils.Yesterday())
  1074  	o1.Message = "zz" + model.NewId() + "b"
  1075  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
  1076  
  1077  	o1a := &model.Post{}
  1078  	o1a.ChannelId = c1.Id
  1079  	o1a.UserId = model.NewId()
  1080  	o1a.CreateAt = o1.CreateAt
  1081  	o1a.Message = "zz" + model.NewId() + "b"
  1082  	_ = store.Must(ss.Post().Save(o1a)).(*model.Post)
  1083  
  1084  	o2 := &model.Post{}
  1085  	o2.ChannelId = c1.Id
  1086  	o2.UserId = model.NewId()
  1087  	o2.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24)
  1088  	o2.Message = "zz" + model.NewId() + "b"
  1089  	o2 = store.Must(ss.Post().Save(o2)).(*model.Post)
  1090  
  1091  	o2a := &model.Post{}
  1092  	o2a.ChannelId = c1.Id
  1093  	o2a.UserId = o2.UserId
  1094  	o2a.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24)
  1095  	o2a.Message = "zz" + model.NewId() + "b"
  1096  	_ = store.Must(ss.Post().Save(o2a)).(*model.Post)
  1097  
  1098  	if r1 := <-ss.Post().AnalyticsUserCountsWithPostsByDay(t1.Id); r1.Err != nil {
  1099  		t.Fatal(r1.Err)
  1100  	} else {
  1101  		row1 := r1.Data.(model.AnalyticsRows)[0]
  1102  		if row1.Value != 2 {
  1103  			t.Fatal("wrong value")
  1104  		}
  1105  
  1106  		row2 := r1.Data.(model.AnalyticsRows)[1]
  1107  		if row2.Value != 1 {
  1108  			t.Fatal("wrong value")
  1109  		}
  1110  	}
  1111  }
  1112  
  1113  func testPostCountsByDay(t *testing.T, ss store.Store) {
  1114  	t1 := &model.Team{}
  1115  	t1.DisplayName = "DisplayName"
  1116  	t1.Name = "zz" + model.NewId() + "b"
  1117  	t1.Email = MakeEmail()
  1118  	t1.Type = model.TEAM_OPEN
  1119  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
  1120  
  1121  	c1 := &model.Channel{}
  1122  	c1.TeamId = t1.Id
  1123  	c1.DisplayName = "Channel2"
  1124  	c1.Name = "zz" + model.NewId() + "b"
  1125  	c1.Type = model.CHANNEL_OPEN
  1126  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
  1127  
  1128  	o1 := &model.Post{}
  1129  	o1.ChannelId = c1.Id
  1130  	o1.UserId = model.NewId()
  1131  	o1.CreateAt = utils.MillisFromTime(utils.Yesterday())
  1132  	o1.Message = "zz" + model.NewId() + "b"
  1133  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
  1134  
  1135  	o1a := &model.Post{}
  1136  	o1a.ChannelId = c1.Id
  1137  	o1a.UserId = model.NewId()
  1138  	o1a.CreateAt = o1.CreateAt
  1139  	o1a.Message = "zz" + model.NewId() + "b"
  1140  	_ = store.Must(ss.Post().Save(o1a)).(*model.Post)
  1141  
  1142  	o2 := &model.Post{}
  1143  	o2.ChannelId = c1.Id
  1144  	o2.UserId = model.NewId()
  1145  	o2.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24 * 2)
  1146  	o2.Message = "zz" + model.NewId() + "b"
  1147  	o2 = store.Must(ss.Post().Save(o2)).(*model.Post)
  1148  
  1149  	o2a := &model.Post{}
  1150  	o2a.ChannelId = c1.Id
  1151  	o2a.UserId = o2.UserId
  1152  	o2a.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24 * 2)
  1153  	o2a.Message = "zz" + model.NewId() + "b"
  1154  	_ = store.Must(ss.Post().Save(o2a)).(*model.Post)
  1155  
  1156  	time.Sleep(1 * time.Second)
  1157  
  1158  	if r1 := <-ss.Post().AnalyticsPostCountsByDay(t1.Id); r1.Err != nil {
  1159  		t.Fatal(r1.Err)
  1160  	} else {
  1161  		row1 := r1.Data.(model.AnalyticsRows)[0]
  1162  		if row1.Value != 2 {
  1163  			t.Fatal(row1)
  1164  		}
  1165  
  1166  		row2 := r1.Data.(model.AnalyticsRows)[1]
  1167  		if row2.Value != 2 {
  1168  			t.Fatal("wrong value")
  1169  		}
  1170  	}
  1171  
  1172  	if r1 := <-ss.Post().AnalyticsPostCount(t1.Id, false, false); r1.Err != nil {
  1173  		t.Fatal(r1.Err)
  1174  	} else {
  1175  		if r1.Data.(int64) != 4 {
  1176  			t.Fatal("wrong value")
  1177  		}
  1178  	}
  1179  }
  1180  
  1181  func testPostStoreGetFlaggedPostsForTeam(t *testing.T, ss store.Store) {
  1182  	c1 := &model.Channel{}
  1183  	c1.TeamId = model.NewId()
  1184  	c1.DisplayName = "Channel1"
  1185  	c1.Name = "zz" + model.NewId() + "b"
  1186  	c1.Type = model.CHANNEL_OPEN
  1187  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
  1188  
  1189  	o1 := &model.Post{}
  1190  	o1.ChannelId = c1.Id
  1191  	o1.UserId = model.NewId()
  1192  	o1.Message = "zz" + model.NewId() + "b"
  1193  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1194  	time.Sleep(2 * time.Millisecond)
  1195  
  1196  	o2 := &model.Post{}
  1197  	o2.ChannelId = o1.ChannelId
  1198  	o2.UserId = model.NewId()
  1199  	o2.Message = "zz" + model.NewId() + "b"
  1200  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1201  	time.Sleep(2 * time.Millisecond)
  1202  
  1203  	o3 := &model.Post{}
  1204  	o3.ChannelId = o1.ChannelId
  1205  	o3.UserId = model.NewId()
  1206  	o3.Message = "zz" + model.NewId() + "b"
  1207  	o3.DeleteAt = 1
  1208  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1209  	time.Sleep(2 * time.Millisecond)
  1210  
  1211  	o4 := &model.Post{}
  1212  	o4.ChannelId = model.NewId()
  1213  	o4.UserId = model.NewId()
  1214  	o4.Message = "zz" + model.NewId() + "b"
  1215  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
  1216  	time.Sleep(2 * time.Millisecond)
  1217  
  1218  	c2 := &model.Channel{}
  1219  	c2.DisplayName = "DMChannel1"
  1220  	c2.Name = "zz" + model.NewId() + "b"
  1221  	c2.Type = model.CHANNEL_DIRECT
  1222  
  1223  	m1 := &model.ChannelMember{}
  1224  	m1.ChannelId = c2.Id
  1225  	m1.UserId = o1.UserId
  1226  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  1227  
  1228  	m2 := &model.ChannelMember{}
  1229  	m2.ChannelId = c2.Id
  1230  	m2.UserId = model.NewId()
  1231  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  1232  
  1233  	c2 = store.Must(ss.Channel().SaveDirectChannel(c2, m1, m2)).(*model.Channel)
  1234  
  1235  	o5 := &model.Post{}
  1236  	o5.ChannelId = c2.Id
  1237  	o5.UserId = m2.UserId
  1238  	o5.Message = "zz" + model.NewId() + "b"
  1239  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
  1240  	time.Sleep(2 * time.Millisecond)
  1241  
  1242  	r1 := (<-ss.Post().GetFlaggedPosts(o1.ChannelId, 0, 2)).Data.(*model.PostList)
  1243  
  1244  	if len(r1.Order) != 0 {
  1245  		t.Fatal("should be empty")
  1246  	}
  1247  
  1248  	preferences := model.Preferences{
  1249  		{
  1250  			UserId:   o1.UserId,
  1251  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1252  			Name:     o1.Id,
  1253  			Value:    "true",
  1254  		},
  1255  	}
  1256  
  1257  	store.Must(ss.Preference().Save(&preferences))
  1258  
  1259  	r2 := (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1260  
  1261  	if len(r2.Order) != 1 {
  1262  		t.Fatal("should have 1 post")
  1263  	}
  1264  
  1265  	preferences = model.Preferences{
  1266  		{
  1267  			UserId:   o1.UserId,
  1268  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1269  			Name:     o2.Id,
  1270  			Value:    "true",
  1271  		},
  1272  	}
  1273  
  1274  	store.Must(ss.Preference().Save(&preferences))
  1275  
  1276  	r3 := (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 1)).Data.(*model.PostList)
  1277  
  1278  	if len(r3.Order) != 1 {
  1279  		t.Fatal("should have 1 post")
  1280  	}
  1281  
  1282  	r3 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 1, 1)).Data.(*model.PostList)
  1283  
  1284  	if len(r3.Order) != 1 {
  1285  		t.Fatal("should have 1 post")
  1286  	}
  1287  
  1288  	r3 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 1000, 10)).Data.(*model.PostList)
  1289  
  1290  	if len(r3.Order) != 0 {
  1291  		t.Fatal("should be empty")
  1292  	}
  1293  
  1294  	r4 := (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1295  
  1296  	if len(r4.Order) != 2 {
  1297  		t.Fatal("should have 2 posts")
  1298  	}
  1299  
  1300  	preferences = model.Preferences{
  1301  		{
  1302  			UserId:   o1.UserId,
  1303  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1304  			Name:     o3.Id,
  1305  			Value:    "true",
  1306  		},
  1307  	}
  1308  
  1309  	store.Must(ss.Preference().Save(&preferences))
  1310  
  1311  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1312  
  1313  	if len(r4.Order) != 2 {
  1314  		t.Fatal("should have 2 posts")
  1315  	}
  1316  
  1317  	preferences = model.Preferences{
  1318  		{
  1319  			UserId:   o1.UserId,
  1320  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1321  			Name:     o4.Id,
  1322  			Value:    "true",
  1323  		},
  1324  	}
  1325  	store.Must(ss.Preference().Save(&preferences))
  1326  
  1327  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1328  
  1329  	if len(r4.Order) != 2 {
  1330  		t.Fatal("should have 2 posts")
  1331  	}
  1332  
  1333  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, model.NewId(), 0, 2)).Data.(*model.PostList)
  1334  
  1335  	if len(r4.Order) != 0 {
  1336  		t.Fatal("should have 0 posts")
  1337  	}
  1338  
  1339  	preferences = model.Preferences{
  1340  		{
  1341  			UserId:   o1.UserId,
  1342  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1343  			Name:     o5.Id,
  1344  			Value:    "true",
  1345  		},
  1346  	}
  1347  	store.Must(ss.Preference().Save(&preferences))
  1348  
  1349  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 10)).Data.(*model.PostList)
  1350  
  1351  	if len(r4.Order) != 3 {
  1352  		t.Fatal("should have 3 posts")
  1353  	}
  1354  }
  1355  
  1356  func testPostStoreGetFlaggedPosts(t *testing.T, ss store.Store) {
  1357  	o1 := &model.Post{}
  1358  	o1.ChannelId = model.NewId()
  1359  	o1.UserId = model.NewId()
  1360  	o1.Message = "zz" + model.NewId() + "b"
  1361  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1362  	time.Sleep(2 * time.Millisecond)
  1363  
  1364  	o2 := &model.Post{}
  1365  	o2.ChannelId = o1.ChannelId
  1366  	o2.UserId = model.NewId()
  1367  	o2.Message = "zz" + model.NewId() + "b"
  1368  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1369  	time.Sleep(2 * time.Millisecond)
  1370  
  1371  	o3 := &model.Post{}
  1372  	o3.ChannelId = o1.ChannelId
  1373  	o3.UserId = model.NewId()
  1374  	o3.Message = "zz" + model.NewId() + "b"
  1375  	o3.DeleteAt = 1
  1376  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1377  	time.Sleep(2 * time.Millisecond)
  1378  
  1379  	r1 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1380  
  1381  	if len(r1.Order) != 0 {
  1382  		t.Fatal("should be empty")
  1383  	}
  1384  
  1385  	preferences := model.Preferences{
  1386  		{
  1387  			UserId:   o1.UserId,
  1388  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1389  			Name:     o1.Id,
  1390  			Value:    "true",
  1391  		},
  1392  	}
  1393  
  1394  	store.Must(ss.Preference().Save(&preferences))
  1395  
  1396  	r2 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1397  
  1398  	if len(r2.Order) != 1 {
  1399  		t.Fatal("should have 1 post")
  1400  	}
  1401  
  1402  	preferences = model.Preferences{
  1403  		{
  1404  			UserId:   o1.UserId,
  1405  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1406  			Name:     o2.Id,
  1407  			Value:    "true",
  1408  		},
  1409  	}
  1410  
  1411  	store.Must(ss.Preference().Save(&preferences))
  1412  
  1413  	r3 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 1)).Data.(*model.PostList)
  1414  
  1415  	if len(r3.Order) != 1 {
  1416  		t.Fatal("should have 1 post")
  1417  	}
  1418  
  1419  	r3 = (<-ss.Post().GetFlaggedPosts(o1.UserId, 1, 1)).Data.(*model.PostList)
  1420  
  1421  	if len(r3.Order) != 1 {
  1422  		t.Fatal("should have 1 post")
  1423  	}
  1424  
  1425  	r3 = (<-ss.Post().GetFlaggedPosts(o1.UserId, 1000, 10)).Data.(*model.PostList)
  1426  
  1427  	if len(r3.Order) != 0 {
  1428  		t.Fatal("should be empty")
  1429  	}
  1430  
  1431  	r4 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1432  
  1433  	if len(r4.Order) != 2 {
  1434  		t.Fatal("should have 2 posts")
  1435  	}
  1436  
  1437  	preferences = model.Preferences{
  1438  		{
  1439  			UserId:   o1.UserId,
  1440  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1441  			Name:     o3.Id,
  1442  			Value:    "true",
  1443  		},
  1444  	}
  1445  
  1446  	store.Must(ss.Preference().Save(&preferences))
  1447  
  1448  	r4 = (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1449  
  1450  	if len(r4.Order) != 2 {
  1451  		t.Fatal("should have 2 posts")
  1452  	}
  1453  }
  1454  
  1455  func testPostStoreGetFlaggedPostsForChannel(t *testing.T, ss store.Store) {
  1456  	o1 := &model.Post{}
  1457  	o1.ChannelId = model.NewId()
  1458  	o1.UserId = model.NewId()
  1459  	o1.Message = "zz" + model.NewId() + "b"
  1460  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1461  	time.Sleep(2 * time.Millisecond)
  1462  
  1463  	o2 := &model.Post{}
  1464  	o2.ChannelId = o1.ChannelId
  1465  	o2.UserId = model.NewId()
  1466  	o2.Message = "zz" + model.NewId() + "b"
  1467  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1468  	time.Sleep(2 * time.Millisecond)
  1469  
  1470  	// deleted post
  1471  	o3 := &model.Post{}
  1472  	o3.ChannelId = model.NewId()
  1473  	o3.UserId = o1.ChannelId
  1474  	o3.Message = "zz" + model.NewId() + "b"
  1475  	o3.DeleteAt = 1
  1476  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1477  	time.Sleep(2 * time.Millisecond)
  1478  
  1479  	o4 := &model.Post{}
  1480  	o4.ChannelId = model.NewId()
  1481  	o4.UserId = model.NewId()
  1482  	o4.Message = "zz" + model.NewId() + "b"
  1483  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
  1484  	time.Sleep(2 * time.Millisecond)
  1485  
  1486  	r := (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 10)).Data.(*model.PostList)
  1487  
  1488  	if len(r.Order) != 0 {
  1489  		t.Fatal("should be empty")
  1490  	}
  1491  
  1492  	preference := model.Preference{
  1493  		UserId:   o1.UserId,
  1494  		Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1495  		Name:     o1.Id,
  1496  		Value:    "true",
  1497  	}
  1498  
  1499  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1500  
  1501  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 10)).Data.(*model.PostList)
  1502  
  1503  	if len(r.Order) != 1 {
  1504  		t.Fatal("should have 1 post")
  1505  	}
  1506  
  1507  	preference.Name = o2.Id
  1508  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1509  
  1510  	preference.Name = o3.Id
  1511  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1512  
  1513  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 1)).Data.(*model.PostList)
  1514  
  1515  	if len(r.Order) != 1 {
  1516  		t.Fatal("should have 1 post")
  1517  	}
  1518  
  1519  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 1, 1)).Data.(*model.PostList)
  1520  
  1521  	if len(r.Order) != 1 {
  1522  		t.Fatal("should have 1 post")
  1523  	}
  1524  
  1525  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 1000, 10)).Data.(*model.PostList)
  1526  
  1527  	if len(r.Order) != 0 {
  1528  		t.Fatal("should be empty")
  1529  	}
  1530  
  1531  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 10)).Data.(*model.PostList)
  1532  
  1533  	if len(r.Order) != 2 {
  1534  		t.Fatal("should have 2 posts")
  1535  	}
  1536  
  1537  	preference.Name = o4.Id
  1538  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1539  
  1540  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o4.ChannelId, 0, 10)).Data.(*model.PostList)
  1541  
  1542  	if len(r.Order) != 1 {
  1543  		t.Fatal("should have 1 post")
  1544  	}
  1545  }
  1546  
  1547  func testPostStoreGetPostsCreatedAt(t *testing.T, ss store.Store) {
  1548  	createTime := model.GetMillis() + 1
  1549  
  1550  	o0 := &model.Post{}
  1551  	o0.ChannelId = model.NewId()
  1552  	o0.UserId = model.NewId()
  1553  	o0.Message = "zz" + model.NewId() + "b"
  1554  	o0.CreateAt = createTime
  1555  	o0 = (<-ss.Post().Save(o0)).Data.(*model.Post)
  1556  
  1557  	o1 := &model.Post{}
  1558  	o1.ChannelId = o0.ChannelId
  1559  	o1.UserId = model.NewId()
  1560  	o1.Message = "zz" + model.NewId() + "b"
  1561  	o1.CreateAt = createTime
  1562  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1563  
  1564  	o2 := &model.Post{}
  1565  	o2.ChannelId = o1.ChannelId
  1566  	o2.UserId = model.NewId()
  1567  	o2.Message = "zz" + model.NewId() + "b"
  1568  	o2.ParentId = o1.Id
  1569  	o2.RootId = o1.Id
  1570  	o2.CreateAt = createTime + 1
  1571  	_ = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1572  
  1573  	o3 := &model.Post{}
  1574  	o3.ChannelId = model.NewId()
  1575  	o3.UserId = model.NewId()
  1576  	o3.Message = "zz" + model.NewId() + "b"
  1577  	o3.CreateAt = createTime
  1578  	_ = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1579  
  1580  	r1 := (<-ss.Post().GetPostsCreatedAt(o1.ChannelId, createTime)).Data.([]*model.Post)
  1581  	assert.Equal(t, 2, len(r1))
  1582  }
  1583  
  1584  func testPostStoreOverwrite(t *testing.T, ss store.Store) {
  1585  	o1 := &model.Post{}
  1586  	o1.ChannelId = model.NewId()
  1587  	o1.UserId = model.NewId()
  1588  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1589  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1590  
  1591  	o2 := &model.Post{}
  1592  	o2.ChannelId = o1.ChannelId
  1593  	o2.UserId = model.NewId()
  1594  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
  1595  	o2.ParentId = o1.Id
  1596  	o2.RootId = o1.Id
  1597  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1598  
  1599  	o3 := &model.Post{}
  1600  	o3.ChannelId = o1.ChannelId
  1601  	o3.UserId = model.NewId()
  1602  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
  1603  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1604  
  1605  	ro1 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
  1606  	ro2 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
  1607  	ro3 := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
  1608  
  1609  	if ro1.Message != o1.Message {
  1610  		t.Fatal("Failed to save/get")
  1611  	}
  1612  
  1613  	o1a := &model.Post{}
  1614  	*o1a = *ro1
  1615  	o1a.Message = ro1.Message + "BBBBBBBBBB"
  1616  	if result := <-ss.Post().Overwrite(o1a); result.Err != nil {
  1617  		t.Fatal(result.Err)
  1618  	}
  1619  
  1620  	ro1a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
  1621  
  1622  	if ro1a.Message != o1a.Message {
  1623  		t.Fatal("Failed to overwrite/get")
  1624  	}
  1625  
  1626  	o2a := &model.Post{}
  1627  	*o2a = *ro2
  1628  	o2a.Message = ro2.Message + "DDDDDDD"
  1629  	if result := <-ss.Post().Overwrite(o2a); result.Err != nil {
  1630  		t.Fatal(result.Err)
  1631  	}
  1632  
  1633  	ro2a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
  1634  
  1635  	if ro2a.Message != o2a.Message {
  1636  		t.Fatal("Failed to overwrite/get")
  1637  	}
  1638  
  1639  	o3a := &model.Post{}
  1640  	*o3a = *ro3
  1641  	o3a.Message = ro3.Message + "WWWWWWW"
  1642  	if result := <-ss.Post().Overwrite(o3a); result.Err != nil {
  1643  		t.Fatal(result.Err)
  1644  	}
  1645  
  1646  	ro3a := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
  1647  
  1648  	if ro3a.Message != o3a.Message && ro3a.Hashtags != o3a.Hashtags {
  1649  		t.Fatal("Failed to overwrite/get")
  1650  	}
  1651  
  1652  	o4 := store.Must(ss.Post().Save(&model.Post{
  1653  		ChannelId: model.NewId(),
  1654  		UserId:    model.NewId(),
  1655  		Message:   model.NewId(),
  1656  		Filenames: []string{"test"},
  1657  	})).(*model.Post)
  1658  
  1659  	ro4 := (<-ss.Post().Get(o4.Id)).Data.(*model.PostList).Posts[o4.Id]
  1660  
  1661  	o4a := &model.Post{}
  1662  	*o4a = *ro4
  1663  	o4a.Filenames = []string{}
  1664  	o4a.FileIds = []string{model.NewId()}
  1665  	if result := <-ss.Post().Overwrite(o4a); result.Err != nil {
  1666  		t.Fatal(result.Err)
  1667  	}
  1668  
  1669  	if ro4a := store.Must(ss.Post().Get(o4.Id)).(*model.PostList).Posts[o4.Id]; len(ro4a.Filenames) != 0 {
  1670  		t.Fatal("Failed to clear Filenames")
  1671  	} else if len(ro4a.FileIds) != 1 {
  1672  		t.Fatal("Failed to set FileIds")
  1673  	}
  1674  }
  1675  
  1676  func testPostStoreGetPostsByIds(t *testing.T, ss store.Store) {
  1677  	o1 := &model.Post{}
  1678  	o1.ChannelId = model.NewId()
  1679  	o1.UserId = model.NewId()
  1680  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1681  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1682  
  1683  	o2 := &model.Post{}
  1684  	o2.ChannelId = o1.ChannelId
  1685  	o2.UserId = model.NewId()
  1686  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
  1687  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1688  
  1689  	o3 := &model.Post{}
  1690  	o3.ChannelId = o1.ChannelId
  1691  	o3.UserId = model.NewId()
  1692  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
  1693  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1694  
  1695  	ro1 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
  1696  	ro2 := (<-ss.Post().Get(o2.Id)).Data.(*model.PostList).Posts[o2.Id]
  1697  	ro3 := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
  1698  
  1699  	postIds := []string{
  1700  		ro1.Id,
  1701  		ro2.Id,
  1702  		ro3.Id,
  1703  	}
  1704  
  1705  	if ro4 := store.Must(ss.Post().GetPostsByIds(postIds)).([]*model.Post); len(ro4) != 3 {
  1706  		t.Fatalf("Expected 3 posts in results. Got %v", len(ro4))
  1707  	}
  1708  
  1709  	store.Must(ss.Post().Delete(ro1.Id, model.GetMillis(), ""))
  1710  
  1711  	if ro5 := store.Must(ss.Post().GetPostsByIds(postIds)).([]*model.Post); len(ro5) != 3 {
  1712  		t.Fatalf("Expected 3 posts in results. Got %v", len(ro5))
  1713  	}
  1714  }
  1715  
  1716  func testPostStoreGetPostsBatchForIndexing(t *testing.T, ss store.Store) {
  1717  	c1 := &model.Channel{}
  1718  	c1.TeamId = model.NewId()
  1719  	c1.DisplayName = "Channel1"
  1720  	c1.Name = "zz" + model.NewId() + "b"
  1721  	c1.Type = model.CHANNEL_OPEN
  1722  	c1 = (<-ss.Channel().Save(c1, -1)).Data.(*model.Channel)
  1723  
  1724  	c2 := &model.Channel{}
  1725  	c2.TeamId = model.NewId()
  1726  	c2.DisplayName = "Channel2"
  1727  	c2.Name = "zz" + model.NewId() + "b"
  1728  	c2.Type = model.CHANNEL_OPEN
  1729  	c2 = (<-ss.Channel().Save(c2, -1)).Data.(*model.Channel)
  1730  
  1731  	o1 := &model.Post{}
  1732  	o1.ChannelId = c1.Id
  1733  	o1.UserId = model.NewId()
  1734  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1735  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1736  
  1737  	o2 := &model.Post{}
  1738  	o2.ChannelId = c2.Id
  1739  	o2.UserId = model.NewId()
  1740  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
  1741  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1742  
  1743  	o3 := &model.Post{}
  1744  	o3.ChannelId = c1.Id
  1745  	o3.UserId = model.NewId()
  1746  	o3.ParentId = o1.Id
  1747  	o3.RootId = o1.Id
  1748  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
  1749  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1750  
  1751  	if r := store.Must(ss.Post().GetPostsBatchForIndexing(o1.CreateAt, model.GetMillis()+100000, 100)).([]*model.PostForIndexing); len(r) != 3 {
  1752  		t.Fatalf("Expected 3 posts in results. Got %v", len(r))
  1753  	} else {
  1754  		for _, p := range r {
  1755  			if p.Id == o1.Id {
  1756  				if p.TeamId != c1.TeamId {
  1757  					t.Fatalf("Unexpected team ID")
  1758  				}
  1759  				if p.ParentCreateAt != nil {
  1760  					t.Fatalf("Unexpected parent create at")
  1761  				}
  1762  			} else if p.Id == o2.Id {
  1763  				if p.TeamId != c2.TeamId {
  1764  					t.Fatalf("Unexpected team ID")
  1765  				}
  1766  				if p.ParentCreateAt != nil {
  1767  					t.Fatalf("Unexpected parent create at")
  1768  				}
  1769  			} else if p.Id == o3.Id {
  1770  				if p.TeamId != c1.TeamId {
  1771  					t.Fatalf("Unexpected team ID")
  1772  				}
  1773  				if *p.ParentCreateAt != o1.CreateAt {
  1774  					t.Fatalf("Unexpected parent create at")
  1775  				}
  1776  			} else {
  1777  				t.Fatalf("unexpected post returned")
  1778  			}
  1779  		}
  1780  	}
  1781  }
  1782  
  1783  func testPostStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
  1784  	o1 := &model.Post{}
  1785  	o1.ChannelId = model.NewId()
  1786  	o1.UserId = model.NewId()
  1787  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1788  	o1.CreateAt = 1000
  1789  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1790  
  1791  	o2 := &model.Post{}
  1792  	o2.ChannelId = model.NewId()
  1793  	o2.UserId = model.NewId()
  1794  	o2.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1795  	o2.CreateAt = 1000
  1796  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1797  
  1798  	o3 := &model.Post{}
  1799  	o3.ChannelId = model.NewId()
  1800  	o3.UserId = model.NewId()
  1801  	o3.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1802  	o3.CreateAt = 100000
  1803  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1804  
  1805  	store.Must(ss.Post().PermanentDeleteBatch(2000, 1000))
  1806  
  1807  	if p := <-ss.Post().Get(o1.Id); p.Err == nil {
  1808  		t.Fatalf("Should have not found post 1 after purge")
  1809  	}
  1810  
  1811  	if p := <-ss.Post().Get(o2.Id); p.Err == nil {
  1812  		t.Fatalf("Should have not found post 2 after purge")
  1813  	}
  1814  
  1815  	if p := <-ss.Post().Get(o3.Id); p.Err != nil {
  1816  		t.Fatalf("Should have found post 3 after purge")
  1817  	}
  1818  }
  1819  
  1820  func testPostStoreGetOldest(t *testing.T, ss store.Store) {
  1821  	o0 := &model.Post{}
  1822  	o0.ChannelId = model.NewId()
  1823  	o0.UserId = model.NewId()
  1824  	o0.Message = "zz" + model.NewId() + "b"
  1825  	o0.CreateAt = 3
  1826  	o0 = (<-ss.Post().Save(o0)).Data.(*model.Post)
  1827  
  1828  	o1 := &model.Post{}
  1829  	o1.ChannelId = o0.Id
  1830  	o1.UserId = model.NewId()
  1831  	o1.Message = "zz" + model.NewId() + "b"
  1832  	o1.CreateAt = 2
  1833  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1834  
  1835  	o2 := &model.Post{}
  1836  	o2.ChannelId = o1.ChannelId
  1837  	o2.UserId = model.NewId()
  1838  	o2.Message = "zz" + model.NewId() + "b"
  1839  	o2.CreateAt = 1
  1840  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1841  
  1842  	r1 := (<-ss.Post().GetOldest()).Data.(*model.Post)
  1843  
  1844  	assert.EqualValues(t, o2.Id, r1.Id)
  1845  }
  1846  
  1847  func testGetMaxPostSize(t *testing.T, ss store.Store) {
  1848  	assert.Equal(t, model.POST_MESSAGE_MAX_RUNES_V2, (<-ss.Post().GetMaxPostSize()).Data.(int))
  1849  	assert.Equal(t, model.POST_MESSAGE_MAX_RUNES_V2, (<-ss.Post().GetMaxPostSize()).Data.(int))
  1850  }
  1851  
  1852  func testPostStoreGetParentsForExportAfter(t *testing.T, ss store.Store) {
  1853  	t1 := model.Team{}
  1854  	t1.DisplayName = "Name"
  1855  	t1.Name = model.NewId()
  1856  	t1.Email = MakeEmail()
  1857  	t1.Type = model.TEAM_OPEN
  1858  	store.Must(ss.Team().Save(&t1))
  1859  
  1860  	c1 := model.Channel{}
  1861  	c1.TeamId = t1.Id
  1862  	c1.DisplayName = "Channel1"
  1863  	c1.Name = "zz" + model.NewId() + "b"
  1864  	c1.Type = model.CHANNEL_OPEN
  1865  	store.Must(ss.Channel().Save(&c1, -1))
  1866  
  1867  	u1 := model.User{}
  1868  	u1.Username = model.NewId()
  1869  	u1.Email = MakeEmail()
  1870  	u1.Nickname = model.NewId()
  1871  	store.Must(ss.User().Save(&u1))
  1872  
  1873  	p1 := &model.Post{}
  1874  	p1.ChannelId = c1.Id
  1875  	p1.UserId = u1.Id
  1876  	p1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1877  	p1.CreateAt = 1000
  1878  	p1 = (<-ss.Post().Save(p1)).Data.(*model.Post)
  1879  
  1880  	r1 := <-ss.Post().GetParentsForExportAfter(10000, strings.Repeat("0", 26))
  1881  	assert.Nil(t, r1.Err)
  1882  	d1 := r1.Data.([]*model.PostForExport)
  1883  
  1884  	found := false
  1885  	for _, p := range d1 {
  1886  		if p.Id == p1.Id {
  1887  			found = true
  1888  			assert.Equal(t, p.Id, p1.Id)
  1889  			assert.Equal(t, p.Message, p1.Message)
  1890  			assert.Equal(t, p.Username, u1.Username)
  1891  			assert.Equal(t, p.TeamName, t1.Name)
  1892  			assert.Equal(t, p.ChannelName, c1.Name)
  1893  		}
  1894  	}
  1895  	assert.True(t, found)
  1896  }
  1897  
  1898  func testPostStoreGetRepliesForExport(t *testing.T, ss store.Store) {
  1899  	t1 := model.Team{}
  1900  	t1.DisplayName = "Name"
  1901  	t1.Name = model.NewId()
  1902  	t1.Email = MakeEmail()
  1903  	t1.Type = model.TEAM_OPEN
  1904  	store.Must(ss.Team().Save(&t1))
  1905  
  1906  	c1 := model.Channel{}
  1907  	c1.TeamId = t1.Id
  1908  	c1.DisplayName = "Channel1"
  1909  	c1.Name = "zz" + model.NewId() + "b"
  1910  	c1.Type = model.CHANNEL_OPEN
  1911  	store.Must(ss.Channel().Save(&c1, -1))
  1912  
  1913  	u1 := model.User{}
  1914  	u1.Email = MakeEmail()
  1915  	u1.Nickname = model.NewId()
  1916  	store.Must(ss.User().Save(&u1))
  1917  
  1918  	p1 := &model.Post{}
  1919  	p1.ChannelId = c1.Id
  1920  	p1.UserId = u1.Id
  1921  	p1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1922  	p1.CreateAt = 1000
  1923  	p1 = (<-ss.Post().Save(p1)).Data.(*model.Post)
  1924  
  1925  	p2 := &model.Post{}
  1926  	p2.ChannelId = c1.Id
  1927  	p2.UserId = u1.Id
  1928  	p2.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1929  	p2.CreateAt = 1001
  1930  	p2.ParentId = p1.Id
  1931  	p2.RootId = p1.Id
  1932  	p2 = (<-ss.Post().Save(p2)).Data.(*model.Post)
  1933  
  1934  	r1 := <-ss.Post().GetRepliesForExport(p1.Id)
  1935  	assert.Nil(t, r1.Err)
  1936  
  1937  	d1 := r1.Data.([]*model.ReplyForExport)
  1938  	assert.Len(t, d1, 1)
  1939  
  1940  	reply1 := d1[0]
  1941  	assert.Equal(t, reply1.Id, p2.Id)
  1942  	assert.Equal(t, reply1.Message, p2.Message)
  1943  	assert.Equal(t, reply1.Username, u1.Username)
  1944  }