web feature fetching user icon

adding database pool to all functions to verify before fetching
This commit is contained in:
farhan 2020-12-17 18:26:55 +00:00
parent 726ac7e0bd
commit 5bc9a68d53
8 changed files with 27 additions and 23 deletions

View File

@ -5,7 +5,7 @@ build:
go build -o fedilogue $(FEDILOGUE_GOFILES) go build -o fedilogue $(FEDILOGUE_GOFILES)
go build -o fedictl $(FEDICTL_GOFILES) go build -o fedictl $(FEDICTL_GOFILES)
run: run:
go run $(GOFILES) go run $(FEDILOGUE_GOFILES)
acctkey: acctkey:
mkdir -p keys mkdir -p keys
openssl genrsa -out keys/acctprivate.pem 1024 openssl genrsa -out keys/acctprivate.pem 1024

9
ctl.go
View File

@ -8,9 +8,10 @@ import (
"log" "log"
"net" "net"
"os" "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") log.Print("Starting ctl listener on 127.0.0.1:5555")
l, err := net.Listen("tcp", "127.0.0.1:5555") l, err := net.Listen("tcp", "127.0.0.1:5555")
if err != nil { if err != nil {
@ -32,12 +33,12 @@ func startctl(reportPostChan chan ReportPost) {
for { for {
c := <-commandClient // New client connection 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) sizebyte := make([]byte, 4)
var commandmap CommandMap var commandmap CommandMap
@ -79,7 +80,7 @@ func handleClient(commandClient net.Conn, reportPostChan chan ReportPost) {
} else { } else {
responseback.Message = "Added: " + commandmap.Endpoint responseback.Message = "Added: " + commandmap.Endpoint
runninginstances[commandmap.Endpoint] = RunningInstance{} runninginstances[commandmap.Endpoint] = RunningInstance{}
go StartInstance(commandmap.Endpoint, reportPostChan) go StartInstance(commandmap.Endpoint, reportPostChan, pool)
} }
ri_mutex.Unlock() ri_mutex.Unlock()
case "suspend": case "suspend":

2
db.go
View File

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

View File

@ -40,17 +40,15 @@ func main() {
log.Print("Autostarting " + endpoint) log.Print("Autostarting " + endpoint)
ri_mutex.Lock() ri_mutex.Lock()
_, exists := runninginstances[endpoint] _, exists := runninginstances[endpoint]
if exists == true { if exists == false {
log.Print("Already exists: " + endpoint)
} else {
runninginstances[endpoint] = RunningInstance{} runninginstances[endpoint] = RunningInstance{}
go StartInstance(endpoint, reportPostChan) go StartInstance(endpoint, reportPostChan, pool)
} }
ri_mutex.Unlock() ri_mutex.Unlock()
} }
go startctl(reportPostChan) go startctl(reportPostChan, pool)
go webmain(reportPostChan) go webmain(reportPostChan, pool)
runtime.Goexit() runtime.Goexit()
} }

View File

@ -9,6 +9,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"time" "time"
"github.com/jackc/pgx/pgxpool"
) )
var p *bluemonday.Policy var p *bluemonday.Policy
@ -70,7 +71,7 @@ func GetNodeInfo(endpoint string) (http.Client, NodeInfo) {
return 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) http_client, nodeinfo := GetNodeInfo(endpoint)
ri_mutex.Lock() ri_mutex.Lock()
m := runninginstances[endpoint] m := runninginstances[endpoint]
@ -89,14 +90,14 @@ func StartInstance(endpoint string, reportPostChan chan ReportPost) {
m.CaptureType = "Poll" m.CaptureType = "Poll"
runninginstances[endpoint] = m runninginstances[endpoint] = m
ri_mutex.Unlock() ri_mutex.Unlock()
PollMastodonPleroma(endpoint, reportPostChan, http_client) PollMastodonPleroma(endpoint, reportPostChan, http_client, pool)
} else if nodeinfo.Software.Name == "mastodon" { } else if nodeinfo.Software.Name == "mastodon" {
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name) log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
m.CaptureType = "Stream" m.CaptureType = "Stream"
runninginstances[endpoint] = m runninginstances[endpoint] = m
ri_mutex.Unlock() ri_mutex.Unlock()
PollMastodonPleroma(endpoint, reportPostChan, http_client) // PollMastodonPleroma(endpoint, reportPostChan, http_client, pool)
StreamMastodon(endpoint, reportPostChan) StreamMastodon(endpoint, reportPostChan, pool)
} }
} }

View File

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"time" "time"
"github.com/jackc/pgx/pgxpool"
) )
type ImageData struct { type ImageData struct {
@ -36,6 +37,8 @@ type UserInfo struct {
Name string `"json:name"` Name string `"json:name"`
Summary string `"json:summary"` Summary string `"json:summary"`
Url string `"json:Url"` Url string `"json:Url"`
Icon ImageData `"json:icon"`
Image ImageData `"json:image"`
// ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"` // ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"`
// Discoverable bool `"json:discoverable"` // Discoverable bool `"json:discoverable"`
@ -52,7 +55,6 @@ type PostInfo struct {
func fetch_user_info(http_client http.Client, uri string) (UserInfo, error) { func fetch_user_info(http_client http.Client, uri string) (UserInfo, error) {
var userinfo UserInfo var userinfo UserInfo
// http_client := http.Client{}
req, err := http.NewRequest(http.MethodGet, uri, nil) req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil { if err != nil {
return UserInfo{}, err return UserInfo{}, err
@ -98,7 +100,7 @@ func fetch_post(http_client http.Client, uri string) (PostInfo, error) {
return postinfo, nil 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) newposts := make([]ReportPost, 0)
min_id := "" min_id := ""
@ -311,7 +313,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost, http_c
if exists == false || o.Status == KEEPALIVE { if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{} m := RunningInstance{}
runninginstances[newinstance] = m runninginstances[newinstance] = m
go StartInstance(newinstance, reportPostChan) go StartInstance(newinstance, reportPostChan, pool)
} }
ri_mutex.Unlock() ri_mutex.Unlock()

View File

@ -10,9 +10,10 @@ import (
"net/http" "net/http"
"strings" "strings"
"time" "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{} http_client := http.Client{}
var client_id string var client_id string
@ -173,7 +174,7 @@ func StreamMastodon(endpoint string, reportPostChan chan ReportPost) {
if exists == false || o.Status == KEEPALIVE { if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{} m := RunningInstance{}
runninginstances[newinstance] = m runninginstances[newinstance] = m
go StartInstance(newinstance, reportPostChan) go StartInstance(newinstance, reportPostChan, pool)
} }
ri_mutex.Unlock() ri_mutex.Unlock()

5
web.go
View File

@ -11,6 +11,7 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
"github.com/jackc/pgx/pgxpool"
) )
// CreateObject - Used by post web receiver // 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 = strings.ReplaceAll(newpost.normalized, "\t", " ")
newpost.normalized = spaceReg.ReplaceAllString(newpost.normalized, " ") newpost.normalized = spaceReg.ReplaceAllString(newpost.normalized, " ")
newpost.Account.Acct = realuser.PreferredUsername + "@" + newinstance newpost.Account.Acct = realuser.PreferredUsername + "@" + newinstance
newpost.Account.Avatar = "ttt" newpost.Account.Avatar = realuser.Icon.Url
newpost.Account.Bot = false newpost.Account.Bot = false
newpost.Account.Created_at = time.Now().Format(time.RFC3339) newpost.Account.Created_at = time.Now().Format(time.RFC3339)
newpost.Account.Display_name = realuser.Name newpost.Account.Display_name = realuser.Name
@ -260,7 +261,7 @@ func errorHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("404 --> ", r.URL.Path) 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/webfinger", webfinger)
http.HandleFunc("/.well-known/host-meta", hostmeta) http.HandleFunc("/.well-known/host-meta", hostmeta)
http.HandleFunc("/inbox", inboxHandler(reportPostChan)) http.HandleFunc("/inbox", inboxHandler(reportPostChan))