github.com/mattermost/mattermost-server/v5@v5.39.3/store/sqlstore/product_notices_store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package sqlstore 5 6 import ( 7 "time" 8 9 sq "github.com/Masterminds/squirrel" 10 "github.com/pkg/errors" 11 12 "github.com/mattermost/mattermost-server/v5/model" 13 "github.com/mattermost/mattermost-server/v5/store" 14 ) 15 16 type SqlProductNoticesStore struct { 17 *SqlStore 18 } 19 20 func newSqlProductNoticesStore(sqlStore *SqlStore) store.ProductNoticesStore { 21 s := SqlProductNoticesStore{sqlStore} 22 23 for _, db := range sqlStore.GetAllConns() { 24 table := db.AddTableWithName(model.ProductNoticeViewState{}, "ProductNoticeViewState").SetKeys(false, "UserId", "NoticeId") 25 table.ColMap("UserId").SetMaxSize(26) 26 table.ColMap("NoticeId").SetMaxSize(26) 27 } 28 29 return s 30 } 31 32 func (s SqlProductNoticesStore) createIndexesIfNotExists() { 33 s.CreateIndexIfNotExists("idx_notice_views_timestamp", "ProductNoticeViewState", "Timestamp") 34 s.CreateIndexIfNotExists("idx_notice_views_notice_id", "ProductNoticeViewState", "NoticeId") 35 } 36 37 func (s SqlProductNoticesStore) Clear(notices []string) error { 38 sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.Eq{"NoticeId": notices}).ToSql() 39 if _, err := s.GetMaster().Exec(sql, args...); err != nil { 40 return errors.Wrapf(err, "failed to delete records from ProductNoticeViewState") 41 } 42 return nil 43 } 44 45 func (s SqlProductNoticesStore) ClearOldNotices(currentNotices *model.ProductNotices) error { 46 var notices []string 47 for _, currentNotice := range *currentNotices { 48 notices = append(notices, currentNotice.ID) 49 } 50 sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.NotEq{"NoticeId": notices}).ToSql() 51 if _, err := s.GetMaster().Exec(sql, args...); err != nil { 52 return errors.Wrapf(err, "failed to delete records from ProductNoticeViewState") 53 } 54 return nil 55 } 56 57 func (s SqlProductNoticesStore) View(userId string, notices []string) error { 58 transaction, err := s.GetMaster().Begin() 59 if err != nil { 60 return errors.Wrap(err, "begin_transaction") 61 } 62 defer finalizeTransaction(transaction) 63 64 var noticeStates []model.ProductNoticeViewState 65 sql, args, _ := s.getQueryBuilder(). 66 Select("*"). 67 From("ProductNoticeViewState"). 68 Where(sq.And{sq.Eq{"UserId": userId}, sq.Eq{"NoticeId": notices}}). 69 ToSql() 70 if _, err := transaction.Select(¬iceStates, sql, args...); err != nil { 71 return errors.Wrapf(err, "failed to get ProductNoticeViewState with userId=%s", userId) 72 } 73 74 now := time.Now().UTC().Unix() 75 76 // update existing records 77 for i := range noticeStates { 78 noticeStates[i].Viewed += 1 79 noticeStates[i].Timestamp = now 80 if _, err := transaction.Update(¬iceStates[i]); err != nil { 81 return errors.Wrapf(err, "failed to update ProductNoticeViewState") 82 } 83 } 84 85 // add new ones 86 haveNoticeState := func(n string) bool { 87 for _, ns := range noticeStates { 88 if ns.NoticeId == n { 89 return true 90 } 91 } 92 return false 93 } 94 95 for _, noticeId := range notices { 96 if !haveNoticeState(noticeId) { 97 if err := transaction.Insert(&model.ProductNoticeViewState{ 98 UserId: userId, 99 NoticeId: noticeId, 100 Viewed: 1, 101 Timestamp: now, 102 }); err != nil { 103 return errors.Wrapf(err, "failed to insert ProductNoticeViewState") 104 } 105 } 106 } 107 108 if err := transaction.Commit(); err != nil { 109 return errors.Wrap(err, "commit_transaction") 110 } 111 112 return nil 113 } 114 115 func (s SqlProductNoticesStore) GetViews(userId string) ([]model.ProductNoticeViewState, error) { 116 var noticeStates []model.ProductNoticeViewState 117 sql, args, _ := s.getQueryBuilder().Select("*").From("ProductNoticeViewState").Where(sq.Eq{"UserId": userId}).ToSql() 118 if _, err := s.GetReplica().Select(¬iceStates, sql, args...); err != nil { 119 return nil, errors.Wrapf(err, "failed to get ProductNoticeViewState with userId=%s", userId) 120 } 121 return noticeStates, nil 122 }