From 5bc9a68d533fb4eb10f4d0863951c34576efdd7a Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Thu, 17 Dec 2020 18:26:55 +0000 Subject: [PATCH] web feature fetching user icon adding database pool to all functions to verify before fetching --- Makefile | 2 +- ctl.go | 9 +++++---- db.go | 2 +- fedilogue.go | 10 ++++------ instance.go | 9 +++++---- poll.go | 8 +++++--- stream.go | 5 +++-- web.go | 5 +++-- 8 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index b3577a7..6ee58d0 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ build: go build -o fedilogue $(FEDILOGUE_GOFILES) go build -o fedictl $(FEDICTL_GOFILES) run: - go run $(GOFILES) + go run $(FEDILOGUE_GOFILES) acctkey: mkdir -p keys openssl genrsa -out keys/acctprivate.pem 1024 diff --git a/ctl.go b/ctl.go index 080da81..48cc8a1 100644 --- a/ctl.go +++ b/ctl.go @@ -8,9 +8,10 @@ import ( "log" "net" "os" + "github.com/jackc/pgx/pgxpool" ) -func startctl(reportPostChan chan ReportPost) { +func startctl(reportPostChan chan ReportPost, pool *pgxpool.Pool) { log.Print("Starting ctl listener on 127.0.0.1:5555") l, err := net.Listen("tcp", "127.0.0.1:5555") if err != nil { @@ -32,12 +33,12 @@ func startctl(reportPostChan chan ReportPost) { for { c := <-commandClient // New client connection - go handleClient(c, reportPostChan) + go handleClient(c, reportPostChan, pool) } } -func handleClient(commandClient net.Conn, reportPostChan chan ReportPost) { +func handleClient(commandClient net.Conn, reportPostChan chan ReportPost, pool *pgxpool.Pool) { sizebyte := make([]byte, 4) var commandmap CommandMap @@ -79,7 +80,7 @@ func handleClient(commandClient net.Conn, reportPostChan chan ReportPost) { } else { responseback.Message = "Added: " + commandmap.Endpoint runninginstances[commandmap.Endpoint] = RunningInstance{} - go StartInstance(commandmap.Endpoint, reportPostChan) + go StartInstance(commandmap.Endpoint, reportPostChan, pool) } ri_mutex.Unlock() case "suspend": diff --git a/db.go b/db.go index 1cabca3..8dd68b8 100644 --- a/db.go +++ b/db.go @@ -3,8 +3,8 @@ package main import ( "context" "fmt" - "github.com/jackc/pgx/pgxpool" "log" + "github.com/jackc/pgx/pgxpool" ) func postHandler(reportPostChan chan ReportPost, pool *pgxpool.Pool) { diff --git a/fedilogue.go b/fedilogue.go index 5cfbf17..f86146d 100644 --- a/fedilogue.go +++ b/fedilogue.go @@ -40,17 +40,15 @@ func main() { log.Print("Autostarting " + endpoint) ri_mutex.Lock() _, exists := runninginstances[endpoint] - if exists == true { - log.Print("Already exists: " + endpoint) - } else { + if exists == false { runninginstances[endpoint] = RunningInstance{} - go StartInstance(endpoint, reportPostChan) + go StartInstance(endpoint, reportPostChan, pool) } ri_mutex.Unlock() } - go startctl(reportPostChan) - go webmain(reportPostChan) + go startctl(reportPostChan, pool) + go webmain(reportPostChan, pool) runtime.Goexit() } diff --git a/instance.go b/instance.go index b34d932..cc6f609 100644 --- a/instance.go +++ b/instance.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" "time" + "github.com/jackc/pgx/pgxpool" ) var p *bluemonday.Policy @@ -70,7 +71,7 @@ func GetNodeInfo(endpoint string) (http.Client, NodeInfo) { return http_client, nodeinfo } -func StartInstance(endpoint string, reportPostChan chan ReportPost) { +func StartInstance(endpoint string, reportPostChan chan ReportPost, pool *pgxpool.Pool) { http_client, nodeinfo := GetNodeInfo(endpoint) ri_mutex.Lock() m := runninginstances[endpoint] @@ -89,14 +90,14 @@ func StartInstance(endpoint string, reportPostChan chan ReportPost) { m.CaptureType = "Poll" runninginstances[endpoint] = m ri_mutex.Unlock() - PollMastodonPleroma(endpoint, reportPostChan, http_client) + PollMastodonPleroma(endpoint, reportPostChan, http_client, pool) } else if nodeinfo.Software.Name == "mastodon" { log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name) m.CaptureType = "Stream" runninginstances[endpoint] = m ri_mutex.Unlock() - PollMastodonPleroma(endpoint, reportPostChan, http_client) - StreamMastodon(endpoint, reportPostChan) +// PollMastodonPleroma(endpoint, reportPostChan, http_client, pool) + StreamMastodon(endpoint, reportPostChan, pool) } } diff --git a/poll.go b/poll.go index 950332c..68b74a5 100644 --- a/poll.go +++ b/poll.go @@ -10,6 +10,7 @@ import ( "net/http" "strings" "time" + "github.com/jackc/pgx/pgxpool" ) type ImageData struct { @@ -36,6 +37,8 @@ type UserInfo struct { Name string `"json:name"` Summary string `"json:summary"` Url string `"json:Url"` + Icon ImageData `"json:icon"` + Image ImageData `"json:image"` // ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"` // Discoverable bool `"json:discoverable"` @@ -52,7 +55,6 @@ type PostInfo struct { func fetch_user_info(http_client http.Client, uri string) (UserInfo, error) { var userinfo UserInfo - // http_client := http.Client{} req, err := http.NewRequest(http.MethodGet, uri, nil) if err != nil { return UserInfo{}, err @@ -98,7 +100,7 @@ func fetch_post(http_client http.Client, uri string) (PostInfo, error) { return postinfo, nil } -func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost, http_client http.Client) { +func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost, http_client http.Client, pool *pgxpool.Pool) { newposts := make([]ReportPost, 0) min_id := "" @@ -311,7 +313,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost, http_c if exists == false || o.Status == KEEPALIVE { m := RunningInstance{} runninginstances[newinstance] = m - go StartInstance(newinstance, reportPostChan) + go StartInstance(newinstance, reportPostChan, pool) } ri_mutex.Unlock() diff --git a/stream.go b/stream.go index b8d43ba..e44f140 100644 --- a/stream.go +++ b/stream.go @@ -10,9 +10,10 @@ import ( "net/http" "strings" "time" + "github.com/jackc/pgx/pgxpool" ) -func StreamMastodon(endpoint string, reportPostChan chan ReportPost) { +func StreamMastodon(endpoint string, reportPostChan chan ReportPost, pool *pgxpool.Pool) { http_client := http.Client{} var client_id string @@ -173,7 +174,7 @@ func StreamMastodon(endpoint string, reportPostChan chan ReportPost) { if exists == false || o.Status == KEEPALIVE { m := RunningInstance{} runninginstances[newinstance] = m - go StartInstance(newinstance, reportPostChan) + go StartInstance(newinstance, reportPostChan, pool) } ri_mutex.Unlock() diff --git a/web.go b/web.go index d8bfbe4..8ca2154 100644 --- a/web.go +++ b/web.go @@ -11,6 +11,7 @@ import ( "os" "strings" "time" + "github.com/jackc/pgx/pgxpool" ) // CreateObject - Used by post web receiver @@ -160,7 +161,7 @@ func inboxHandler(reportPostChan chan ReportPost) http.HandlerFunc { newpost.normalized = strings.ReplaceAll(newpost.normalized, "\t", " ") newpost.normalized = spaceReg.ReplaceAllString(newpost.normalized, " ") newpost.Account.Acct = realuser.PreferredUsername + "@" + newinstance - newpost.Account.Avatar = "ttt" + newpost.Account.Avatar = realuser.Icon.Url newpost.Account.Bot = false newpost.Account.Created_at = time.Now().Format(time.RFC3339) newpost.Account.Display_name = realuser.Name @@ -260,7 +261,7 @@ func errorHandler(w http.ResponseWriter, r *http.Request) { fmt.Println("404 --> ", r.URL.Path) } -func webmain(reportPostChan chan ReportPost) { +func webmain(reportPostChan chan ReportPost, pool *pgxpool.Pool) { http.HandleFunc("/.well-known/webfinger", webfinger) http.HandleFunc("/.well-known/host-meta", hostmeta) http.HandleFunc("/inbox", inboxHandler(reportPostChan))