Skip to content

Commit

Permalink
threadcap: Fallback to comments more often than Castopod, handle Orde…
Browse files Browse the repository at this point in the history
…redCollectionPage. Regen npm.
  • Loading branch information
johnspurlock-skymethod committed Jun 9, 2022
1 parent 95fb1c4 commit 6691c6e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
16 changes: 11 additions & 5 deletions npm/threadcap/cjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function findOrFetchJson(url, after, fetcher, cache, opts) {
if (status !== 200)
throw new Error(`Expected 200 response for ${url}, found ${status} body=${bodyText}`);
const contentType = headers['content-type'] || '<none>';
if (!contentType.toLowerCase().includes('json'))
const foundJson = contentType.toLowerCase().includes('json') || contentType === '<none>' && bodyText.startsWith('{"');
if (!foundJson)
throw new Error(`Expected json response for ${url}, found ${contentType} body=${bodyText}`);
return JSON.parse(bodyText);
}
Expand Down Expand Up @@ -110,20 +111,25 @@ async function fetchActivityPubCommenter(attributedTo, opts) {
return computeCommenter(object, updateTime);
}
async function fetchActivityPubReplies(id, opts) {
var _a;
const { fetcher, cache, updateTime, callbacks, debug } = opts;
const fetchedObject = await findOrFetchActivityPubObject(id, updateTime, fetcher, cache);
const object = unwrapActivityIfNecessary(fetchedObject, id, callbacks);
const replies = object.type === 'PodcastEpisode' ? object.comments : object.replies;
const replies = object.type === 'PodcastEpisode' ? object.comments : (_a = object.replies) !== null && _a !== void 0 ? _a : object.comments;
if (replies === undefined) {
const message = object.type === 'PodcastEpisode' ? `No 'comments' found on PodcastEpisode object` : `No 'replies' found on object`;
let message = object.type === 'PodcastEpisode' ? `No 'comments' found on PodcastEpisode object` : `No 'replies' found on object`;
const tryPleromaWorkaround = id.includes('/objects/');
if (tryPleromaWorkaround) {
message += ', trying Pleroma workaround';
}
callbacks === null || callbacks === void 0 ? void 0 : callbacks.onEvent({
kind: 'warning',
url: id,
nodeId: id,
message,
object
});
if (id.includes('/objects/')) {
if (tryPleromaWorkaround) {
return await mastodonFindReplies(id, {
after: updateTime,
fetcher,
Expand All @@ -137,7 +143,7 @@ async function fetchActivityPubReplies(id, opts) {
const fetched = new Set();
if (typeof replies === 'string') {
const obj = await findOrFetchActivityPubObject(replies, updateTime, fetcher, cache);
if (obj.type === 'OrderedCollection') {
if (obj.type === 'OrderedCollection' || obj.type === 'OrderedCollectionPage') {
return await collectRepliesFromOrderedCollection(obj, updateTime, id, fetcher, cache, callbacks, fetched);
}
else {
Expand Down
16 changes: 11 additions & 5 deletions npm/threadcap/esm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function findOrFetchJson(url, after, fetcher, cache, opts) {
if (status !== 200)
throw new Error(`Expected 200 response for ${url}, found ${status} body=${bodyText}`);
const contentType = headers['content-type'] || '<none>';
if (!contentType.toLowerCase().includes('json'))
const foundJson = contentType.toLowerCase().includes('json') || contentType === '<none>' && bodyText.startsWith('{"');
if (!foundJson)
throw new Error(`Expected json response for ${url}, found ${contentType} body=${bodyText}`);
return JSON.parse(bodyText);
}
Expand Down Expand Up @@ -110,20 +111,25 @@ async function fetchActivityPubCommenter(attributedTo, opts) {
return computeCommenter(object, updateTime);
}
async function fetchActivityPubReplies(id, opts) {
var _a;
const { fetcher, cache, updateTime, callbacks, debug } = opts;
const fetchedObject = await findOrFetchActivityPubObject(id, updateTime, fetcher, cache);
const object = unwrapActivityIfNecessary(fetchedObject, id, callbacks);
const replies = object.type === 'PodcastEpisode' ? object.comments : object.replies;
const replies = object.type === 'PodcastEpisode' ? object.comments : (_a = object.replies) !== null && _a !== void 0 ? _a : object.comments;
if (replies === undefined) {
const message = object.type === 'PodcastEpisode' ? `No 'comments' found on PodcastEpisode object` : `No 'replies' found on object`;
let message = object.type === 'PodcastEpisode' ? `No 'comments' found on PodcastEpisode object` : `No 'replies' found on object`;
const tryPleromaWorkaround = id.includes('/objects/');
if (tryPleromaWorkaround) {
message += ', trying Pleroma workaround';
}
callbacks === null || callbacks === void 0 ? void 0 : callbacks.onEvent({
kind: 'warning',
url: id,
nodeId: id,
message,
object
});
if (id.includes('/objects/')) {
if (tryPleromaWorkaround) {
return await mastodonFindReplies(id, {
after: updateTime,
fetcher,
Expand All @@ -137,7 +143,7 @@ async function fetchActivityPubReplies(id, opts) {
const fetched = new Set();
if (typeof replies === 'string') {
const obj = await findOrFetchActivityPubObject(replies, updateTime, fetcher, cache);
if (obj.type === 'OrderedCollection') {
if (obj.type === 'OrderedCollection' || obj.type === 'OrderedCollectionPage') {
return await collectRepliesFromOrderedCollection(obj, updateTime, id, fetcher, cache, callbacks, fetched);
}
else {
Expand Down
6 changes: 4 additions & 2 deletions src/threadcap/threadcap_activitypub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ async function fetchActivityPubReplies(id: string, opts: ProtocolUpdateMethodOpt
const { fetcher, cache, updateTime, callbacks, debug } = opts;
const fetchedObject = await findOrFetchActivityPubObject(id, updateTime, fetcher, cache);
const object = unwrapActivityIfNecessary(fetchedObject, id, callbacks);
const replies = object.type === 'PodcastEpisode' ? object.comments : object.replies; // castopod uses 'comments' url to an OrderedCollection
// castopod uses 'comments' url to an OrderedCollection
// also found 'comments' url to an OrderedCollectionPage (no 'replies')
const replies = object.type === 'PodcastEpisode' ? object.comments : (object.replies ?? object.comments);
if (replies === undefined) {
let message = object.type === 'PodcastEpisode' ? `No 'comments' found on PodcastEpisode object` : `No 'replies' found on object`;
const tryPleromaWorkaround = id.includes('/objects/');
Expand All @@ -84,7 +86,7 @@ async function fetchActivityPubReplies(id: string, opts: ProtocolUpdateMethodOpt
const fetched = new Set<string>();
if (typeof replies === 'string') {
const obj = await findOrFetchActivityPubObject(replies, updateTime, fetcher, cache);
if (obj.type === 'OrderedCollection') {
if (obj.type === 'OrderedCollection' || obj.type === 'OrderedCollectionPage') {
return await collectRepliesFromOrderedCollection(obj, updateTime, id, fetcher, cache, callbacks, fetched);
} else {
throw new Error(`Expected 'replies' to point to an OrderedCollection, found ${JSON.stringify(obj)}`);
Expand Down
3 changes: 2 additions & 1 deletion src/threadcap/threadcap_implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export async function findOrFetchJson(url: string, after: Instant, fetcher: Fetc
const { status, headers, bodyText } = response;
if (status !== 200) throw new Error(`Expected 200 response for ${url}, found ${status} body=${bodyText}`);
const contentType = headers['content-type'] || '<none>';
if (!contentType.toLowerCase().includes('json')) throw new Error(`Expected json response for ${url}, found ${contentType} body=${bodyText}`);
const foundJson = contentType.toLowerCase().includes('json') || contentType === '<none>' && bodyText.startsWith('{"');
if (!foundJson) throw new Error(`Expected json response for ${url}, found ${contentType} body=${bodyText}`);
return JSON.parse(bodyText);
}

Expand Down

0 comments on commit 6691c6e

Please sign in to comment.