107 lines
5.2 KiB
JavaScript
107 lines
5.2 KiB
JavaScript
const sanitizeHtml = require("sanitize-html");
|
|
const auth = require("../../../middleware/auth");
|
|
const databaseHandler = require("../../../lib/database-handler");
|
|
|
|
module.exports = {
|
|
route: (routeObj) => {
|
|
routeObj.post(auth.auth_token(true, "write:statuses"), (req, res) => {
|
|
const id = process.hrtime.bigint().toString(10);
|
|
const token = req.brainz.token;
|
|
const account = databaseHandler.getAccountByToken(token.token);
|
|
const created_at = Date.now();
|
|
const statusText = sanitizeHtml(req.body.status, {
|
|
allowedTags: [],
|
|
allowedAttributes: {},
|
|
});
|
|
const status = {
|
|
"@id": `https://${req.header.host}/users/${account.username}/statuses/${id}`,
|
|
"@type": "https://www.w3.org/ns/activitystreams#Note",
|
|
"http://ostatus.org#atomUri": `https://${req.header.host}/users/${account.username}/statuses/${id}`,
|
|
"http://ostatus.org#conversation": null, // TODO: wut
|
|
"http://ostatus.org#inReplyToAtomUri": null,
|
|
"https://www.w3.org/ns/activitystreams#attachment": [],
|
|
"https://www.w3.org/ns/activitystreams#attributedTo": {
|
|
"@id": `https://${req.headers.host}/users/${account.username}`,
|
|
},
|
|
"https://www.w3.org/ns/activitystreams#cc": {
|
|
"@id": `https://${req.headers.host}/users/${account.username}/followers`,
|
|
},
|
|
"https://www.w3.org/ns/activitystreams#content": [
|
|
`<p>${statusText.split("\n").join("</p><p>")}</p>`,
|
|
],
|
|
"https://www.w3.org/ns/activitystreams#inReplyTo": null,
|
|
"https://www.w3.org/ns/activitystreams#published": {
|
|
"@type": "http://www.w3.org/2001/XMLSchema#dateTime",
|
|
"@value": new Date(created_at).toISOString(),
|
|
},
|
|
"https://www.w3.org/ns/activitystreams#replies": {
|
|
"@id": `https://${req.headers.host}/users/${account.username}/statuses/${id}/replies`,
|
|
"@type": "https://www.w3.org/ns/activitystreams#Collection",
|
|
"https://www.w3.org/ns/activitystreams#first": {
|
|
"@type": "https://www.w3.org/ns/activitystreams#CollectionPage",
|
|
"https://www.w3.org/ns/activitystreams#items": [],
|
|
"https://www.w3.org/ns/activitystreams#next": {
|
|
"@id": `https://${req.headers.host}/users/${account.username}/statuses/${id}/replies?only_other_accounts=true&page=true`,
|
|
},
|
|
"https://www.w3.org/ns/activitystreams#partOf": {
|
|
"@id": `https://${req.headers.host}/users/${account.username}/statuses/${id}/replies`,
|
|
},
|
|
},
|
|
},
|
|
"https://www.w3.org/ns/activitystreams#tag": [],
|
|
"https://www.w3.org/ns/activitystreams#to": {
|
|
"@id": "https://www.w3.org/ns/activitystreams#Public",
|
|
},
|
|
"https://www.w3.org/ns/activitystreams#url": {
|
|
"@id": `https://${req.headers.host}/@${account.username}/${id}`,
|
|
},
|
|
};
|
|
|
|
status["https://www.w3.org/ns/activitystreams#summary"] = req.body.spoiler_text ?? "";
|
|
|
|
status["https://www.w3.org/ns/activitystreams#sensitive"] = req.body.spoiler_text ?? req.body.spoiler_text ? true : false;
|
|
|
|
databaseHandler.activity[`https://${req.header.host}/users/${account.username}/statuses/${id}`] = {
|
|
object: JSON.stringify(status),
|
|
type: status["@type"],
|
|
local: true,
|
|
owner: account.username,
|
|
created_at,
|
|
};
|
|
|
|
// TODO: Distribution queue.
|
|
|
|
const retObj = {
|
|
id: status["@id"],
|
|
created_at: status["https://www.w3.org/ns/activitystreams#published"]["@value"],
|
|
in_reply_to_id: null,
|
|
in_reply_to_account_id: null,
|
|
sensitive: status["https://www.w3.org/ns/activitystreams#sensitive"],
|
|
spoiler_text: status["https://www.w3.org/ns/activitystreams#summary"],
|
|
visibility: "public", // TODO: Private posting capability.
|
|
language: "en", // TODO: Other languages.
|
|
uri: status["@id"],
|
|
url: status["@id"],
|
|
replies_count: 0, // Future copy paste jobs - These counts should be accurate.
|
|
reblogs_count: 0,
|
|
favourites_count: 0,
|
|
favourited: false,
|
|
reblogged: false,
|
|
muted: false,
|
|
bookmarked: false,
|
|
content: status["https://www.w3.org/ns/activitystreams#content"][0],
|
|
reblog: null,
|
|
account: {},
|
|
media_attachments: [], // TODO: Support media.
|
|
mentions: [],
|
|
tags: [],
|
|
emojis: [],
|
|
card: null,
|
|
poll: null,
|
|
};
|
|
|
|
res.json(retObj);
|
|
return;
|
|
});
|
|
},
|
|
};
|