diff options
Diffstat (limited to 'store/sql_channel_store.go')
-rw-r--r-- | store/sql_channel_store.go | 102 |
1 files changed, 98 insertions, 4 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index ff1716957..d72722f7c 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -309,18 +309,79 @@ func (s SqlChannelStore) extraUpdated(channel *model.Channel) StoreChannel { return storeChannel } +func (s SqlChannelStore) GetChannelUnread(channelId, userId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var unreadChannel model.ChannelUnread + err := s.GetReplica().SelectOne(&unreadChannel, + `SELECT + Channels.TeamId TeamId, Channels.Id ChannelId, (Channels.TotalMsgCount - ChannelMembers.MsgCount) MsgCount, ChannelMembers.MentionCount MentionCount, ChannelMembers.NotifyProps NotifyProps + FROM + Channels, ChannelMembers + WHERE + Id = ChannelId + AND Id = :ChannelId + AND UserId = :UserId + AND DeleteAt = 0`, + map[string]interface{}{"ChannelId": channelId, "UserId": userId}) + + if err != nil { + result.Err = model.NewAppError("SqlChannelStore.GetChannelUnread", "store.sql_channel.get_unread.app_error", nil, "channelId="+channelId+" "+err.Error(), http.StatusInternalServerError) + if err == sql.ErrNoRows { + result.Err.StatusCode = http.StatusNotFound + } + } else { + result.Data = &unreadChannel + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (us SqlChannelStore) InvalidateChannel(id string) { channelCache.Remove(id) } func (us SqlChannelStore) InvalidateChannelByName(teamId, name string) { - channelCache.Remove(teamId + name) + channelByNameCache.Remove(teamId + name) } func (s SqlChannelStore) Get(id string, allowFromCache bool) StoreChannel { return s.get(id, false, allowFromCache) } +func (s SqlChannelStore) GetPinnedPosts(channelId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + pl := &model.PostList{} + + var posts []*model.Post + if _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE IsPinned = true AND ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt ASC", map[string]interface{}{"ChannelId": channelId}); err != nil { + result.Err = model.NewLocAppError("SqlPostStore.GetPinnedPosts", "store.sql_channel.pinned_posts.app_error", nil, err.Error()) + } else { + for _, post := range posts { + pl.AddPost(post) + pl.AddOrder(post.Id) + } + } + + result.Data = pl + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) GetFromMaster(id string) StoreChannel { return s.get(id, true, false) } @@ -360,9 +421,9 @@ func (s SqlChannelStore) get(id string, master bool, allowFromCache bool) StoreC } if obj, err := db.Get(model.Channel{}, id); err != nil { - result.Err = model.NewLocAppError("SqlChannelStore.Get", "store.sql_channel.get.find.app_error", nil, "id="+id+", "+err.Error()) + result.Err = model.NewAppError("SqlChannelStore.Get", "store.sql_channel.get.find.app_error", nil, "id="+id+", "+err.Error(), http.StatusInternalServerError) } else if obj == nil { - result.Err = model.NewAppError("SqlChannelStore.Get", "store.sql_channel.get.existing.app_error", nil, "id="+id, http.StatusBadRequest) + result.Err = model.NewAppError("SqlChannelStore.Get", "store.sql_channel.get.existing.app_error", nil, "id="+id, http.StatusNotFound) } else { result.Data = obj.(*model.Channel) channelCache.AddWithExpiresInSecs(id, obj.(*model.Channel), CHANNEL_CACHE_SEC) @@ -524,6 +585,40 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset in return storeChannel } +func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + data := &model.ChannelList{} + _, err := s.GetReplica().Select(data, + `SELECT + * + FROM + Channels + WHERE + TeamId = :TeamId + AND Type = 'O' + AND DeleteAt = 0 + ORDER BY DisplayName + LIMIT :Limit + OFFSET :Offset`, + map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset}) + + if err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.GetPublicChannelsForTeam", "store.sql_channel.get_public_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error()) + } else { + result.Data = data + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + type channelIdWithCountAndUpdateAt struct { Id string TotalMsgCount int64 @@ -624,7 +719,6 @@ func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bo } } } - if err := s.GetReplica().SelectOne(&channel, query, map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil { if err == sql.ErrNoRows { result.Err = model.NewLocAppError("SqlChannelStore.GetByName", MISSING_CHANNEL_ERROR, nil, "teamId="+teamId+", "+"name="+name+", "+err.Error()) |