Esempio n. 1
0
// This is only called after the initial login
func (covH *HistoryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	if r.URL.Query()["user"] != nil {
		// Always handle username in lowercase
		username := strings.ToLower(r.URL.Query()["user"][0])
		// Query to see if User exists
		u, k, err := db.GetUser(c, username)

		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting User:"******"!\n\n")
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		LogUserRequest(c, u, *r, true, "", "")

		ud := workflow.MakeLoginUserData(u)
		// // Store updated User in DB
		// err = db.PutUser(c, ud.User, k)
		// if err != nil {
		// 	fmt.Fprint(os.Stderr, "DB Error Put User:"******"!\n\n")
		// 	return
		// }
		var count int
		ud.UiUserData.History, count, err = db.GetHistory(c, username)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting list of messages:"+err.Error()+"!\n\n")
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		ud.UiUserData.ArchiveHistoryLength = count
		ud.User.ArchiveHistoryLength = count

		// Store updated User in DB
		err = db.PutUser(c, ud.User, k)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Put User:"******"!\n\n")
			return
		}

		// NewSession(*ud.UiUserData, username)

		s, err := workflow.Stringify(ud.UiUserData)
		if err != nil {
			fmt.Println("Error converting messages to json", err)
		}
		fmt.Fprint(w, string(s[:]))

	} else {
		fmt.Fprint(os.Stderr, "Error: username not provided for getting history!\n\n")
	}
}
Esempio n. 2
0
func (newuserH *NewUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if r.FormValue("user") != "" {
		// Always handle username in lowercase
		username := strings.ToLower(r.FormValue("user"))

		// Query to see if User exists
		u, _, err := db.GetUser(c, username)

		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting User:"******"!\n\n")
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		if u.Username != "" {
			http.Error(w, "Cannot create User. Username already exists!", 500)
			return
		}

		u = db.User{
			Username:   username,
			Screenname: r.FormValue("screenname"),
			Date:       time.Now(),
		}

		key := db.UserKey(c)
		_, err = datastore.Put(c, key, &u)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Creating User:"******"!\n\n")
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		LogUserRequest(c, u, *r, false, "", "")

		s, err := workflow.Stringify(u)
		if err != nil {
			fmt.Println("Error converting User object to json", err)
		}
		fmt.Fprint(w, string(s[:]))

	}

}
Esempio n. 3
0
func (covH *GetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	// Check if User has logged in (by simply providing a username in the query parameter for now
	// because we are not checking password at the moment)
	// If logged in, serve the requested static file (assuming all URL request
	// without its own path handler is a request to a file in the static folder
	// If not logged in, redirect to the parent index page for login
	if r.URL.Query()["user"] != nil {

		// Check to make sure that the provided User actually exists
		username := strings.ToLower(r.URL.Query()["user"][0])
		u, _, err := db.GetUser(c, username)

		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting User:"******"!\n\n")
			http.ServeFile(w, r, "static/index.html")
			return
		}

		if u.Username == "" {
			fmt.Fprint(os.Stderr, "User does not exist.\n\n")

			http.ServeFile(w, r, "static/index.html")
			return
		}

		LogUserRequest(c, u, *r, true, "", "")

		if len(r.URL.Path[len(COV):]) != 0 {
			http.ServeFile(w, r, "static/astro-world"+r.URL.Path)
			return
		} else {
			http.ServeFile(w, r, "static/astro-world/index.html")
			return
		}
	} else {
		http.ServeFile(w, r, "static/index.html")
		return
	}
}
Esempio n. 4
0
func (covH *ResponseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if r.FormValue("user") != "" {
		// Always handle username in lowercase
		username := strings.ToLower(r.FormValue("user"))
		promptId := r.FormValue("promptId")
		phaseId := r.FormValue("phaseId")
		questionText := r.FormValue("questionText")
		gotoPhaseId := r.FormValue("gotophase")
		var texts []string
		dec := json.NewDecoder(strings.NewReader(questionText))
		err := dec.Decode(&texts)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s", err)
			log.Fatal(err)
			return
		}

		// Query to see if User exists
		u, k, err := db.GetUser(c, username)

		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting User:"******"!\n\n")
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// First time User will have empty currentPhaseId & currentPromptId
		if u.CurrentPhaseId != "" {
			if u.CurrentPhaseId != phaseId {
				fmt.Fprint(os.Stderr, "Out of sync error! User info and DB are out of sync.\n\n. Revert to what's in the DB")
			}
		}
		if u.CurrentPromptId != "" {
			if u.CurrentPromptId != promptId {
				fmt.Fprint(os.Stderr, "Out of sync error! User info and DB are out of sync.\n\n. Revert to what's in the DB")
			}
		}

		var ud *workflow.UserData

		if gotoPhaseId != "" {
			ud = workflow.MakeUserData(u, gotoPhaseId)
		} else {
			// Process submitted answers
			ud = workflow.MakeUserData(u, "")
			ud.CurrentPrompt.ProcessResponse(r.FormValue("jsonResponse"), &ud.User, ud.UiUserData, c)
		}
		responseId := ud.CurrentPrompt.GetResponseId()
		responseText := ud.CurrentPrompt.GetResponseText()

		LogUserRequest(c, ud.User, *r, false, responseId, responseText)

		// Get the count of existing messages
		rc, err := db.GetHistoryCount(c, username)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting count of messages:"+err.Error()+"!\n\n")
			return
		}

		//TODO need to find a way to save the responses that are not text
		//Process submitted answers
		rc++
		rc1 := rc
		rc++
		rc2 := rc

		m := []db.Message{
			db.Message{
				Texts:     texts,
				Mtype:     db.ROBOT,
				Date:      time.Now(),
				MessageNo: rc1,
			},
			db.Message{
				Id:        responseId,
				Texts:     []string{responseText},
				Mtype:     db.HUMAN,
				Date:      time.Now(),
				MessageNo: rc2,
			}}

		// TODO what does this comment mean?
		// We set the same parent key on every Message entity to ensure each Message
		// is in the same entity group. Queries across the single entity group
		// will be consistent. However, the write rate to a single entity group
		// should be limited to ~1/second.
		var keys = []*datastore.Key{
			datastore.NewIncompleteKey(c, "Message", db.UserHistoryKey(c, username)),
			datastore.NewIncompleteKey(c, "Message", db.UserHistoryKey(c, username))}

		_, err = datastore.PutMulti(c, keys, m)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Adding Messages:"+err.Error()+"!\n\n")
			return
			//http.Error(w, err.Error(), http.StatusInternalServerError)
			//return
		}

		// TODO cleanup
		// currentSequenceOrder := ud.CurrentPrompt.GetSequenceOrder()

		// Move to the next prompt
		ud.UpdateWithNextPrompt()

		// Update history
		var count int
		ud.UiUserData.History, count, err = db.GetHistory(c, username)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Getting list of messages:"+err.Error()+"!\n\n")
			return
		}
		// TODO - Not the cleanest way to do this
		// Reset ArchieveHistoryLength whe UpdateWithNextPrompt
		// then let the server deal with setting the new value
		// OR if changing phase purposely
		if (ud.UiUserData.ArchiveHistoryLength < 0) || (gotoPhaseId != "") {
			ud.UiUserData.ArchiveHistoryLength = count
			ud.User.ArchiveHistoryLength = count
		}

		// Store updated User in DB
		err = db.PutUser(c, ud.User, k)
		if err != nil {
			fmt.Fprint(os.Stderr, "DB Error Put User:"******"!\n\n")
			return
		}

		s, err := workflow.Stringify(ud.UiUserData)
		if err != nil {
			fmt.Println("Error converting messages to json", err)
			return
		}
		fmt.Fprint(w, string(s[:]))
	}

}