github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/link_metadata_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  	"database/sql"
     8  	"net/http"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  )
    13  
    14  type SqlLinkMetadataStore struct {
    15  	SqlStore
    16  }
    17  
    18  func NewSqlLinkMetadataStore(sqlStore SqlStore) store.LinkMetadataStore {
    19  	s := &SqlLinkMetadataStore{sqlStore}
    20  
    21  	for _, db := range sqlStore.GetAllConns() {
    22  		table := db.AddTableWithName(model.LinkMetadata{}, "LinkMetadata").SetKeys(false, "Hash")
    23  		table.ColMap("URL").SetMaxSize(2048)
    24  		table.ColMap("Type").SetMaxSize(16)
    25  		table.ColMap("Data").SetMaxSize(4096)
    26  	}
    27  
    28  	return s
    29  }
    30  
    31  func (s SqlLinkMetadataStore) CreateIndexesIfNotExists() {
    32  	if s.DriverName() == model.DATABASE_DRIVER_MYSQL {
    33  		s.CreateCompositeIndexIfNotExists("idx_link_metadata_url_timestamp", "LinkMetadata", []string{"URL(512)", "Timestamp"})
    34  	} else {
    35  		s.CreateCompositeIndexIfNotExists("idx_link_metadata_url_timestamp", "LinkMetadata", []string{"URL", "Timestamp"})
    36  	}
    37  }
    38  
    39  func (s SqlLinkMetadataStore) Save(metadata *model.LinkMetadata) store.StoreChannel {
    40  	return store.Do(func(result *store.StoreResult) {
    41  		if result.Err = metadata.IsValid(); result.Err != nil {
    42  			return
    43  		}
    44  
    45  		metadata.PreSave()
    46  
    47  		err := s.GetMaster().Insert(metadata)
    48  		if err != nil && !IsUniqueConstraintError(err, []string{"PRIMARY", "linkmetadata_pkey"}) {
    49  			result.Err = model.NewAppError("SqlLinkMetadataStore.Save", "store.sql_link_metadata.save.app_error", nil, "url="+metadata.URL+", "+err.Error(), http.StatusInternalServerError)
    50  			return
    51  		}
    52  
    53  		result.Data = metadata
    54  	})
    55  }
    56  
    57  func (s SqlLinkMetadataStore) Get(url string, timestamp int64) store.StoreChannel {
    58  	return store.Do(func(result *store.StoreResult) {
    59  		var metadata *model.LinkMetadata
    60  
    61  		err := s.GetReplica().SelectOne(&metadata,
    62  			`SELECT
    63  				*
    64  			FROM
    65  				LinkMetadata
    66  			WHERE
    67  				URL = :URL
    68  				AND Timestamp = :Timestamp`, map[string]interface{}{"URL": url, "Timestamp": timestamp})
    69  		if err != nil {
    70  			result.Err = model.NewAppError("SqlLinkMetadataStore.Get", "store.sql_link_metadata.get.app_error", nil, "url="+url+", "+err.Error(), http.StatusInternalServerError)
    71  
    72  			if err == sql.ErrNoRows {
    73  				result.Err.StatusCode = http.StatusNotFound
    74  			}
    75  
    76  			return
    77  		}
    78  
    79  		err = metadata.DeserializeDataToConcreteType()
    80  		if err != nil {
    81  			result.Err = model.NewAppError("SqlLinkMetadataStore.Get", "store.sql_link_metadata.get.app_error", nil, "url="+url+", "+err.Error(), http.StatusInternalServerError)
    82  
    83  			return
    84  		}
    85  
    86  		result.Data = metadata
    87  	})
    88  }