Exemplo n.º 1
0
func TestAddEditDeletePm(t *testing.T) {
	var pm nerdz.Pm

	pm.Message = "Hi bro. Join telegram now"
	pm.To = withClosedProfile.Counter

	if err := me.Add(&pm); err != nil {
		t.Fatalf("No errors should occur while adding a new pm to a non blacklisted user, but got %v", err)
	}

	pm.Message = "Pm edit is impossible (since in IM messages are not editable)"
	if err := me.Edit(&pm); err == nil {
		t.Fatalf("Pm edit shouldn't work")
	}

	if err := me.Delete(&pm); err != nil {
		t.Fatalf("Pm delete failed with error: %s", err.Error())
	}
}
Exemplo n.º 2
0
// 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)
	}
}