// DeleteConversation handles the request and deletes the conversation func DeleteConversation() echo.HandlerFunc { // swagger:route DELETE /me/pms/{other} user pms DeleteUserPms // // Delete the conversation beteen the current user and other // // Consumes: // - application/json // // Produces: // - application/json // // Security: // oauth: pms:write // // Responses: // default: apiResponse return func(c echo.Context) error { if !rest.IsGranted("pms:write", c) { return rest.InvalidScopeResponse("pms:write", c) } var other *nerdz.User var err error if other, err = rest.User("other", c); err != nil { return err } me := c.Get("me").(*nerdz.User) if err = me.DeleteConversation(other.ID()); err != nil { errstr := err.Error() c.JSON(http.StatusBadRequest, &rest.Response{ Data: nil, HumanMessage: errstr, Message: errstr, Status: http.StatusBadRequest, Success: false, }) return errors.New(errstr) } message := "Success" c.JSON(http.StatusOK, &rest.Response{ Data: nil, HumanMessage: message, Message: message, Status: http.StatusOK, Success: true, }) return nil } }
// Conversation handles the request and returns the user private conversation with the other user func Conversation() echo.HandlerFunc { // swagger:route GET /me/pms/{other} user post pms getUserConversation // // Returns the private conversation of the current user with the other user // // You can personalize the request via query string parameters // // Produces: // - application/json // // Security: // oauth: profile:read // // Responses: // default: apiResponse return func(c echo.Context) error { if !rest.IsGranted("pms:read", c) { return rest.InvalidScopeResponse("pms:read", c) } var other *nerdz.User var err error if other, err = rest.User("other", c); err != nil { return err } // fetch conversation between me and other var conversation *[]nerdz.Pm options := c.Get("pmsOptions").(*nerdz.PmsOptions) me := c.Get("me").(*nerdz.User) conversation, err = me.Pms(other.ID(), *options) if err != nil { errstr := "Unable to fetch conversation with the specified user" c.JSON(http.StatusBadRequest, &rest.Response{ HumanMessage: errstr, Message: "me.Conversation error", Status: http.StatusBadRequest, Success: false, }) return errors.New(errstr) } var conversationTO []*nerdz.PmTO for _, pm := range *conversation { conversationTO = append(conversationTO, pm.GetTO(me)) } return rest.SelectFields(conversationTO, c) } }
// NewPm handles the request and creates a new pm func NewPm() echo.HandlerFunc { // swagger:route POST /me/pms/{other} user pm NewUserPm // // Creates a new pm with from me to other user // // Consumes: // - application/json // // Produces: // - application/json // // Security: // oauth: pms:write // // Responses: // default: apiResponse return func(c echo.Context) error { if !rest.IsGranted("pms:write", c) { return rest.InvalidScopeResponse("pms:write", c) } // Read a rest.NewMessage from the body request. message := rest.NewMessage{} if err := c.Bind(&message); err != nil { errstr := err.Error() c.JSON(http.StatusBadRequest, &rest.Response{ Data: nil, HumanMessage: errstr, Message: errstr, Status: http.StatusBadRequest, Success: false, }) return errors.New(errstr) } var other *nerdz.User var err error if other, err = rest.User("other", c); err != nil { return err } // Create a nerdz.Pm from the message // and current context. pm := nerdz.Pm{} pm.Message = message.Message pm.Lang = message.Lang pm.To = other.ID() // Send it me := c.Get("me").(*nerdz.User) if err = me.Add(&pm); err != nil { errstr := err.Error() c.JSON(http.StatusBadRequest, &rest.Response{ Data: nil, HumanMessage: errstr, Message: errstr, Status: http.StatusBadRequest, Success: false, }) return errors.New(errstr) } // Extract the TO from the new pm and return // selected fields. return rest.SelectFields(pm.GetTO(me), c) } }