192 lines
5.1 KiB
Go
Raw Normal View History

package main
import (
2020-12-17 04:23:25 +00:00
"encoding/json"
"io/ioutil"
"net/http"
"time"
2022-01-01 02:26:39 +00:00
"gitlab.com/khanzf/fedilogue/shared"
)
type ImageData struct {
2020-12-17 04:23:25 +00:00
Type string `"json:type"`
Url string `"json:url"`
}
type PublicKeyData struct {
2020-12-17 04:23:25 +00:00
Id string `"json:id"`
Owner string `"json:owner"`
PublicKeyPem string `"json:publicKeyPem"`
}
type UserInfo struct {
2020-12-17 04:23:25 +00:00
Id string `"json:id"`
Type string `"json:type"`
Following string `"json:following"`
Followers string `"json:followers"`
Inbox string `"json:inbox"`
Outbox string `"json:outbox"`
Featured string `"json:featured"`
PreferredUsername string `"json:preferredUsername"`
PublicKey PublicKeyData `"json:publicKeyPem"`
Name string `"json:name"`
Summary string `"json:summary"`
Url string `"json:Url"`
Icon ImageData `"json:icon"`
Image ImageData `"json:image"`
2020-12-17 04:23:25 +00:00
// ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"`
// Discoverable bool `"json:discoverable"`
}
type PostInfo struct {
2020-12-17 04:23:25 +00:00
Id string `"json:id"`
Type string `"json:type"`
Published string `"json:published"`
Url string `"json:Url"`
Content string `"json:content"`
}
2022-01-01 02:26:39 +00:00
func PollMastodonPleroma(endpoint string, o *shared.RunningInstance) {
newactivities := make([]shared.ReportActivity, 0)
min_id := ""
parsing_error := 0
use_auth := false
var last_refresh int64
var client_id string
var client_secret string
var oauthData OAuth
var err error
for _, extaccount := range settings.Externalaccounts {
if extaccount.Endpoint == endpoint {
use_auth = true
2021-01-29 17:44:16 -05:00
o, _ := GetRunner(endpoint)
2022-01-01 02:26:39 +00:00
if o.Banned == true {
return // banned endpoint
}
err = get_client(endpoint, &o)
if err != nil {
2022-01-01 02:26:39 +00:00
logErr("Unable to register client for "+endpoint+": ", err)
2020-12-23 08:20:03 +00:00
return
}
oauthData, err = oauth_login(endpoint, &o, extaccount.Username, extaccount.Password)
if err != nil {
2022-01-01 02:26:39 +00:00
logErr("Unable to login to "+endpoint+": ", err)
return
}
last_refresh = time.Now().Unix()
}
}
for {
ri_mutex.Lock()
m := runninginstances[endpoint]
ri_mutex.Unlock()
api_timeline := "https://" + endpoint + "/api/v1/timelines/public?limit=40&since_id=" + min_id
req, err := http.NewRequest("GET", api_timeline, nil)
req.Header.Set("User-Agent", "Tusky")
if err != nil {
2022-01-01 02:26:39 +00:00
logFatal.Fatal("Unable to create new request for "+endpoint+": ", err)
return
}
if use_auth == true {
2020-12-17 04:23:25 +00:00
if time.Now().Unix() > last_refresh+oauthData.Expires_in {
oauthData, err = oauth_refresh(endpoint, client_id, client_secret, oauthData.Refresh_token)
if err != nil {
2022-01-01 02:26:39 +00:00
logWarn("Unable to refresh oauth token for "+endpoint+": ", err)
return
}
last_refresh = time.Now().Unix()
}
req.Header.Add("Authorization", oauthData.Access_token)
}
m.LastRun = time.Now().Format(time.RFC3339)
resp, err := DoTries(o, req)
if err != nil {
2022-01-01 02:26:39 +00:00
m.Status = shared.CLIENT_ISSUE
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
2022-01-01 02:26:39 +00:00
logWarn("Giving up on "+endpoint+": ", err.Error())
return
}
2022-01-01 02:26:39 +00:00
if resp.StatusCode == shared.TOOMANYREQUESTS { // Short Delay, 30 seconds
2021-02-11 21:05:01 +00:00
logWarn("Delaying "+endpoint+", gave status ", resp.StatusCode, ", 1 hour delay")
_, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close() // Release as soon as done
m.Status = resp.StatusCode
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
time.Sleep(time.Second * 30)
continue
2022-01-01 02:26:39 +00:00
} else if resp.StatusCode == shared.INTERNAL_ERROR { // Longer delay, 1 hour
logWarn("Suspending "+endpoint+", gave status ", resp.StatusCode, ", 1 hour delay")
_, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close() // Release as soon as done
m.Status = 765
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
time.Sleep(time.Second * 3600)
continue
} else if resp.StatusCode != 200 { // Crash
2022-01-01 02:26:39 +00:00
logErr("Terminating "+endpoint+", gave status ", resp.StatusCode)
_, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close() // Release as soon as done
m.Status = resp.StatusCode
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
return
}
err = json.NewDecoder(resp.Body).Decode(&newactivities)
2021-01-30 14:30:49 +00:00
resp.Body.Close() // Release as soon as done
if err != nil {
if parsing_error > 5 {
2022-01-01 02:26:39 +00:00
m.Status = shared.BAD_RESPONSE
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
2021-02-11 21:05:01 +00:00
logErr("Giving up on " + endpoint + " after 5 unmarshal errors.")
return
}
parsing_error = parsing_error + 1
time.Sleep(time.Second * 30)
}
2022-01-01 02:26:39 +00:00
m.Status = shared.RUNNING
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
for _, newactivity := range newactivities {
go check_activity(newactivity.Uri)
matchset := re.FindStringSubmatch(newactivity.Uri)
2021-01-10 21:47:08 +00:00
if matchset != nil {
newinstance := matchset[1]
2021-01-10 21:47:08 +00:00
// Check min_id
if newactivity.Id > min_id {
min_id = newactivity.Id
2021-01-10 21:47:08 +00:00
}
2021-01-10 21:47:08 +00:00
go CheckInstance(newinstance, endpoint)
}
}
time.Sleep(time.Second * 10)
}
}