github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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_user_id", "ProductNoticeViewState", "UserId")
    35  	s.CreateIndexIfNotExists("idx_notice_views_notice_id", "ProductNoticeViewState", "NoticeId")
    36  
    37  	s.CreateCompositeIndexIfNotExists("idx_notice_views_user_notice", "ProductNoticeViewState", []string{"UserId", "NoticeId"})
    38  
    39  }
    40  
    41  func (s SqlProductNoticesStore) Clear(notices []string) error {
    42  	sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.Eq{"NoticeId": notices}).ToSql()
    43  	if _, err := s.GetMaster().Exec(sql, args...); err != nil {
    44  		return errors.Wrapf(err, "failed to delete records from ProductNoticeViewState")
    45  	}
    46  	return nil
    47  }
    48  
    49  func (s SqlProductNoticesStore) ClearOldNotices(currentNotices *model.ProductNotices) error {
    50  	var notices []string
    51  	for _, currentNotice := range *currentNotices {
    52  		notices = append(notices, currentNotice.ID)
    53  	}
    54  	sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.NotEq{"NoticeId": notices}).ToSql()
    55  	if _, err := s.GetMaster().Exec(sql, args...); err != nil {
    56  		return errors.Wrapf(err, "failed to delete records from ProductNoticeViewState")
    57  	}
    58  	return nil
    59  }
    60  
    61  func (s SqlProductNoticesStore) View(userId string, notices []string) error {
    62  	transaction, err := s.GetMaster().Begin()
    63  	if err != nil {
    64  		return errors.Wrap(err, "begin_transaction")
    65  	}
    66  	defer finalizeTransaction(transaction)
    67  
    68  	var noticeStates []model.ProductNoticeViewState
    69  	sql, args, _ := s.getQueryBuilder().
    70  		Select("*").
    71  		From("ProductNoticeViewState").
    72  		Where(sq.And{sq.Eq{"UserId": userId}, sq.Eq{"NoticeId": notices}}).
    73  		ToSql()
    74  	if _, err := transaction.Select(&noticeStates, sql, args...); err != nil {
    75  		return errors.Wrapf(err, "failed to get ProductNoticeViewState with userId=%s", userId)
    76  	}
    77  
    78  	now := time.Now().UTC().Unix()
    79  
    80  	// update existing records
    81  	for i := range noticeStates {
    82  		noticeStates[i].Viewed += 1
    83  		noticeStates[i].Timestamp = now
    84  		if _, err := transaction.Update(&noticeStates[i]); err != nil {
    85  			return errors.Wrapf(err, "failed to update ProductNoticeViewState")
    86  		}
    87  	}
    88  
    89  	// add new ones
    90  	haveNoticeState := func(n string) bool {
    91  		for _, ns := range noticeStates {
    92  			if ns.NoticeId == n {
    93  				return true
    94  			}
    95  		}
    96  		return false
    97  	}
    98  
    99  	for _, noticeId := range notices {
   100  		if !haveNoticeState(noticeId) {
   101  			if err := transaction.Insert(&model.ProductNoticeViewState{
   102  				UserId:    userId,
   103  				NoticeId:  noticeId,
   104  				Viewed:    1,
   105  				Timestamp: now,
   106  			}); err != nil {
   107  				return errors.Wrapf(err, "failed to insert ProductNoticeViewState")
   108  			}
   109  		}
   110  	}
   111  
   112  	if err := transaction.Commit(); err != nil {
   113  		return errors.Wrap(err, "commit_transaction")
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func (s SqlProductNoticesStore) GetViews(userId string) ([]model.ProductNoticeViewState, error) {
   120  	var noticeStates []model.ProductNoticeViewState
   121  	sql, args, _ := s.getQueryBuilder().Select("*").From("ProductNoticeViewState").Where(sq.Eq{"UserId": userId}).ToSql()
   122  	if _, err := s.GetReplica().Select(&noticeStates, sql, args...); err != nil {
   123  		return nil, errors.Wrapf(err, "failed to get ProductNoticeViewState with userId=%s", userId)
   124  	}
   125  	return noticeStates, nil
   126  }