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 {
id Int @unique @default(autoincrement())
postId String @id
matrixId String @unique
}

View file

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