2023-10-01 04:18:05 +00:00
|
|
|
package lookup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-08-01 22:15:34 +00:00
|
|
|
"github.com/reiver/go-mstdn"
|
|
|
|
"github.com/reiver/go-mstdn/ent"
|
2023-10-01 04:18:05 +00:00
|
|
|
)
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
var _ http.Handler = internalHandler{}
|
2023-10-01 04:18:05 +00:00
|
|
|
|
|
|
|
const Path string = "/api/v1/accounts/lookup"
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
func Handler(fn LoaderFunc) http.Handler {
|
|
|
|
return internalHandler{
|
|
|
|
loaderFunc:fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalHandler struct {
|
|
|
|
loaderFunc LoaderFunc
|
2023-10-01 04:18:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
func (internalHandler) Path() string {
|
2023-10-01 04:18:05 +00:00
|
|
|
return Path
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
func (receiver internalHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
2023-10-01 04:18:05 +00:00
|
|
|
if nil == resp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil == req {
|
|
|
|
mstdn.InternalServerError(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if http.MethodGet != req.Method {
|
|
|
|
mstdn.MethodNotAllowed(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
fn := receiver.loaderFunc
|
2023-10-01 04:18:05 +00:00
|
|
|
if nil == fn {
|
|
|
|
mstdn.InternalServerError(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var acct string
|
|
|
|
{
|
|
|
|
const key string = "acct"
|
|
|
|
|
|
|
|
url := req.URL
|
|
|
|
if nil == url {
|
|
|
|
mstdn.InternalServerError(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query := url.Query()
|
|
|
|
if nil == query {
|
|
|
|
mstdn.InternalServerError(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !query.Has(key) {
|
|
|
|
mstdn.BadRequest(resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acct = query.Get("acct")
|
|
|
|
}
|
|
|
|
|
|
|
|
var account ent.Account
|
|
|
|
{
|
|
|
|
|
|
|
|
err := fn(&account, acct)
|
|
|
|
if nil != err {
|
|
|
|
mstdn.Error(resp, err.ErrHTTP())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mstdn.OK(resp, account)
|
|
|
|
}
|