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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Use this sample app to try the [Threads API](https://developers.facebook.com/doc

- **Login and Account:** Sign in with Threads. View your profile details or sign out.
- **Publishing:** Publish text, image, video, and carousel posts.
- **Publishing Options:** Add topic tags, link attachments, alt text, and polls. Set the reply audience, add spoilers, or create ghost posts.
- **Publishing Options:** Add topic tags, link attachments, alt text, and polls. Set the reply audience, add spoilers, or create ghost posts. Select **AI-Generated Content** to request an **AI info** label on a post (not available for ghost posts).
- **Replies, Quotes, and Reposts:** Reply to, quote, or repost an existing post.
- **Post Browsing:** Browse your posts, your replies, and posts that mention you. View post details and conversations.
- **Reply Management:** Hide or unhide replies in the direct-replies view.
Expand Down
24 changes: 24 additions & 0 deletions postman/threads-api.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@
"description": "The ID of the location being tagged.",
"disabled": true
},
{
"key": "is_made_with_ai",
"value": "true",
"description": "Optional boolean. Set to true to request an AI info label. Defaults to false and does not prevent automatic labeling. Not supported for ghost posts.",
"disabled": true
},
{
"key": "auto_publish_text",
"value": "true",
Expand Down Expand Up @@ -672,6 +678,12 @@
"description": "The ID of the location being tagged.",
"disabled": true
},
{
"key": "is_made_with_ai",
"value": "true",
"description": "Optional boolean. Set to true to request an AI info label. Defaults to false and does not prevent automatic labeling. Not supported for ghost posts.",
"disabled": true
},
{
"key": "text_entities",
"value": "[{\"entity_type\": \"SPOILER\",\"offset\": 0,\"length\": 4}]",
Expand Down Expand Up @@ -841,6 +853,12 @@
"description": "The ID of the location being tagged.",
"disabled": true
},
{
"key": "is_made_with_ai",
"value": "true",
"description": "Optional boolean. Set to true to request an AI info label. Defaults to false and does not prevent automatic labeling. Not supported for ghost posts.",
"disabled": true
},
{
"key": "text_entities",
"value": "[{\"entity_type\": \"SPOILER\",\"offset\": 0,\"length\": 4}]",
Expand Down Expand Up @@ -1330,6 +1348,12 @@
"description": "The ID of the location being tagged.",
"disabled": true
},
{
"key": "is_made_with_ai",
"value": "true",
"description": "Optional boolean. Set to true to request an AI info label. Defaults to false and does not prevent automatic labeling. Not supported for ghost posts.",
"disabled": true
},
{
"key": "text_entities",
"value": "[{\"entity_type\": \"SPOILER\",\"offset\": 0,\"length\": 4}]",
Expand Down
5 changes: 5 additions & 0 deletions public/scripts/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ document.addEventListener('DOMContentLoaded', async () => {
'poll-attachment-options'
);

const aiCheckbox = document.getElementById('is-made-with-ai');
if (ghostPostCheckbox) {
ghostPostCheckbox.addEventListener('change', async () => {
if (aiCheckbox) {
aiCheckbox.disabled = ghostPostCheckbox.checked;
if (ghostPostCheckbox.checked) aiCheckbox.checked = false;
}
if (locationInput) {
locationInput.disabled = ghostPostCheckbox.checked;
if (ghostPostCheckbox.checked) locationInput.value = '';
Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const PARAMS__DELETE_QUOTA_USAGE = 'delete_quota_usage';
const PARAMS__FIELDS = 'fields';
const PARAMS__HIDE = 'hide';
const PARAMS__INPUT_TOKEN = 'input_token';
const PARAMS__IS_MADE_WITH_AI = 'is_made_with_ai';
const PARAMS__LIMIT = 'limit';
const PARAMS__LINK_ATTACHMENT = 'link_attachment';
const PARAMS__LOCATION_ID = 'location_id';
Expand Down Expand Up @@ -860,6 +861,7 @@ app.post(
quotePostId,
spoilerMedia,
ghostPostMedia,
isMadeWithAI,
} = req.body;

const pollOptions = [
Expand Down Expand Up @@ -965,11 +967,26 @@ app.post(
});
}

if (isMadeWithAI !== undefined && isMadeWithAI !== 'on') {
return res.status(400).json({
error: true,
message: 'Check the AI-Generated Content option and try again.',
});
}
const discloseAI = isMadeWithAI === 'on';
if (discloseAI && ghostPostMedia) {
return res.status(400).json({
error: true,
message: 'Ghost posts cannot request an AI info label.',
});
}

const params = {
[PARAMS__REPLY_CONTROL]: replyControl,
[PARAMS__REPLY_TO_ID]: replyToId,
};
if (locationID) params[PARAMS__LOCATION_ID] = locationID;
if (discloseAI) params[PARAMS__IS_MADE_WITH_AI] = true;
if (linkAttachment) {
params[PARAMS__LINK_ATTACHMENT] = linkAttachment;
}
Expand Down
14 changes: 14 additions & 0 deletions test/attachment-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('media hides and disables text attachments, restores drafts, and preserves
const pollControl = element();
pollControl.querySelectorAll = () => [...pollInputs, pollButton];
const ghost = element();
const ai = element('on');
const linkControl = element();
const mediaType = element();
const attachments = element();
Expand All @@ -53,6 +54,7 @@ test('media hides and disables text attachments, restores drafts, and preserves
'.reply-options-media-control': element(),
};
const fields = {
'is-made-with-ai': ai,
'media-type': mediaType,
'link-attachment': link,
'location-id': element('123'),
Expand Down Expand Up @@ -132,9 +134,14 @@ test('media hides and disables text attachments, restores drafts, and preserves
const location = fields['location-id'];
assert.equal(location.value, '123');
assert.equal(location.disabled, false);
assert.equal(ai.checked, false);
assert.equal(ai.disabled, false);
ai.checked = true;
ghost.checked = true;
await ghost.events.change();
assert.equal(location.value, '');
assert.equal(ai.checked, false);
assert.equal(ai.disabled, true);
assert.equal(location.disabled, true);
assertTextAttachments(false);
assert.equal(link.value, '');
Expand All @@ -147,12 +154,19 @@ test('media hides and disables text attachments, restores drafts, and preserves
ghost.checked = false;
await ghost.events.change();
assert.equal(location.value, '');
assert.equal(ai.checked, false);
assert.equal(ai.disabled, false);
assert.equal(location.disabled, false);
assertTextAttachments(true);

const html = pug.renderFile(path.join(__dirname, '../views/upload.pug'), {
title: 'Upload',
});
const checkbox = html.match(/<input[^>]*name="isMadeWithAI"[^>]*>/)?.[0];
assert.ok(checkbox);
assert.match(checkbox, /type="checkbox"/);
assert.match(checkbox, /value="on"/);
assert.doesNotMatch(checkbox, /\b(?:checked|disabled)\b/);
assert.match(
html,
/<div class="poll-attachment-media-control(?: [^"]+)?"><button id="poll-attachment-button"/
Expand Down
100 changes: 98 additions & 2 deletions test/attachment-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async function checkError(response, status, message) {
assert.deepEqual(await response.json(), { error: true, message });
}

test('upload validates attachments and applies location tags only to parent containers', async (t) => {
test('upload validates attachments and applies location tags and AI disclosure only to parent containers', async (t) => {
const calls = [];
const locationID = '9007199254740993123';
const { request } = await startApp(t, {
Expand Down Expand Up @@ -164,6 +164,8 @@ test('upload validates attachments and applies location tags only to parent cont
);
}
assert.equal(calls.length, 5);
for (const target of calls)
assert.equal(target.searchParams.has('is_made_with_ai'), false);
assert.equal(calls[0].searchParams.has('location_id'), false);
for (const index of [0, 1, 3, 4]) {
assert.equal(calls[index].searchParams.has('link_attachment'), false);
Expand Down Expand Up @@ -206,7 +208,10 @@ test('upload validates attachments and applies location tags only to parent cont
'/upload',
uploadForm({
...mediaSet(count, mode),
...(mode === 'mixed' && { locationId: locationID }),
...(mode === 'mixed' && {
locationId: locationID,
isMadeWithAI: 'on',
}),
})
);
assert.equal(response.status, 200);
Expand All @@ -216,6 +221,7 @@ test('upload validates attachments and applies location tags only to parent cont
const fields = mediaSet(count, mode);
for (const [index, child] of batch.slice(0, -1).entries()) {
assert.equal(child.searchParams.get('is_carousel_item'), 'true');
assert.equal(child.searchParams.has('is_made_with_ai'), false);
assert.equal(child.searchParams.has('location_id'), false);
assert.equal(
child.searchParams.get('media_type'),
Expand All @@ -227,6 +233,10 @@ test('upload validates attachments and applies location tags only to parent cont
);
}
assert.equal(batch.at(-1).searchParams.get('media_type'), 'CAROUSEL');
assert.equal(
batch.at(-1).searchParams.get('is_made_with_ai'),
mode === 'mixed' ? 'true' : null
);
assert.equal(
batch.at(-1).searchParams.get('location_id'),
mode === 'mixed' ? locationID : null
Expand Down Expand Up @@ -464,6 +474,92 @@ test('upload validates attachments and applies location tags only to parent cont
assert.equal(target.searchParams.get('fields'), 'status');
}
assert.deepEqual(logs, []);

const disclosedCalls = [];
let disclosureReads = 0;
const { request: disclose } = await startApp(t, {
get: async () => {
disclosureReads++;
assert.fail('Unexpected Graph read.');
},
post: async (url) => {
disclosedCalls.push({ target: new URL(url) });
return { status: 200, data: { id: '999' } };
},
});
const invalidMessage =
'Check the AI-Generated Content option and try again.';
const repeated = uploadForm({ isMadeWithAI: 'on' });
repeated.append('isMadeWithAI', 'on');
for (const body of [
repeated,
uploadForm({ 'isMadeWithAI[value]': 'on' }),
uploadForm({ isMadeWithAI: 'true' }),
]) {
await checkError(await disclose('/upload', body), 400, invalidMessage);
}
await checkError(
await disclose(
'/upload',
uploadForm({ ghostPostMedia: 'true', isMadeWithAI: 'on' })
),
400,
'Ghost posts cannot request an AI info label.'
);
assert.equal(disclosedCalls.length, 0);
assert.equal(disclosureReads, 0);

for (const [mediaType, attachments] of [
['TEXT', {}],
['IMAGE', media],
['VIDEO', mediaSet(1, 'Video')],
]) {
const before = disclosedCalls.length;
const response = await disclose(
'/upload',
uploadForm({ ...attachments, isMadeWithAI: 'on' })
);
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), { id: '999' });
assert.equal(disclosedCalls.length, before + 1);
const { target } = disclosedCalls.at(-1);
assert.equal(target.pathname, '/456/threads');
assert.equal(target.searchParams.get('media_type'), mediaType);
assert.equal(target.searchParams.get('is_made_with_ai'), 'true');
}
const disclosedPublish = await disclose(
'/publish',
uploadForm({ containerId: '999' })
);
assert.equal(disclosedPublish.status, 200);
assert.deepEqual(await disclosedPublish.json(), { id: '999' });
assert.equal(disclosedCalls.length, 4);
const { target: disclosedPublishTarget } = disclosedCalls.at(-1);
assert.equal(disclosedPublishTarget.pathname, '/456/threads_publish');
assert.equal(disclosedPublishTarget.searchParams.get('creation_id'), '999');
assert.equal(
disclosedPublishTarget.searchParams.has('is_made_with_ai'),
false
);

const beforeAutoPublish = disclosedCalls.length;
const autoPublished = await disclose(
'/upload',
uploadForm({ autoPublishText: 'on', isMadeWithAI: 'on' })
);
assert.equal(autoPublished.status, 200);
assert.deepEqual(await autoPublished.json(), {
redirectUrl: '/threads/999',
});
assert.equal(disclosedCalls.length, beforeAutoPublish + 1);
const { target: autoPublishTarget } = disclosedCalls.at(-1);
assert.equal(autoPublishTarget.pathname, '/456/threads');
assert.equal(autoPublishTarget.searchParams.get('media_type'), 'TEXT');
assert.equal(
autoPublishTarget.searchParams.get('auto_publish_text'),
'true'
);
assert.equal(autoPublishTarget.searchParams.get('is_made_with_ai'), 'true');
});

test('browser rejects incompatible media attachments before submitting', () => {
Expand Down
6 changes: 6 additions & 0 deletions views/upload.pug
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ block content
input(type='checkbox' name='autoPublishText' id='autoPublishText')
| Auto-Publish (only for text posts)

div
label.upload-checkbox
input(type='checkbox' name='isMadeWithAI' id='is-made-with-ai' value='on' aria-describedby='ai-info-hint')
| AI-Generated Content
p#ai-info-hint Select to request an AI info label. Leaving this unchecked does not prevent automatic labeling.

div.ghost-post-media-control
label.upload-checkbox
input(type='checkbox' name='ghostPostMedia' value='true')
Expand Down