Feature scheduled messages (#28932)
* Create scheduled post api (#27920) * Added migration files for Postgres * Added migrations for MySQL * Added store method * Added API and store tests * Renamed migration after syncing with master * Added app layer tests * API is ready * API is ready * API is ready * Renamed migration after syncing with master * Updated migration list * Fixed retry layer tests * Allowed posts with empty messages * Review fixes * Reverted an incorrect change * Renamed migration and fixed ID assignment * CI * Send post button changes (#28019) * added Split button * WIP * Added core menu options * WIP * WIP * WIP * Handled displaying error in creating scheduled post * lint fixes * webapp i18n fix * Review fixes * Fixed a webapp test * A few more fixes * Removed a duplicate comment * Scheduled post job (#28088) * Added the job function * Added query for fetching scheduled posts for pricessing * WIP * WIP * WIP * WIP * WIP * WIP * Reafactoring of scheduled post job * Lint fixes * Updated i18n files * FInishing touches * Added tests for GetScheduledPosts * Added tests for PermanentlyDeleteScheduledPosts * Updated all layer * Some changes as discussed with team * Added tests for UpdatedScheduledPost * Code review refactoring * Added job test * MM-60120 - Custom time selection (#28120) * Added a common date time picker modal and used it for post reminder * Added a common date time picker modal and used it for post reminderggp * Added modal for custom schedule time and fixed TZ issue * WIP * Removed event from useSubmit hook * Removed event from useSubmit hook * Added timezone handling * fixed type error * Updated i18n strings * Minor cleanup * updated snapshots * review fixes * Handled event * Supported for having a DM thread open in RHS while in a regular channel * Review fixes * MM-60136 - Scheduled messages tab (#28133) * WIP * WIP * Created Tabs and Tab wrapper with added styling * Added API to get scheduled posts * WIP * Displated scheduled post count * i18n fix * Added tests * Handled asetting active tab absed on URL: * Reverted unintended change * Added API to client ad OpenAPI specs * Renamed file * Adding fileinfo to schedule posts * Partial review fixes * Made get scheduled post API return posts by teamID * review fixes * Moved scheduled post redux code to MM-redux package * Usedd selector factory * WIP: * WIP: * Lint fix * Fixed an incorrect openapi spec file * Removed redundent permission check * Clreaed scheduled post data on logout * Removed unused i18n string: * lint fix * Render scheduled posts (#28208) * WIP * WIP * Created Tabs and Tab wrapper with added styling * Added API to get scheduled posts * WIP * Displated scheduled post count * i18n fix * Added tests * Handled asetting active tab absed on URL: * Reverted unintended change * Added API to client ad OpenAPI specs * Renamed file * Created common component for draft list item * WIP * WIP * Adding fileinfo to schedule posts * Basic rendering * Added count badge to tabs * WIP * Made the Drafts LHS iteam appear if no drafts exist but scheduled posts do * Fixed icon size * Partial review fixes * Made get scheduled post API return posts by teamID * Handled initial vs team switch load * Displayed scheduled date in panel header * Added error message and error indiocator * WIP * review fixes * WIP Adding error reason tag * Added error codes * Moved scheduled post redux code to MM-redux package * Usedd selector factory * WIP: * WIP: * Lint fix * Fixed an incorrect openapi spec file * Removed redundent permission check * Clreaed scheduled post data on logout * Removed unused i18n string: * lint fix * Opened rescheduling modal * Updated graphic for empty state of schduled post list * Added delete scheduled post option and modal * Badge and timezone fix * WIP: * Added send now confirmation modal * lint * Webapp i18n fix * Fixed webapp test * Fixed a bug where DM/GM scheduled posts weren't immideatly showing up in UI * Minor fixes * WIP * Review fixes * Review fixes * Optimisations * Fixed reducer name * Moment optimizatin * Updated route check * MM-60144 - added API to update a scheduled post (#28248) * WIP * Added api and ap layer for update scheduled post ̛̦̄ * Added API to OpenAI specs, Go client and TS client * removed permissio check * Added tests * Fixed tests * Added PreUpdate method on scheduled post model * MM-60131 - Reschedule post integration (#28281) * Handled rescheduling post in webapp * Added error handling * MM-60146 - Delete scheduled post api (#28265) * WIP * Added api and ap layer for update scheduled post ̛̦̄ * Added API to OpenAI specs, Go client and TS client * removed permissio check * Added tests * Fixed tests * Added PreUpdate method on scheduled post model * Added delete scheduled post API * Added API to Go client and OpenAPI specs * Added API to TS client * Added tests * CI * Rmeoved two incorrect code comments * MM-60653 - Integrated delete scheduled post API (#28296) * Integrated delete scheduled apost API * Lint fix * Review fixes * Excluded draft checks from scheduled posts (#28370) * Excluded draft checks from scheduled posts * Added a removed todo * MM-60125 - Scheduled post channel indicator (#28320) * Integrated delete scheduled apost API * Lint fix * Added state for storing scheduled posts by channel ID * Refactored redux store to store scheudled posts by ID, thens tore IDs everywhere * Refactored redux store to store scheudled posts by ID, thens tore IDs everywhere * WIP * Added scheduled post indiocator * Handled single and multiple scheudled posts * Review fixes * Fixed styling and handled center channel, RHS and threads view * Lint fix * i18n fix * Fixed a cycling dependency * Lint fix * Added some more comments * Updated styling * Review fixes * Added common component for remote user time and scheduled post indicator * Updated scheduled post count * Minor change * Moved CSS code around * Fixed a bug where files in scheduled post didn't show up until refresh (#28359) --------- Co-authored-by: Daniel Espino García <larkox@gmail.com> * Scheduled post config (#28485) * Added config * Added config on server and webapp side * Added config check in server and webapp * Added license check * Added license check * Added placeholder help text * Added license check to job * Fixed job test * Review fixes * Updated English text * Review fixes * MM-60118 - Added index on ScheduledPosts table (#28579) * Added index * Updated indexes * Scheduled posts misc fixes (#28625) * Added detailed logging for scheduled post job * Limited scheduled posts processing to 24 hours * Marked old scheduled posts as unable to send * Added t5ests * converted some logs to trace level * Fixed a bug causing error message to show up on deleting a scheduled post in a deleted thread (#28630) * Fixed scheduled posts link in RHS (#28659) * Fixed scheduled posts link in RHS * Review fixes * Fix permission name in scheduled posts by team (#28580) * Fix permission name * fix wording --------- Co-authored-by: Mattermost Build <build@mattermost.com> * FIxed width of generic modal header to fix browser channel modal (#28639) * Only consider error-free scheduled posts for indicator in channel and RHS (#28683) * Show only errro free scheudled posts in post box indicator * Fixed a bug to handle no scheduled posts * Fixed draft and scheudled post UI in mobile view (#28680) * MM-60873 and MM-60872 - Fixed a bug with updating scheduled posts (#28656) * Fixed a bug with updating scheduled posts * Better selectors * MOved shceuled post message length validation to app layer * MM-60732 - Scheduled posts channel link now takes you to the first scheduled post in channel/thread in list (#28768) * Ordered scheudle dposts by schgeudled at nad create at * Ordered in client * Added scroll to target * Removed classname prop * Fixed tests * Added doc * Import fix * MM-60961 - Fixed a bug where API used incoming create at date for scheduled post (#28703) * Fixed a bug where API used incoming create at date for scheduled post * Stopped sending created at value for scheduled post * MM-60785 - Fixed a bug where scheduled posts of channel we are no longer member of didn't show up (#28637) * Fixed a bug where scheduled posts of channel we are no longer member of didn't show up * Added a comment * CI * Used data loader to optimise laoding missing channels * Minor refactoring * MM-60963 - Added common checks for post and scheduled posts (#28713) * Added commen checks for post and scheuled posts * Sanitised scheduled posts * Fixed tests * Splitted post checks into app and context functions * Added checks on scheduiled posts job as well: * i18n fix * Fixed a test * Renamed a func * removed duplicate check * Scheduled posts UI fixes (#28828) * Fixed send button and time picker borders * Fixed center alignment of time picker * Removed on for today and tomorrow * Lint fix * Date time modal hover state fix * Badge fix * Fixed a mnerge issue * Scheduled Post send now and add schedule on draft (#28851) * Added send now option on scheduled posts * Minor refactoring * WIP * WIP * WIP * Lint fix * i18n fix * Snapshot update * Review fixes * Scheduled post inline editing (#28893) * Added send now option on scheduled posts * Minor refactoring * WIP * WIP * WIP * Lint fix * i18n fix * Snapshot update * Displayed editing component in scheduled post * Added handling for updating scheduled post * Handle events * Fixed escape key issue in scheudled post editing * Fixes * Displayed error message for editing error * Don't show mention warning * Handled dev mode (#28918) * MInor fixes * client fix * Fixes * CI * Removed dev mode behaviour temperorily (#29008) --------- Co-authored-by: Daniel Espino García <larkox@gmail.com> Co-authored-by: Eva Sarafianou <eva.sarafianou@gmail.com> Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
aaf9234c8e
Коммит
e281b3f37e
@@ -50,6 +50,7 @@ type OpenTracingLayer struct {
|
||||
RemoteClusterStore store.RemoteClusterStore
|
||||
RetentionPolicyStore store.RetentionPolicyStore
|
||||
RoleStore store.RoleStore
|
||||
ScheduledPostStore store.ScheduledPostStore
|
||||
SchemeStore store.SchemeStore
|
||||
SessionStore store.SessionStore
|
||||
SharedChannelStore store.SharedChannelStore
|
||||
@@ -190,6 +191,10 @@ func (s *OpenTracingLayer) Role() store.RoleStore {
|
||||
return s.RoleStore
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayer) ScheduledPost() store.ScheduledPostStore {
|
||||
return s.ScheduledPostStore
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayer) Scheme() store.SchemeStore {
|
||||
return s.SchemeStore
|
||||
}
|
||||
@@ -401,6 +406,11 @@ type OpenTracingLayerRoleStore struct {
|
||||
Root *OpenTracingLayer
|
||||
}
|
||||
|
||||
type OpenTracingLayerScheduledPostStore struct {
|
||||
store.ScheduledPostStore
|
||||
Root *OpenTracingLayer
|
||||
}
|
||||
|
||||
type OpenTracingLayerSchemeStore struct {
|
||||
store.SchemeStore
|
||||
Root *OpenTracingLayer
|
||||
@@ -8602,6 +8612,145 @@ func (s *OpenTracingLayerRoleStore) Save(role *model.Role) (*model.Role, error)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) CreateScheduledPost(scheduledPost *model.ScheduledPost) (*model.ScheduledPost, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.CreateScheduledPost")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.ScheduledPostStore.CreateScheduledPost(scheduledPost)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) Get(scheduledPostId string) (*model.ScheduledPost, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.Get")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.ScheduledPostStore.Get(scheduledPostId)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) GetMaxMessageSize() int {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.GetMaxMessageSize")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result := s.ScheduledPostStore.GetMaxMessageSize()
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) GetPendingScheduledPosts(beforeTime int64, afterTime int64, lastScheduledPostId string, perPage uint64) ([]*model.ScheduledPost, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.GetPendingScheduledPosts")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.ScheduledPostStore.GetPendingScheduledPosts(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) GetScheduledPostsForUser(userId string, teamId string) ([]*model.ScheduledPost, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.GetScheduledPostsForUser")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.ScheduledPostStore.GetScheduledPostsForUser(userId, teamId)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) PermanentlyDeleteScheduledPosts(scheduledPostIDs []string) error {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.PermanentlyDeleteScheduledPosts")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
err := s.ScheduledPostStore.PermanentlyDeleteScheduledPosts(scheduledPostIDs)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) UpdateOldScheduledPosts(beforeTime int64) error {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.UpdateOldScheduledPosts")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
err := s.ScheduledPostStore.UpdateOldScheduledPosts(beforeTime)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerScheduledPostStore) UpdatedScheduledPost(scheduledPost *model.ScheduledPost) error {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ScheduledPostStore.UpdatedScheduledPost")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
err := s.ScheduledPostStore.UpdatedScheduledPost(scheduledPost)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerSchemeStore) CountByScope(scope string) (int64, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SchemeStore.CountByScope")
|
||||
@@ -13672,6 +13821,7 @@ func New(childStore store.Store, ctx context.Context) *OpenTracingLayer {
|
||||
newStore.RemoteClusterStore = &OpenTracingLayerRemoteClusterStore{RemoteClusterStore: childStore.RemoteCluster(), Root: &newStore}
|
||||
newStore.RetentionPolicyStore = &OpenTracingLayerRetentionPolicyStore{RetentionPolicyStore: childStore.RetentionPolicy(), Root: &newStore}
|
||||
newStore.RoleStore = &OpenTracingLayerRoleStore{RoleStore: childStore.Role(), Root: &newStore}
|
||||
newStore.ScheduledPostStore = &OpenTracingLayerScheduledPostStore{ScheduledPostStore: childStore.ScheduledPost(), Root: &newStore}
|
||||
newStore.SchemeStore = &OpenTracingLayerSchemeStore{SchemeStore: childStore.Scheme(), Root: &newStore}
|
||||
newStore.SessionStore = &OpenTracingLayerSessionStore{SessionStore: childStore.Session(), Root: &newStore}
|
||||
newStore.SharedChannelStore = &OpenTracingLayerSharedChannelStore{SharedChannelStore: childStore.SharedChannel(), Root: &newStore}
|
||||
|
||||
@@ -54,6 +54,7 @@ type RetryLayer struct {
|
||||
RemoteClusterStore store.RemoteClusterStore
|
||||
RetentionPolicyStore store.RetentionPolicyStore
|
||||
RoleStore store.RoleStore
|
||||
ScheduledPostStore store.ScheduledPostStore
|
||||
SchemeStore store.SchemeStore
|
||||
SessionStore store.SessionStore
|
||||
SharedChannelStore store.SharedChannelStore
|
||||
@@ -194,6 +195,10 @@ func (s *RetryLayer) Role() store.RoleStore {
|
||||
return s.RoleStore
|
||||
}
|
||||
|
||||
func (s *RetryLayer) ScheduledPost() store.ScheduledPostStore {
|
||||
return s.ScheduledPostStore
|
||||
}
|
||||
|
||||
func (s *RetryLayer) Scheme() store.SchemeStore {
|
||||
return s.SchemeStore
|
||||
}
|
||||
@@ -405,6 +410,11 @@ type RetryLayerRoleStore struct {
|
||||
Root *RetryLayer
|
||||
}
|
||||
|
||||
type RetryLayerScheduledPostStore struct {
|
||||
store.ScheduledPostStore
|
||||
Root *RetryLayer
|
||||
}
|
||||
|
||||
type RetryLayerSchemeStore struct {
|
||||
store.SchemeStore
|
||||
Root *RetryLayer
|
||||
@@ -9812,6 +9822,159 @@ func (s *RetryLayerRoleStore) Save(role *model.Role) (*model.Role, error) {
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) CreateScheduledPost(scheduledPost *model.ScheduledPost) (*model.ScheduledPost, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.ScheduledPostStore.CreateScheduledPost(scheduledPost)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return result, err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return result, err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) Get(scheduledPostId string) (*model.ScheduledPost, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.ScheduledPostStore.Get(scheduledPostId)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return result, err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return result, err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) GetMaxMessageSize() int {
|
||||
|
||||
return s.ScheduledPostStore.GetMaxMessageSize()
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) GetPendingScheduledPosts(beforeTime int64, afterTime int64, lastScheduledPostId string, perPage uint64) ([]*model.ScheduledPost, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.ScheduledPostStore.GetPendingScheduledPosts(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return result, err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return result, err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) GetScheduledPostsForUser(userId string, teamId string) ([]*model.ScheduledPost, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.ScheduledPostStore.GetScheduledPostsForUser(userId, teamId)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return result, err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return result, err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) PermanentlyDeleteScheduledPosts(scheduledPostIDs []string) error {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
err := s.ScheduledPostStore.PermanentlyDeleteScheduledPosts(scheduledPostIDs)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) UpdateOldScheduledPosts(beforeTime int64) error {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
err := s.ScheduledPostStore.UpdateOldScheduledPosts(beforeTime)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerScheduledPostStore) UpdatedScheduledPost(scheduledPost *model.ScheduledPost) error {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
err := s.ScheduledPostStore.UpdatedScheduledPost(scheduledPost)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerSchemeStore) CountByScope(scope string) (int64, error) {
|
||||
|
||||
tries := 0
|
||||
@@ -15605,6 +15768,7 @@ func New(childStore store.Store) *RetryLayer {
|
||||
newStore.RemoteClusterStore = &RetryLayerRemoteClusterStore{RemoteClusterStore: childStore.RemoteCluster(), Root: &newStore}
|
||||
newStore.RetentionPolicyStore = &RetryLayerRetentionPolicyStore{RetentionPolicyStore: childStore.RetentionPolicy(), Root: &newStore}
|
||||
newStore.RoleStore = &RetryLayerRoleStore{RoleStore: childStore.Role(), Root: &newStore}
|
||||
newStore.ScheduledPostStore = &RetryLayerScheduledPostStore{ScheduledPostStore: childStore.ScheduledPost(), Root: &newStore}
|
||||
newStore.SchemeStore = &RetryLayerSchemeStore{SchemeStore: childStore.Scheme(), Root: &newStore}
|
||||
newStore.SessionStore = &RetryLayerSessionStore{SessionStore: childStore.Session(), Root: &newStore}
|
||||
newStore.SharedChannelStore = &RetryLayerSharedChannelStore{SharedChannelStore: childStore.SharedChannel(), Root: &newStore}
|
||||
|
||||
@@ -62,6 +62,7 @@ func genStore() *mocks.Store {
|
||||
mock.On("PostPersistentNotification").Return(&mocks.PostPersistentNotificationStore{})
|
||||
mock.On("DesktopTokens").Return(&mocks.DesktopTokensStore{})
|
||||
mock.On("ChannelBookmark").Return(&mocks.ChannelBookmarkStore{})
|
||||
mock.On("ScheduledPost").Return(&mocks.ScheduledPostStore{})
|
||||
return mock
|
||||
}
|
||||
|
||||
|
||||
289
server/channels/store/sqlstore/scheduled_post_store.go
Обычный файл
289
server/channels/store/sqlstore/scheduled_post_store.go
Обычный файл
@@ -0,0 +1,289 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
sq "github.com/mattermost/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type SqlScheduledPostStore struct {
|
||||
*SqlStore
|
||||
maxMessageSizeOnce sync.Once
|
||||
maxMessageSizeCached int
|
||||
}
|
||||
|
||||
func newScheduledPostStore(sqlStore *SqlStore) *SqlScheduledPostStore {
|
||||
return &SqlScheduledPostStore{
|
||||
SqlStore: sqlStore,
|
||||
maxMessageSizeCached: model.PostMessageMaxRunesV2,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) columns(prefix string) []string {
|
||||
if prefix != "" && !strings.HasSuffix(prefix, ".") {
|
||||
prefix = prefix + "."
|
||||
}
|
||||
|
||||
return []string{
|
||||
prefix + "Id",
|
||||
prefix + "CreateAt",
|
||||
prefix + "UpdateAt",
|
||||
prefix + "UserId",
|
||||
prefix + "ChannelId",
|
||||
prefix + "RootId",
|
||||
prefix + "Message",
|
||||
prefix + "Props",
|
||||
prefix + "FileIds",
|
||||
prefix + "Priority",
|
||||
prefix + "ScheduledAt",
|
||||
prefix + "ProcessedAt",
|
||||
prefix + "ErrorCode",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) scheduledPostToSlice(scheduledPost *model.ScheduledPost) []interface{} {
|
||||
return []interface{}{
|
||||
scheduledPost.Id,
|
||||
scheduledPost.CreateAt,
|
||||
scheduledPost.UpdateAt,
|
||||
scheduledPost.UserId,
|
||||
scheduledPost.ChannelId,
|
||||
scheduledPost.RootId,
|
||||
scheduledPost.Message,
|
||||
model.StringInterfaceToJSON(scheduledPost.GetProps()),
|
||||
model.ArrayToJSON(scheduledPost.FileIds),
|
||||
model.StringInterfaceToJSON(scheduledPost.Priority),
|
||||
scheduledPost.ScheduledAt,
|
||||
scheduledPost.ProcessedAt,
|
||||
scheduledPost.ErrorCode,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) CreateScheduledPost(scheduledPost *model.ScheduledPost) (*model.ScheduledPost, error) {
|
||||
scheduledPost.PreSave()
|
||||
|
||||
builder := s.getQueryBuilder().
|
||||
Insert("ScheduledPosts").
|
||||
Columns(s.columns("")...).
|
||||
Values(s.scheduledPostToSlice(scheduledPost)...)
|
||||
|
||||
query, args, err := builder.ToSql()
|
||||
if err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.CreateScheduledPost failed to generate SQL from query builder", mlog.Err(err))
|
||||
return nil, errors.Wrap(err, "SqlScheduledPostStore.CreateScheduledPost failed to generate SQL from query builder")
|
||||
}
|
||||
|
||||
if _, err := s.GetMasterX().Exec(query, args...); err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.CreateScheduledPost failed to insert scheduled post", mlog.Err(err))
|
||||
return nil, errors.Wrap(err, "SqlScheduledPostStore.CreateScheduledPost failed to insert scheduled post")
|
||||
}
|
||||
|
||||
return scheduledPost, nil
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) GetScheduledPostsForUser(userId, teamId string) ([]*model.ScheduledPost, error) {
|
||||
// return scheduled posts for this user for
|
||||
// specified team.
|
||||
//
|
||||
//An empty teamId fetches scheduled posts belonging to
|
||||
// DMs and GMs (DMs and GMs do not belong to any team
|
||||
|
||||
// We're intentionally including scheduled posts from archived channels,
|
||||
// or channels the user no longer belongs to as we want to still show those
|
||||
// scheduled posts with appropriate error to the user.
|
||||
// This is why we're not joining with ChannelMembers, and directly
|
||||
// joining with Channels table.
|
||||
|
||||
query := s.getQueryBuilder().
|
||||
Select(s.columns("sp")...).
|
||||
From("ScheduledPosts AS sp").
|
||||
InnerJoin("Channels as c on sp.ChannelId = c.Id").
|
||||
Where(sq.Eq{
|
||||
"sp.UserId": userId,
|
||||
"c.TeamId": teamId,
|
||||
}).
|
||||
OrderBy("sp.ScheduledAt, sp.CreateAt")
|
||||
|
||||
var scheduledPosts []*model.ScheduledPost
|
||||
|
||||
if err := s.GetReplicaX().SelectBuilder(&scheduledPosts, query); err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.GetScheduledPostsForUser: failed to fetch scheduled posts for user", mlog.String("user_id", userId), mlog.String("team_id", teamId), mlog.Err(err))
|
||||
|
||||
return nil, errors.Wrapf(err, "SqlScheduledPostStore.GetScheduledPostsForUser: failed to fetch scheduled posts for user, userId: %s, teamID: %s", userId, teamId)
|
||||
}
|
||||
|
||||
return scheduledPosts, nil
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) GetMaxMessageSize() int {
|
||||
s.maxMessageSizeOnce.Do(func() {
|
||||
var err error
|
||||
s.maxMessageSizeCached, err = s.SqlStore.determineMaxColumnSize("ScheduledPosts", "Message")
|
||||
if err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.getMaxMessageSize: error occurred during determining max column size for ScheduledPosts.Message column", mlog.Err(err))
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
return s.maxMessageSizeCached
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) GetPendingScheduledPosts(beforeTime, afterTime int64, lastScheduledPostId string, perPage uint64) ([]*model.ScheduledPost, error) {
|
||||
query := s.getQueryBuilder().
|
||||
Select(s.columns("")...).
|
||||
From("ScheduledPosts").
|
||||
Where(sq.Eq{"ErrorCode": ""}).
|
||||
OrderBy("ScheduledAt DESC", "Id").
|
||||
Limit(perPage)
|
||||
|
||||
if lastScheduledPostId == "" {
|
||||
query = query.Where(sq.And{
|
||||
sq.LtOrEq{"ScheduledAt": beforeTime},
|
||||
sq.GtOrEq{"ScheduledAt": afterTime},
|
||||
})
|
||||
}
|
||||
if lastScheduledPostId != "" {
|
||||
query = query.
|
||||
Where(sq.Or{
|
||||
sq.And{
|
||||
sq.LtOrEq{"ScheduledAt": beforeTime},
|
||||
sq.GtOrEq{"ScheduledAt": afterTime},
|
||||
},
|
||||
sq.And{
|
||||
sq.Eq{"ScheduledAt": beforeTime},
|
||||
sq.Gt{"Id": lastScheduledPostId},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
var scheduledPosts []*model.ScheduledPost
|
||||
if err := s.GetReplicaX().SelectBuilder(&scheduledPosts, query); err != nil {
|
||||
mlog.Error(
|
||||
"SqlScheduledPostStore.GetPendingScheduledPosts: failed to fetch pending scheduled posts for processing",
|
||||
mlog.Int("before_time", beforeTime),
|
||||
mlog.String("last_scheduled_post_id", lastScheduledPostId),
|
||||
mlog.Uint("items_per_page", perPage), mlog.Err(err),
|
||||
)
|
||||
|
||||
return nil, errors.Wrapf(
|
||||
err,
|
||||
"SqlScheduledPostStore.GetPendingScheduledPosts: failed to fetch pending scheduled posts for processing, before_time: %d, last_scheduled_post_id: %s, items_per_page: %d",
|
||||
beforeTime, lastScheduledPostId, perPage,
|
||||
)
|
||||
}
|
||||
|
||||
return scheduledPosts, nil
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) PermanentlyDeleteScheduledPosts(scheduledPostIDs []string) error {
|
||||
if len(scheduledPostIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := s.getQueryBuilder().
|
||||
Delete("ScheduledPosts").
|
||||
Where(sq.Eq{"Id": scheduledPostIDs})
|
||||
|
||||
sql, params, err := query.ToSql()
|
||||
if err != nil {
|
||||
errToReturn := errors.Wrapf(err, "PermanentlyDeleteScheduledPosts: failed to generate SQL query for permanently deleting batch of scheduled posts")
|
||||
s.Logger().Error(errToReturn.Error())
|
||||
return errToReturn
|
||||
}
|
||||
|
||||
if _, err := s.GetMasterX().Exec(sql, params...); err != nil {
|
||||
errToReturn := errors.Wrapf(err, "PermanentlyDeleteScheduledPosts: failed to delete batch of scheduled posts from database")
|
||||
s.Logger().Error(errToReturn.Error())
|
||||
return errToReturn
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) UpdatedScheduledPost(scheduledPost *model.ScheduledPost) error {
|
||||
scheduledPost.PreUpdate()
|
||||
|
||||
builder := s.getQueryBuilder().
|
||||
Update("ScheduledPosts").
|
||||
SetMap(s.toUpdateMap(scheduledPost)).
|
||||
Where(sq.Eq{"Id": scheduledPost.Id})
|
||||
|
||||
query, args, err := builder.ToSql()
|
||||
if err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.UpdatedScheduledPost failed to generate SQL from updating scheduled posts", mlog.String("scheduled_post_id", scheduledPost.Id), mlog.Err(err))
|
||||
return errors.Wrap(err, "SqlScheduledPostStore.UpdatedScheduledPost failed to generate SQL from bulk updating scheduled posts")
|
||||
}
|
||||
|
||||
_, err = s.GetMasterX().Exec(query, args...)
|
||||
if err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.UpdatedScheduledPost failed to update scheduled post", mlog.String("scheduled_post_id", scheduledPost.Id), mlog.Err(err))
|
||||
return errors.Wrap(err, "SqlScheduledPostStore.UpdatedScheduledPost failed to update scheduled post")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) toUpdateMap(scheduledPost *model.ScheduledPost) map[string]interface{} {
|
||||
now := model.GetMillis()
|
||||
return map[string]interface{}{
|
||||
"UpdateAt": now,
|
||||
"Message": scheduledPost.Message,
|
||||
"Props": model.StringInterfaceToJSON(scheduledPost.GetProps()),
|
||||
"FileIds": model.ArrayToJSON(scheduledPost.FileIds),
|
||||
"Priority": model.StringInterfaceToJSON(scheduledPost.Priority),
|
||||
"ScheduledAt": scheduledPost.ScheduledAt,
|
||||
"ProcessedAt": now,
|
||||
"ErrorCode": scheduledPost.ErrorCode,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) Get(scheduledPostId string) (*model.ScheduledPost, error) {
|
||||
query := s.getQueryBuilder().
|
||||
Select(s.columns("")...).
|
||||
From("ScheduledPosts").
|
||||
Where(sq.Eq{
|
||||
"Id": scheduledPostId,
|
||||
})
|
||||
|
||||
scheduledPost := &model.ScheduledPost{}
|
||||
|
||||
if err := s.GetReplicaX().GetBuilder(scheduledPost, query); err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.Get: failed to get single scheduled post by ID from database", mlog.String("scheduled_post_id", scheduledPostId), mlog.Err(err))
|
||||
|
||||
return nil, errors.Wrapf(err, "SqlScheduledPostStore.Get: failed to get single scheduled post by ID from database, scheduledPostId: %s", scheduledPostId)
|
||||
}
|
||||
|
||||
return scheduledPost, nil
|
||||
}
|
||||
|
||||
func (s *SqlScheduledPostStore) UpdateOldScheduledPosts(beforeTime int64) error {
|
||||
builder := s.getQueryBuilder().
|
||||
Update("ScheduledPosts").
|
||||
Set("ErrorCode", model.ScheduledPostErrorUnableToSend).
|
||||
Set("ProcessedAt", model.GetMillis()).
|
||||
Where(sq.And{
|
||||
sq.Eq{"ErrorCode": ""},
|
||||
sq.Lt{"ScheduledAt": beforeTime},
|
||||
})
|
||||
|
||||
query, args, err := builder.ToSql()
|
||||
if err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.UpdateOldScheduledPosts failed to generate SQL from updating old scheduled posts", mlog.Err(err))
|
||||
return errors.Wrap(err, "SqlScheduledPostStore.UpdateOldScheduledPosts failed to generate SQL from updating old scheduled posts")
|
||||
}
|
||||
|
||||
_, err = s.GetMasterX().Exec(query, args...)
|
||||
if err != nil {
|
||||
mlog.Error("SqlScheduledPostStore.UpdateOldScheduledPosts failed to update old scheduled posts", mlog.Err(err))
|
||||
return errors.Wrap(err, "SqlScheduledPostStore.UpdateOldScheduledPosts failed to update old scheduled posts")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
14
server/channels/store/sqlstore/scheduled_post_store_test.go
Обычный файл
14
server/channels/store/sqlstore/scheduled_post_store_test.go
Обычный файл
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
|
||||
)
|
||||
|
||||
func TestScheduledPostStore(t *testing.T) {
|
||||
StoreTestWithSqlStore(t, storetest.TestScheduledPostStore)
|
||||
}
|
||||
@@ -111,6 +111,7 @@ type SqlStoreStores struct {
|
||||
postPersistentNotification store.PostPersistentNotificationStore
|
||||
desktopTokens store.DesktopTokensStore
|
||||
channelBookmarks store.ChannelBookmarkStore
|
||||
scheduledPost store.ScheduledPostStore
|
||||
}
|
||||
|
||||
type SqlStore struct {
|
||||
@@ -236,6 +237,7 @@ func New(settings model.SqlSettings, logger mlog.LoggerIFace, metrics einterface
|
||||
store.stores.postPersistentNotification = newSqlPostPersistentNotificationStore(store)
|
||||
store.stores.desktopTokens = newSqlDesktopTokensStore(store, metrics)
|
||||
store.stores.channelBookmarks = newSqlChannelBookmarkStore(store)
|
||||
store.stores.scheduledPost = newScheduledPostStore(store)
|
||||
|
||||
store.stores.preference.(*SqlPreferenceStore).deleteUnusedFeatures()
|
||||
|
||||
@@ -1294,3 +1296,51 @@ func (ss *SqlStore) GetAppliedMigrations() ([]model.AppliedMigration, error) {
|
||||
|
||||
return migrations, nil
|
||||
}
|
||||
|
||||
func (ss *SqlStore) determineMaxColumnSize(tableName, columnName string) (int, error) {
|
||||
var columnSizeBytes int32
|
||||
ss.getQueryPlaceholder()
|
||||
|
||||
if ss.DriverName() == model.DatabaseDriverPostgres {
|
||||
if err := ss.GetReplicaX().Get(&columnSizeBytes, `
|
||||
SELECT
|
||||
COALESCE(character_maximum_length, 0)
|
||||
FROM
|
||||
information_schema.columns
|
||||
WHERE
|
||||
lower(table_name) = lower($1)
|
||||
AND lower(column_name) = lower($2)
|
||||
`, tableName, columnName); err != nil {
|
||||
mlog.Warn("Unable to determine the maximum supported column size for Postgres", mlog.Err(err))
|
||||
return 0, err
|
||||
}
|
||||
} else if ss.DriverName() == model.DatabaseDriverMysql {
|
||||
if err := ss.GetReplicaX().Get(&columnSizeBytes, `
|
||||
SELECT
|
||||
COALESCE(CHARACTER_MAXIMUM_LENGTH, 0)
|
||||
FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE
|
||||
table_schema = DATABASE()
|
||||
AND lower(table_name) = lower(?)
|
||||
AND lower(column_name) = lower(?)
|
||||
LIMIT 0, 1
|
||||
`, tableName, columnName); err != nil {
|
||||
mlog.Warn("Unable to determine the maximum supported column size for MySQL", mlog.Err(err))
|
||||
return 0, err
|
||||
}
|
||||
} else {
|
||||
mlog.Warn("No implementation found to determine the maximum supported column size")
|
||||
}
|
||||
|
||||
// Assume a worst-case representation of four bytes per rune.
|
||||
maxColumnSize := int(columnSizeBytes) / 4
|
||||
|
||||
mlog.Info("Column has size restrictions", mlog.String("table_name", tableName), mlog.String("column_name", columnName), mlog.Int("max_characters", maxColumnSize), mlog.Int("max_bytes", columnSizeBytes))
|
||||
|
||||
return maxColumnSize, nil
|
||||
}
|
||||
|
||||
func (ss *SqlStore) ScheduledPost() store.ScheduledPostStore {
|
||||
return ss.stores.scheduledPost
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ type Store interface {
|
||||
PostPersistentNotification() PostPersistentNotificationStore
|
||||
DesktopTokens() DesktopTokensStore
|
||||
ChannelBookmark() ChannelBookmarkStore
|
||||
ScheduledPost() ScheduledPostStore
|
||||
}
|
||||
|
||||
type RetentionPolicyStore interface {
|
||||
@@ -1055,6 +1056,17 @@ type ChannelBookmarkStore interface {
|
||||
GetBookmarksForChannelSince(channelID string, since int64) ([]*model.ChannelBookmarkWithFileInfo, error)
|
||||
}
|
||||
|
||||
type ScheduledPostStore interface {
|
||||
GetMaxMessageSize() int
|
||||
CreateScheduledPost(scheduledPost *model.ScheduledPost) (*model.ScheduledPost, error)
|
||||
GetScheduledPostsForUser(userId, teamId string) ([]*model.ScheduledPost, error)
|
||||
GetPendingScheduledPosts(beforeTime, afterTime int64, lastScheduledPostId string, perPage uint64) ([]*model.ScheduledPost, error)
|
||||
PermanentlyDeleteScheduledPosts(scheduledPostIDs []string) error
|
||||
UpdatedScheduledPost(scheduledPost *model.ScheduledPost) error
|
||||
Get(scheduledPostId string) (*model.ScheduledPost, error)
|
||||
UpdateOldScheduledPosts(beforeTime int64) error
|
||||
}
|
||||
|
||||
// ChannelSearchOpts contains options for searching channels.
|
||||
//
|
||||
// NotAssociatedToGroup will exclude channels that have associated, active GroupChannels records.
|
||||
|
||||
221
server/channels/store/storetest/mocks/ScheduledPostStore.go
Обычный файл
221
server/channels/store/storetest/mocks/ScheduledPostStore.go
Обычный файл
@@ -0,0 +1,221 @@
|
||||
// Code generated by mockery v2.42.2. DO NOT EDIT.
|
||||
|
||||
// Regenerate this file using `make store-mocks`.
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
model "github.com/mattermost/mattermost/server/public/model"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// ScheduledPostStore is an autogenerated mock type for the ScheduledPostStore type
|
||||
type ScheduledPostStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// CreateScheduledPost provides a mock function with given fields: scheduledPost
|
||||
func (_m *ScheduledPostStore) CreateScheduledPost(scheduledPost *model.ScheduledPost) (*model.ScheduledPost, error) {
|
||||
ret := _m.Called(scheduledPost)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for CreateScheduledPost")
|
||||
}
|
||||
|
||||
var r0 *model.ScheduledPost
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(*model.ScheduledPost) (*model.ScheduledPost, error)); ok {
|
||||
return rf(scheduledPost)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(*model.ScheduledPost) *model.ScheduledPost); ok {
|
||||
r0 = rf(scheduledPost)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*model.ScheduledPost)
|
||||
}
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(*model.ScheduledPost) error); ok {
|
||||
r1 = rf(scheduledPost)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// Get provides a mock function with given fields: scheduledPostId
|
||||
func (_m *ScheduledPostStore) Get(scheduledPostId string) (*model.ScheduledPost, error) {
|
||||
ret := _m.Called(scheduledPostId)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Get")
|
||||
}
|
||||
|
||||
var r0 *model.ScheduledPost
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(string) (*model.ScheduledPost, error)); ok {
|
||||
return rf(scheduledPostId)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(string) *model.ScheduledPost); ok {
|
||||
r0 = rf(scheduledPostId)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*model.ScheduledPost)
|
||||
}
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(string) error); ok {
|
||||
r1 = rf(scheduledPostId)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetMaxMessageSize provides a mock function with given fields:
|
||||
func (_m *ScheduledPostStore) GetMaxMessageSize() int {
|
||||
ret := _m.Called()
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetMaxMessageSize")
|
||||
}
|
||||
|
||||
var r0 int
|
||||
if rf, ok := ret.Get(0).(func() int); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(int)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// GetPendingScheduledPosts provides a mock function with given fields: beforeTime, afterTime, lastScheduledPostId, perPage
|
||||
func (_m *ScheduledPostStore) GetPendingScheduledPosts(beforeTime int64, afterTime int64, lastScheduledPostId string, perPage uint64) ([]*model.ScheduledPost, error) {
|
||||
ret := _m.Called(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetPendingScheduledPosts")
|
||||
}
|
||||
|
||||
var r0 []*model.ScheduledPost
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(int64, int64, string, uint64) ([]*model.ScheduledPost, error)); ok {
|
||||
return rf(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(int64, int64, string, uint64) []*model.ScheduledPost); ok {
|
||||
r0 = rf(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*model.ScheduledPost)
|
||||
}
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(int64, int64, string, uint64) error); ok {
|
||||
r1 = rf(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetScheduledPostsForUser provides a mock function with given fields: userId, teamId
|
||||
func (_m *ScheduledPostStore) GetScheduledPostsForUser(userId string, teamId string) ([]*model.ScheduledPost, error) {
|
||||
ret := _m.Called(userId, teamId)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetScheduledPostsForUser")
|
||||
}
|
||||
|
||||
var r0 []*model.ScheduledPost
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(string, string) ([]*model.ScheduledPost, error)); ok {
|
||||
return rf(userId, teamId)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(string, string) []*model.ScheduledPost); ok {
|
||||
r0 = rf(userId, teamId)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*model.ScheduledPost)
|
||||
}
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(string, string) error); ok {
|
||||
r1 = rf(userId, teamId)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// PermanentlyDeleteScheduledPosts provides a mock function with given fields: scheduledPostIDs
|
||||
func (_m *ScheduledPostStore) PermanentlyDeleteScheduledPosts(scheduledPostIDs []string) error {
|
||||
ret := _m.Called(scheduledPostIDs)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for PermanentlyDeleteScheduledPosts")
|
||||
}
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func([]string) error); ok {
|
||||
r0 = rf(scheduledPostIDs)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// UpdateOldScheduledPosts provides a mock function with given fields: beforeTime
|
||||
func (_m *ScheduledPostStore) UpdateOldScheduledPosts(beforeTime int64) error {
|
||||
ret := _m.Called(beforeTime)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for UpdateOldScheduledPosts")
|
||||
}
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(int64) error); ok {
|
||||
r0 = rf(beforeTime)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// UpdatedScheduledPost provides a mock function with given fields: scheduledPost
|
||||
func (_m *ScheduledPostStore) UpdatedScheduledPost(scheduledPost *model.ScheduledPost) error {
|
||||
ret := _m.Called(scheduledPost)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for UpdatedScheduledPost")
|
||||
}
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(*model.ScheduledPost) error); ok {
|
||||
r0 = rf(scheduledPost)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// NewScheduledPostStore creates a new instance of ScheduledPostStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewScheduledPostStore(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *ScheduledPostStore {
|
||||
mock := &ScheduledPostStore{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||
|
||||
return mock
|
||||
}
|
||||
@@ -919,6 +919,26 @@ func (_m *Store) Role() store.RoleStore {
|
||||
return r0
|
||||
}
|
||||
|
||||
// ScheduledPost provides a mock function with given fields:
|
||||
func (_m *Store) ScheduledPost() store.ScheduledPostStore {
|
||||
ret := _m.Called()
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for ScheduledPost")
|
||||
}
|
||||
|
||||
var r0 store.ScheduledPostStore
|
||||
if rf, ok := ret.Get(0).(func() store.ScheduledPostStore); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.ScheduledPostStore)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Scheme provides a mock function with given fields:
|
||||
func (_m *Store) Scheme() store.SchemeStore {
|
||||
ret := _m.Called()
|
||||
|
||||
472
server/channels/store/storetest/scheduled_post_store.go
Обычный файл
472
server/channels/store/storetest/scheduled_post_store.go
Обычный файл
@@ -0,0 +1,472 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package storetest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestScheduledPostStore(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
|
||||
t.Run("CreateScheduledPost", func(t *testing.T) { testCreateScheduledPost(t, rctx, ss, s) })
|
||||
t.Run("GetPendingScheduledPosts", func(t *testing.T) { testGetScheduledPosts(t, rctx, ss, s) })
|
||||
t.Run("PermanentlyDeleteScheduledPosts", func(t *testing.T) { testPermanentlyDeleteScheduledPosts(t, rctx, ss, s) })
|
||||
t.Run("UpdatedScheduledPost", func(t *testing.T) { testUpdatedScheduledPost(t, rctx, ss, s) })
|
||||
t.Run("UpdateOldScheduledPosts", func(t *testing.T) { testUpdateOldScheduledPosts(t, rctx, ss, s) })
|
||||
}
|
||||
|
||||
func testCreateScheduledPost(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
|
||||
channel := &model.Channel{
|
||||
TeamId: "team_id_1",
|
||||
Type: model.ChannelTypeOpen,
|
||||
Name: "channel_name",
|
||||
DisplayName: "Channel Name",
|
||||
}
|
||||
|
||||
createdChannel, err := ss.Channel().Save(rctx, channel, 1000)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
_ = ss.Channel().PermanentDelete(rctx, createdChannel.Id)
|
||||
}()
|
||||
|
||||
t.Run("base case", func(t *testing.T) {
|
||||
userId := model.NewId()
|
||||
scheduledPost := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() + 100000, // 100 seconds in the future
|
||||
}
|
||||
|
||||
createdScheduledPost, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
|
||||
defer func() {
|
||||
_ = ss.ScheduledPost().PermanentlyDeleteScheduledPosts([]string{createdScheduledPost.Id})
|
||||
}()
|
||||
|
||||
scheduledPostsFromDatabase, err := ss.ScheduledPost().GetScheduledPostsForUser(userId, "team_id_1")
|
||||
assert.NoError(t, err)
|
||||
require.Equal(t, 1, len(scheduledPostsFromDatabase))
|
||||
assert.Equal(t, scheduledPost.Id, scheduledPostsFromDatabase[0].Id)
|
||||
})
|
||||
|
||||
t.Run("scheduling in past SHOULD BE allowed", func(t *testing.T) {
|
||||
// this is only allowed in store layer and user won't be able to do so as the checks
|
||||
// in app layer would stop them.
|
||||
userId := model.NewId()
|
||||
scheduledPost := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() - 100000, // 100 seconds in the past
|
||||
}
|
||||
|
||||
_, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
_ = ss.ScheduledPost().PermanentlyDeleteScheduledPosts([]string{scheduledPost.Id})
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func testGetScheduledPosts(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
|
||||
t.Run("should handle no scheduled posts exist", func(t *testing.T) {
|
||||
apr2022 := time.Date(2100, time.April, 1, 1, 0, 0, 0, time.UTC)
|
||||
afterTime := time.Date(2100, time.March, 1, 1, 0, 0, 0, time.UTC)
|
||||
scheduledPosts, err := ss.ScheduledPost().GetPendingScheduledPosts(model.GetMillisForTime(apr2022), model.GetMillisForTime(afterTime), "", 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(scheduledPosts))
|
||||
})
|
||||
|
||||
t.Run("base case", func(t *testing.T) {
|
||||
// creating some sample scheduled posts
|
||||
// Create a time object for 1 January 2100, 1 AM
|
||||
jan2100 := time.Date(2100, time.January, 1, 1, 0, 0, 0, time.UTC)
|
||||
scheduledPost1 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillisForTime(jan2100),
|
||||
}
|
||||
|
||||
createdScheduledPost1, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost1)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost1.Id)
|
||||
|
||||
feb2100 := time.Date(2100, time.February, 1, 1, 0, 0, 0, time.UTC)
|
||||
scheduledPost2 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillisForTime(feb2100),
|
||||
}
|
||||
|
||||
createdScheduledPost2, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost2)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost2.Id)
|
||||
|
||||
mar2100 := time.Date(2100, time.March, 1, 1, 0, 0, 0, time.UTC)
|
||||
scheduledPost3 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillisForTime(mar2100),
|
||||
}
|
||||
|
||||
createdScheduledPost3, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost3)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost3.Id)
|
||||
|
||||
defer func() {
|
||||
_ = ss.ScheduledPost().PermanentlyDeleteScheduledPosts([]string{
|
||||
createdScheduledPost1.Id,
|
||||
createdScheduledPost2.Id,
|
||||
createdScheduledPost3.Id,
|
||||
})
|
||||
}()
|
||||
|
||||
apr2022 := time.Date(2100, time.April, 1, 1, 0, 0, 0, time.UTC)
|
||||
afterTime := time.Date(2100, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
scheduledPosts, err := ss.ScheduledPost().GetPendingScheduledPosts(model.GetMillisForTime(apr2022), model.GetMillisForTime(afterTime), "", 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 3, len(scheduledPosts))
|
||||
|
||||
mar2100midnight := time.Date(2100, time.March, 1, 0, 0, 0, 0, time.UTC)
|
||||
afterTime = time.Date(2100, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
scheduledPosts, err = ss.ScheduledPost().GetPendingScheduledPosts(model.GetMillisForTime(mar2100midnight), model.GetMillisForTime(afterTime), "", 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 2, len(scheduledPosts))
|
||||
|
||||
jan2100Midnight := time.Date(2100, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
afterTime = time.Date(2099, time.December, 31, 0, 0, 0, 0, time.UTC)
|
||||
scheduledPosts, err = ss.ScheduledPost().GetPendingScheduledPosts(model.GetMillisForTime(jan2100Midnight), model.GetMillisForTime(afterTime), "", 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(scheduledPosts))
|
||||
})
|
||||
}
|
||||
|
||||
func testPermanentlyDeleteScheduledPosts(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
|
||||
scheduledPostIDs := []string{}
|
||||
|
||||
scheduledPost := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() + 100000,
|
||||
}
|
||||
|
||||
createdScheduledPost, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
scheduledPostIDs = append(scheduledPostIDs, createdScheduledPost.Id)
|
||||
|
||||
scheduledPost = &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post 2",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() + 100000,
|
||||
}
|
||||
|
||||
createdScheduledPost, err = ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
scheduledPostIDs = append(scheduledPostIDs, createdScheduledPost.Id)
|
||||
|
||||
scheduledPost = &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post 3",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() + 100000,
|
||||
}
|
||||
|
||||
createdScheduledPost, err = ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
scheduledPostIDs = append(scheduledPostIDs, createdScheduledPost.Id)
|
||||
|
||||
scheduledPost = &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "this is a scheduled post 4",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() + 100000,
|
||||
}
|
||||
|
||||
createdScheduledPost, err = ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
scheduledPostIDs = append(scheduledPostIDs, createdScheduledPost.Id)
|
||||
|
||||
// verify 4 scheduled posts exist
|
||||
scheduledPosts, err := ss.ScheduledPost().GetPendingScheduledPosts(model.GetMillis()+50000000, model.GetMillis()-100000000, "", 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 4, len(scheduledPosts))
|
||||
|
||||
// now we'll delete all scheduled posts
|
||||
err = ss.ScheduledPost().PermanentlyDeleteScheduledPosts(scheduledPostIDs)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// now there should be no posts
|
||||
scheduledPosts, err = ss.ScheduledPost().GetPendingScheduledPosts(model.GetMillis()+50000000, model.GetMillis()-100000000, "", 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(scheduledPosts))
|
||||
}
|
||||
|
||||
func testUpdatedScheduledPost(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
|
||||
channel := &model.Channel{
|
||||
TeamId: "team_id_1",
|
||||
Type: model.ChannelTypeOpen,
|
||||
Name: "channel_name",
|
||||
DisplayName: "Channel Name",
|
||||
}
|
||||
|
||||
createdChannel, err := ss.Channel().Save(rctx, channel, 1000)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
_ = ss.Channel().PermanentDelete(rctx, createdChannel.Id)
|
||||
}()
|
||||
|
||||
t.Run("it should update only limited fields", func(t *testing.T) {
|
||||
userId := model.NewId()
|
||||
scheduledPost := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillis(),
|
||||
}
|
||||
|
||||
createdScheduledPost, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
|
||||
// now we'll update the scheduled post
|
||||
updateTimestamp := model.GetMillis()
|
||||
|
||||
fileID1 := model.NewId()
|
||||
fileID2 := model.NewId()
|
||||
|
||||
newScheduledAt := model.GetMillis()
|
||||
newUserId := model.NewId()
|
||||
|
||||
updateSchedulePost := &model.ScheduledPost{
|
||||
Id: createdScheduledPost.Id,
|
||||
ScheduledAt: newScheduledAt,
|
||||
ErrorCode: "test_error_code",
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
Message: "updated message",
|
||||
UpdateAt: updateTimestamp,
|
||||
UserId: newUserId, // this should not update
|
||||
ChannelId: model.NewId(), // this should not update
|
||||
FileIds: []string{fileID1, fileID2},
|
||||
Priority: model.StringInterface{
|
||||
"priority": "urgent",
|
||||
"requested_ack": false,
|
||||
"persistent_notifications": false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = ss.ScheduledPost().UpdatedScheduledPost(updateSchedulePost)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// now we'll get it and verify that intended fields updated and other fields did not
|
||||
userScheduledPosts, err := ss.ScheduledPost().GetScheduledPostsForUser(userId, channel.TeamId)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(userScheduledPosts))
|
||||
|
||||
// fields that should have changed
|
||||
assert.Equal(t, newScheduledAt, userScheduledPosts[0].ScheduledAt)
|
||||
assert.Equal(t, "test_error_code", userScheduledPosts[0].ErrorCode)
|
||||
assert.Equal(t, "updated message", userScheduledPosts[0].Message)
|
||||
assert.Equal(t, 2, len(userScheduledPosts[0].FileIds))
|
||||
assert.Equal(t, "urgent", userScheduledPosts[0].Priority["priority"])
|
||||
assert.Equal(t, false, userScheduledPosts[0].Priority["requested_ack"])
|
||||
assert.Equal(t, false, userScheduledPosts[0].Priority["persistent_notifications"])
|
||||
|
||||
// fields that should not have changed. Checking them against the original value
|
||||
assert.Equal(t, createdScheduledPost.Id, userScheduledPosts[0].Id)
|
||||
assert.Equal(t, userId, userScheduledPosts[0].UserId)
|
||||
assert.Equal(t, channel.Id, userScheduledPosts[0].ChannelId)
|
||||
})
|
||||
|
||||
t.Run("it should update old scheduled post", func(t *testing.T) {
|
||||
userId := model.NewId()
|
||||
scheduledPost := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: model.GetMillis() - (24 * 60 * 60 * 1000), // 1 day in the past
|
||||
}
|
||||
|
||||
createdScheduledPost, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost.Id)
|
||||
|
||||
// now we'll update the scheduled post
|
||||
processedAt := model.GetMillis()
|
||||
scheduledPost.ProcessedAt = processedAt
|
||||
scheduledPost.ErrorCode = model.ScheduledPostErrorUnknownError
|
||||
|
||||
err = ss.ScheduledPost().UpdatedScheduledPost(scheduledPost)
|
||||
assert.NoError(t, err)
|
||||
|
||||
updatedScheduledPost, err := ss.ScheduledPost().Get(scheduledPost.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, processedAt, updatedScheduledPost.ProcessedAt)
|
||||
assert.Equal(t, model.ScheduledPostErrorUnknownError, updatedScheduledPost.ErrorCode)
|
||||
})
|
||||
}
|
||||
|
||||
func testUpdateOldScheduledPosts(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
|
||||
setupScheduledPosts := func(baseTime int64, userId, teamId string) func() {
|
||||
channel := &model.Channel{
|
||||
TeamId: teamId,
|
||||
Type: model.ChannelTypeOpen,
|
||||
Name: "channel_name",
|
||||
DisplayName: "Channel Name",
|
||||
}
|
||||
createdChannel, err := ss.Channel().Save(rctx, channel, 1000)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Scheduled post 1
|
||||
scheduledPost1 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is a scheduled post",
|
||||
},
|
||||
ScheduledAt: baseTime + 86400000, // 1 day in the future
|
||||
}
|
||||
|
||||
createdScheduledPost1, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost1)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost1.Id)
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Scheduled post 2
|
||||
scheduledPost2 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is second scheduled post",
|
||||
},
|
||||
ScheduledAt: baseTime + (2 * 86400000), // 2 days in the future
|
||||
}
|
||||
|
||||
createdScheduledPost2, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost2)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost2.Id)
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Scheduled post 3
|
||||
scheduledPost3 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is third scheduled post",
|
||||
},
|
||||
ScheduledAt: baseTime + (3 * 86400000), // 3 days in the future
|
||||
}
|
||||
|
||||
createdScheduledPost3, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost3)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost3.Id)
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Scheduled post 4
|
||||
scheduledPost4 := &model.ScheduledPost{
|
||||
Draft: model.Draft{
|
||||
CreateAt: model.GetMillis(),
|
||||
UserId: userId,
|
||||
ChannelId: createdChannel.Id,
|
||||
Message: "this is fourth scheduled post",
|
||||
},
|
||||
ScheduledAt: baseTime + (4 * 86400000), // 4 days in the future
|
||||
}
|
||||
|
||||
createdScheduledPost4, err := ss.ScheduledPost().CreateScheduledPost(scheduledPost4)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, createdScheduledPost4.Id)
|
||||
|
||||
return func() {
|
||||
_ = ss.ScheduledPost().PermanentlyDeleteScheduledPosts([]string{
|
||||
createdScheduledPost1.Id,
|
||||
createdScheduledPost2.Id,
|
||||
createdScheduledPost3.Id,
|
||||
createdScheduledPost4.Id,
|
||||
})
|
||||
|
||||
_ = ss.Channel().PermanentDelete(rctx, createdChannel.Id)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("should update only old scheduled posts", func(t *testing.T) {
|
||||
now := model.GetMillis()
|
||||
userId := model.NewId()
|
||||
teamId := model.NewId()
|
||||
cleanup := setupScheduledPosts(now, userId, teamId)
|
||||
defer cleanup()
|
||||
|
||||
err := ss.ScheduledPost().UpdateOldScheduledPosts(now + 2.5*86400000) // marking all posts older than 2 days from now
|
||||
assert.NoError(t, err)
|
||||
|
||||
scheduledPosts, err := ss.ScheduledPost().GetScheduledPostsForUser(userId, teamId)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 4, len(scheduledPosts))
|
||||
assert.Equal(t, model.ScheduledPostErrorUnableToSend, scheduledPosts[0].ErrorCode)
|
||||
assert.Equal(t, model.ScheduledPostErrorUnableToSend, scheduledPosts[1].ErrorCode)
|
||||
assert.Equal(t, "", scheduledPosts[2].ErrorCode)
|
||||
assert.Equal(t, "", scheduledPosts[3].ErrorCode)
|
||||
})
|
||||
}
|
||||
@@ -65,6 +65,7 @@ type Store struct {
|
||||
PostPersistentNotificationStore mocks.PostPersistentNotificationStore
|
||||
DesktopTokensStore mocks.DesktopTokensStore
|
||||
ChannelBookmarkStore mocks.ChannelBookmarkStore
|
||||
ScheduledPostStore mocks.ScheduledPostStore
|
||||
}
|
||||
|
||||
func (s *Store) SetContext(context context.Context) { s.context = context }
|
||||
@@ -117,6 +118,7 @@ func (s *Store) Group() store.GroupStore { return &s.GroupSt
|
||||
func (s *Store) LinkMetadata() store.LinkMetadataStore { return &s.LinkMetadataStore }
|
||||
func (s *Store) SharedChannel() store.SharedChannelStore { return &s.SharedChannelStore }
|
||||
func (s *Store) PostPriority() store.PostPriorityStore { return &s.PostPriorityStore }
|
||||
func (s *Store) ScheduledPost() store.ScheduledPostStore { return &s.ScheduledPostStore }
|
||||
func (s *Store) PostAcknowledgement() store.PostAcknowledgementStore {
|
||||
return &s.PostAcknowledgementStore
|
||||
}
|
||||
@@ -188,5 +190,6 @@ func (s *Store) AssertExpectations(t mock.TestingT) bool {
|
||||
&s.PostPersistentNotificationStore,
|
||||
&s.DesktopTokensStore,
|
||||
&s.ChannelBookmarkStore,
|
||||
&s.ScheduledPostStore,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ type TimerLayer struct {
|
||||
RemoteClusterStore store.RemoteClusterStore
|
||||
RetentionPolicyStore store.RetentionPolicyStore
|
||||
RoleStore store.RoleStore
|
||||
ScheduledPostStore store.ScheduledPostStore
|
||||
SchemeStore store.SchemeStore
|
||||
SessionStore store.SessionStore
|
||||
SharedChannelStore store.SharedChannelStore
|
||||
@@ -190,6 +191,10 @@ func (s *TimerLayer) Role() store.RoleStore {
|
||||
return s.RoleStore
|
||||
}
|
||||
|
||||
func (s *TimerLayer) ScheduledPost() store.ScheduledPostStore {
|
||||
return s.ScheduledPostStore
|
||||
}
|
||||
|
||||
func (s *TimerLayer) Scheme() store.SchemeStore {
|
||||
return s.SchemeStore
|
||||
}
|
||||
@@ -401,6 +406,11 @@ type TimerLayerRoleStore struct {
|
||||
Root *TimerLayer
|
||||
}
|
||||
|
||||
type TimerLayerScheduledPostStore struct {
|
||||
store.ScheduledPostStore
|
||||
Root *TimerLayer
|
||||
}
|
||||
|
||||
type TimerLayerSchemeStore struct {
|
||||
store.SchemeStore
|
||||
Root *TimerLayer
|
||||
@@ -7753,6 +7763,134 @@ func (s *TimerLayerRoleStore) Save(role *model.Role) (*model.Role, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) CreateScheduledPost(scheduledPost *model.ScheduledPost) (*model.ScheduledPost, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.ScheduledPostStore.CreateScheduledPost(scheduledPost)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.CreateScheduledPost", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) Get(scheduledPostId string) (*model.ScheduledPost, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.ScheduledPostStore.Get(scheduledPostId)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.Get", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) GetMaxMessageSize() int {
|
||||
start := time.Now()
|
||||
|
||||
result := s.ScheduledPostStore.GetMaxMessageSize()
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if true {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.GetMaxMessageSize", success, elapsed)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) GetPendingScheduledPosts(beforeTime int64, afterTime int64, lastScheduledPostId string, perPage uint64) ([]*model.ScheduledPost, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.ScheduledPostStore.GetPendingScheduledPosts(beforeTime, afterTime, lastScheduledPostId, perPage)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.GetPendingScheduledPosts", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) GetScheduledPostsForUser(userId string, teamId string) ([]*model.ScheduledPost, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.ScheduledPostStore.GetScheduledPostsForUser(userId, teamId)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.GetScheduledPostsForUser", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) PermanentlyDeleteScheduledPosts(scheduledPostIDs []string) error {
|
||||
start := time.Now()
|
||||
|
||||
err := s.ScheduledPostStore.PermanentlyDeleteScheduledPosts(scheduledPostIDs)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.PermanentlyDeleteScheduledPosts", success, elapsed)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) UpdateOldScheduledPosts(beforeTime int64) error {
|
||||
start := time.Now()
|
||||
|
||||
err := s.ScheduledPostStore.UpdateOldScheduledPosts(beforeTime)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.UpdateOldScheduledPosts", success, elapsed)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *TimerLayerScheduledPostStore) UpdatedScheduledPost(scheduledPost *model.ScheduledPost) error {
|
||||
start := time.Now()
|
||||
|
||||
err := s.ScheduledPostStore.UpdatedScheduledPost(scheduledPost)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ScheduledPostStore.UpdatedScheduledPost", success, elapsed)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *TimerLayerSchemeStore) CountByScope(scope string) (int64, error) {
|
||||
start := time.Now()
|
||||
|
||||
@@ -12313,6 +12451,7 @@ func New(childStore store.Store, metrics einterfaces.MetricsInterface) *TimerLay
|
||||
newStore.RemoteClusterStore = &TimerLayerRemoteClusterStore{RemoteClusterStore: childStore.RemoteCluster(), Root: &newStore}
|
||||
newStore.RetentionPolicyStore = &TimerLayerRetentionPolicyStore{RetentionPolicyStore: childStore.RetentionPolicy(), Root: &newStore}
|
||||
newStore.RoleStore = &TimerLayerRoleStore{RoleStore: childStore.Role(), Root: &newStore}
|
||||
newStore.ScheduledPostStore = &TimerLayerScheduledPostStore{ScheduledPostStore: childStore.ScheduledPost(), Root: &newStore}
|
||||
newStore.SchemeStore = &TimerLayerSchemeStore{SchemeStore: childStore.Scheme(), Root: &newStore}
|
||||
newStore.SessionStore = &TimerLayerSessionStore{SessionStore: childStore.Session(), Root: &newStore}
|
||||
newStore.SharedChannelStore = &TimerLayerSharedChannelStore{SharedChannelStore: childStore.SharedChannel(), Root: &newStore}
|
||||
|
||||
Ссылка в новой задаче
Block a user