Ejemplo n.º 1
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()))
}