Ejemplo 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")
	}
}
Ejemplo n.º 2
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[:]))
	}

}