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