Initial create_post.js
This commit is contained in:
commit
b86f7fe33e
4 changed files with 1725 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
json/
|
||||
node_modules/
|
126
create_post.js
Normal file
126
create_post.js
Normal file
|
@ -0,0 +1,126 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// TODO: .env compatibility.
|
||||
const { confirm, editor, input } = require("@inquirer/prompts");
|
||||
|
||||
const commonmark = require("commonmark");
|
||||
const chrono = require('chrono-node');
|
||||
const path = require('path');
|
||||
const fs = require('node:fs/promises');
|
||||
|
||||
(async () => {
|
||||
// Queries with enquirer
|
||||
// User to post as, autofill .env
|
||||
const user = await input({ message: "User to post as ", default: "rallias" });
|
||||
// Domain to post as, autofill .env
|
||||
const domain = await input({ message: "Domain to post as ", default: "dev.brainz.social" });
|
||||
// Post to reply to, autofill empty
|
||||
// Subject, autofill empty
|
||||
const subject = await input({ message: "Subject ", default: "" });
|
||||
// Post content, autofill empty
|
||||
const postContent = (await editor({ message: "Post content " })).replace(/\n$/, "");
|
||||
// Poll yes/no
|
||||
let pollOptions = [], endTime = Date.now()+86400000, multipleChoice = false;
|
||||
const isPoll = await confirm({ message: "Is this a poll post? ", default: false });
|
||||
if (isPoll) {
|
||||
let pollDone = false;
|
||||
while (!pollDone) {
|
||||
const pollOption = await input({ message: "Enter poll option (empty to finish) ", default: "" });
|
||||
if (pollOption === "") {
|
||||
pollDone = true;
|
||||
} else {
|
||||
pollOptions.push(pollOption);
|
||||
}
|
||||
}
|
||||
multipleChoice = await confirm({ message: "Allow multiple selection? ", default: false });
|
||||
let hasEndTime = false;
|
||||
while (!hasEndTime) {
|
||||
endTime = chrono.parseDate(await(input({message: "How long should poll run (time in future/length of time)? "})), new Date(Date.now()), {forwardDate: true});
|
||||
if (endTime > Date.now()) {
|
||||
hasEndTime = true;
|
||||
} else {
|
||||
console.log("Unable to parse date. Try something like \"In one day\" or \"25th of December at Noon\". See \"chrono-node\" NPM module for further details.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Support attachments.
|
||||
let postContentHtml = (new commonmark.HtmlRenderer()).render(new commonmark.Parser().parse(postContent)).replace(/\n$/, "");
|
||||
let timestamp = Date.now();
|
||||
let postId = `${timestamp}`;
|
||||
|
||||
let postObj = {
|
||||
"@context": [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
{
|
||||
"ostatus": "http://ostatus.org#",
|
||||
"sensitive": "as:sensitive",
|
||||
"toot": "http://joinmastodon.org/ns#",
|
||||
"votersCount": "toot:votersCount",
|
||||
"litepub": "http://litepub.social/ns#",
|
||||
"directMessage": "litepub:directMessage"
|
||||
}
|
||||
],
|
||||
"id": `https://${domain}/users/${user}/statuses/${postId}`,
|
||||
"type": "Note",
|
||||
"inReplyTo": null,
|
||||
"published": (new Date(Date.now())).toISOString(),
|
||||
"url": `https://${domain}/users/${user}/statuses/${postId}`,
|
||||
"attributedTo": `https://${domain}/users/${user}`,
|
||||
"to": [
|
||||
"https://www.w3.org/ns/activitystreams#Public"
|
||||
],
|
||||
"cc": [
|
||||
`https://${domain}/users/${user}/followers`
|
||||
],
|
||||
"sensitive": false,
|
||||
"content": postContentHtml,
|
||||
"contentMap": {
|
||||
// TODO: Multiple Language Support
|
||||
"en": postContentHtml
|
||||
},
|
||||
"attachment": [],
|
||||
"tag": [],
|
||||
"replies": {
|
||||
"id": `https://${domain}/users/${user}/statuses/${postId}/replies`,
|
||||
"type": "Collection",
|
||||
"first": {
|
||||
"type": "CollectionPage",
|
||||
"next": `https://${domain}/users/${user}/statuses/${postId}/replies?only_other_accounts=true\u0026page=true`,
|
||||
"partOf": `https://${domain}/users/${user}/statuses/${postId}/replies`,
|
||||
"items": []
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (subject) {
|
||||
postObj.summary = subject;
|
||||
postObj.sensitive = true;
|
||||
}
|
||||
|
||||
if (isPoll) {
|
||||
postObj.type = "Question";
|
||||
const options = pollOptions.map((value) => {
|
||||
return {
|
||||
type: "Note",
|
||||
name: value,
|
||||
replies: {
|
||||
type: "Collection",
|
||||
totalItems: 0
|
||||
}
|
||||
}
|
||||
});
|
||||
if ( multipleChoice ) {
|
||||
postObj.anyOf = options;
|
||||
} else {
|
||||
postObj.oneOf = options;
|
||||
}
|
||||
postObj.endTime = new Date(endTime).toISOString();
|
||||
postObj.votersCount = 0;
|
||||
};
|
||||
const writeDir = path.join(__dirname, "json", "users", user, "statuses");
|
||||
const writePath = path.join(writeDir, `${postId}.json`);
|
||||
await fs.mkdir(writeDir, {recursive: true});
|
||||
await fs.writeFile(writePath, JSON.stringify(postObj));
|
||||
console.log(`Post created successfully! ${writePath}`);
|
||||
})();
|
1579
package-lock.json
generated
Normal file
1579
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
18
package.json
Normal file
18
package.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "brainz-social",
|
||||
"version": "0.0.1",
|
||||
"description": "Brainz Social application",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Andrew Pietila <andrew@hax.technology>",
|
||||
"license": "WTFPL",
|
||||
"dependencies": {
|
||||
"@inquirer/prompts": "^4.2.1",
|
||||
"argparse": "^2.0.1",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"chrono-node": "^2.7.5",
|
||||
"commonmark": "^0.31.0"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue