func (cs BoltChannelStore) GetChannels(userId string) StoreChannel { storeChannel := make(StoreChannel) go func() { var result StoreResult var channel *model.Channel items, err := cs.channelsBucket.Items() if err != nil || len(userId) <= 0 { result.Err = model.NewAppError("BoltChannelStore.GetChannels", "Error while get items", "") } else { data := make(map[*model.Channel]bool) for _, item := range items { channel = model.ChannelFromJson(strings.NewReader(string(item.Value))) if channel != nil { data[channel] = true } } result.Data = data } storeChannel <- result close(storeChannel) return }() return storeChannel }
func (cs BoltChannelStore) GetByName(name string) StoreChannel { storeChannel := make(StoreChannel) go func() { var result StoreResult items, err := cs.channelsBucket.Items() if err != nil { result.Err = model.NewAppError("BoltChannelStore.GetByName", "Error while get by name", "") storeChannel <- result close(storeChannel) return } for _, item := range items { channel := model.ChannelFromJson(strings.NewReader(string(item.Value))) if channel.Name == name { result.Data = channel storeChannel <- result close(storeChannel) return } } }() return storeChannel }
func updateChannel(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { sessionContext := context.Get(r, "context").(Context) channel := model.ChannelFromJson(r.Body) if channel == nil { sessionContext.SetInvalidParam("create Channel", "") w.WriteHeader(http.StatusBadRequest) return } if !channel.IsValid() { sessionContext.SetInvalidParam("create Channel", "Channel not valid") w.WriteHeader(http.StatusBadRequest) return } result := <-Srv.Store.Channel().Save(channel) if result.Err != nil { sessionContext.SetInvalidParam("create Channel", "") w.WriteHeader(http.StatusBadRequest) return } w.WriteHeader(http.StatusOK) createdChannel := result.Data.(*model.Channel) w.Write([]byte(createdChannel.ToJson())) }
func createChannel(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { sessionContext := context.Get(r, "context").(Context) channel := model.ChannelFromJson(r.Body) if channel == nil { sessionContext.SetInvalidParam("create Channel", "") w.WriteHeader(http.StatusBadRequest) return } if !channel.IsValid() { sessionContext.SetInvalidParam("create Channel", "Channel not valid") w.WriteHeader(http.StatusBadRequest) return } if channel.Type == model.CHANNEL_DIRECT { sessionContext.SetInvalidParam("createDirectChannel", "Must use createDirectChannel api service for direct message channel creation") w.WriteHeader(http.StatusBadRequest) return } if strings.Index(channel.Name, "__") > 0 { sessionContext.SetInvalidParam("createDirectChannel", "Invalid character '__' in channel name for non-direct channel") w.WriteHeader(http.StatusBadRequest) return } result := <-Srv.Store.Channel().Save(channel) if result.Err != nil { sessionContext.SetInvalidParam("create Channel", "") w.WriteHeader(http.StatusBadRequest) return } firstMember := model.ChannelMember{UserId: sessionContext.User.Id, ChannelId: channel.Id, Role: model.CHANNEL_ROLE_ADMIN} rm := <-Srv.Store.Channel().SaveMember(&firstMember) if rm.Err != nil { sessionContext.SetInvalidParam("create Channel", "create member") w.WriteHeader(http.StatusBadRequest) return } w.WriteHeader(http.StatusOK) createdChannel := result.Data.(*model.Channel) w.Write([]byte(createdChannel.ToJson())) }
func (cs BoltChannelStore) Get(channelId string) StoreChannel { storeChannel := make(StoreChannel) go func() { var result StoreResult channelJson, err := cs.channelsBucket.Get([]byte(channelId)) if err != nil { result.Err = model.NewAppError("BoltChannelStore.Get", "Error while get", "") } else { result.Data = model.ChannelFromJson(strings.NewReader(string(channelJson))) } storeChannel <- result close(storeChannel) return }() return storeChannel }