2021-02-14 10:57:35 +03:00
|
|
|
const sendEventWithMeta = async (roomId, 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',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-08-23 10:25:48 +03:00
|
|
|
const thread = async (roomId, eventId, content, meta) => {
|
2022-04-26 06:09:55 +03:00
|
|
|
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',
|
2023-08-23 10:25:48 +03:00
|
|
|
event_id: eventId,
|
2022-04-26 06:09:55 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-02-14 10:57:35 +03:00
|
|
|
const hasAttachment = (res) => {
|
2021-02-19 09:09:40 +03:00
|
|
|
if (res.status) res = res.status;
|
2021-02-14 10:57:35 +03:00
|
|
|
if (!res.media_attachments) return '<br>';
|
|
|
|
return res.media_attachments.map((media) => {
|
|
|
|
const mediaURL = new URL(media.remote_url);
|
|
|
|
media.name = new URLSearchParams(mediaURL.search).get('name') || 'Unknown file name.';
|
|
|
|
return `File attachment: <a href="${media.remote_url}">${media.name}</a><br>`;
|
|
|
|
}).join('<br>');
|
|
|
|
};
|
|
|
|
|
|
|
|
const notifyFormatter = (res, roomId) => {
|
|
|
|
userDetails = `<b><a href="${config.fediverse.domain}/${res.account.id}">
|
2021-02-19 09:09:40 +03:00
|
|
|
${res.account.acct}</a>`;
|
2021-02-14 10:57:35 +03:00
|
|
|
switch (res.type) {
|
|
|
|
case 'follow':
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'follow' : res.meta = 'redact';
|
|
|
|
meta = `${res.meta} ${res.account.id}`;
|
|
|
|
content = `${userDetails}
|
2021-02-19 09:09:40 +03:00
|
|
|
<font color="#03b381"><b>has followed you.</font>
|
|
|
|
<blockquote><i>${res.account.note}</i></blockquote>`;
|
2021-02-14 10:57:35 +03:00
|
|
|
sendEventWithMeta(roomId, content, meta);
|
|
|
|
break;
|
|
|
|
case 'favourite':
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'favourite' : res.meta = 'redact';
|
|
|
|
meta = `${res.meta} ${res.status.id}`;
|
|
|
|
content = `${userDetails}
|
2021-02-21 09:10:32 +03:00
|
|
|
<font color="#03b381"><b>has <a href="${config.fediverse.domain}/notice/${res.status.id}">favorited</a>
|
2021-02-19 09:09:40 +03:00
|
|
|
your post:</font>
|
|
|
|
<blockquote><i>${res.status.content}</i><br>
|
|
|
|
${hasAttachment(res)}
|
2021-02-21 09:10:32 +03:00
|
|
|
<br>(id: ${res.status.id}) ${registrar.post.visibilityEmoji(res.status.visibility)}
|
2021-02-19 09:09:40 +03:00
|
|
|
</blockquote>`;
|
2021-02-14 10:57:35 +03:00
|
|
|
sendEventWithMeta(roomId, content, res.meta);
|
|
|
|
break;
|
|
|
|
case 'mention':
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'mention' : res.meta = 'redact';
|
|
|
|
meta = `${res.meta} ${res.status.id}`;
|
|
|
|
content = `${userDetails}
|
2021-02-21 09:10:32 +03:00
|
|
|
<font color="#03b381"><b>has <a href="${config.fediverse.domain}/notice/${res.status.id}">mentioned</a>
|
2021-02-19 09:09:40 +03:00
|
|
|
you:</font><blockquote><i>${res.status.content}</i><br>
|
2021-03-01 09:06:28 +03:00
|
|
|
${hasAttachment(res)}
|
2021-02-21 09:10:32 +03:00
|
|
|
<br>(id: ${res.status.id}) ${registrar.post.visibilityEmoji(res.status.visibility)}
|
2021-02-19 09:09:40 +03:00
|
|
|
</blockquote>`;
|
2021-02-14 10:57:35 +03:00
|
|
|
sendEventWithMeta(roomId, content, meta);
|
|
|
|
break;
|
|
|
|
case 'reblog':
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'reblog' : res.meta = 'redact';
|
|
|
|
meta = `${res.meta} ${res.status.id}`;
|
|
|
|
content = `${userDetails}
|
2021-02-21 09:10:32 +03:00
|
|
|
<font color="#03b381"><b>has <a href="${config.fediverse.domain}/notice/${res.status.id}">repeated</a>
|
2021-02-19 09:09:40 +03:00
|
|
|
your post:</font><blockquote><i>${res.status.content}</i><br>
|
|
|
|
${hasAttachment(res)}
|
2021-02-21 09:10:32 +03:00
|
|
|
<br>(id: ${res.status.id}) ${registrar.post.visibilityEmoji(res.status.visibility)}
|
2021-02-19 09:09:40 +03:00
|
|
|
</blockquote>`;
|
2021-02-14 10:57:35 +03:00
|
|
|
sendEventWithMeta(roomId, content, meta);
|
|
|
|
break;
|
2023-08-23 10:25:48 +03:00
|
|
|
case 'pleroma:emoji_reaction':
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'react' : res.meta = 'redact';
|
|
|
|
meta = `${res.meta} ${res.status.id}`;
|
|
|
|
content = `${userDetails}
|
|
|
|
<font color="#03b381"><b>has <a href="${config.fediverse.domain}/notice/${res.status.id}">reacted</a> with
|
|
|
|
${ res.emoji_url ? `<a href="${res.emoji_url}">${res.emoji}</a>` : `<span>${res.emoji}</span>` }
|
|
|
|
to your post:</font><blockquote><i>${res.status.content}</i><br>
|
|
|
|
${hasAttachment(res)}
|
|
|
|
<br>(id: ${res.status.id}) ${registrar.post.visibilityEmoji(res.status.visibility)}
|
|
|
|
</blockquote>`;
|
|
|
|
sendEventWithMeta(roomId, content, meta);
|
|
|
|
break;
|
2021-02-14 10:57:35 +03:00
|
|
|
default:
|
2021-02-23 06:06:23 +03:00
|
|
|
return console.log('Unknown notification type.');
|
2021-02-14 10:57:35 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-26 06:09:55 +03:00
|
|
|
const isOriginal = (res, roomId, event) => {
|
2021-02-14 10:57:35 +03:00
|
|
|
if (res.data) res = res.data;
|
2021-02-21 09:10:32 +03:00
|
|
|
userDetails = `<b><a href="${config.fediverse.domain}/notice/${res.id}">
|
2021-02-14 10:57:35 +03:00
|
|
|
${res.account.acct}</a>`;
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'status' : res.meta = 'redact';
|
|
|
|
meta = `${res.meta} ${res.id}`;
|
|
|
|
content = `${userDetails}
|
|
|
|
<blockquote><i>${res.content}</i><br>
|
|
|
|
${hasAttachment(res)}
|
|
|
|
<br>(id: ${res.id}) ${registrar.post.visibilityEmoji(res.visibility)}
|
|
|
|
</blockquote>`;
|
2022-04-26 06:09:55 +03:00
|
|
|
if (res.label == 'thread') thread(roomId, event, content, meta);
|
|
|
|
else sendEventWithMeta(roomId, content, meta);
|
2021-02-14 10:57:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const isReblog = (res, roomId) => {
|
|
|
|
if (res.data) res = res.data;
|
2021-02-21 09:10:32 +03:00
|
|
|
userDetails = `<b><a href="${config.fediverse.domain}/${res.id}">
|
2021-02-14 10:57:35 +03:00
|
|
|
${res.account.acct}</a>`;
|
|
|
|
fediverse.auth.me !== res.account.url ? res.meta = 'status' : res.meta = 'unreblog';
|
|
|
|
meta = `${res.meta} ${res.reblog.id}`;
|
|
|
|
content = `${userDetails}
|
2021-02-21 09:10:32 +03:00
|
|
|
<font color="#7886D7"><b>has repeated</a>
|
|
|
|
<a href="${config.fediverse.domain}/notice/${res.reblog.id}">${res.reblog.account.acct}</a>'s post:</font>
|
2021-02-14 10:57:35 +03:00
|
|
|
<blockquote><i>${res.content}</i><br>
|
|
|
|
${hasAttachment(res)}
|
|
|
|
<br>(id: ${res.reblog.id}) ${registrar.post.visibilityEmoji(res.visibility)}
|
|
|
|
</blockquote>`;
|
|
|
|
sendEventWithMeta(roomId, content, meta);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.sendEventWithMeta = sendEventWithMeta;
|
|
|
|
|
2022-04-26 06:09:55 +03:00
|
|
|
module.exports.thread = thread;
|
|
|
|
|
|
|
|
module.exports.formatter = (res, roomId, event) => {
|
2021-02-14 10:57:35 +03:00
|
|
|
const filtered = (res.label === 'notifications')
|
|
|
|
? notifyFormatter(res, roomId)
|
|
|
|
: (res.reblog == null)
|
2022-04-26 06:09:55 +03:00
|
|
|
? isOriginal(res, roomId, event)
|
2021-02-14 10:57:35 +03:00
|
|
|
: isReblog(res, roomId);
|
|
|
|
return filtered;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.follow = (roomId, account, event, original) => {
|
|
|
|
axios({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${config.fediverse.domain}/api/v1/accounts/${account[0].id}/follow`,
|
|
|
|
headers: { Authorization: `Bearer ${fediverse.auth.access_token}` },
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
matrix.utils.addReact(event, '✅');
|
|
|
|
matrix.utils.editNoticeHTML(roomId, original, `<code>Followed ${account[0].acct}.</code>`);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
matrix.utils.addReact(event, '❌');
|
|
|
|
matrix.utils.sendError(event, roomId, e);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.unfollow = (roomId, account, event, original) => {
|
|
|
|
axios({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${config.fediverse.domain}/api/v1/accounts/${account[0].id}/unfollow`,
|
|
|
|
headers: { Authorization: `Bearer ${fediverse.auth.access_token}` },
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
matrix.utils.addReact(event, '✅');
|
|
|
|
matrix.utils.editNoticeHTML(roomId, original, `<code>Unfollowed ${account[0].acct}.</code>`);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
matrix.utils.addReact(event, '❌');
|
|
|
|
matrix.utils.sendError(event, roomId, e);
|
|
|
|
});
|
|
|
|
};
|
2021-02-22 12:48:10 +03:00
|
|
|
|
2021-02-26 07:21:41 +03:00
|
|
|
module.exports.getStatusMentions = (notice, event) => {
|
2021-02-22 12:48:10 +03:00
|
|
|
const users = axios({
|
|
|
|
method: 'GET',
|
|
|
|
url: `${config.fediverse.domain}/api/v1/statuses/${notice}`,
|
|
|
|
headers: { Authorization: `Bearer ${fediverse.auth.access_token}` },
|
|
|
|
}).then((notice) => {
|
2021-02-26 07:21:41 +03:00
|
|
|
const users = [];
|
|
|
|
users.push('@' + notice.data.account.acct);
|
|
|
|
for(let i = 0; i < notice.data.mentions.length; i++) {
|
|
|
|
if(!config.fediverse.username.includes(notice.data.mentions[i].acct))
|
|
|
|
users.push('@' + notice.data.mentions[i].acct)
|
|
|
|
}
|
|
|
|
return users;
|
2021-02-22 12:48:10 +03:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
matrix.utils.addReact(event, '❌');
|
|
|
|
matrix.utils.sendError(event, roomId, e);
|
|
|
|
});
|
|
|
|
return users;
|
|
|
|
};
|