-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for threads #46
Merged
+82
−37
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import process from 'node:process'; | |
import path from 'node:path'; | ||
import { login } from './lib/login.js'; | ||
import { validateAccount, validateRequest, validateAndExtendRequestReferences } from './lib/validator.js'; | ||
import { REPLY_IN_THREAD } from './lib/posts.js'; | ||
|
||
// The JSON file must contains the following fields: | ||
// - "account": a string field indicating the account to use to perform the action. | ||
|
@@ -20,15 +21,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+ {0,3}([-_*])[ \t]*(?:\1[ \t]*){2,}\n+/g); | ||
const requests = threadElements?.length ? | ||
threadElements.map((richText, i) => ({ | ||
...request, | ||
...(i === 0 ? undefined : { | ||
action: 'reply', | ||
replyURL: REPLY_IN_THREAD, | ||
}), | ||
richText, | ||
})) : [request]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel this would be a lot more readable if it's just if-else blocks instead of lots of ternary branches, it kills way more brain cells than it deserves to understand this expression... |
||
|
||
// 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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that at some point, it would be good to include a proper markdown parser - for example, we can support alternative link text, which is not currently supported in bluesky UI, but supported in their API (the hacker news bot uses this extensively https://bsky.app/profile/newsycombinator.bsky.social), and we can support that too with markdown link syntax, which is a nice-to-have.