Another sync commit with an useless message

This commit is contained in:
Tulir Asokan
2017-11-15 01:16:58 +02:00
parent 0539af129b
commit 667421c727
9 changed files with 507 additions and 70 deletions
+123 -22
View File
@@ -15,9 +15,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
const {Bridge} = require("matrix-appservice-bridge")
const crypto = require("crypto")
const YAML = require("yamljs")
const commands = require("./commands")
const MatrixUser = require("./matrix-user")
const YAML = require("yamljs")
const TelegramUser = require("./telegram-user")
const Portal = require("./portal")
class MautrixTelegram {
constructor(config) {
@@ -25,6 +27,8 @@ class MautrixTelegram {
this.matrixUsersByID = new Map()
this.telegramUsersByID = new Map()
this.portalsByPeerID = new Map()
this.portalsByRoomID = new Map()
const self = this
this.bridge = new Bridge({
@@ -69,38 +73,135 @@ class MautrixTelegram {
this.config.bridge.username_template.replace("${ID}", id))
}
getMatrixUser(id) {
let user = this.matrixUsersByID.get(id)
if (user) {
return Promise.resolve(user)
async getPortalByPeer(peer) {
let portal = this.portalsByPeerID.get(peer.id)
if (portal) {
return portal
}
return this.bridge.getUserStore().select({
const entries = await this.bridge.getRoomStore().select({
type: "portal",
id: peer.id,
})
// Handle possible db query race conditions
portal = this.portalsByPeerID.get(peer.id)
if (portal) {
return portal
}
if (entries.length) {
portal = Portal.fromEntry(this, entries[0])
} else {
portal = new Portal(this, undefined, peer)
}
this.portalsByPeerID.set(peer.id, portal)
if (portal.roomID) {
this.portalsByRoomID.set(portal.roomID, portal)
}
return portal
}
async getPortalByRoomID(id) {
let portal = this.portalsByRoomID.get(id)
if (portal) {
return portal
}
// Check if we have it stored in the by-peer map
for (const [_, portalByPeer] of this.portalsByPeerID) {
if (portalByPeer.roomID === id) {
this.portalsByRoomID.set(id, portal)
return portalByPeer
}
}
const entries = await this.bridge.getRoomStore().select({
type: "portal",
roomID: id,
})
// Handle possible db query race conditions
let portal = this.portalsByRoomID.get(id)
if (portal) {
return portal
}
if (entries.length) {
portal = Portal.fromEntry(this, entries[0])
} else {
// Don't create portals based on room ID
return undefined
}
this.portalsByPeerID.set(portal.id, portal)
this.portalsByRoomID.set(id, portal)
return portal
}
async getTelegramUser(id) {
let user = this.telegramUsersByID.get(id)
if (user) {
return user
}
const entries = await this.bridge.getUserStore().select({
type: "remote",
id,
})
// Handle possible db query race conditions
if (this.telegramUsersByID.has(id)) {
return this.telegramUsersByID.get(id)
}
if (entries.length) {
user = TelegramUser.fromEntry(this, entries[0])
} else {
user = new TelegramUser(this, id)
}
this.telegramUsersByID.set(id, user)
return user
}
async getMatrixUser(id) {
let user = this.matrixUsersByID.get(id)
if (user) {
return user
}
const entries = this.bridge.getUserStore().select({
type: "matrix",
id,
}).then(entries => {
this.matrixUsersByID.get(id)
if (user) {
return Promise.resolve(user)
}
if (entries.length) {
user = MatrixUser.fromEntry(this, entries[0])
} else {
user = new MatrixUser(this, id)
}
this.matrixUsersByID.set(id, user)
return user
})
// Handle possible db query race conditions
if (this.matrixUsersByID.has(id)) {
return this.matrixUsersByID.get(id)
}
if (entries.length) {
user = MatrixUser.fromEntry(this, entries[0])
} else {
user = new MatrixUser(this, id)
}
this.matrixUsersByID.set(id, user)
return user
}
putUser(user) {
const entry = user.toEntry()
const query = {
return this.bridge.getUserStore().upsert({
type: entry.type,
id: entry.id,
}
return this.bridge.getUserStore().upsert(query, entry)
}, entry)
}
putRoom(room) {
const entry = room.toEntry()
return this.bridge.getUserStore().upsert({
type: entry.type,
id: entry.id,
}, entry)
}
handleMatrixEvent(evt) {