Esempio n. 1
0
func loadUser(c appengine.Context, user *user.User) (*storage.User, error) {
	if u, err := storage.UserByID(c, storage.UserID(user.ID)); err != nil {
		return nil, err
	} else if u == nil {
		// New user
		newUser := storage.User{
			ID:           user.ID,
			EmailAddress: user.Email,
		}
		if err := newUser.Save(c); err != nil {
			return nil, err
		}

		return &newUser, nil
	} else {
		return u, nil
	}
}
Esempio n. 2
0
func (handler taskRequestHandler) handleRequest(pfc *PFContext) {
	if userID := pfc.R.PostFormValue("userID"); userID != "" {
		pfc.UserID = storage.UserID(userID)
		if channelID := pfc.R.PostFormValue("channelID"); channelID != "" {
			pfc.ChannelID = channelID
		}

		if user, err := storage.UserByID(pfc.C, pfc.UserID); err != nil {
			pfc.C.Errorf("Error loading user: %s", err)
			http.Error(pfc.W, "Unexpected error", http.StatusInternalServerError)
			return
		} else {
			pfc.User = user
		}
	}

	var response interface{}
	taskMessage, err := handler.RouteHandler(pfc)
	if err != nil {
		pfc.C.Errorf("Task failed: %s", err.Error())
		http.Error(pfc.W, err.Error(), http.StatusInternalServerError)
		response = map[string]string{"error": err.Error()}
	} else {
		response = taskMessage
	}

	if !taskMessage.Silent {
		if channelID := pfc.R.PostFormValue("channelID"); channelID != "" {
			if err := channel.SendJSON(pfc.C, channelID, response); err != nil {
				pfc.C.Criticalf("Error writing to channel: %s", err)
			}
		} else {
			pfc.C.Warningf("Channel ID is empty!")
		}
	}
}