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f,QAAoB,KAAK,WAAW,EAAQ,GAE5C,OAAO,eAAe,EAAK,KAAK,QAAQ,aAAc,CACpD,IAAK,WACH,OAAO,CACT,EACA,IAAK,SAAU,GACb,EAAc,OAAO,OAAO,CAAC,EAAG,EAClC,KAIJ,IAAI,EAAc,KAgBlB,OAdI,KAAK,QAAQ,eACf,QAAoB,KAAK,WAAW,EAAG,GAEvC,OAAO,eAAe,EAAK,KAAK,QAAQ,aAAc,CACpD,IAAK,WACH,OAAO,CACT,EACA,IAAK,SAAU,GACb,EAAc,OAAO,OAAO,CAAC,EAAG,EAClC,KAIJ,MAAM,8BAA8B,GAAU,gBAAgB,GAAU,SAAU,GAC3E,IAAO,MAAK,UACb,SACI,KAAK,YAAY,EAAQ,EAAQ,GAErC,SACI,KAAK,YAAY,EAAQ,EAAG,GAEhC,SACI,KAAK,YAAY,EAAG,EAAQ,EACpC,GACD,CAEL,EAGF,OAAO,QAAU","file":"/npm/telegraf-session-mysql@5.3.0/lib/session.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/telegraf-session-mysql@5.3.0/lib/session.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const mysql = require('mysql2/promise')\nconst debug = require('debug')('telegraf:session-redis')\n\nclass MySQLSession {\n  constructor (connection, options = {}) {\n    this.connection = connection\n\n    this.options = Object.assign({\n      property: 'session',\n      userProperty: '',\n      chatProperty: '',\n      table: 'sessions',\n      interval: 300000,\n      lifetime: 300,\n      getSessionKey: (ctx) => {\n        if (ctx.updateType === 'callback_query') {\n          ctx = ctx.update.callback_query.message\n        }\n        if (!ctx.from || !ctx.chat) {\n          return\n        }\n        return [ctx.from.id, ctx.chat.id]\n      }\n    }, options)\n\n    this.sessions = {}\n    this.sessionsDts = {}\n\n    this.interval = setInterval(() => {\n      const currentTime = Math.floor(new Date() / 1000)\n\n      Object.keys(this.sessionsDts).forEach((key) => {\n        if (currentTime - this.sessionsDts[key] >= this.options.lifetime) {\n          delete this.sessions[key]\n          delete this.sessionsDts[key]\n        }\n      })\n    }, this.options.interval)\n  }\n\n  async connect () {\n    this.client = mysql.createPool(this.connection)\n\n    return this.client.execute(\n      `CREATE TABLE IF NOT EXISTS ${this.options.table}\n      (\n        user_id BIGINT(20) NOT NULL,\n        chat_id BIGINT(20) NOT NULL,\n        session LONGTEXT NOT NULL,\n        UNIQUE KEY user_id (user_id, chat_id)\n      )\n      engine=innodb\n      DEFAULT charset=utf8;`\n    )\n      .catch((err) => {\n        throw new Error(`Can't create table for sessions ${err}`)\n      })\n  }\n\n  async getSession (userID, chatID) {\n    const key = userID + ':' + chatID\n\n    if (!this.sessions[key]) {\n      try {\n        const [rows] = await this.client.query(`\n        SELECT session\n        FROM ${this.options.table}\n        WHERE user_id = ?\n        AND chat_id = ?`, [userID, chatID])\n\n        if (rows && rows.length) {\n          try {\n            this.sessions[key] = JSON.parse(rows[0].session)\n          } catch (err) {\n            console.error(\"Can't parse session state\", err)\n            this.sessions[key] = {}\n          }\n        } else {\n          this.sessions[key] = {}\n        }\n      } catch (err) {\n        console.error(\"Can't get user session:\", err)\n        this.sessions[key] = {}\n      }\n\n      this.sessionsDts[key] = Math.floor(new Date() / 1000)\n    }\n    debug('session state', key, this.sessions[key])\n    return this.sessions[key]\n  }\n\n  async saveSession (userID, chatID, session) {\n    debug('save session', userID + ':' + chatID, session)\n    if (!session || Object.keys(session).length === 0) {\n      try {\n        await this.client.query(`\n          DELETE FROM ${this.options.table}\n          WHERE  user_id = ?\n          AND chat_id = ?\n        `, [userID, chatID])\n      } catch (err) {\n        console.error(\"Can't delete (save) user session:\", err)\n        return false\n      }\n    }\n\n    try {\n      const sessionString = JSON.stringify(session)\n      return await this.client.query(`\n        INSERT INTO ${this.options.table} (user_id, chat_id, session) value (?, ?, ?) ON DUPLICATE KEY UPDATE session = ?\n    `, [userID, chatID, sessionString, sessionString])\n    } catch (err) {\n      console.error(\"Can't save user session:\", err)\n      return false\n    }\n  }\n\n  destroy () {\n    this.client.end()\n    this.client = null\n\n    delete this.options\n    this.options = null\n\n    clearInterval(this.interval)\n    this.interval = null\n\n    delete this.sessions\n    this.sessions = null\n\n    delete this.sessionsDts\n    this.sessionsDts = null\n\n    debug('destroy sessions')\n  }\n\n  middleware () {\n    return async (ctx, next) => {\n      const [userID, chatID] = this.options.getSessionKey(ctx)\n\n      if (!userID || !chatID) {\n        return next()\n      }\n\n      let session = null\n      if (this.options.property) {\n        session = await this.getSession(userID, chatID)\n\n        Object.defineProperty(ctx, this.options.property, {\n          get: function () {\n            return session\n          },\n          set: function (newValue) {\n            session = Object.assign({}, newValue)\n          }\n        })\n      }\n\n      let userSession = null\n\n      if (this.options.userProperty) {\n        userSession = await this.getSession(userID, 0)\n\n        Object.defineProperty(ctx, this.options.userProperty, {\n          get: function () {\n            return userSession\n          },\n          set: function (newValue) {\n            userSession = Object.assign({}, newValue)\n          }\n        })\n      }\n\n      let chatSession = null\n\n      if (this.options.chatProperty) {\n        chatSession = await this.getSession(0, chatID)\n\n        Object.defineProperty(ctx, this.options.chatProperty, {\n          get: function () {\n            return chatSession\n          },\n          set: function (newValue) {\n            chatSession = Object.assign({}, newValue)\n          }\n        })\n      }\n\n      debug(`session snapshot for Chat: ${chatID || 'None'} User: ${userID || 'None'}`, session)\n      return next().then(async() => {\n        if (session) {\n          await this.saveSession(userID, chatID, session)\n        }\n        if (userSession) {\n          await this.saveSession(userID, 0, userSession)\n        }\n        if (chatSession) {\n          await this.saveSession(0, chatID, chatSession)\n        }\n      })\n    }\n  }\n}\n\nmodule.exports = MySQLSession\n"]}