<commit_before>// Walkhub
// Copyright (C) 2015 Pronovix
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package walkhub

import (
	"errors"
	"net/http"
	"time"

	"github.com/nbio/hitch"
	"github.com/tamasd/ab"
	"github.com/tamasd/ab/providers/auth/google"
	"github.com/tamasd/ab/services/auth"
	"github.com/tamasd/hitch-session"
	"google.golang.org/api/plus/v1"
)

//go:generate abt --output=usergen.go --generate-service-struct-name=UserService --generate-service-list=false entity User

var UserDelegate = &auth.SessionUserDelegate{}

type User struct {
	UUID     string `dbtype:"uuid" dbdefault:"uuid_generate_v4()"`
	Name     string `constructor:"true"`
	Mail     string `constructor:"true"`
	Admin    bool
	Created  time.Time
	LastSeen time.Time
}

func afterUserSchemaSQL(sql string) (_sql string) {
	return sql + "\nALTER TABLE auth ADD CONSTRAINT auth_uuid_fkey FOREIGN KEY (uuid) REFERENCES \"user\" (uuid) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE;"
}

func afterUserServiceRegister(h *hitch.Hitch) {
	h.Get("/api/user", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		sess := session.GetSession(r)
		if sess["uid"] == "" {
			ab.Fail(r, http.StatusForbidden, errors.New("user is not logged in"))
		}

		db := ab.GetDB(r)

		user, err := LoadUser(db, sess["uid"])
		ab.MaybeFail(r, http.StatusInternalServerError, err)

		ab.Render(r).
			JSON(user)
	}))
}

func LastSeenMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<commit_msg>/api/user returns empty response instead of 403<commit_after>// Walkhub
// Copyright (C) 2015 Pronovix
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package walkhub

import (
	"errors"
	"net/http"
	"time"

	"github.com/nbio/hitch"
	"github.com/tamasd/ab"
	"github.com/tamasd/ab/providers/auth/google"
	"github.com/tamasd/ab/services/auth"
	"github.com/tamasd/hitch-session"
	"google.golang.org/api/plus/v1"
)

//go:generate abt --output=usergen.go --generate-service-struct-name=UserService --generate-service-list=false entity User

var UserDelegate = &auth.SessionUserDelegate{}

type User struct {
	UUID     string `dbtype:"uuid" dbdefault:"uuid_generate_v4()"`
	Name     string `constructor:"true"`
	Mail     string `constructor:"true"`
	Admin    bool
	Created  time.Time
	LastSeen time.Time
}

func afterUserSchemaSQL(sql string) (_sql string) {
	return sql + "\nALTER TABLE auth ADD CONSTRAINT auth_uuid_fkey FOREIGN KEY (uuid) REFERENCES \"user\" (uuid) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE;"
}

func afterUserServiceRegister(h *hitch.Hitch) {
	h.Get("/api/user", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		sess := session.GetSession(r)
		if sess["uid"] != "" {
			db := ab.GetDB(r)

			user, err := LoadUser(db, sess["uid"])
			ab.MaybeFail(r, http.StatusInternalServerError, err)

			ab.Render(r).
				JSON(user)
		}
	}))
}

func LastSeenMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

{'fraction_non_alphanumeric': 0.12070978286247958, 'fraction_numerical': 0.003969180480971282, 'mean_word_length': 3.993006993006993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3148776', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1377, 'n_words': 429}