示例#1
0
文件: me.go 项目: nerdzeu/nerdz-api
// 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
文件: me.go 项目: nerdzeu/nerdz-api
// 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
	}
}
示例#3
0
文件: me.go 项目: nerdzeu/nerdz-api
// 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)
	}
}
示例#4
0
文件: me.go 项目: nerdzeu/nerdz-api
// 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)
	}
}
示例#5
0
文件: rest.go 项目: nerdzeu/nerdz-api
// GetUserInfo returns the *UserInfo of the user
func GetUserInfo(user *nerdz.User) *UserInfo {
	var info UserInfo
	info.Info = *user.Info().GetTO()
	info.Contacts = *user.ContactInfo().GetTO()
	info.Personal = *user.PersonalInfo().GetTO()
	return &info
}
示例#6
0
文件: me.go 项目: nerdzeu/nerdz-api
// 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)
	}
}