Beispiel #1
1
// 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)
}
Beispiel #2
0
// 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")
}
Beispiel #3
0
// 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)
}
Beispiel #4
0
// 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)
}
Beispiel #5
0
// 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)
}
Beispiel #6
0
// 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")
}
Beispiel #7
0
// AcceptClient upgrades connection to socket
func AcceptClient(c *echo.Context) (int, interface{}) {

	// Return if not authorized
	hauth, ok := c.Get("habloAuth").(*models.HabloAuth)
	if !ok {
		return msg.Forbidden("method requires hablo authentication")
	}

	// Load the authenticated profile
	var profile models.Profile
	log.Printf("Awaiting for Profile[ DigitsID=%d ] find", hauth.DigitsID)
	err := mangos.FindOne(constants.CProfiles, bson.M{"_id": hauth.DigitsID}, &profile)
	if err != nil {
		log.Printf("Cannot find Profile[ DigitsID=%d ]", hauth.DigitsID)
		return msg.Forbidden(err)
	}

	// Create the client
	client := &Client{
		id:      bson.NewObjectId(),
		profile: &profile,
		socket:  c.Socket(),
	}
	binds.Set(profile.ID, client)
	defer func() {
		// Unbind
		binds.Del(profile.ID)
	}()

	// Lock and pull events
	log.Println("Accepted client:", client)
	err = client.LockAndPull()
	if err != nil {
		return msg.InternalError(err)
	}

	// Clean terminate
	log.Println("Finished application:", client)
	return msg.Ok("connection finished")
}
Beispiel #8
0
// List writes a list of groups available for the current session
func List(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// List all groups where digitID is member of
	var groups = make([]models.Group, 0)
	err := mangos.FindAll(constants.CGroups, bson.M{"members": digitsID}, &groups)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(groups)
}
Beispiel #9
0
// Retrieve writes the settings for the user
func Retrieve(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("not a digitsID")
	}

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

	return msg.Ok(settings)
}
Beispiel #10
0
// List inserts a new friendship in database
func List(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("session required")
	}

	// Find all user friends
	var friends = make([]models.Friend, 0)
	err := mangos.FindAll(constants.CFriends, bson.M{"digits_id": digitsID}, &friends)
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok(friends)
}
Beispiel #11
0
// 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")
}
Beispiel #12
0
// CloseSession takes a digits auth and converts it into internal auth
func CloseSession(c *echo.Context) (int, interface{}) {
	hauth, ok := c.Get("habloAuth").(*models.HabloAuth)
	if !ok {
		return msg.Forbidden("must include an authorization")
	}

	log.Printf("Awaiting for [HabloAuth = %s] deletion", hauth.ID.Hex())
	err := mangos.Execute(collection, func(c *mgo.Collection) error {
		return c.RemoveId(hauth.ID)
	})
	if err != nil {
		log.Printf("Cannot remove [HabloAuth = %s]: %s", hauth.ID.Hex(), err)
		return msg.InternalError(err)
	}

	log.Printf("Removed [HabloAuth = %s]", hauth.ID.Hex())
	return msg.Ok("see you")
}
Beispiel #13
0
// Delete resets the settings for the user
func Delete(c *echo.Context) (int, interface{}) {
	digitsID, ok := c.Get("digitsID").(int64)
	if !ok {
		return msg.Forbidden("not a digitsID")
	}

	enabled := true
	defaults := models.SettingsUpdate{
		GlobalPushEnabled: &enabled,
		PushSound:         "",
	}
	err := mangos.Update(constants.CSettings, bson.M{"_id": digitsID}, bson.M{"$set": defaults})
	if err != nil {
		return msg.InternalError(err)
	}

	return msg.Ok("done")
}
Beispiel #14
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)
}
Beispiel #15
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")
}
Beispiel #16
0
// 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)
}
Beispiel #17
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")
}
Beispiel #18
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)
}
Beispiel #19
0
// Delete removes the indicated object from database.
// ID must be the same as session.
func Delete(c *echo.Context) (int, interface{}) {
	var err error
	reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	attid, _ := c.Get("digitsID").(int64)

	if reqid == attid {
		// Execute delete
		log.Printf("Awaiting for [Profile = %d] delete", reqid)
		err = mangos.Execute(collection, func(c *mgo.Collection) error {
			return c.RemoveId(reqid)
		})
		if err != nil {
			log.Printf("Cannot delete [Profile = %d]: %s", reqid, err)
			return msg.InternalError(err)
		}

		log.Printf("Deleted [Profile = %d]", reqid)
		return msg.Ok("deleted")
	}

	return msg.Forbidden("the id does not belongs to user")
}