Add file handling
This commit is contained in:
parent
5510dc059b
commit
877804cf42
|
@ -15,9 +15,9 @@ Register that one with synapse and start the bridge with `npm run start`.
|
||||||
## Features and roadmap
|
## Features and roadmap
|
||||||
- Matrix -> VK
|
- Matrix -> VK
|
||||||
- [x] Text content
|
- [x] Text content
|
||||||
- [ ] Image content
|
- [x] Image content
|
||||||
- [ ] Audio/Video content
|
- [x] Audio/Video content
|
||||||
- [ ] Other files
|
- [x] Other files
|
||||||
- [x] Replies
|
- [x] Replies
|
||||||
- [ ] Typing notifs - not possible yet
|
- [ ] Typing notifs - not possible yet
|
||||||
- [ ] Presence - not possible yet
|
- [ ] Presence - not possible yet
|
||||||
|
@ -26,7 +26,7 @@ Register that one with synapse and start the bridge with `npm run start`.
|
||||||
- [ ] Message redacts
|
- [ ] Message redacts
|
||||||
- VK -> Matrix
|
- VK -> Matrix
|
||||||
- [x] Text content
|
- [x] Text content
|
||||||
- [ ] Image content
|
- [x] Image content
|
||||||
- [ ] Audio/Video content
|
- [ ] Audio/Video content
|
||||||
- [ ] Stickers
|
- [ ] Stickers
|
||||||
- [ ] Other files
|
- [ ] Other files
|
||||||
|
|
|
@ -47,8 +47,10 @@ if (options.help) {
|
||||||
// here we define some information about our protocol, what features it supports etc.
|
// here we define some information about our protocol, what features it supports etc.
|
||||||
const protocol: IProtocolInformation = {
|
const protocol: IProtocolInformation = {
|
||||||
features: {
|
features: {
|
||||||
file: false,
|
image: true,
|
||||||
|
file: true,
|
||||||
presence: false,
|
presence: false,
|
||||||
|
reply: true,
|
||||||
},
|
},
|
||||||
id: "vk", // an internal ID for the protocol, all lowercase
|
id: "vk", // an internal ID for the protocol, all lowercase
|
||||||
displayname: "VK", // a human-readable name of the protocol
|
displayname: "VK", // a human-readable name of the protocol
|
||||||
|
@ -86,6 +88,8 @@ async function run() {
|
||||||
puppet.on("puppetDelete", vk.deletePuppet.bind(vk));
|
puppet.on("puppetDelete", vk.deletePuppet.bind(vk));
|
||||||
puppet.on("message", vk.handleMatrixMessage.bind(vk));
|
puppet.on("message", vk.handleMatrixMessage.bind(vk));
|
||||||
puppet.on("reply", vk.handleMatrixReply.bind(vk));
|
puppet.on("reply", vk.handleMatrixReply.bind(vk));
|
||||||
|
puppet.on("image", vk.handleMatrixImage.bind(vk));
|
||||||
|
puppet.on("file", vk.handleMatrixFile.bind(vk));
|
||||||
|
|
||||||
|
|
||||||
puppet.setCreateRoomHook(vk.createRoom.bind(vk));
|
puppet.setCreateRoomHook(vk.createRoom.bind(vk));
|
||||||
|
|
135
src/vk.ts
135
src/vk.ts
|
@ -8,11 +8,13 @@ import {
|
||||||
IFileEvent,
|
IFileEvent,
|
||||||
MessageDeduplicator,
|
MessageDeduplicator,
|
||||||
Log,
|
Log,
|
||||||
|
ISendingUser,
|
||||||
} from "mx-puppet-bridge";
|
} from "mx-puppet-bridge";
|
||||||
|
|
||||||
import { VK, MessageContext, Context } from "vk-io";
|
import { VK, MessageContext, Context, AttachmentType } from "vk-io";
|
||||||
import { userInfo } from "os";
|
import { userInfo } from "os";
|
||||||
import { runInThisContext } from "vm";
|
import { runInThisContext } from "vm";
|
||||||
|
import { lookup } from "dns";
|
||||||
|
|
||||||
// here we create our log instance
|
// here we create our log instance
|
||||||
const log = new Log("VKPuppet:vk");
|
const log = new Log("VKPuppet:vk");
|
||||||
|
@ -196,6 +198,7 @@ export class VkPuppet {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
log.info("Sending reply", Number(eventId));
|
||||||
const response = await p.client.api.messages.send({
|
const response = await p.client.api.messages.send({
|
||||||
peer_id: Number(room.roomId),
|
peer_id: Number(room.roomId),
|
||||||
message: data.body,
|
message: data.body,
|
||||||
|
@ -208,6 +211,100 @@ export class VkPuppet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async handleMatrixImage(
|
||||||
|
room: IRemoteRoom,
|
||||||
|
data: IFileEvent,
|
||||||
|
asUser: ISendingUser | null,
|
||||||
|
event: any,
|
||||||
|
) {
|
||||||
|
const p = this.puppets[room.puppetId];
|
||||||
|
if (!p) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const MAXFILESIZE = 50000000;
|
||||||
|
const size = data.info ? data.info.size || 0 : 0;
|
||||||
|
|
||||||
|
if (size < MAXFILESIZE) {
|
||||||
|
try {
|
||||||
|
log.info("Sending image...");
|
||||||
|
const attachment = await p.client.upload.messagePhoto({
|
||||||
|
peer_id: Number(room.roomId),
|
||||||
|
source: {
|
||||||
|
value: data.url,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
log.info("Image sent", attachment);
|
||||||
|
const response = await p.client.api.messages.send({
|
||||||
|
peer_id: Number(room.roomId),
|
||||||
|
random_id: new Date().getTime(),
|
||||||
|
attachment: [`photo${attachment.ownerId}_${attachment.id}`],
|
||||||
|
});
|
||||||
|
await this.puppet.eventSync.insert(room, data.eventId!, response.toString());
|
||||||
|
} catch (err) {
|
||||||
|
log.error("Error sending to vk", err.error || err.body || err);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const response = await p.client.api.messages.send({
|
||||||
|
peer_id: Number(room.roomId),
|
||||||
|
message: `File ${data.filename} was sent, but it is too big for VK. You may download it there:\n${data.url}`,
|
||||||
|
random_id: new Date().getTime(),
|
||||||
|
});
|
||||||
|
await this.puppet.eventSync.insert(room, data.eventId!, response.toString());
|
||||||
|
} catch (err) {
|
||||||
|
log.error("Error sending to vk", err.error || err.body || err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handleMatrixFile(
|
||||||
|
room: IRemoteRoom,
|
||||||
|
data: IFileEvent,
|
||||||
|
asUser: ISendingUser | null,
|
||||||
|
event: any,
|
||||||
|
) {
|
||||||
|
const p = this.puppets[room.puppetId];
|
||||||
|
if (!p) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const MAXFILESIZE = 50000000;
|
||||||
|
const size = data.info ? data.info.size || 0 : 0;
|
||||||
|
|
||||||
|
if (size < MAXFILESIZE) {
|
||||||
|
try {
|
||||||
|
log.info("Sending file...");
|
||||||
|
const attachment = await p.client.upload.messageDocument({
|
||||||
|
peer_id: Number(room.roomId),
|
||||||
|
source: {
|
||||||
|
value: data.url,
|
||||||
|
filename: data.filename,
|
||||||
|
contentType:
|
||||||
|
},
|
||||||
|
});
|
||||||
|
log.info("File sent", attachment);
|
||||||
|
const response = await p.client.api.messages.send({
|
||||||
|
peer_id: Number(room.roomId),
|
||||||
|
random_id: new Date().getTime(),
|
||||||
|
attachment: [`doc${attachment.ownerId}_${attachment.id}`],
|
||||||
|
});
|
||||||
|
await this.puppet.eventSync.insert(room, data.eventId!, response.toString());
|
||||||
|
} catch (err) {
|
||||||
|
log.error("Error sending to vk", err.error || err.body || err);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const response = await p.client.api.messages.send({
|
||||||
|
peer_id: Number(room.roomId),
|
||||||
|
message: `File ${data.filename} was sent, but it is too big for VK. You may download it there:\n${data.url}`,
|
||||||
|
random_id: new Date().getTime(),
|
||||||
|
});
|
||||||
|
await this.puppet.eventSync.insert(room, data.eventId!, response.toString());
|
||||||
|
} catch (err) {
|
||||||
|
log.error("Error sending to vk", err.error || err.body || err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async createRoom(room: IRemoteRoom): Promise<IRemoteRoom | null> {
|
public async createRoom(room: IRemoteRoom): Promise<IRemoteRoom | null> {
|
||||||
const p = this.puppets[room.puppetId];
|
const p = this.puppets[room.puppetId];
|
||||||
if (!p) {
|
if (!p) {
|
||||||
|
@ -235,18 +332,46 @@ export class VkPuppet {
|
||||||
const params = await this.getSendParams(puppetId, context.peerId, context.senderId, context.id.toString());
|
const params = await this.getSendParams(puppetId, context.peerId, context.senderId, context.id.toString());
|
||||||
|
|
||||||
if (context.hasText) {
|
if (context.hasText) {
|
||||||
const opts: IMessageEvent = {
|
|
||||||
body: context.text || "Attachment",
|
|
||||||
};
|
|
||||||
if (context.hasReplyMessage) {
|
if (context.hasReplyMessage) {
|
||||||
if (this.puppet.eventSync.getMatrix(params.room, context.replyMessage!.id.toString())) {
|
if (this.puppet.eventSync.getMatrix(params.room, context.replyMessage!.id.toString())) {
|
||||||
|
const opts: IMessageEvent = {
|
||||||
|
body: context.text || "Attachment",
|
||||||
|
};
|
||||||
// We got referenced message in room, using matrix reply
|
// We got referenced message in room, using matrix reply
|
||||||
await this.puppet.sendReply(params, context.replyMessage!.id.toString(), opts);
|
await this.puppet.sendReply(params, context.replyMessage!.id.toString(), opts);
|
||||||
} else {
|
} else {
|
||||||
// Using a fallback
|
// Using a fallback
|
||||||
|
const opts: IMessageEvent = {
|
||||||
|
body: await this.prependReply(
|
||||||
|
puppetId, context.text || "",
|
||||||
|
context.replyMessage?.text || "",
|
||||||
|
context.senderId.toString(),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
await this.puppet.sendMessage(params, opts);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const opts: IMessageEvent = {
|
||||||
|
body: context.text || "Attachment",
|
||||||
|
};
|
||||||
|
await this.puppet.sendMessage(params, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (context.hasAttachments()) {
|
||||||
|
for (const f of context.attachments) {
|
||||||
|
if (f.type === AttachmentType.PHOTO) {
|
||||||
|
log.info(f);
|
||||||
|
try {
|
||||||
|
// tslint:disable-next-line: no-string-literal
|
||||||
|
await this.puppet.sendFileDetect(params, f["largeSizeUrl"]);
|
||||||
|
} catch (err) {
|
||||||
|
const opts: IMessageEvent = {
|
||||||
|
body: `Image was sent: ${f["largeSizeUrl"]}`,
|
||||||
|
};
|
||||||
|
await this.puppet.sendMessage(params, opts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await this.puppet.sendMessage(params, opts);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue