Skip to content

Commit

Permalink
feat: add support for threads
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 22, 2024
1 parent 0abbca2 commit 7b2c720
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
16 changes: 13 additions & 3 deletions actions/login-and-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ if (Object.hasOwn(request, 'richTextFile')) {
richTextFile = path.resolve(path.dirname(requestFilePath), request.richTextFile);
request.richText = fs.readFileSync(richTextFile, 'utf-8');
}
const threadElements = request.action !== 'repost' && request.richText?.split(/\n\n---+\n\n/g);
const requests = threadElements?.length ?
threadElements.map((richText, i) => ({
...request,
...(i === 0 ? undefined : {
action: 'reply',
replyURL: 'REPLACEME'
}),
richText,
})) : [request];

// Validate the account field.
const account = validateAccount(request, process.env);
validateRequest(request);
requests.forEach(validateRequest);

// Authenticate.
const agent = await login(account);

// Validate and extend the post URLs in the request into { cid, uri } records.
await validateAndExtendRequestReferences(agent, request);
await Promise.all(requests.map(request => validateAndExtendRequestReferences(agent, request)));

export { agent, request, requestFilePath, richTextFile };
export { agent, requests, requestFilePath, richTextFile };
66 changes: 36 additions & 30 deletions actions/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,45 @@ import { post } from './lib/posts.js';
// and already in the processed directory.

assert(process.argv[2], `Usage: node process.js $base_path/new/$any_name.json`);
const { agent, request, requestFilePath, richTextFile } = await import('./login-and-validate.js');
const { agent, requests, requestFilePath, richTextFile } = await import('./login-and-validate.js');

let result;
switch(request.action) {
case 'post': {
console.log(`Posting...`, request.richText);
result = await post(agent, request);
break;
};
case 'repost': {
console.log('Reposting...', request.repostURL);
assert(request.repostInfo); // Extended by validateAndExtendRequestReferences.
result = await agent.repost(request.repostInfo.uri, request.repostInfo.cid);
break;
let previousPostURL;
for (const request of requests) {
let result;
switch(request.action) {
case 'post': {
console.log(`Posting...`, request.richText);
result = await post(agent, request);
break;
};
case 'repost': {
console.log('Reposting...', request.repostURL);
assert(request.repostInfo); // Extended by validateAndExtendRequestReferences.
result = await agent.repost(request.repostInfo.uri, request.repostInfo.cid);
break;
}
case 'quote-post': {
console.log(`Quote posting...`, request.repostURL, request.richText);
result = await post(agent, request);
break;
}
case 'reply': {
if (request.replyURL === 'REPLACEME') {
request.replyURL = previousPostURL;
}
console.log(`Replying...`, request.replyURL, request.richText);
result = await post(agent, request);
break;
}
default:
assert.fail('Unknown action ' + request.action);
}
case 'quote-post': {
console.log(`Quote posting...`, request.repostURL, request.richText);
result = await post(agent, request);
break;
}
case 'reply': {
console.log(`Replying...`, request.replyURL, request.richText);
result = await post(agent, request);
break;
}
default:
assert.fail('Unknown action ' + request.action);
console.log('Result', result);
// Extend the result to be written to the processed JSON file.
request.result = result;
previousPostURL = result.uri;
}

console.log('Result', result);
// Extend the result to be written to the processed JSON file.
request.result = result;

const date = new Date().toISOString().slice(0, 10);

const processedDir = path.join(requestFilePath, '..', '..', 'processed');
Expand All @@ -70,7 +76,7 @@ do {
} while (newFile == null);

console.log('Writing..', newFilePath);
await newFile.writeFile(JSON.stringify(request, null, 2), 'utf8');
await newFile.writeFile(JSON.stringify(requests, null, 2), 'utf8');
await newFile.close();

console.log(`Removing..${requestFilePath}`);
Expand Down
18 changes: 18 additions & 0 deletions automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ Content automation is done in the form of adding new JSON files to `records/new`
4. When the PR is merged (either with _Squash and merge_ or with a merge commit, _Rebase and merge_ is not supported), the [process-json](./.github/workflows/process.yml) workflow will run to perform the requested actions, and when it's done, it will move the processed JSON files to `./records/processed` and renamed the file to `YYYY-MM-DD-ID.json` where ID is an incremental ID based on the number of files already processed on that date. It will also add in additional details of the performed actions (e.g. CID and URI of the posted post).
5. When the process workflow is complete (likely within a minute), you should see a commit from the GitHub bot in the main branch moving the JSON file.

### Threads

To send several messages replying to one another, you can separate each tweet
using a Markdown separator (a new paragraph consisting of only three dashes or
more) inside the `richText` or `richTextFile`:

```markdown
Here is the first tweet.

---

Here is the second tweet.

---

Here is the third tweet.
```

## Set up automation in a repository

1. [Set up repository secrets](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository) for accounts.
Expand Down

0 comments on commit 7b2c720

Please sign in to comment.