initial commit
This commit is contained in:
commit
a1806cbac9
17 changed files with 3155 additions and 0 deletions
115
src/index.ts
Normal file
115
src/index.ts
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import { MatrixClient, MessageEvent, SimpleFsStorageProvider } from 'matrix-bot-sdk';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
function parseAsyncRegexFile(file: Buffer): RegExp[] {
|
||||
const data = file.toString();
|
||||
return data
|
||||
.split('\n')
|
||||
.filter(regex => !regex.trim().startsWith('#'))
|
||||
.map(regex => new RegExp(regex));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const config = JSON.parse((await readFile(join(__dirname, '..', 'config.json'))).toString());
|
||||
console.log(config);
|
||||
const mastodon = await (
|
||||
await import('masto')
|
||||
).login({
|
||||
url: config.mastodonHomeserverURL,
|
||||
accessToken: config.mastodonAccessToken,
|
||||
disableVersionCheck: true
|
||||
});
|
||||
const matrix = new MatrixClient(
|
||||
config.matrixHomeserverURL,
|
||||
config.matrixAccessToken,
|
||||
new SimpleFsStorageProvider('matrix_db.json')
|
||||
);
|
||||
const nicknameRegexes = parseAsyncRegexFile(await readFile(join(__dirname, '..', 'domains.txt')));
|
||||
const domainRegexes = parseAsyncRegexFile(await readFile(join(__dirname, '..', 'nicknames.txt')));
|
||||
const db = new PrismaClient();
|
||||
const ws = await mastodon.v1.stream.streamUser();
|
||||
ws.on('notification', async notification => {
|
||||
console.log('tests');
|
||||
if (
|
||||
nicknameRegexes.some(regex => regex.test(notification.account.displayName)) ||
|
||||
nicknameRegexes.some(regex => regex.test(notification.account.acct.split('@').at(0) || '')) ||
|
||||
domainRegexes.some(regex => regex.test(notification.account.acct.split('@').at(-1) || ''))
|
||||
) {
|
||||
await mastodon.v1.accounts.block(notification.account.id);
|
||||
await matrix.sendText(
|
||||
config.matrixRoomId,
|
||||
`Юзер ${notification.account.acct} був заблокований автоматично`
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
notification.type === 'mention' &&
|
||||
!notification.status?.inReplyToId &&
|
||||
!notification.status?.inReplyToAccountId &&
|
||||
notification.status?.visibility === 'public'
|
||||
) {
|
||||
const matrixId = await matrix.sendText(
|
||||
config.matrixRoomId,
|
||||
`Одобряємо?\n${notification.status.url}\nт - Так\nн - Ні`
|
||||
);
|
||||
await db.post.create({
|
||||
data: {
|
||||
postId: notification.status.id,
|
||||
matrixId
|
||||
}
|
||||
});
|
||||
}
|
||||
if (notification.type === 'follow') {
|
||||
await mastodon.v1.statuses.create({
|
||||
visibility: 'public',
|
||||
status: `Вітаємо у нашій спільноті! @${notification.account.acct}`
|
||||
});
|
||||
console.log('follow message');
|
||||
}
|
||||
});
|
||||
matrix.on('room.message', async (roomId, event) => {
|
||||
if (
|
||||
roomId === config.matrixRoomId &&
|
||||
event['content'] &&
|
||||
['т', 'н'].includes(event['content']['body'].split('\n').at(-1).trim()) &&
|
||||
event['content']['m.relates_to']['m.in_reply_to']
|
||||
) {
|
||||
const post = await db.post.findUnique({
|
||||
where: {
|
||||
matrixId: event['content']['m.relates_to']['m.in_reply_to']['event_id']
|
||||
}
|
||||
});
|
||||
if (post && event['content']['body'].split('\n').at(-1) === 'т') {
|
||||
console.log('test');
|
||||
await mastodon.v1.statuses.reblog(post.postId);
|
||||
await matrix.replyText(
|
||||
config.matrixRoomId,
|
||||
event['content']['m.relates_to']['m.in_reply_to']['event_id'],
|
||||
'Пост успішно опубліковано'
|
||||
);
|
||||
}
|
||||
if (post && event['content']['body'].split('\n').at(-1) === 'н') {
|
||||
await matrix.redactEvent(
|
||||
config.matrixRoomId,
|
||||
event['content']['m.relates_to']['event_id'],
|
||||
'Не одобрили :('
|
||||
);
|
||||
await db.post.delete({
|
||||
where: {
|
||||
matrixId: event['content']['m.relates_to']['m.in_reply_to']['event_id']
|
||||
}
|
||||
});
|
||||
await matrix.replyText(
|
||||
config.matrixRoomId,
|
||||
event['content']['m.relates_to']['m.in_reply_to']['event_id'],
|
||||
'Пост відхилено'
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
await db.$connect();
|
||||
await matrix.start();
|
||||
}
|
||||
main().then(() => console.log('everything started!'));
|
||||
Loading…
Add table
Add a link
Reference in a new issue