Пример #1
0
// NewBlacklisted handles the request and creates and adds target user to current user whitelist
func NewBlacklisted() echo.HandlerFunc {

	// swagger:route POST /me/whitelist/{target} user whitelist NewBlacklisted
	//
	// Adds target to the whitelist of the current user
	//
	//  Produces:
	//  - application/json
	//
	//  Consumes:
	//  - application/json
	//
	//  Security:
	//      oauth: profile:write
	//
	//  Responses:
	//      default: apiResponse

	return func(c echo.Context) error {
		if !rest.IsGranted("profile:write", c) {
			return rest.InvalidScopeResponse("profile:write", c)
		}

		var target *nerdz.User
		var err error
		if target, err = rest.User("target", c); err != nil {
			return err
		}

		// 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)
		}

		me := c.Get("me").(*nerdz.User)
		if err = me.BlacklistUser(target, message.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)
		}
		// Return selected field from the followed User
		return rest.SelectFields(target.GetTO(me), c)
	}
}
Пример #2
0
// DeletePostUserLock handles the request and deletes the lock for the notification of the target user
// on the specified post
func DeletePostUserLock() echo.HandlerFunc {

	// swagger:route DELETE /projects/{id}/posts/{pid}/locks/{target} project post vote DeleteProjectPostUserLock
	//
	// Deletes the lock  for the notification of the target user on the specified post
	//
	// Consumes:
	// - application/json
	//
	//	Produces:
	//	- application/json
	//
	//	Security:
	//		oauth: project_messages:write
	//
	//	Responses:
	//		default: apiResponse

	return func(c echo.Context) error {
		if !rest.IsGranted("project_messages:write", c) {
			return rest.InvalidScopeResponse("project_messages:write", c)
		}
		var target *nerdz.User
		var err error
		if target, err = rest.User("target", c); err != nil {
			return err
		}

		me := c.Get("me").(*nerdz.User)
		post := c.Get("post").(*nerdz.ProjectPost)
		err = me.Unlock(post, target)
		if 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)
		}

		errstr := "Success"
		c.JSON(http.StatusOK, &rest.Response{
			Data:         nil,
			HumanMessage: errstr,
			Message:      errstr,
			Status:       http.StatusOK,
			Success:      true,
		})
		return nil
	}
}
Пример #3
0
// 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
	}
}
Пример #4
0
// 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)
	}
}
Пример #5
0
// DeleteBlacklisted handles the request and deletes the target user from the current user whitelist
func DeleteBlacklisted() echo.HandlerFunc {

	// swagger:route DELETE /me/following/users/{target} user blacklist DeleteBlacklisted
	//
	// Deletes target user from the current user blacklist
	//
	// Consumes:
	// - application/json
	//
	//  Security:
	//      oauth: profile:write
	//
	//  Responses:
	//      default: apiResponse

	return func(c echo.Context) error {
		if !rest.IsGranted("profile:write", c) {
			return rest.InvalidScopeResponse("profile:write", c)
		}
		var target *nerdz.User
		var err error
		if target, err = rest.User("target", c); err != nil {
			return err
		}

		me := c.Get("me").(*nerdz.User)
		if err = me.UnblacklistUser(target); 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
	}
}
Пример #6
0
// SetOther is the middleware that checks if the current logged user can see the required profile
// and if the required profile exists. On success sets the "other" = *User variable in the context
func SetOther() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return echo.HandlerFunc(func(c echo.Context) error {
			var other *nerdz.User
			var err error
			if other, err = rest.User("id", c); err != nil {
				return err
			}
			// store the other User into the context
			c.Set("other", other)
			// pass context to the next handler
			return next(c)
		})
	}
}
Пример #7
0
// NewPostUserLock handles the request and creates a new lock for the notification
// caused by the target user
func NewPostUserLock() echo.HandlerFunc {

	// swagger:route POST /projects/{id}/posts/{pid}/locks/{target} project post vote NewUserNewPostUserLock
	//
	// Locks the notification from the target user to the current logged user, on the specified post
	//
	// Consumes:
	// - application/json
	//
	//	Produces:
	//	- application/json
	//
	//	Security:
	//		oauth: project_messages:write
	//
	//	Responses:
	//		default: apiResponse

	return func(c echo.Context) error {
		if !rest.IsGranted("project_messages:write", c) {
			return rest.InvalidScopeResponse("project_messages:write", c)
		}

		var target *nerdz.User
		var err error
		if target, err = rest.User("target", c); err != nil {
			return err
		}
		me := c.Get("me").(*nerdz.User)
		post := c.Get("post").(*nerdz.ProjectPost)
		var lock *[]nerdz.Lock
		lock, err = me.Lock(post, target)
		if 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 lock and return selected fields.
		return rest.SelectFields((*lock)[0].(*nerdz.ProjectPostUserLock).GetTO(me), c)
	}
}
Пример #8
0
// NewUserFollowing handles the request and creates and adds target to the following list of the current user
func NewUserFollowing() echo.HandlerFunc {

	// swagger:route POST /me/following/users/{target} userfollowing NewUserFollowing
	//
	// Adds target to the following list of the current user
	//
	//  Produces:
	//  - application/json
	//
	//  Security:
	//      oauth: following:write
	//
	//  Responses:
	//      default: apiResponse

	return func(c echo.Context) error {
		if !rest.IsGranted("following:write", c) {
			return rest.InvalidScopeResponse("following:write", c)
		}

		var target *nerdz.User
		var err error
		if target, err = rest.User("target", c); err != nil {
			return err
		}
		me := c.Get("me").(*nerdz.User)
		if err = me.Follow(target); 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)
		}
		// Return selected field from the followed User
		return rest.SelectFields(target.GetTO(me), c)
	}
}
Пример #9
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)
	}
}