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 fedictl $(FEDICTL_GOFILES)
run:
go run $(GOFILES)
go run $(FEDILOGUE_GOFILES)
acctkey:
mkdir -p keys
openssl genrsa -out keys/acctprivate.pem 1024

9
ctl.go
View File

@ -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":

2
db.go
View File

@ -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) {

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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()

View File

@ -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()

5
web.go
View File

@ -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))