Initial support for user tokens

This commit is contained in:
Inex Code 2020-11-22 09:59:39 +00:00
parent 0dca30bddb
commit 73042f8e93
3 changed files with 76 additions and 18 deletions

View file

@ -48,8 +48,6 @@ Register that one with synapse and start the bridge with `npm run start`.
- [ ] Access token revoked on VK side - [ ] Access token revoked on VK side
- [ ] Bot is kicked out on VK side - [ ] Bot is kicked out on VK side
- Probably more, send an issue! - Probably more, send an issue!
- Support for user tokens
- [ ] Auth as a user instead of group
## Usage ## Usage
@ -78,3 +76,44 @@ Also, make sure your registration file contains this:
``` ```
de.sorunome.msc2409.push_ephemeral: true de.sorunome.msc2409.push_ephemeral: true
``` ```
## Using a user token instead of group bot
This is experimental and is not the main goal of this bridge.
To get a user token, use something like this:
```
https://oauth.vk.com/authorize?client_id=<CLIENT_ID>&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=friends,messages,offline,docs,photos,video'&response_type=token&v=5.126
```
- Matrix -> VK (AS A USER)
- [x] Text content
- [x] Image content
- [x] Audio/Video content
- [x] Other files
- [x] Replies
- [ ] Typing notifs
- [ ] Presence
- [ ] Read notifications
- [x] Message edits
- [x] Message redacts - in 24 hours
- [ ] Initiate rooms from the matrix side
- VK (AS A USER) -> Matrix
- [x] Auth as a user instead of group
- [x] Text content
- [x] Forwards
- [ ] Image content
- [ ] Audio content
- [ ] Video content
- [ ] Stickers
- [ ] Other files
- [ ] Presence
- [x] Typing notifs
- [x] User profiles
- [ ] Read notifications
- [x] Message edits
- [ ] Message redacts
- [ ] Autopopulate rooms with users
To avoid imposture, do **not** use relay mode with user tokens!

View file

@ -1,6 +1,6 @@
{ {
"name": "mx-puppet-vk", "name": "mx-puppet-vk",
"version": "0.1.0", "version": "0.2.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View file

@ -163,12 +163,23 @@ export class VkPuppet {
await this.puppet.setUserTyping(params, context.isTyping); await this.puppet.setUserTyping(params, context.isTyping);
} }
}); });
data.id = Number((await client.api.groups.getById({}))[0].id) * -1; try {
await client.api.groups.getById({});
log.info("Got group token");
data.isUserToken = false;
} catch (err) {
log.info("Got user token");
data.isUserToken = true;
}
this.puppets[puppetId] = { this.puppets[puppetId] = {
client, client,
data, data,
}; };
try {
await client.updates.start(); await client.updates.start();
} catch (err) {
log.error("Failed to initialize update listener", err)
}
} }
public async deletePuppet(puppetId: number) { public async deletePuppet(puppetId: number) {
@ -210,7 +221,7 @@ export class VkPuppet {
random_id: new Date().getTime(), random_id: new Date().getTime(),
}); });
await this.puppet.eventSync.insert(room, data.eventId!, await this.puppet.eventSync.insert(room, data.eventId!,
response[0]["conversation_message_id"].toString()); p.data.isUserToken ? response[0]["message_id"].toString() : response[0]["conversation_message_id"].toString());
} catch (err) { } catch (err) {
log.error("Error sending to vk", err.error || err.body || err); log.error("Error sending to vk", err.error || err.body || err);
} }
@ -231,10 +242,12 @@ export class VkPuppet {
try { try {
const response = await p.client.api.messages.edit({ const response = await p.client.api.messages.edit({
peer_id: Number(room.roomId), peer_id: Number(room.roomId),
conversation_message_id: Number(eventId), conversation_message_id: p.data.isUserToken ? undefined : Number(eventId),
message_id: p.data.isUserToken ? Number(eventId) : undefined,
message: asUser ? `${asUser.displayname}: ${data.body}` : data.body, message: asUser ? `${asUser.displayname}: ${data.body}` : data.body,
random_id: new Date().getTime(), random_id: new Date().getTime(),
}); });
log.info("SYNC Matrix edit", response);
await this.puppet.eventSync.insert(room, data.eventId!, response.toString()); await this.puppet.eventSync.insert(room, data.eventId!, response.toString());
} catch (err) { } catch (err) {
log.error("Error sending edit to vk", err.error || err.body || err); log.error("Error sending edit to vk", err.error || err.body || err);
@ -254,13 +267,12 @@ export class VkPuppet {
} }
try { try {
await this.handleMatrixEdit(room, eventId, { body: "[ДАННЫЕ УДАЛЕНЫ]", eventId }, asUser); p.data.isUserToken ? await p.client.api.messages.delete({
// broken in chats without admin access
/*await p.client.api.messages.delete({
spam: 0, spam: 0,
delete_for_all: 1, delete_for_all: 1,
message_ids: Number(eventId), message_ids: Number(eventId),
});*/ })
: await this.handleMatrixEdit(room, eventId, { body: "[ДАННЫЕ УДАЛЕНЫ]", eventId }, asUser);
} catch (err) { } catch (err) {
log.error("Error sending edit to vk", err.error || err.body || err); log.error("Error sending edit to vk", err.error || err.body || err);
} }
@ -290,10 +302,11 @@ export class VkPuppet {
peer_ids: Number(room.roomId), peer_ids: Number(room.roomId),
message: asUser ? `${asUser.displayname}: ${await this.stripReply(data.body)}` : await this.stripReply(data.body), message: asUser ? `${asUser.displayname}: ${await this.stripReply(data.body)}` : await this.stripReply(data.body),
random_id: new Date().getTime(), random_id: new Date().getTime(),
forward: `{"peer_id":${Number(room.roomId)},"conversation_message_ids":${Number(eventId)},"is_reply": true}`, forward: p.data.isUserToken ? undefined : `{"peer_id":${Number(room.roomId)},"conversation_message_ids":${Number(eventId)},"is_reply": true}`,
reply_to: p.data.isUserToken ? Number(room.roomId) : undefined,
}); });
await this.puppet.eventSync.insert(room, data.eventId!, await this.puppet.eventSync.insert(room, data.eventId!,
response[0]["conversation_message_id"].toString()); p.data.isUserToken ? response[0]["message_id"].toString() : response[0]["conversation_message_id"].toString());
} catch (err) { } catch (err) {
log.error("Error sending to vk", err.error || err.body || err); log.error("Error sending to vk", err.error || err.body || err);
} }
@ -334,7 +347,7 @@ export class VkPuppet {
attachment: [`photo${attachment.ownerId}_${attachment.id}`], attachment: [`photo${attachment.ownerId}_${attachment.id}`],
}); });
await this.puppet.eventSync.insert(room, data.eventId!, await this.puppet.eventSync.insert(room, data.eventId!,
response[0]["conversation_message_id"].toString()); p.data.isUserToken ? response[0]["message_id"].toString() : response[0]["conversation_message_id"].toString());
} catch (err) { } catch (err) {
log.error("Error sending to vk", err.error || err.body || err); log.error("Error sending to vk", err.error || err.body || err);
} }
@ -390,7 +403,7 @@ export class VkPuppet {
random_id: new Date().getTime(), random_id: new Date().getTime(),
}); });
await this.puppet.eventSync.insert(room, data.eventId!, await this.puppet.eventSync.insert(room, data.eventId!,
response[0]["conversation_message_id"].toString()); p.data.isUserToken ? response[0]["message_id"].toString() : response[0]["conversation_message_id"].toString());
} catch (err) { } catch (err) {
log.error("Error sending to vk", err.error || err.body || err); log.error("Error sending to vk", err.error || err.body || err);
} }
@ -403,7 +416,7 @@ export class VkPuppet {
random_id: new Date().getTime(), random_id: new Date().getTime(),
}); });
await this.puppet.eventSync.insert(room, data.eventId!, await this.puppet.eventSync.insert(room, data.eventId!,
response[0]["conversation_message_id"].toString()); p.data.isUserToken ? response[0]["message_id"].toString() : response[0]["conversation_message_id"].toString());
} catch (err) { } catch (err) {
log.error("Error sending to vk", err.error || err.body || err); log.error("Error sending to vk", err.error || err.body || err);
} }
@ -459,7 +472,7 @@ export class VkPuppet {
} }
const params = await this.getSendParams(puppetId, context.peerId, context.senderId, const params = await this.getSendParams(puppetId, context.peerId, context.senderId,
context.conversationMessageId?.toString() || context.id.toString()); p.data.isUserToken ? context.id.toString() : context.conversationMessageId?.toString() || context.id.toString());
if (context.hasText || context.hasForwards) { if (context.hasText || context.hasForwards) {
let msgText: string = context.text || ""; let msgText: string = context.text || "";
@ -548,8 +561,14 @@ export class VkPuppet {
if (!p) { if (!p) {
return; return;
} }
log.info(context);
// As VK always sends edit as outbox, we won't work with any edits from groups // As VK always sends edit as outbox, we won't work with any edits from groups
if (context.senderType === "group") { if (!p.data.isUserToken && context.senderType === "group") {
return; // Deduping
}
// With users it works ok
if (p.data.isUserToken && context.isOutbox === true) {
return; // Deduping return; // Deduping
} }