package main import ( "context" "strings" "log" "encoding/json" "github.com/jackc/pgx/pgxpool" "time" "io/ioutil" "net/http" ) type ImageType struct { // Type string `json:"type"` Url string `json:"url"` } type PublicKeyType struct { PublicKeyPem string `json:"publicKeyPem"` } type UserJson struct { ID string `json:"id"` Type string `json:"type"` Inbox string `json:"inbox"` Outbox string `json:"outbox"` Followers string `json:"followers"` Following string `json:"following"` Url string `json:"url"` PreferredUsername string `json:"preferredUsername"` Name string `json:"name"` Summary string `json:"summary"` Icon ImageType `json:"icon"` Image ImageType `json:"image"` PublicKey PublicKeyType `json:"publicKey"` instance string } type PostJson struct { ID string `json:"id"` InReplyTo string `json:"inReplyTo"` normalized string posthash []byte 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"` Actor string `json:"actor"` AttributedTo string `json:"attributedTo"` instance string } type ConnRequest struct { conn chan *pgxpool.Conn b chan bool } func requestConn() { conn, _:= pool.Acquire(context.Background()) defer conn.Release() for connRequest := range requestconnchan { connRequest.conn <-conn _ = <-connRequest.b } } func GetHTTPSession(endpoint string) (RunningInstance) { ri_mutex.Lock() o, exist := runninginstances[endpoint] ri_mutex.Unlock() if exist == false { o := RunningInstance{} new_client := http.Client{} o.client = new_client o.Status = KEEPALIVE ri_mutex.Lock() runninginstances[endpoint] = o ri_mutex.Unlock() } return o } func check_post(uri string) (PostJson, error) { connrequest := ConnRequest{} connrequest.conn = make(chan *pgxpool.Conn) connrequest.b = make(chan bool) requestconnchan <- connrequest myconn := <-connrequest.conn var postjson PostJson selectRet := myconn.QueryRow(context.Background(), "SELECT id, inReplyTo, published, summary, content, normalized, attributedto, posthash, received_at FROM posts WHERE id = $1", uri) err := selectRet.Scan(&postjson.ID, &postjson.InReplyTo, &postjson.Published, &postjson.Summary, &postjson.Content, &postjson.normalized, &postjson.AttributedTo, &postjson.posthash, &postjson.receivedAt) close(connrequest.b) if err == nil { return postjson, nil } endslash := strings.Index(uri[8:], "/") if endslash == -1 { return postjson, nil } log.Print("Was: " + uri, " ", endslash) postjson.instance = uri[8:endslash+8] o := GetHTTPSession(postjson.instance) req, _ := http.NewRequest("GET", uri, nil) req.Header.Add("Accept", "application/ld+json") resp, err := o.client.Do(req) if err != nil { return postjson, nil } /* err = json.NewDecoder(resp.Body).Decode(&postjson) if err != nil { return postjson, err } */ body, err := ioutil.ReadAll(resp.Body) if err != nil { return postjson, nil } err = json.Unmarshal(body, &postjson) if err != nil { return postjson, nil } if postjson.InReplyTo != "" && postjson.InReplyTo != uri { log.Print("GOING INTO NEW POST: " + postjson.InReplyTo + " " + uri) go check_post(postjson.InReplyTo) } // If AttributedTo is blank, this is likely an authentication failure // For now, skip it... if postjson.AttributedTo == "" { return postjson, nil } check_user(postjson.AttributedTo) // This must be done BEFORE the `INSERT INTO posts` below connrequest = ConnRequest{} connrequest.conn = make(chan *pgxpool.Conn) connrequest.b = make(chan bool) requestconnchan <- connrequest myconn = <-connrequest.conn _, err = myconn.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) close(connrequest.b) if err != nil { log.Print("INSERT posts error of " + uri + ": ", err) return postjson, err } for _, to := range postjson.To{ if to != "https://www.w3.org/ns/activitystreams#Public" && to != "" { go check_user(to) } } return postjson, nil } func check_user(uri string) (UserJson, error) { connrequest := ConnRequest{} connrequest.conn = make(chan *pgxpool.Conn) connrequest.b = make(chan bool) requestconnchan <- connrequest myconn := <-connrequest.conn var userjson UserJson selectRet := myconn.QueryRow(context.Background(), "SELECT id, actor_type, inbox, outbox, followers, following, url, preferredUsername, name, summary, icon, image, publicKey, instance FROM accounts WHERE id = $1", uri) err := selectRet.Scan(&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) close(connrequest.b) if err == nil { return userjson, nil } log.Print("The URI: " + uri) endslash := strings.Index(uri[8:], "/") log.Print("on " + uri + " ", endslash) userjson.instance = uri[8:endslash+8] o := GetHTTPSession(userjson.instance) req, _ := http.NewRequest("GET", uri, nil) req.Header.Add("Accept", "application/ld+json") resp, err := o.client.Do(req) if err != nil { log.Print("Retrieval error: ", err) return userjson, err } err = json.NewDecoder(resp.Body).Decode(&userjson) if err != nil { log.Print("Retrieval error: ", err) return userjson, err } connrequest = ConnRequest{} connrequest.conn = make(chan *pgxpool.Conn) connrequest.b = make(chan bool) requestconnchan <- connrequest myconn = <-connrequest.conn _, err = myconn.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) close(connrequest.b) if err != nil { log.Print("INSERT accounts error: ", err) return userjson, err } return userjson, nil }