func login(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { sessionContext := context.Get(r, "context").(Context) props := model.MapFromJson(r.Body) result := <-Srv.Store.User().GetByLogin(props["login"]) if result.Err != nil { sessionContext.SetInvalidParam("get User by Login", "Login = "******"login"]) w.WriteHeader(sessionContext.Err.StatusCode) return } user := result.Data.(*model.User) if user.ComparePassword(props["password"]) { w.WriteHeader(http.StatusOK) user.SetToken() user.Sanitize() msg := &model.Message{} msg.UserId = user.Id msg.Action = model.ACTION_NEW_USER msg.Props = make(map[string]string) PublishAndForget(msg) w.Write([]byte(user.ToJson())) } else { w.WriteHeader(http.StatusUnauthorized) } }
func leave(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { sessionContext := context.Get(r, "context").(Context) vars := mux.Vars(r) props := model.MapFromJson(r.Body) channelId := string(vars["id"]) cr := <-Srv.Store.Channel().Get(channelId) if cr.Err != nil { sessionContext.SetInvalidParam("leave User from Channel", "Channel ID = "+channelId) w.WriteHeader(http.StatusBadRequest) return } cm := <-Srv.Store.Channel().GetMember(channelId, sessionContext.User.Id) if cm.Err != nil { sessionContext.SetInvalidParam("leave User from Channel", "Channel ID = "+channelId) w.WriteHeader(http.StatusBadRequest) return } m := cm.Data.(*model.ChannelMember) if m.Role == model.CHANNEL_ROLE_ADMIN { err := SetNewChannelAdmin(channelId, string(props["user_id"]), sessionContext.User.Id) if err != nil { sessionContext.SetInvalidParam("leave User from Channel", "Channel ID = "+channelId) w.WriteHeader(http.StatusBadRequest) return } } result := <-Srv.Store.Channel().DeleteMember(m) if result.Err != nil { sessionContext.SetInvalidParam("leave User from Channel", "Channel ID = "+channelId) w.WriteHeader(http.StatusBadRequest) return } w.WriteHeader(http.StatusOK) }
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())) }