Пример #1
0
// Retrieve writes profile public information, if requested id is the same as the
// session, then full data is written.
func Retrieve(c *echo.Context) (int, interface{}) {
	reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	attid, _ := c.Get("digitsID").(int64)

	if reqid == attid {
		// Serve private profile
		var profile models.Profile

		log.Printf("Awaiting for [Profile = %d] fetch", reqid)
		err := mangos.FindOne(collection, bson.M{"_id": reqid}, &profile)
		if err != nil {
			log.Printf("Cannot retrieve [Profile = %d]: %s", reqid, err)
			return msg.InternalError(err)
		}

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

	// Serve public profile
	var public models.PublicProfile
	log.Printf("Awaiting for [PublicProfile = %d] fetch", reqid)
	err := mangos.FindOne(collection, bson.M{"_id": reqid}, &public)
	if err != nil {
		log.Printf("Cannot retrieve [PublicProfile = %d]: %s", reqid, err)
		return msg.InternalError(err)
	}

	log.Printf("Served PublicProfile ->\n%s", msg.Detail(public))
	return msg.Ok(public)
}
Пример #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")
}
Пример #3
0
// Run implements runnable interface
func (job *pullMessageJob) Run() {
	msg := job.message
	if msg.GroupID.Hex() != "" {
		// Group message
		var group models.Group
		err := mangos.FindOne(constants.CGroups, bson.M{"_id": msg.GroupID}, &group)
		if err != nil {
			log.Printf("Could not send group message: %s, %s", msg, err)
			return
		}

		for _, member := range group.Members {
			client := binds.Get(member)
			if client != nil {
				client.Send(msg)
				log.Printf("Sent group message %s, Member = %d", msg, member)
				return
			}
		}

		return
	}

	// Direct message, get binds
	client := binds.Get(msg.To)
	if client != nil {
		client.Send(msg)
		log.Printf("Sent message %s", msg)
		return
	}

	log.Printf("Could not find any client that matches Client[ ID=%s ]", msg.ID)
}
Пример #4
0
// GetHabloAuth returns an hablo authorization if available and valid
func GetHabloAuth(c *echo.Context) (*models.HabloAuth, error) {
	header := c.Request().Header
	authv := header.Get("Authorization")

	// Get bearer token
	if !strings.HasPrefix(strings.ToLower(authv), "bearer") {
		return nil, ErrNoToken
	}

	values := strings.Split(authv, " ")
	if len(values) < 2 {
		return nil, ErrNoToken
	}

	token := values[1]

	// Find token in collection
	var auth models.HabloAuth
	err := mangos.FindOne(models.NCAuth, bson.M{"token": token}, &auth)
	if err != nil {
		return nil, err
	}

	return &auth, nil
}
Пример #5
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)
}
Пример #6
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)
}
Пример #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")
}