2020-11-24 20:36:47 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-12 05:50:43 +00:00
|
|
|
"fmt"
|
2020-12-17 04:23:25 +00:00
|
|
|
"github.com/jackc/pgx/pgxpool"
|
|
|
|
"log"
|
2020-11-24 20:36:47 -05:00
|
|
|
)
|
|
|
|
|
2020-12-14 23:09:51 +00:00
|
|
|
func postHandler(reportPostChan chan ReportPost, pool *pgxpool.Pool) {
|
|
|
|
for { // Write posts
|
|
|
|
v := <-reportPostChan
|
|
|
|
go writePost(pool, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 20:36:47 -05:00
|
|
|
func writePost(pool *pgxpool.Pool, reportpost ReportPost) {
|
|
|
|
conn, err := pool.Acquire(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error connecting to database:", err)
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
// Insert new account if new
|
|
|
|
var accountid int
|
2020-12-07 15:47:44 -05:00
|
|
|
err = conn.QueryRow(context.Background(), "INSERT INTO accounts (acct, avatar, bot, created_at, display_name, uri) VALUES($1, $2, $3, $4, $5, $6) ON CONFLICT(acct) DO UPDATE SET acct=EXCLUDED.acct RETURNING id", reportpost.Account.Acct, reportpost.Account.Avatar, reportpost.Account.Bot, reportpost.Account.Created_at, reportpost.Account.Display_name, reportpost.Account.Url).Scan(&accountid)
|
2020-11-24 20:36:47 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Print("First ", err)
|
|
|
|
log.Print("--------------------------")
|
|
|
|
log.Print("Reported error: ", err)
|
|
|
|
log.Print("Account Acct: ", reportpost.Account.Acct)
|
|
|
|
log.Print("Account Avatar: ", reportpost.Account.Avatar)
|
|
|
|
log.Print("Account Avatar len: ", len(reportpost.Account.Avatar))
|
|
|
|
log.Print("Account Bot: ", reportpost.Account.Bot)
|
|
|
|
log.Print("Account Created_at: ", reportpost.Account.Created_at)
|
|
|
|
log.Print("Account Display: ", reportpost.Account.Display_name)
|
|
|
|
log.Print("Account URL: ", reportpost.Account.Url)
|
|
|
|
log.Print(reportpost)
|
|
|
|
log.Print("--------------------------")
|
|
|
|
log.Fatal("Unable to write record to database")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert new post if new
|
2020-12-16 02:41:45 +00:00
|
|
|
_, err = conn.Exec(context.Background(), "INSERT INTO posts (uri, content, created_at, normalized, account_id, posthash) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (posthash) DO NOTHING", reportpost.Uri, reportpost.Content, reportpost.Created_at, reportpost.normalized, accountid, reportpost.posthash)
|
2020-11-24 20:36:47 -05:00
|
|
|
if err != nil { // For now I want to know why this failed.
|
|
|
|
log.Print("Second ", err)
|
|
|
|
log.Print("--------------------------")
|
|
|
|
log.Print("Reported error: ", err)
|
2020-12-16 02:41:45 +00:00
|
|
|
log.Print("Uri: ", reportpost.Uri)
|
2020-11-24 20:36:47 -05:00
|
|
|
log.Print("Content: ", reportpost.Content)
|
|
|
|
log.Print("Created_at: ", reportpost.Created_at)
|
|
|
|
log.Print("normalized: ", reportpost.normalized)
|
|
|
|
log.Print("account_id", accountid)
|
|
|
|
log.Print("posthash: ", reportpost.posthash)
|
|
|
|
log.Print("--------------------------")
|
|
|
|
log.Fatal("Unable to write record to database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 04:23:25 +00:00
|
|
|
func getDbPool() *pgxpool.Pool {
|
2020-11-24 20:36:47 -05:00
|
|
|
// Setup Database
|
2020-12-17 04:23:25 +00:00
|
|
|
dbURI := fmt.Sprintf("postgres://%s:%s@%s:%d/fedilogue", settings.Database.Username, settings.Database.Password, settings.Database.Host, settings.Database.Port)
|
|
|
|
pool, err := pgxpool.Connect(context.Background(), dbURI)
|
2020-11-24 20:36:47 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to connect to database:", err)
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|