Example #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
	}
}
Example #2
0
func (handler htmlRequestHandler) handleRequest(pfc *PFContext) {
	w := pfc.W

	aeUser := user.Current(pfc.C)
	if handler.LoginRequired && aeUser == nil {
		w.Header().Set("Location", pfc.LoginURL)
		w.WriteHeader(http.StatusFound)
		return
	} else if aeUser != nil {
		pfc.UserID = storage.UserID(aeUser.ID)
		if user, err := loadUser(pfc.C, aeUser); err != nil {
			pfc.C.Errorf("Error loading user: %s", err)
			http.Error(w, "Unexpected error", http.StatusInternalServerError)
			return
		} else {
			pfc.User = user
		}
	}

	handler.RouteHandler(pfc)
}
Example #3
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!")
		}
	}
}
Example #4
0
func (handler jsonRequestHandler) handleRequest(pfc *PFContext) {
	w := pfc.W
	c := pfc.C

	aeUser := user.Current(pfc.C)
	if handler.LoginRequired && aeUser == nil {
		jsonObj := map[string]string{"errorMessage": _l("Please sign in")}
		bf, _ := json.Marshal(jsonObj)

		w.Header().Set("Content-type", "application/json; charset=utf-8")
		http.Error(w, string(bf), 401)
		return
	} else if aeUser != nil {
		pfc.UserID = storage.UserID(aeUser.ID)
		if !handler.NoFormPreparse {
			if clientID := pfc.R.PostFormValue("client"); clientID != "" {
				pfc.ChannelID = aeUser.ID + "," + clientID
			}
		}

		if user, err := loadUser(pfc.C, aeUser); err != nil {
			c.Errorf("Error loading user: %s", err)
			http.Error(w, "Unexpected error", http.StatusInternalServerError)
			return
		} else {
			pfc.User = user
		}
	}

	if returnValue, err := handler.RouteHandler(pfc); err == nil {
		var jsonObj interface{}
		if message, ok := returnValue.(string); ok {
			jsonObj = map[string]string{"message": message}
		} else {
			jsonObj = returnValue
		}

		bf, _ := json.Marshal(jsonObj)
		w.Header().Set("Content-type", "application/json; charset=utf-8")
		w.Write(bf)
	} else {
		message := _l("An unexpected error has occurred")
		httpCode := http.StatusInternalServerError

		c.Errorf("Error: %s", err)

		if readableError, ok := err.(ReadableError); ok {
			message = err.Error()
			httpCode = readableError.httpCode

			if readableError.err != nil {
				c.Errorf("Source: %s", *readableError.err)
			}
		}

		jsonObj := map[string]string{"errorMessage": message}
		bf, _ := json.Marshal(jsonObj)

		w.Header().Set("Content-type", "application/json; charset=utf-8")
		http.Error(w, string(bf), httpCode)
	}
}