Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,14 +1304,31 @@ app.get(
postLoaded = true;
if (/^\d+$/.test(threadId)) {
try {
const locationUrl = buildGraphAPIURL(
threadId,
{ [PARAMS__FIELDS]: FIELD__LOCATION_ID },
req.session.access_token
const locationUrl = new URL(
buildGraphAPIURL(
encodeURIComponent(threadId),
{ [PARAMS__FIELDS]: FIELD__LOCATION_ID },
null
)
);
if (
locationUrl.origin !== 'https://graph.threads.com' ||
locationUrl.username ||
locationUrl.password ||
locationUrl.hash
) {
throw new Error('Invalid post location request URL.');
}
if (req.session.access_token) {
locationUrl.searchParams.append(
PARAMS__ACCESS_TOKEN,
req.session.access_token
);
}
const locationResponse = await axios.get(
locationUrl.toString(),
{ httpsAgent: agent, maxRedirects: 0 }
);
const locationResponse = await axios.get(locationUrl, {
httpsAgent: agent,
});
data.location_id = locationResponse.data?.location_id;
} catch {
data.location_id = undefined;
Expand Down
2 changes: 1 addition & 1 deletion test/own-post-deletion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ test('owned detail shows Delete after Quote and Repost and reuses the existing g
assert.equal(reads[1].searchParams.get('fields'), 'location_id');
assert.deepEqual(
readConfigs.map((config) => config.maxRedirects),
[undefined, undefined, 0, 0, 0]
[undefined, 0, 0, 0, 0]
);
const quotePosition = html.indexOf('<td>Quote</td>');
const repostPosition = html.indexOf('<td>Repost</td>');
Expand Down
11 changes: 10 additions & 1 deletion test/post-location.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test('post location IDs link to location details while poll and ghost fields kee
const calls = [];
let locationId = '345';
let locationUnavailable = false;
let locationConfig;
const request = await startApp(t, async (url, options) => {
const apiUrl = new URL(url);
calls.push(apiUrl);
Expand All @@ -84,6 +85,7 @@ test('post location IDs link to location details while poll and ghost fields kee
if (apiUrl.pathname === '/345')
return { data: { id: '345', name: 'Example Location' } };
if (apiUrl.searchParams.get('fields') === 'location_id') {
locationConfig = options;
if (locationUnavailable)
throw new Error('Optional location field unavailable.');
return { data: { location_id: locationId } };
Expand All @@ -102,8 +104,11 @@ test('post location IDs link to location details while poll and ghost fields kee
assert.ok(
!calls[0].searchParams.get('fields').split(',').includes('location_id')
);
assert.equal(calls[1].origin, 'https://graph.threads.com');
assert.equal(calls[1].pathname, '/123');
assert.equal(calls[1].searchParams.get('fields'), 'location_id');
assert.equal(calls[1].searchParams.get('access_token'), 'test-token');
assert.equal(locationConfig.maxRedirects, 0);
assert.match(
html,
/<td>Location ID<\/td><td><a href="\/locationSearch\?locationId=345">345<\/a>/
Expand Down Expand Up @@ -157,12 +162,16 @@ test('failed primary loads and signed-out requests never perform an optional loc
response: { data: { error: { message: 'Post unavailable.' } } },
};
});
let signedOutCalls = 0;
const signedOut = await startApp(
t,
() => assert.fail('Unexpected API request'),
() => {
signedOutCalls++;
},
false
);
assert.equal((await signedOut('/threads/123')).status, 302);
assert.equal(signedOutCalls, 0);
const html = await (await request('/threads/123')).text();
assert.equal(calls, 1);
assert.doesNotMatch(html, /locationId=/);
Expand Down