示例#1
1
文件: friends.go 项目: cedmundo/hablo
// Update inserts a new friendship in database
func Update(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	friendID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		return msg.BadRequest(err)
	}

	// Bind body request to friend
	var friend models.FriendUpdate
	err = c.Bind(&friend)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Validate input
	err = validator.Validate(friend)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Insert into database
	err = mangos.Update(constants.CFriends, bson.M{"friend_id": int64(friendID), "digits_id": digitsID}, friend)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(friend)
}
示例#2
0
文件: groups.go 项目: cedmundo/hablo
// DelAdmin removes a member from an existing group
func DelAdmin(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Get group id and convert from string to objectId
	rawMID := c.Param("mid")
	if !bson.IsObjectIdHex(rawMID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// find the group
	memberID := bson.ObjectIdHex(rawMID)

	// Get group id and convert from string to objectId
	rawGID := c.Param("gid")
	if !bson.IsObjectIdHex(rawGID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// find the group
	groupID := bson.ObjectIdHex(rawGID)
	err := mangos.Update(constants.CGroups, bson.M{
		"$and": []bson.M{
			bson.M{"_id": groupID},
			bson.M{"admins": digitsID},
		},
	}, bson.M{"$pull": bson.M{"admins": memberID}})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("deleted admin")
}
示例#3
0
文件: groups.go 项目: cedmundo/hablo
// Update patches a group in database
func Update(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Bind request body to a group update
	var group models.GroupUpdate
	err := c.Bind(&group)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Get group id and convert from string to objectId
	rawGID := c.Param("gid")
	if !bson.IsObjectIdHex(rawGID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// Update the object, only if its a admin
	groupID := bson.ObjectIdHex(rawGID)
	err = mangos.Update(constants.CGroups, bson.M{
		"$and": []bson.M{
			bson.M{"_id": groupID},
			bson.M{"admins": digitsID},
		},
	}, bson.M{"$set": group})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(group)
}
示例#4
0
文件: friends.go 项目: cedmundo/hablo
// Create inserts a new friendship in database
func Create(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Bind body request to friend
	var friend models.Friend
	err := c.Bind(&friend)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Validate input
	err = validator.Validate(friend)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Set internal fields
	friend.ID = bson.NewObjectId()
	friend.DigitsID = digitsID
	friend.CreatedAt = time.Now()

	// Insert into database
	err = mangos.Insert(constants.CFriends, friend)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(friend)
}
示例#5
0
文件: groups.go 项目: cedmundo/hablo
// Retrieve writes the requested group from database
func Retrieve(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Get group id and convert it from string to objectid
	rawGID := c.Param("gid")
	if !bson.IsObjectIdHex(rawGID) {
		return msg.BadRequest("bad id: not a ObjectId")
	}

	// find the group
	groupID := bson.ObjectIdHex(rawGID)
	var group models.Group
	err := mangos.FindOne(constants.CGroups, bson.M{"_id": groupID}, &group)
	if err != nil {
		return msg.InternalError(err)
	}

	// check if user is member (is not part of the query because we need to light bson query)
	for _, member := range group.Members {
		if member == digitsID {
			return msg.Ok(group)
		}
	}

	return msg.Forbidden("you must be part of the group")
}
示例#6
0
文件: groups.go 项目: cedmundo/hablo
// Create inserts a new group into database
func Create(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Bind request body with group
	var group models.Group
	err := c.Bind(&group)

	// Validate group
	err = validator.Validate(group)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Set values
	group.ID = bson.NewObjectId()
	group.Admins = []int64{digitsID}
	group.Creator = digitsID
	group.Members = []int64{digitsID}

	// Create group
	err = mangos.Insert(constants.CGroups, group)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(group)
}
示例#7
0
// ExchageDigits takes a digits auth and converts it into internal auth
func ExchageDigits(c *echo.Context) (int, interface{}) {
	// Get digits authoriation
	dauth, ok := c.Get("digitsAuth").(*models.DigitsAuth)
	if !ok {
		return msg.Forbidden("must include a digits authorization")
	}

	var device models.Device
	var err error

	// Bind body to device data
	err = c.Bind(&device)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Valdate profile data
	err = validator.Validate(device)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Create internal session
	device.CreatedAt = time.Now()
	toCreate := &models.HabloAuth{
		ID:       bson.NewObjectId(),
		DigitsID: dauth.ID,
		Token:    auth.GenerateToken(dauth),
		Enabled:  true,
		Expires:  false,
		Device:   device,
	}

	log.Printf("Awaiting for [HabloAuth = %s] inserction", toCreate.ID.Hex())
	err = mangos.Insert(collection, toCreate)
	if err != nil {
		log.Printf("Cannot insert [HabloAuth = %s]: %s", toCreate.ID.Hex(), err)
		return msg.InternalError(err)
	}

	log.Printf("Created and served HabloAuth ->\n%s", msg.Detail(toCreate.ID))
	return msg.Ok(toCreate)
}
示例#8
0
// Update merges remote object state into database database, overwritting values.
// ID must be the same as session.
func Update(c *echo.Context) (int, interface{}) {
	reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	attid, _ := c.Get("digitsID").(int64)

	if reqid == attid {
		var profile models.ProfileUpdate
		var err error

		// Bind body to profile data
		err = c.Bind(&profile)
		if err != nil {
			return msg.BadRequest(err)
		}

		// Valdate profile data
		err = validator.Validate(profile)
		if err != nil {
			return msg.BadRequest(err)
		}

		// If email is written, require confirmation again
		if profile.Email != "" {
			profile.RequiresConfirm = true
		}

		// Execute update
		log.Printf("Awaiting for [Profile = %d] update", reqid)
		err = mangos.Execute(collection, func(c *mgo.Collection) error {
			return c.Update(bson.M{"_id": reqid}, bson.M{"$set": profile})
		})
		if err != nil {
			log.Printf("Cannot update [Profile = %d]: %s", reqid, err)
			return msg.InternalError(err)
		}

		log.Printf("Updated Profile ->\n%s", msg.Detail(profile))
		return msg.Ok("updated")
	}

	return msg.Forbidden("the id does not belongs to user")
}
示例#9
0
// Create inserts a new valid profile into database.
func Create(c *echo.Context) (int, interface{}) {
	var profile models.Profile
	var err error

	// Get the digits authorization
	auth, ok := c.Get("digitsAuth").(*models.DigitsAuth)
	if !ok {
		return msg.Forbidden("invalid authorization")
	}

	// Bind body to profile data
	err = c.Bind(&profile)
	if err != nil {
		return msg.BadRequest(err)
	}

	// Valdate profile data
	err = validator.Validate(profile)
	if err != nil {
		return msg.BadRequest(err)
	}

	// New profiles always require confirmation
	profile.ID = auth.ID
	profile.RequiresConfirm = true
	profile.CreatedAt = time.Now()

	// Insert into database
	log.Printf("Awaiting for [Profile = %d] inserction", profile.ID)
	err = mangos.Insert(collection, &profile)
	if err != nil {
		log.Printf("Cannot insert [Profile = %d]: %s", profile.ID, err)
		return msg.InternalError(err)
	}

	// TODO: Schedule welcome message and stuff.
	// Done
	log.Printf("Created profile ->\n%s", msg.Detail(profile))
	return msg.Ok(profile)
}
示例#10
0
文件: friends.go 项目: cedmundo/hablo
// Delete inserts a new friendship in database
func Delete(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	friendID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		return msg.BadRequest(err)
	}

	err = mangos.Delete(constants.CFriends, bson.M{"friend_id": int64(friendID), "digits_id": digitsID})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("deleted")
}
示例#11
0
// Update sets the settings for the user
func Update(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("not a digitsID")
	}

	var settings models.SettingsUpdate
	err := c.Bind(&settings)
	if err != nil {
		return msg.BadRequest(err)
	}

	log.Printf("Awaiting for Settings[ ID=%d ] get", digitsID)
	err = mangos.Update(constants.CSettings, bson.M{"_id": digitsID}, bson.M{"$set": &settings})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("done")
}
示例#12
0
文件: friends.go 项目: cedmundo/hablo
// Retrieve inserts a new friendship in database
func Retrieve(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	friendID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		return msg.BadRequest(err)
	}

	// Find all user friends
	var friend models.Friend
	err = mangos.FindOne(constants.CFriends, bson.M{"friend_id": int64(friendID), "digits_id": digitsID}, &friend)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(friend)
}