func HandleCreateIRLMoji(r render.Render, bindErr binding.Errors, im models.IRLMoji, db *models.DB, backchannel Backchannel) { if bindErr.Count() > 0 { r.JSON(400, JsonErrBinding(bindErr)) return } if backchannel.UserId() == "" { r.JSON(403, JsonErr("The provided credentials were invalid.")) return } user, err := db.GetUserWithId(backchannel.UserId()) if err != nil { r.JSON(403, "Could not find a user with your credentials.") return } // Now let's create that user, shall we? insertedIM, err := db.InsertIM(user.Id, im.Emoji, im.Picture) if err != nil { log.Println("Error creating user:"******"Sorry, an internal server error has occurred.")) return } r.JSON(200, map[string]*models.IRLMoji{"irlmoji": insertedIM}) }
func addAttribute(attr attribute, err binding.Errors, params martini.Params, writer http.ResponseWriter, db *mgo.Database) (int, string) { setJsonResponseHeader(writer) if err.Count() > 0 { return http.StatusConflict, utils.JsonString(errorMsg{err.Overall["missing-requirement"]}) } resource := strings.ToLower(params["resource"]) query := bson.M{"resource": resource} update := mgo.Change{Upsert: true, Update: bson.M{"$addToSet": bson.M{"attributes": attr}}} if _, dbErr := db.C(dbInfo.Collection).Find(query).Apply(update, &attr); dbErr != nil { return http.StatusConflict, utils.JsonString(errorMsg{dbErr.Error()}) } return http.StatusOK, "{}" }
func HandleToggleHeart(r render.Render, bindErr binding.Errors, heart models.Heart, db *models.DB, backchannel Backchannel) { if bindErr.Count() > 0 { r.JSON(400, JsonErrBinding(bindErr)) return } if backchannel.UserId() == "" { r.JSON(403, JsonErr("The provided credentials were invalid.")) return } im, err := db.GetIMWithId(heart.IRLMojiId) if err != nil { r.JSON(404, JsonErr("The provided IRLMoji id was invalid.")) return } user, err := db.GetUserWithId(backchannel.UserId()) if err != nil { r.JSON(403, "Could not find a user with your credentials.") return } _, err = db.ToggleHeart(user.Id, im.Id) if err != nil { log.Println("Error toggling heart:", err.Error()) r.JSON(500, "Sorry, an internal server error has occurred.") } im, err = db.GetIMWithId(heart.IRLMojiId) if err != nil { log.Println("Error getting IRLMoji after toggling heart:", err.Error()) r.JSON(500, "Sorry, an internal server error has occurred.") return } err = db.AnnotateHearted(im, user.Id) if err != nil { log.Println("Error annotating hearted info:", err.Error()) r.JSON(500, "Sorry, an internal server error has occurred.") return } r.JSON(200, map[string]*models.IRLMoji{"irlmoji": im}) }
func HandleCreateUserByTwitter(r render.Render, bindErr binding.Errors, userForm models.UserForm, db *models.DB) { if bindErr.Count() > 0 { r.JSON(400, JsonErrBinding(bindErr)) return } // Build Twitter client config := &oauth1a.ClientConfig{ ConsumerKey: TWITTER_CONSUMER_KEY, ConsumerSecret: TWITTER_CONSUMER_SECRET, } oauthUser := oauth1a.NewAuthorizedConfig(userForm.TwitterAccessToken, userForm.TwitterAccessSecret) client := twittergo.NewClient(config, oauthUser) // Build request to send to Twitter req, err := http.NewRequest("GET", "/1.1/account/verify_credentials.json", nil) if err != nil { log.Println("Could not build request for Twitter:", err.Error()) r.JSON(500, JsonErr("Sorry, an internal server error has occurred.")) } // Send it resp, err := client.SendRequest(req) if err != nil { log.Println("Error sending request to Twitter:", err.Error()) r.JSON(504, JsonErr("Could not communicate properly with Twitter, "+ "please try again soon.")) return } // Parse the response var result = &twittergo.User{} if err = resp.Parse(result); err != nil { log.Println("Error parsing Twitter result:", err.Error(), "Response:", resp) r.JSON(504, JsonErr("Got a bad response from Twitter, please try "+ "again soon.")) return } user, err := db.GetUserWithId(result.IdStr()) if err == nil { user, err = db.UpdateUserAuth(user.Id, userForm.TwitterAccessToken, userForm.TwitterAccessSecret) if err != nil { log.Println("Error updating user auth:", err.Error()) r.JSON(500, JsonErr("Sorry, an internal server error has occurred.")) return } r.JSON(200, map[string]*models.User{"user": user}) return } // Now let's create that user, shall we? user, err = db.CreateUser( result.IdStr(), result.ScreenName(), (*result)["profile_image_url"].(string), userForm.TwitterAccessToken, userForm.TwitterAccessSecret, ) if err != nil { log.Println("Error creating user:"******"Sorry, an internal server error has occurred.")) return } r.JSON(200, map[string]*models.User{"user": user}) }