fixed replies and added edit in matrix mesages

This commit is contained in:
qugalet 2023-07-19 23:38:12 +03:00
parent 5986b173a3
commit 534b6c436d
2 changed files with 58 additions and 19 deletions

View file

@ -11,6 +11,7 @@ datasource db {
} }
model Post { model Post {
id Int @unique @default(autoincrement())
postId String @id postId String @id
matrixId String @unique matrixId String @unique
} }

View file

@ -24,6 +24,26 @@ function parseAsyncRegexFile(file: Buffer): RegExp[] {
.map(regex => new RegExp(regex)); .map(regex => new RegExp(regex));
} }
async function editText(
client: MatrixClient,
roomId: string,
eventId: string,
text: string
): Promise<void> {
await client.sendEvent(roomId, 'm.room.message', {
body: text,
msgtype: 'm.text',
'm.new_content': {
body: text,
msgtype: 'm.text'
},
'm.relates_to': {
rel_type: 'm.replace',
event_id: eventId
}
});
}
async function main() { async function main() {
const mastodon = await ( const mastodon = await (
await import('masto') await import('masto')
@ -63,14 +83,23 @@ async function main() {
) { ) {
const matrixId = await matrix.sendText( const matrixId = await matrix.sendText(
process.env.MATRIX_ROOM_ID, process.env.MATRIX_ROOM_ID,
`Одобряємо?\n${notification.status.url}\nт - Так\nн - Ні` `${notification.status.url}\nY/т - Так\nN/н - Ні`
); );
await db.post.create({ const { id } = await db.post.create({
data: { data: {
postId: notification.status.id, postId: notification.status.id,
matrixId matrixId
},
select: {
id: true
} }
}); });
await editText(
matrix,
process.env.MATRIX_ROOM_ID,
matrixId,
`#${id} ${notification.status.url}\nY/т - Так\nN/н - Ні`
);
} }
if (notification.type === 'follow') { if (notification.type === 'follow') {
await mastodon.v1.statuses.create({ await mastodon.v1.statuses.create({
@ -79,7 +108,12 @@ async function main() {
}); });
console.log('follow message'); console.log('follow message');
} }
if (notification.type === 'mention' && notification.status?.visibility === 'direct') { if (
notification.type === 'mention' &&
notification.status?.visibility === 'direct' &&
!notification.status.inReplyToId &&
notification.status.content.includes('ping')
) {
await mastodon.v1.statuses.create({ await mastodon.v1.statuses.create({
inReplyToId: notification.status.id, inReplyToId: notification.status.id,
visibility: 'direct', visibility: 'direct',
@ -100,30 +134,34 @@ async function main() {
matrixId: event['content']['m.relates_to']['m.in_reply_to']['event_id'] matrixId: event['content']['m.relates_to']['m.in_reply_to']['event_id']
} }
}); });
if (post && event['content']['body'].split('\n').at(-1) === 'т') { if (
console.log('test'); post &&
['y', 'т'].includes((event['content']['body'].split('\n').at(-1) as string).toLowerCase())
) {
await mastodon.v1.statuses.reblog(post.postId); await mastodon.v1.statuses.reblog(post.postId);
await matrix.replyText( await matrix.replyText(process.env.MATRIX_ROOM_ID, event, 'Пост успішно опубліковано');
process.env.MATRIX_ROOM_ID, await editText(
event['content']['m.relates_to']['m.in_reply_to']['event_id'], matrix,
'Пост успішно опубліковано' roomId,
event['event_id'],
event['content']['body'].split('\n').at(0) + ' ✅'
); );
} }
if (post && event['content']['body'].split('\n').at(-1) === 'н') { if (
await matrix.redactEvent( post &&
process.env.MATRIX_ROOM_ID, ['n', 'н'].includes((event['content']['body'].split('\n').at(-1) as string).toLowerCase())
event['content']['m.relates_to']['event_id'], ) {
'Не одобрили :('
);
await db.post.delete({ await db.post.delete({
where: { where: {
matrixId: event['content']['m.relates_to']['m.in_reply_to']['event_id'] matrixId: event['content']['m.relates_to']['m.in_reply_to']['event_id']
} }
}); });
await matrix.replyText( await matrix.replyText(process.env.MATRIX_ROOM_ID, event, 'Пост відхилено');
process.env.MATRIX_ROOM_ID, await editText(
event['content']['m.relates_to']['m.in_reply_to']['event_id'], matrix,
'Пост відхилено' roomId,
event['event_id'],
event['content']['body'].split('\n').at(0) + ' ❌'
); );
} }
} }