package main import ( "context" "strings" "log" "encoding/json" "time" "io/ioutil" "net/http" "html" "errors" ) 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 } 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) { var postjson PostJson for _, banned := range settings.Banned { if strings.Index(uri, "https://" + banned + "/") == 0 { return postjson, errors.New("Banned instance") } } selectRet := pool.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) if err == nil { return postjson, nil } endslash := strings.Index(uri[8:], "/") if endslash == -1 { return postjson, errors.New("Invalid URI " + uri) } 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, errors.New("Connection error to " + uri) } body, err := ioutil.ReadAll(resp.Body) if err != nil { return postjson, errors.New("Read error on " + uri) } err = json.Unmarshal(body, &postjson) if err != nil { return postjson, err } if postjson.InReplyTo != "" && postjson.InReplyTo != uri { if 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, errors.New("Invalid AttributedTo value on " + uri) } _, err = check_user(postjson.AttributedTo) // This must be done BEFORE the `INSERT INTO posts` below if err != nil { return postjson, err } postjson.normalized = html.UnescapeString(strings.ToLower(p.Sanitize(postjson.Content))) spaceReg.ReplaceAllString(postjson.normalized, " ") _, err = pool.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) 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) { var userjson UserJson for _, banned := range settings.Banned { if strings.Index(uri, "https://" + banned + "/") == 0 { return userjson, errors.New("Banned instance") } } selectRet := pool.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) if err == nil { return userjson, nil } endslash := strings.Index(uri[8:], "/") if endslash == -1 { return userjson, errors.New("Invalid user: " + uri) } 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 } _, err = pool.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) if err != nil { // log.Print("INSERT accounts error: ", err) return userjson, err } return userjson, nil }