<commit_before>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"github.com/gorilla/context"
	"github.com/gorilla/sessions"
	"github.com/rs/cors"
	"log"
	"net/http"
)

type State struct {
	Users        map[uuid.UUID]User `json:"users"`         // All participators at the student division meeting.
	SpeakerLists [][]User           `json:"speakersLists"` // A list of speakerLists where each index is a list of sessions in queue to speak
}

type User struct {
	Nick    string `json:"nick"`
	IsAdmin bool   `json:"isAdmin"`
	id      uuid.UUID
	session *sessions.Session
}

const SESSION_KEY = "talarlista_session"
const UUID_KEY = "uuid"

var store = sessions.NewCookieStore([]byte("this is the secret stuff"))
var state State

func listHandler(w http.ResponseWriter, req *http.Request) {
	log.Print("Listhandler begin")
	session, err := store.Get(req, SESSION_KEY)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	session.Save(req, w)

	log.Printf("Session is new %v\n", session.IsNew)
	if session.IsNew {
		var id = uuid.New()
		session.Values[UUID_KEY] = id
		log.Printf("New user id: %v\n", id)

		state.addUser(User{"", false, id, session})
		log.Printf("State with new user: %v", state)
	}

	session.Options = &sessions.Options{ // should this be done inside the previous if-statement?
		MaxAge:   86400,
		HttpOnly: true,
	}


	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")

	user, err := state.getUserFromSession(session)
	if err != nil {
		log.Printf("listHandler: Could not get user from session %v\n", err)
	}
	log.Printf("User: %v\n", user)
	switch req.Method {
	case http.MethodGet:
		listGet(w)
	case http.MethodPost:
		listPost(w, user)
	case http.MethodDelete:
		listDelete(w, user)
	default:
		w.Write([]byte("List unsupported method.\n"))
		log.Print("Unsupported method")
	}
}

func getUUIDfromSession(session *sessions.Session) (uuid.UUID, error) {
	storedId, ok := session.Values[UUID_KEY]
	if !ok {
		return uuid.UUID{}, errors.New("Could not find user from session-stored UUID")
	}

	id, ok := storedId.(uuid.UUID)
	if !ok {
		return uuid.UUID{}, errors.New("Could not cast storedId to uuid.UUID")
	}

	return id, nil
}

func (s State) getUserFromSession(session *sessions.Session) (User, error) {
	id, err := getUUIDfromSession(session)

	if err != nil {
		return User{}, err
	}

	return s.getUser(id)
}

func (s State) getUser(id uuid.UUID) (User, error) {
	user, ok := s.Users[id]
<commit_msg>Store uuid as string in session values. Error handling on session.Save.<commit_after>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"github.com/gorilla/context"
	"github.com/gorilla/sessions"
	"github.com/rs/cors"
	"log"
	"net/http"
)

type State struct {
	Users        map[uuid.UUID]User `json:"users"`         // All participators at the student division meeting.
	SpeakerLists [][]User           `json:"speakersLists"` // A list of speakerLists where each index is a list of sessions in queue to speak
}

type User struct {
	Nick    string `json:"nick"`
	IsAdmin bool   `json:"isAdmin"`
	id      uuid.UUID
	session *sessions.Session
}

const SESSION_KEY = "talarlista_session"
const UUID_KEY = "uuid"

var store = sessions.NewCookieStore([]byte("this is the secret stuff"))
var state State

func listHandler(w http.ResponseWriter, req *http.Request) {
	log.Print("Listhandler begin")
	session, err := store.Get(req, SESSION_KEY)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	log.Printf("Session is new %v\n", session.IsNew)
	if session.IsNew {
		var id = uuid.New()
		session.Values[UUID_KEY] = id.String()

		log.Printf("New user id: %v\n", id)

		state.addUser(User{"", false, id, session})
		log.Printf("State with new user: %v", state)
	}

	err = session.Save(req, w)
	if err != nil {
		log.Printf("Error when saving session to storage: %v", err)
	}


	session.Options = &sessions.Options{ // should this be done inside the previous if-statement?
		MaxAge:   86400,
		HttpOnly: true,
	}


	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")

	user, err := state.getUserFromSession(session)
	if err != nil {
		log.Printf("listHandler: Could not get user from session %v\n", err)
	}
	log.Printf("User: %v\n", user)
	switch req.Method {
	case http.MethodGet:
		listGet(w)
	case http.MethodPost:
		listPost(w, user)
	case http.MethodDelete:
		listDelete(w, user)
	default:
		w.Write([]byte("List unsupported method.\n"))
		log.Print("Unsupported method")
	}
}

func getUUIDfromSession(session *sessions.Session) (uuid.UUID, error) {
	storedValue, ok := session.Values[UUID_KEY]
	if !ok {
		return uuid.UUID{}, errors.New("Could not find user from session-stored UUID")
	}

	stringId, ok := storedValue.(string)
	if !ok {
		return uuid.UUID{}, errors.New("Could not cast stored value to string")
	}

	id, err := uuid.Parse(stringId)
	if err != nil {
		return uuid.UUID{}, errors.New("Could not parse stored string into uuid.UUID")
	}

	return id, nil
}

func (s State) getUserFromSession(session *sessions.Session) (User, error) {
	id, err := getUUIDfromSession(session)

	if err != nil {
		return User{}, err
	}

	return s.getUser(id)
}

func (s State) getUser(id uuid.UUID) (User, error) {
	user, ok := s.Users[id]

{'fraction_non_alphanumeric': 0.14412580943570769, 'fraction_numerical': 0.0018501387604070306, 'mean_word_length': 3.9916897506925206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '864398', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1927, 'n_words': 512}