func Update(u *url.URL, h http.Header, req *models.ChannelMessage, c *models.Context) (int, http.Header, interface{}, error) { if !c.IsLoggedIn() { return response.NewBadRequest(models.ErrAccessDenied) } id, err := request.GetURIInt64(u, "id") if err != nil { return response.NewBadRequest(err) } body := req.Body payload := req.Payload if err := req.ById(id); err != nil { if err == bongo.RecordNotFound { return response.NewNotFound() } return response.NewBadRequest(err) } if req.AccountId != c.Client.Account.Id { isAdmin, err := modelhelper.IsAdmin(c.Client.Account.Nick, c.GroupName) if err != nil { return response.NewBadRequest(err) } if !isAdmin { return response.NewBadRequest(models.ErrAccessDenied) } } if req.Id == 0 { return response.NewBadRequest(err) } req.Body = body req.Payload = payload if err := req.Update(); err != nil { return response.NewBadRequest(err) } cmc := models.NewChannelMessageContainer() return response.HandleResultAndError(cmc, cmc.Fetch(id, request.GetQuery(u))) }
func Create(u *url.URL, h http.Header, req *models.ChannelMessage, c *models.Context) (int, http.Header, interface{}, error) { if !c.IsLoggedIn() { return response.NewBadRequest(models.ErrAccessDenied) } channelId, err := fetchInitialChannelId(u, c) if err != nil { return response.NewBadRequest(err) } ch := models.NewChannel() if err := ch.ById(channelId); err != nil { return response.NewBadRequest(models.ErrChannelNotFound) } canOpen, err := ch.CanOpen(c.Client.Account.Id) if err != nil { return response.NewBadRequest(err) } if !canOpen { return response.NewBadRequest(models.ErrCannotOpenChannel) } // override message type // all of the messages coming from client-side // should be marked as POST req.TypeConstant = models.ChannelMessage_TYPE_POST req.InitialChannelId = channelId req.AccountId = c.Client.Account.Id if req.Payload == nil { req.Payload = gorm.Hstore{} } if c.Client.Account.IsShareLocationEnabled() { // gets the IP of the Client // and adds it to the payload of the ChannelMessage location := parseLocation(c) req.Payload["location"] = location } if err := checkThrottle(channelId, req.AccountId); err != nil { return response.NewBadRequest(err) } if err := req.Create(); err != nil { // todo this should be internal server error return response.NewBadRequest(err) } cml := models.NewChannelMessageList() // override channel id cml.ChannelId = channelId cml.MessageId = req.Id cml.ClientRequestId = req.ClientRequestId if err := cml.Create(); err != nil && !models.IsUniqueConstraintError(err) { // todo this should be internal server error return response.NewBadRequest(err) } cmc := models.NewChannelMessageContainer() err = cmc.Fetch(req.Id, request.GetQuery(u)) if err != nil { return response.NewBadRequest(err) } // assign client request id back to message response because // client uses it for latency compansation cmc.Message.ClientRequestId = req.ClientRequestId return response.HandleResultAndError(cmc, err) }