fix self-federation (#1044)
This commit is contained in:
@@ -79,6 +79,13 @@ func RemoteTrailGet(e *core.RequestEvent) error {
|
||||
}
|
||||
return e.InternalServerError("Sync failed", err)
|
||||
}
|
||||
if record.Id == "" {
|
||||
// Local content that does not exist (e.g. a stale URL to a
|
||||
// missing local trail): performFullSync short-circuits local
|
||||
// IRIs and returns the unsaved shell — surface a real 404
|
||||
// instead of running access/expand on a non-existent record.
|
||||
return e.NotFoundError("Trail not found", nil)
|
||||
}
|
||||
} else {
|
||||
// We already have it locally. Show and update background.
|
||||
updatedAt := record.GetDateTime("updated").Time()
|
||||
@@ -146,19 +153,38 @@ func findLocalTrailByRemoteInfo(e *core.RequestEvent, ctx context.Context, handl
|
||||
// --- Core Sync Logic ---
|
||||
|
||||
func performFullSync(app core.App, ctx context.Context, reqURL *url.URL, localTrail *core.Record) (*core.Record, error) {
|
||||
client := util.SafeHTTPClient()
|
||||
|
||||
iri := localTrail.GetString("iri")
|
||||
|
||||
// Never federate with ourselves: a trail whose IRI is empty or points back
|
||||
// to this instance is local content (we are the source of truth). Syncing it
|
||||
// would fetch our own origin (or fail on an empty URL); just clear the stale
|
||||
// flag so the record is no longer stuck in a permanent re-sync loop.
|
||||
if iri == "" || util.IsLocalIRI(iri) {
|
||||
if localTrail.GetBool("needs_full_sync") {
|
||||
localTrail.Set("needs_full_sync", false)
|
||||
if err := app.Save(localTrail); err != nil {
|
||||
return localTrail, err
|
||||
}
|
||||
}
|
||||
return localTrail, nil
|
||||
}
|
||||
|
||||
client := util.SafeHTTPClient()
|
||||
remoteUrl, _ := url.Parse(iri)
|
||||
remoteUrl.RawQuery = reqURL.RawQuery // Forward params
|
||||
query := reqURL.Query()
|
||||
query.Del("handle")
|
||||
remoteUrl.RawQuery = query.Encode()
|
||||
origin := fmt.Sprintf("%s://%s", remoteUrl.Scheme, remoteUrl.Host)
|
||||
|
||||
req, _ := http.NewRequestWithContext(ctx, "GET", remoteUrl.String(), nil)
|
||||
res, err := client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
if err != nil {
|
||||
return localTrail, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return localTrail, fmt.Errorf("remote trail fetch %s returned: %d", remoteUrl.String(), res.StatusCode)
|
||||
}
|
||||
|
||||
var remoteMap map[string]any
|
||||
if err := json.NewDecoder(res.Body).Decode(&remoteMap); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user