feat(matrix/fediverse): allow users to directly reply on Fediverse posts from Matrix, using <mx-reply> on our new meta field.

feat(matrix/fediverse): allow users to favorite, reblog, and redact Fediverse posts from Matrix, using m.reaction on our new meta field.
feat(fediverse): add suggestions for follow/unfollow commands.
refactor(matrix/fediverse): add meta field for commands by reaction, and commands by reply.
refactor(fediverse): relax polling on timeline/notifs thru new handling.
This commit is contained in:
vulet 2021-02-14 15:57:35 +08:00
parent 430fb350c1
commit 42563ebc35
26 changed files with 619 additions and 375 deletions

67
main.js
View file

@ -1,7 +1,7 @@
global.registrar = require('./registrar.js');
matrix_auth.access_token ? auth.matrixTokenLogin() : auth.getMatrixToken();
if (!fediverse_auth.access_token && config.fediverse.username) auth.registerFediverseApp();
matrix.auth.access_token ? auth.matrixTokenLogin() : auth.getMatrixToken();
if (!fediverse.auth.access_token && config.fediverse.username) auth.registerFediverseApp();
matrixClient.on('RoomMember.membership', (event, member) => {
if (member.membership === 'invite' && member.userId === matrixClient.credentials.userId) {
@ -17,57 +17,26 @@ matrixClient.on('RoomMember.membership', (event, member) => {
}
});
matrixClient.on('Room.timeline', async function (event, room, member, toStartOfTimeline) {
matrixClient.on('event', async (event) => {
if (event.getSender() === matrixClient.credentials.userId) return matrix.utils.selfReact(event);
if (!event.getContent()['m.relates_to']) return;
if (event.event.unsigned.age > 10000) return;
return event.getType() === 'm.room.message'
? matrix.utils.handleReply(event) : matrix.utils.handleReact(event);
});
matrixClient.on('Room.timeline', async (event, member, toStartOfTimeline) => {
if (toStartOfTimeline) return;
if (event.isEncrypted()) await event._decryptionPromise;
if (event.getType() !== 'm.room.message') return;
if (event.getSender() === matrixClient.credentials.userId) return;
if (event.event.unsigned.age > 10000) return;
if (event.getContent().body.charAt(0) === '+') {
console.log(`Logs: ${event.event.sender} - ${event.getContent().body}`);
let args = event.getContent().body.slice(1).trim().split(/ +/g);
let command = args.shift().toLowerCase();
const userInput = args.join(' ');
const flaggedInput = userInput.substr(userInput.indexOf(' ') + 1);
const address = args.slice(0, 1).join(' ').replace(/"/g, '');
args = [];
switch(command) {
case 'config':
return;
case 'help': case 'beg': case 'flood': case 'asdf':
args.push(matrixClient, room);
break;
case 'tip':
args.push(matrixClient, room, address, flaggedInput);
break;
case 'archive': case 'rearchive':
args.push(matrixClient, room, userInput, !!~command.indexOf('re'));
command = 'archive';
break;
case 'post': case 'reply': case 'media': case 'mediareply':
case 'random': case 'randomreply': case 'randommedia': case 'randommediareply':
args.push(matrixClient, room, userInput, {
isReply: !!~command.indexOf('reply'),
hasMedia: !!~command.indexOf('media'),
hasSubject: !!~command.indexOf('random'),
});
command = 'media';
break;
case 'proxy':
try {
const url = new URL(userInput);
command = config.invidious.domains.includes(url.hostname)
? 'invidious'
: config.nitter.domains.includes(url.hostname)
? 'nitter'
: 'proxy';
} catch(e) {}
//fallthrough
default:
args.push(matrixClient, room, userInput);
}
registrar[command] && registrar[command].runQuery.apply(null, args);
roomId = event.event.room_id;
content = event.getContent().body;
if (content.charAt(0) === '+') {
const args = content.slice(1).trim().split(/ +/g);
const command = args.shift().toLowerCase();
console.log(`Logs: ${event.event.sender} - ${content}`);
matrix.utils.eventHandler(args, roomId, command, event);
}
});