<commit_before>package main

import (
	"net/http"

	"github.com/julienschmidt/httprouter"
)

func JobDelay(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)

	returnChannel := make(chan bool)
	handler := handlerMap[`jobDelay`].(jobDelay)
	handler.input <- waitSpec{
		JobId: params.ByName(`jobid`),
		Reply: returnChannel,
	}
	<-returnChannel
	w.WriteHeader(http.StatusNoContent)
	w.Write(nil)
}

/* Read functions
 */
func ListJobs(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	if ok, _ := IsAuthorized(params.ByName(`AuthenticatedUser`),
		`jobs_list`, ``, ``, ``); !ok {
		DispatchForbidden(&w, nil)
		return
	}
}

func ShowJob(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	if ok, _ := IsAuthorized(params.ByName(`AuthenticatedUser`),
		`jobs_show`, ``, ``, ``); !ok {
		DispatchForbidden(&w, nil)
		return
	}
}

func SearchJob(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	if ok, _ := IsAuthorized(params.ByName(`AuthenticatedUser`),
		`jobs_search`, ``, ``, ``); !ok {
		DispatchForbidden(&w, nil)
		return
	}
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix<commit_msg>Implement Jobs http handlers<commit_after>package main

import (
	"net/http"


	"github.com/julienschmidt/httprouter"
)

func JobDelay(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)

	returnChannel := make(chan bool)
	handler := handlerMap[`jobDelay`].(jobDelay)
	handler.input <- waitSpec{
		JobId: params.ByName(`jobid`),
		Reply: returnChannel,
	}
	<-returnChannel
	w.WriteHeader(http.StatusNoContent)
	w.Write(nil)
}

/* Read functions
 */
func ListJobs(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	var ok, admin bool
	if ok, admin = IsAuthorized(params.ByName(`AuthenticatedUser`),
		`jobs_list`, ``, ``, ``); !ok {
		DispatchForbidden(&w, nil)
		return
	}

	returnChannel := make(chan msg.Result)
	handler := handlerMap[`jobs_r`].(jobsRead)
	handler.input <- msg.Request{
		Type:       `job`,
		Action:     `list`,
		Reply:      returnChannel,
		RemoteAddr: extractAddress(r.RemoteAddr),
		User:       params.ByName(`AuthenticatedUser`),
		IsAdmin:    admin,
	}
	result := <-returnChannel
	SendMsgResult(&w, &result)
}

func ShowJob(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	var ok, admin bool
	if ok, admin = IsAuthorized(params.ByName(`AuthenticatedUser`),
		`jobs_show`, ``, ``, ``); !ok {
		DispatchForbidden(&w, nil)
		return
	}

	returnChannel := make(chan msg.Result)
	handler := handlerMap[`jobs_r`].(jobsRead)
	handler.input <- msg.Request{
		Type:       `job`,
		Action:     `show`,
		Reply:      returnChannel,
		RemoteAddr: extractAddress(r.RemoteAddr),
		User:       params.ByName(`AuthenticatedUser`),
		IsAdmin:    admin,
		Job:        proto.Job{Id: params.ByName(`jobid`)},
	}
	result := <-returnChannel
	SendMsgResult(&w, &result)
}

func SearchJob(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	var ok, admin bool
	if ok, admin = IsAuthorized(params.ByName(`AuthenticatedUser`),
		`jobs_search`, ``, ``, ``); !ok {
		DispatchForbidden(&w, nil)
		return
	}

	cReq := proto.NewJobFilter()
	err := DecodeJsonBody(r, &cReq)
	if err != nil {
		DispatchBadRequest(&w, err)
		return
	}

	returnChannel := make(chan msg.Result)
	handler := handlerMap[`jobs_r`].(jobsRead)
	handler.input <- msg.Request{
		Type:       `jobs`,
		Action:     `search/idlist`,
		Reply:      returnChannel,
		RemoteAddr: extractAddress(r.RemoteAddr),
		User:       params.ByName(`AuthenticatedUser`),
		IsAdmin:    admin,
		Search: msg.Filter{
			IsDetailed: cReq.Flags.Detailed,
			Job: proto.JobFilter{
				IdList: cReq.Filter.Job.IdList,
			},
		},
	}
	result := <-returnChannel
	SendMsgResult(&w, &result)
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix
{'fraction_non_alphanumeric': 0.15202196704942586, 'fraction_numerical': 0.00199700449326011, 'mean_word_length': 4.314323607427056, 'pattern_counts': {'":': 0, '<': 13, '<?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': '5175906', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1482, 'n_words': 284}