Logging in and auth persistence actually works now
This commit is contained in:
+106
-27
@@ -31,11 +31,46 @@ class TelegramPuppet {
|
||||
this.api_hash = opts.api_hash
|
||||
this.api_id = opts.api_id
|
||||
|
||||
this.puppetStorage = {
|
||||
get: async (key) => {
|
||||
let value = this.data[key]
|
||||
/*if (value && key.match(/_auth_key$/)) {
|
||||
value = this.app.decrypt(value)
|
||||
}*/
|
||||
return value
|
||||
},
|
||||
set: async (key, value) => {
|
||||
/*if (value && key.match(/_auth_key$/)) {
|
||||
value = this.app.encrypt(value)
|
||||
}*/
|
||||
|
||||
if (this.data[key] === value) return Promise.resolve()
|
||||
|
||||
this.data[key] = value
|
||||
await this.matrixUser.saveChanges()
|
||||
},
|
||||
remove: async (...keys) => {
|
||||
keys.forEach((key) => delete this.data[key])
|
||||
await this.matrixUser.saveChanges()
|
||||
},
|
||||
clear: async () => {
|
||||
this.data = {}
|
||||
await this.matrixUser.saveChanges()
|
||||
},
|
||||
}
|
||||
|
||||
this.apiConfig = Object.assign({}, {
|
||||
app_version: pkg.version,
|
||||
lang_code: "en",
|
||||
api_id: opts.api_id,
|
||||
initConnection : 0x69796de9,
|
||||
layer: 57,
|
||||
invokeWithLayer: 0xda9b0d0d,
|
||||
}, opts.api_config)
|
||||
|
||||
if (this.data.dc && this.data[`dc${this.data.dc}_auth_key`]) {
|
||||
this.listen()
|
||||
}
|
||||
}
|
||||
|
||||
static fromSubentry(app, matrixUser, data) {
|
||||
@@ -51,19 +86,17 @@ class TelegramPuppet {
|
||||
|
||||
toSubentry() {
|
||||
return Object.assign({
|
||||
user_id: this.userID
|
||||
user_id: this.userID,
|
||||
}, this.data)
|
||||
}
|
||||
|
||||
get datacenter() {
|
||||
return { dcID: 1 }
|
||||
}
|
||||
|
||||
get client() {
|
||||
if (!this._client) {
|
||||
const self = this
|
||||
this._client = telegram.MTProto({
|
||||
api: this.apiConfig,
|
||||
server: this.serverConfig,
|
||||
app: { storage: this.puppetStorage },
|
||||
})
|
||||
}
|
||||
return this._client
|
||||
@@ -76,40 +109,86 @@ class TelegramPuppet {
|
||||
api_id: this.api_id,
|
||||
api_hash: this.api_hash,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
signIn(phone_number, phone_code_hash, phone_code) {
|
||||
return this.client("auth.signIn", {
|
||||
phone_number, phone_code, phone_code_hash
|
||||
})
|
||||
.then(
|
||||
result => this.signInComplete(result),
|
||||
err => {
|
||||
if (err.type !== "SESSION_PASSWORD_NEEDED") {
|
||||
throw err
|
||||
}
|
||||
this.client("account.getPassword", {}).then(data => {
|
||||
return {
|
||||
status: "need-password",
|
||||
hint: data.hint,
|
||||
salt: data.current_salt
|
||||
}
|
||||
})
|
||||
async signIn(phone_number, phone_code_hash, phone_code) {
|
||||
try {
|
||||
const result = await
|
||||
this.client("auth.signIn", {
|
||||
phone_number, phone_code, phone_code_hash,
|
||||
})
|
||||
this.signInComplete(result)
|
||||
} catch (err) {
|
||||
if (err.message !== "SESSION_PASSWORD_NEEDED") {
|
||||
throw err
|
||||
}
|
||||
const password = await
|
||||
this.client("account.getPassword", {})
|
||||
return {
|
||||
status: "need-password",
|
||||
hint: password.hint,
|
||||
salt: password.current_salt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkPassword(password_hash) {
|
||||
return this.client("auth.checkPassword", {password_hash})
|
||||
.then((result) => this.signInComplete(result))
|
||||
async checkPassword(password_hash) {
|
||||
const result = await this.client("auth.checkPassword", { password_hash })
|
||||
return this.signInComplete(result)
|
||||
}
|
||||
|
||||
getDisplayName() {
|
||||
if (this.data.first_name || this.data.last_name) {
|
||||
return `${this.data.first_name} ${this.data.last_name}`
|
||||
} else if (this.data.username) {
|
||||
return this.data.username
|
||||
}
|
||||
return this.data.phone_number
|
||||
}
|
||||
|
||||
signInComplete(data) {
|
||||
this.userID = data.user.id
|
||||
this.data.username = data.user.username
|
||||
this.data.first_name = data.user.first_name
|
||||
this.data.last_name = data.user.last_name
|
||||
this.data.phone_number = data.user.phone_number
|
||||
this.matrixUser.saveChanges()
|
||||
this.listen()
|
||||
return {
|
||||
status: "ok"
|
||||
status: "ok",
|
||||
}
|
||||
}
|
||||
|
||||
handleUpdate(data) {
|
||||
console.log(data)
|
||||
}
|
||||
|
||||
async listen() {
|
||||
const client = this.client
|
||||
client.on("update", data => this.handleUpdate(data))
|
||||
if (client.bus) {
|
||||
client.bus.untypedMessage.observe(data => this.handleUpdate(data))
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("Updating online status...")
|
||||
//const statusUpdate = await client("account.updateStatus", { offline: false })
|
||||
//console.log(statusUpdate)
|
||||
console.log("Fetching initial state...")
|
||||
const state = await client("updates.getState", {})
|
||||
console.log("Initial state:", state)
|
||||
} catch (err) {
|
||||
console.error("Error getting initial state:", err)
|
||||
}
|
||||
setInterval(async () => {
|
||||
try {
|
||||
const state = client("updates.getState", {})
|
||||
console.log("New state received")
|
||||
} catch (err) {
|
||||
console.error("Error updating state:", err)
|
||||
}
|
||||
}, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TelegramPuppet
|
||||
|
||||
Reference in New Issue
Block a user