func (cs BoltChannelStore) Update(channel *model.Channel) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		channelJson := channel.ToJson()
		err := cs.channelsBucket.Put([]byte(channel.Id), []byte(channelJson))
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.Update", "Error while update", "")
		} else {
			result.Data = channelJson
		}
		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
func (cs BoltChannelStore) SaveDirectChannel(channel *model.Channel, mb1 *model.ChannelMember, mb2 *model.ChannelMember) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		channel.PreSave()
		mb1.ChannelId = channel.Id
		mb2.ChannelId = channel.Id
		err := cs.channelsBucket.Put([]byte(channel.Id), []byte(channel.ToJson()))
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel", "")
		} else {

			items, err := cs.channelMembersBucket.Items()
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel", "")
			}

			count := len(items)
			err = cs.channelMembersBucket.Put([]byte(strconv.Itoa(count+1)), []byte(mb1.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel member", "")
			}
			err = cs.channelMembersBucket.Put([]byte(strconv.Itoa(count+2)), []byte(mb2.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel member", "")
			}
		}

		result.Data = channel

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Beispiel #3
0
func createDirectChannel(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	sessionContext := context.Get(r, "context").(Context)
	props := model.MapFromJson(r.Body)

	otherUserId := string(props["user_id"])

	uc := <-Srv.Store.User().Get(otherUserId)

	if uc.Err != nil {
		sessionContext.SetInvalidParam("create DM Channel", "Channel not valid")
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	m1 := &model.ChannelMember{
		UserId: sessionContext.User.Id,
		Role:   model.CHANNEL_ROLE_ADMIN,
	}

	m2 := &model.ChannelMember{
		UserId: otherUserId,
		Role:   model.CHANNEL_ROLE_USER,
	}

	channel := new(model.Channel)
	channel.Name = model.GetDMNameFromIds(m1.UserId, m2.UserId)

	cc := <-Srv.Store.Channel().SaveDirectChannel(channel, m1, m2)
	if cc.Err != nil {
		sessionContext.SetInvalidParam("create DM Channel", "Channel not valid")
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	createdChannel := cc.Data.(*model.Channel)
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(createdChannel.ToJson()))
}
func (cs BoltChannelStore) Save(channel *model.Channel) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		if channel.Type == model.CHANNEL_DIRECT {
			result.Err = model.NewAppError("BoltChannelStore.Save", "Use Direct channel save to save direct channel", "")
		} else {
			channel.PreSave()
			err := cs.channelsBucket.Put([]byte(channel.Id), []byte(channel.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.Save", "Error while save", "")
			} else {
				result.Data = channel
			}
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}