summaryrefslogtreecommitdiffstats
path: root/store/sqlstore/channel_store.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-11-28 15:02:56 -0600
committerChristopher Speller <crspeller@gmail.com>2017-11-28 13:02:56 -0800
commitb87fae646a624507f5b2c1270cae1d3585f589ac (patch)
treed81b683cda11b08ed04d7e2431b04a84a715d851 /store/sqlstore/channel_store.go
parent27ba68a7894d5204b8d75dc7353774977d62fa15 (diff)
downloadchat-b87fae646a624507f5b2c1270cae1d3585f589ac.tar.gz
chat-b87fae646a624507f5b2c1270cae1d3585f589ac.tar.bz2
chat-b87fae646a624507f5b2c1270cae1d3585f589ac.zip
PLT-5458: If someone posts a channel link to channel_A that you don't belong to, it doesn't render properly (#7833)
* add channel link hints to post props * optimization * update regex, add unit test * fix rebase issue
Diffstat (limited to 'store/sqlstore/channel_store.go')
-rw-r--r--store/sqlstore/channel_store.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go
index 7328e2017..9869b3720 100644
--- a/store/sqlstore/channel_store.go
+++ b/store/sqlstore/channel_store.go
@@ -586,6 +586,65 @@ func (s SqlChannelStore) GetByName(teamId string, name string, allowFromCache bo
return s.getByName(teamId, name, false, allowFromCache)
}
+func (s SqlChannelStore) GetByNames(teamId string, names []string, allowFromCache bool) store.StoreChannel {
+ return store.Do(func(result *store.StoreResult) {
+ var channels []*model.Channel
+
+ if allowFromCache {
+ var misses []string
+ visited := make(map[string]struct{})
+ for _, name := range names {
+ if _, ok := visited[name]; ok {
+ continue
+ }
+ visited[name] = struct{}{}
+ if cacheItem, ok := channelByNameCache.Get(teamId + name); ok {
+ if s.metrics != nil {
+ s.metrics.IncrementMemCacheHitCounter("Channel By Name")
+ }
+ channels = append(channels, cacheItem.(*model.Channel))
+ } else {
+ if s.metrics != nil {
+ s.metrics.IncrementMemCacheMissCounter("Channel By Name")
+ }
+ misses = append(misses, name)
+ }
+ }
+ names = misses
+ }
+
+ if len(names) > 0 {
+ props := map[string]interface{}{}
+ var namePlaceholders []string
+ for _, name := range names {
+ key := fmt.Sprintf("Name%v", len(namePlaceholders))
+ props[key] = name
+ namePlaceholders = append(namePlaceholders, ":"+key)
+ }
+
+ var query string
+ if teamId == "" {
+ query = `SELECT * FROM Channels WHERE Name IN (` + strings.Join(namePlaceholders, ", ") + `) AND DeleteAt = 0`
+ } else {
+ props["TeamId"] = teamId
+ query = `SELECT * FROM Channels WHERE Name IN (` + strings.Join(namePlaceholders, ", ") + `) AND TeamId = :TeamId AND DeleteAt = 0`
+ }
+
+ var dbChannels []*model.Channel
+ if _, err := s.GetReplica().Select(&dbChannels, query, props); err != nil && err != sql.ErrNoRows {
+ result.Err = model.NewAppError("SqlChannelStore.GetByName", "store.sql_channel.get_by_name.existing.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+ for _, channel := range dbChannels {
+ channelByNameCache.AddWithExpiresInSecs(teamId+channel.Name, channel, CHANNEL_CACHE_SEC)
+ channels = append(channels, channel)
+ }
+ }
+
+ result.Data = channels
+ })
+}
+
func (s SqlChannelStore) GetByNameIncludeDeleted(teamId string, name string, allowFromCache bool) store.StoreChannel {
return s.getByName(teamId, name, true, allowFromCache)
}