feat(MSC3440): implement threads for feed and notifications.

fix(sendHtmlNotice): possibly MSC1767 related.
This commit is contained in:
vulet 2022-04-26 03:09:55 +00:00
parent 3122361c6c
commit 5924009154
8 changed files with 56 additions and 10 deletions

View file

@ -0,0 +1,26 @@
exports.runQuery = function (roomId, event, userInput) {
const instance = axios.create({
baseURL: config.fediverse.domain,
method: 'GET',
headers: { Authorization: `Bearer ${fediverse.auth.access_token}` },
});
instance.get(`/api/v1/statuses/${userInput}/context`)
.then(async (response) => {
let story = [];
const original = await instance.get(`/api/v1/statuses/${userInput}`);
const ancestors = response.data.ancestors;
const descendants = response.data.descendants;
story = [...story, ancestors, original.data, descendants];
const book = story.flat();
await fediverse.utils.thread(roomId, event, '<br><hr><h3>...Beginning thread...</h3><hr><br>');
for (const [i, entry] of book.entries()) {
entry.label = 'thread';
fediverse.utils.formatter(entry, roomId, event)
}
await fediverse.utils.thread(roomId, event, '<br><hr><h3>...Thread ended...</h3><hr><br>');
})
.catch((e) => {
matrix.utils.addReact(event, '❌');
matrix.utils.sendError(event, roomId, e);
});
};

View file

@ -8,6 +8,20 @@ const sendEventWithMeta = async (roomId, content, meta) => {
});
};
const thread = async (roomId, event, content, meta) => {
await matrixClient.sendEvent(roomId, 'm.room.message', {
body: content.replace(/<[^<]+?>/g, ''),
msgtype: 'm.notice',
formatted_body: content,
meta: meta,
format: 'org.matrix.custom.html',
'm.relates_to': {
rel_type: 'm.thread',
event_id: event['event']['content']['m.relates_to']['event_id'],
},
})
};
const hasAttachment = (res) => {
if (res.status) res = res.status;
if (!res.media_attachments) return '<br>';
@ -69,7 +83,7 @@ const notifyFormatter = (res, roomId) => {
}
};
const isOriginal = (res, roomId) => {
const isOriginal = (res, roomId, event) => {
if (res.data) res = res.data;
userDetails = `<b><a href="${config.fediverse.domain}/notice/${res.id}">
${res.account.acct}</a>`;
@ -80,7 +94,8 @@ const isOriginal = (res, roomId) => {
${hasAttachment(res)}
<br>(id: ${res.id}) ${registrar.post.visibilityEmoji(res.visibility)}
</blockquote>`;
sendEventWithMeta(roomId, content, meta);
if (res.label == 'thread') thread(roomId, event, content, meta);
else sendEventWithMeta(roomId, content, meta);
};
const isReblog = (res, roomId) => {
@ -101,11 +116,13 @@ const isReblog = (res, roomId) => {
module.exports.sendEventWithMeta = sendEventWithMeta;
module.exports.formatter = (res, roomId) => {
module.exports.thread = thread;
module.exports.formatter = (res, roomId, event) => {
const filtered = (res.label === 'notifications')
? notifyFormatter(res, roomId)
: (res.reblog == null)
? isOriginal(res, roomId)
? isOriginal(res, roomId, event)
: isReblog(res, roomId);
return filtered;
};

View file

@ -47,5 +47,6 @@ module.exports = {
tip: require('./commands/fediverse/tip.js'),
unfollow: require('./commands/fediverse/unfollow.js'),
unpin: require('./commands/fediverse/unpin.js'),
unreblog: require('./commands/fediverse/unreblog.js')
unreblog: require('./commands/fediverse/unreblog.js'),
unroll: require('./commands/fediverse/unroll.js')
};

View file

@ -124,6 +124,7 @@ module.exports.handleReact = async (event) => {
if (reaction.key === '👏') command = 'clap';
if (reaction.key === '🗑️️') command = 'redact';
if (reaction.key === '🌧️') command = 'makeitrain';
if (reaction.key === '') command = 'unroll';
eventHandler(args, roomId, command, event);
};
@ -150,6 +151,7 @@ module.exports.selfReact = async (event) => {
const type = meta.split(' ')[0];
if (type === 'redact' || type === 'unreblog') addReact(event, '🗑️️');
if (type === 'status' || type === 'reblog' || type === 'mention') {
addReact(event, '');
addReact(event, '🔁');
addReact(event, '👏');
if (config.fediverse.tipping === true) addReact(event, '🌧️');