Cleaning up some errors

This commit is contained in:
farhan 2020-12-29 15:47:49 +00:00
parent c93dd89332
commit d50dc1ec0d

View File

@ -5,11 +5,11 @@ import (
"strings"
"log"
"encoding/json"
"github.com/jackc/pgx/pgxpool"
"time"
"io/ioutil"
"net/http"
"html"
"errors"
)
type ImageType struct {
@ -63,11 +63,6 @@ type PostJson struct {
instance string
}
type ConnRequest struct {
conn chan *pgxpool.Conn
b chan bool
}
func GetHTTPSession(endpoint string) (RunningInstance) {
ri_mutex.Lock()
o, exist := runninginstances[endpoint]
@ -87,6 +82,11 @@ func GetHTTPSession(endpoint string) (RunningInstance) {
func check_post(uri string) (PostJson, error) {
var postjson PostJson
for _, banned := range settings.Banned {
if strings.Index(uri, "https://" + banned + "/") == 0 {
return postjson, errors.New("Banned instance")
}
}
selectRet := pool.QueryRow(context.Background(), "SELECT id, inReplyTo, published, summary, content, normalized, attributedto, posthash, received_at FROM posts WHERE id = $1", uri)
err := selectRet.Scan(&postjson.ID, &postjson.InReplyTo, &postjson.Published, &postjson.Summary, &postjson.Content, &postjson.normalized, &postjson.AttributedTo, &postjson.posthash, &postjson.receivedAt)
@ -95,7 +95,7 @@ func check_post(uri string) (PostJson, error) {
}
endslash := strings.Index(uri[8:], "/")
if endslash == -1 {
return postjson, nil
return postjson, errors.New("Invalid URI " + uri)
}
postjson.instance = uri[8:endslash+8]
@ -106,16 +106,16 @@ func check_post(uri string) (PostJson, error) {
resp, err := o.client.Do(req)
if err != nil {
return postjson, nil
return postjson, errors.New("Connection error to " + uri)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return postjson, nil
return postjson, errors.New("Read error on " + uri)
}
err = json.Unmarshal(body, &postjson)
if err != nil {
return postjson, nil
return postjson, err
}
if postjson.InReplyTo != "" && postjson.InReplyTo != uri {
@ -128,7 +128,7 @@ func check_post(uri string) (PostJson, error) {
// For now, skip it...
if postjson.AttributedTo == "" {
return postjson, nil
return postjson, errors.New("Invalid AttributedTo value on " + uri)
}
_, err = check_user(postjson.AttributedTo) // This must be done BEFORE the `INSERT INTO posts` below
@ -156,15 +156,20 @@ func check_post(uri string) (PostJson, error) {
func check_user(uri string) (UserJson, error) {
var userjson UserJson
for _, banned := range settings.Banned {
if strings.Index(uri, "https://" + banned + "/") == 0 {
return userjson, errors.New("Banned instance")
}
}
selectRet := pool.QueryRow(context.Background(), "SELECT id, actor_type, inbox, outbox, followers, following, url, preferredUsername, name, summary, icon, image, publicKey, instance FROM accounts WHERE id = $1", uri)
err := selectRet.Scan(&userjson.ID, &userjson.Type, &userjson.Inbox, &userjson.Outbox, &userjson.Followers, &userjson.Following, &userjson.Url, &userjson.PreferredUsername, &userjson.Name, &userjson.Summary, &userjson.Icon.Url, &userjson.Image.Url, &userjson.PublicKey.PublicKeyPem, &userjson.instance)
if err == nil {
return userjson, nil
return userjson, errors.New("Unable to perform select on " + uri)
}
endslash := strings.Index(uri[8:], "/")
if endslash == -1 {
return userjson, nil
return userjson, errors.New("Invalid user: " + uri)
}
userjson.instance = uri[8:endslash+8]