go fmt'ed

This commit is contained in:
farhan 2020-12-29 20:20:02 +00:00
parent b2974b7501
commit 85e0735fa3
8 changed files with 78 additions and 83 deletions

2
db.go
View File

@ -3,8 +3,8 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"github.com/jackc/pgx/pgxpool" "github.com/jackc/pgx/pgxpool"
"log"
) )
func getDbPool() *pgxpool.Pool { func getDbPool() *pgxpool.Pool {

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"github.com/jackc/pgx/pgxpool"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"log" "log"
"net/http" "net/http"
@ -8,7 +9,6 @@ import (
"regexp" "regexp"
"runtime" "runtime"
"sync" "sync"
"github.com/jackc/pgx/pgxpool"
) )
// Current instances // Current instances

View File

@ -23,7 +23,7 @@ const (
) )
type ObjectType struct { type ObjectType struct {
Id string `json:"id"` Id string `json:"id"`
} }
// Parsing Unmarshal JSON type // Parsing Unmarshal JSON type
@ -52,14 +52,14 @@ type AccountType struct {
// Instance's new min_id value // Instance's new min_id value
type RunningInstance struct { type RunningInstance struct {
Software string `json:"software"` Software string `json:"software"`
Version string `json:"version"` Version string `json:"version"`
Status int `json:"status"` Status int `json:"status"`
LastRun string `json:"lastrun"` LastRun string `json:"lastrun"`
CaptureType string `json:"capturetype"` CaptureType string `json:"capturetype"`
client http.Client client http.Client
client_id string client_id string
client_secret string client_secret string
} }
type NodeInfoSoftware struct { type NodeInfoSoftware struct {

View File

@ -14,13 +14,13 @@ import (
var p *bluemonday.Policy var p *bluemonday.Policy
var spaceReg *regexp.Regexp var spaceReg *regexp.Regexp
func GetRunner(endpoint string) (RunningInstance) { func GetRunner(endpoint string) RunningInstance {
ri_mutex.Lock() ri_mutex.Lock()
o, exists := runninginstances[endpoint] o, exists := runninginstances[endpoint]
if exists == false { if exists == false {
o := RunningInstance{} o := RunningInstance{}
tr := &http.Transport {MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second} tr := &http.Transport{MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second}
o.client = http.Client{Transport: tr} o.client = http.Client{Transport: tr}
o.Status = KEEPALIVE o.Status = KEEPALIVE
@ -31,15 +31,13 @@ func GetRunner(endpoint string) (RunningInstance) {
return o return o
} }
func UpdateRunner(endpoint string, o RunningInstance) { func UpdateRunner(endpoint string, o RunningInstance) {
ri_mutex.Lock() ri_mutex.Lock()
runninginstances[endpoint] = o runninginstances[endpoint] = o
ri_mutex.Unlock() ri_mutex.Unlock()
} }
func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
func GetNodeInfo(endpoint string, o RunningInstance) (RunningInstance) {
/* Checking order /* Checking order
* Mastodon/Pleroma * Mastodon/Pleroma
* Um..nothing else yet * Um..nothing else yet

View File

@ -26,7 +26,7 @@ func (e *authError) Error() string {
return e.msg return e.msg
} }
func register_client(endpoint string, o *RunningInstance) (error) { func register_client(endpoint string, o *RunningInstance) error {
requestBodymap, _ := json.Marshal(map[string]string{ requestBodymap, _ := json.Marshal(map[string]string{
"client_name": "Tusky", // Hard-coded in for now... "client_name": "Tusky", // Hard-coded in for now...
"scopes": "read write follow push", "scopes": "read write follow push",
@ -91,7 +91,7 @@ func register_client(endpoint string, o *RunningInstance) (error) {
return nil return nil
} }
func get_client(endpoint string, o *RunningInstance) (error) { func get_client(endpoint string, o *RunningInstance) error {
var err error var err error
client_file := "clients/" + endpoint client_file := "clients/" + endpoint
_, err = os.Stat(client_file) _, err = os.Stat(client_file)
@ -100,7 +100,7 @@ func get_client(endpoint string, o *RunningInstance) (error) {
if err != nil { if err != nil {
log.Print("Unable to open " + client_file + ", creating new client") log.Print("Unable to open " + client_file + ", creating new client")
return err return err
// return register_client(endpoint, o) // return register_client(endpoint, o)
} }
defer f.Close() defer f.Close()
@ -111,15 +111,15 @@ func get_client(endpoint string, o *RunningInstance) (error) {
if err != nil { if err != nil {
log.Print("Unable to read client_id line of " + client_file + ", building new client") log.Print("Unable to read client_id line of " + client_file + ", building new client")
return err return err
// return register_client(endpoint, o) // return register_client(endpoint, o)
} }
client_secret_bin, _, err := rd.ReadLine() client_secret_bin, _, err := rd.ReadLine()
o.client_secret = string(client_secret_bin) o.client_secret = string(client_secret_bin)
if err != nil { if err != nil {
log.Print("Unable to read client_secret line of " + client_file + ", building new client") log.Print("Unable to read client_secret line of " + client_file + ", building new client")
return err return err
// return register_client(endpoint, o) // return register_client(endpoint, o)
// return o // return o
} }
return nil return nil

View File

@ -2,68 +2,67 @@ package main
import ( import (
"context" "context"
"strings"
"log"
"encoding/json" "encoding/json"
"time"
"io/ioutil"
"net/http"
"html"
"errors" "errors"
"html"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
) )
type ImageType struct { type ImageType struct {
// Type string `json:"type"` // Type string `json:"type"`
Url string `json:"url"` Url string `json:"url"`
} }
type PublicKeyType struct { type PublicKeyType struct {
PublicKeyPem string `json:"publicKeyPem"` PublicKeyPem string `json:"publicKeyPem"`
} }
type UserJson struct { type UserJson struct {
ID string `json:"id"` ID string `json:"id"`
Type string `json:"type"` Type string `json:"type"`
Inbox string `json:"inbox"` Inbox string `json:"inbox"`
Outbox string `json:"outbox"` Outbox string `json:"outbox"`
Followers string `json:"followers"` Followers string `json:"followers"`
Following string `json:"following"` Following string `json:"following"`
Url string `json:"url"` Url string `json:"url"`
PreferredUsername string `json:"preferredUsername"` PreferredUsername string `json:"preferredUsername"`
Name string `json:"name"` Name string `json:"name"`
Summary string `json:"summary"` Summary string `json:"summary"`
Icon ImageType `json:"icon"` Icon ImageType `json:"icon"`
Image ImageType `json:"image"` Image ImageType `json:"image"`
PublicKey PublicKeyType `json:"publicKey"` PublicKey PublicKeyType `json:"publicKey"`
instance string instance string
} }
type PostJson struct { type PostJson struct {
ID string `json:"id"` ID string `json:"id"`
InReplyTo string `json:"inReplyTo"` InReplyTo string `json:"inReplyTo"`
normalized string normalized string
posthash []byte posthash []byte
receivedAt time.Time `json:"created_at"` receivedAt time.Time `json:"created_at"`
Content string `json:"content"`
Conversation string `json:"conversation"`
Published time.Time `json:"published"`
Source string `json:"source"`
Summary string `json:"summary"`
// Ignoring tag for now
To []string `json:"to"`
Type string `json:"type"`
Content string `json:"content"` Actor string `json:"actor"`
Conversation string `json:"conversation"` AttributedTo string `json:"attributedTo"`
Published time.Time `json:"published"`
Source string `json:"source"`
Summary string `json:"summary"`
// Ignoring tag for now
To []string `json:"to"`
Type string `json:"type"`
Actor string `json:"actor"` instance string
AttributedTo string `json:"attributedTo"`
instance string
} }
func GetHTTPSession(endpoint string) (RunningInstance) { func GetHTTPSession(endpoint string) RunningInstance {
ri_mutex.Lock() ri_mutex.Lock()
o, exist := runninginstances[endpoint] o, exist := runninginstances[endpoint]
ri_mutex.Unlock() ri_mutex.Unlock()
@ -83,7 +82,7 @@ func GetHTTPSession(endpoint string) (RunningInstance) {
func check_post(uri string) (PostJson, error) { func check_post(uri string) (PostJson, error) {
var postjson PostJson var postjson PostJson
for _, banned := range settings.Banned { for _, banned := range settings.Banned {
if strings.Index(uri, "https://" + banned + "/") == 0 { if strings.Index(uri, "https://"+banned+"/") == 0 {
return postjson, errors.New("Banned instance") return postjson, errors.New("Banned instance")
} }
} }
@ -98,7 +97,7 @@ func check_post(uri string) (PostJson, error) {
if endslash == -1 { if endslash == -1 {
return postjson, errors.New("Invalid URI " + uri) return postjson, errors.New("Invalid URI " + uri)
} }
postjson.instance = uri[8:endslash+8] postjson.instance = uri[8 : endslash+8]
o := GetHTTPSession(postjson.instance) o := GetHTTPSession(postjson.instance)
req, _ := http.NewRequest("GET", uri, nil) req, _ := http.NewRequest("GET", uri, nil)
@ -141,11 +140,11 @@ func check_post(uri string) (PostJson, error) {
_, err = pool.Exec(context.Background(), "INSERT INTO posts (id, inreplyto, published, summary, content, normalized, attributedto, posthash, instance) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)", postjson.ID, postjson.InReplyTo, postjson.Published, postjson.Summary, postjson.Content, postjson.normalized, postjson.AttributedTo, postjson.posthash, postjson.instance) _, err = pool.Exec(context.Background(), "INSERT INTO posts (id, inreplyto, published, summary, content, normalized, attributedto, posthash, instance) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)", postjson.ID, postjson.InReplyTo, postjson.Published, postjson.Summary, postjson.Content, postjson.normalized, postjson.AttributedTo, postjson.posthash, postjson.instance)
if err != nil { if err != nil {
// log.Print("INSERT posts error of " + uri + ": ", err) // log.Print("INSERT posts error of " + uri + ": ", err)
return postjson, err return postjson, err
} }
for _, to := range postjson.To{ for _, to := range postjson.To {
if to != "https://www.w3.org/ns/activitystreams#Public" && to != "" { if to != "https://www.w3.org/ns/activitystreams#Public" && to != "" {
go check_user(to) go check_user(to)
} }
@ -157,7 +156,7 @@ func check_post(uri string) (PostJson, error) {
func check_user(uri string) (UserJson, error) { func check_user(uri string) (UserJson, error) {
var userjson UserJson var userjson UserJson
for _, banned := range settings.Banned { for _, banned := range settings.Banned {
if strings.Index(uri, "https://" + banned + "/") == 0 { if strings.Index(uri, "https://"+banned+"/") == 0 {
return userjson, errors.New("Banned instance") return userjson, errors.New("Banned instance")
} }
} }
@ -171,7 +170,7 @@ func check_user(uri string) (UserJson, error) {
if endslash == -1 { if endslash == -1 {
return userjson, errors.New("Invalid user: " + uri) return userjson, errors.New("Invalid user: " + uri)
} }
userjson.instance = uri[8:endslash+8] userjson.instance = uri[8 : endslash+8]
o := GetHTTPSession(userjson.instance) o := GetHTTPSession(userjson.instance)
req, _ := http.NewRequest("GET", uri, nil) req, _ := http.NewRequest("GET", uri, nil)
@ -191,7 +190,7 @@ func check_user(uri string) (UserJson, error) {
_, err = pool.Exec(context.Background(), "INSERT INTO accounts (id, actor_type, inbox, outbox, followers, following, url, preferredUsername, name, summary, icon, image, publicKey, instance) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", 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) _, err = pool.Exec(context.Background(), "INSERT INTO accounts (id, actor_type, inbox, outbox, followers, following, url, preferredUsername, name, summary, icon, image, publicKey, instance) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", 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 { if err != nil {
// log.Print("INSERT accounts error: ", err) // log.Print("INSERT accounts error: ", err)
return userjson, err return userjson, err
} }

View File

@ -48,7 +48,7 @@ func StreamMastodon(endpoint string, o *RunningInstance) {
resp, err := http_client.Do(req) resp, err := http_client.Do(req)
if err != nil { if err != nil {
log.Print("Unable to stream " + api_timeline + ": ", err) log.Print("Unable to stream "+api_timeline+": ", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -70,8 +70,6 @@ func StreamMastodon(endpoint string, o *RunningInstance) {
continue continue
} }
switch strings.TrimSpace(token[0]) { switch strings.TrimSpace(token[0]) {
case "event": case "event":
name = strings.TrimSpace(token[1]) name = strings.TrimSpace(token[1])
@ -117,10 +115,10 @@ func StreamMastodon(endpoint string, o *RunningInstance) {
} }
} }
// ri_mutex.Lock() // ri_mutex.Lock()
// m = runninginstances[endpoint] // m = runninginstances[endpoint]
// m.LastRun = time.Now().Format(time.RFC3339) // m.LastRun = time.Now().Format(time.RFC3339)
// m.Status = STREAM_ENDED // m.Status = STREAM_ENDED
// runninginstances[endpoint] = m // runninginstances[endpoint] = m
// ri_mutex.Unlock() // ri_mutex.Unlock()
} }

14
web.go
View File

@ -1,21 +1,21 @@
package main package main
import ( import (
// "crypto/sha1" // "crypto/sha1"
"encoding/json" "encoding/json"
"fmt" "fmt"
// "html" // "html"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
// "time" // "time"
) )
// CreateObject - Used by post web receiver // CreateObject - Used by post web receiver
type CreateObject struct { type CreateObject struct {
ID string `json:"id"` ID string `json:"id"`
Actor string `json:"actor"` Actor string `json:"actor"`
Cc []string `json:"cc"` Cc []string `json:"cc"`
Content string `json:"content"` Content string `json:"content"`
@ -25,8 +25,8 @@ type CreateObject struct {
// RelayBase - The base object used by web receiver // RelayBase - The base object used by web receiver
type RelayBase struct { type RelayBase struct {
Actor string `json:"actor"` Actor string `json:"actor"`
Cc []string `json:"cc"` Cc []string `json:"cc"`
Object json.RawMessage `json:"Object"` Object json.RawMessage `json:"Object"`
ID string `json:"id"` ID string `json:"id"`
Published string `json:"published"` Published string `json:"published"`
@ -119,7 +119,7 @@ func inboxHandler() http.HandlerFunc {
go check_post(createobject.ID) go check_post(createobject.ID)
slashend := strings.Index(createobject.ID[8:], "/") slashend := strings.Index(createobject.ID[8:], "/")
newinstance := createobject.ID[8:8+slashend] newinstance := createobject.ID[8 : 8+slashend]
log.Print("The at sign is: ", newinstance) log.Print("The at sign is: ", newinstance)
// Only done if we are crawling // Only done if we are crawling