package main import ( "github.com/microcosm-cc/bluemonday" "encoding/json" "crypto/sha1" "io/ioutil" "net/http" "strings" "html" "time" "fmt" ) func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { // Make this a global variable p := bluemonday.NewPolicy() newposts := make([]ReportPost, 0) min_id := "" http_client := http.Client{Timeout: 5 * time.Second} for { m := runninginstances[endpoint] api_timeline := "https://" + endpoint + "/api/v1/timelines/public?limit=40&min_id=" + min_id resp, err := http_client.Get(api_timeline) m.LastRun = time.Now().Format("2006.01.02-15:04:05") if err != nil { ri_mutex.Lock() m.Status = CLIENT_ISSUE runninginstances[endpoint] = m ri_mutex.Unlock() return } if resp.StatusCode == 429 { // Apparently you just need to do this to throw away the body _, _ = ioutil.ReadAll(resp.Body) resp.Body.Close() // Release as soon as done ri_mutex.Lock() m.Status = TOOMANYREQUESTS runninginstances[endpoint] = m ri_mutex.Unlock() time.Sleep(time.Second * 30) continue } body, err := ioutil.ReadAll(resp.Body) if err != nil { ri_mutex.Lock() m.Status = BAD_RESPONSE runninginstances[endpoint] = m ri_mutex.Unlock() return } err = json.Unmarshal(body, &newposts) if err != nil { ri_mutex.Lock() m.Status = BAD_RESPONSE runninginstances[endpoint] = m ri_mutex.Unlock() return } resp.Body.Close() // Release as soon as done ri_mutex.Lock() m.Status = RUNNING runninginstances[endpoint] = m ri_mutex.Unlock() for _, newpost := range newposts { if newpost.Account.Acct == "" { continue } posthash := sha1.New() at_sign := strings.Index(newpost.Account.Acct, "@") if at_sign == -1 { at_sign = len(newpost.Account.Acct) newpost.Account.Acct += "@" + endpoint } // Calculate the post hash fmt.Fprint(posthash, newpost.Url) fmt.Fprint(posthash, newpost.normalized) fmt.Fprint(posthash, newpost.Account.Acct) fmt.Fprint(posthash, newpost.Account.Display_name) newpost.posthash = posthash.Sum(nil) newpost.normalized = html.UnescapeString(strings.ToLower(p.Sanitize(newpost.Content))) reportPostChan <- newpost // Check min_id if newpost.Id > min_id { min_id = newpost.Id } newinstance := newpost.Account.Acct[at_sign+1:] ri_mutex.Lock() _, exists := runninginstances[newinstance] if exists == false { m := RunningInstance{} runninginstances[newinstance] = m go StartInstance(newinstance, reportPostChan) } ri_mutex.Unlock() } time.Sleep(time.Second * 10) } } // Change this to return a proper "err" func GetNodeInfo(endpoint string) (NodeInfo) { var nodeinfo NodeInfo api_nodeinfo := "https://" + endpoint + "/nodeinfo/2.0.json" http_client := http.Client{Timeout: 5 * time.Second} resp, err := http_client.Get(api_nodeinfo) if err != nil { fmt.Println("Make a legit error here") return NodeInfo{} } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) err = json.Unmarshal(body, &nodeinfo) if err != nil { fmt.Println("Make a legit error here") fmt.Println("Unmarshal 2"); return NodeInfo{} } return nodeinfo } func StartInstance(endpoint string, reportPostChan chan ReportPost) { nodeinfo := GetNodeInfo(endpoint) if nodeinfo.Software.Name == "" { var m = runninginstances[endpoint] m.Software = "" m.LastRun = time.Now().Format("2006.01.02-15:04:05") m.Status = UNSUPPORTED_INSTANCE ri_mutex.Lock() runninginstances[endpoint] = m ri_mutex.Unlock() return } if nodeinfo.Software.Name == "pleroma" || nodeinfo.Software.Name == "mastodon" { go PollMastodonPleroma(endpoint, reportPostChan) } } /* func SuspendInstance(suspendinstance InstanceReport) { for i, runninginstance := range runninginstances { if runninginstance.Endpoint == suspendinstance.endpoint { (runninginstances)[i].Status = suspendinstance.status (runninginstances)[i].LastRun = time.Now().Format("2006.01.02-15:04:05") return } } } */