aboutsummaryrefslogtreecommitdiffstats
path: root/lib/store.js.map
blob: f576e0c0feca2798fd68fb9bfea5e21bc8410d1b (plain) (blame)
1
{"version":3,"file":"store.js","sourceRoot":"","sources":["store.ts"],"names":[],"mappings":";;AAAA,kBAAe;IACb,SAAS,EAAE,EAAE;IACb,SAAS,EAAE,EAAE;IACb,MAAM,EAAE,EAAE;IACV,SAAS,EAAE,EAAE;IACb,MAAM,EAAE,EAAE;IACV,KAAK,EAAE,EAAE;IACT,IAAI,EAAE,EAAE;IACR,QAAQ,EAAE,EAAE;IACZ,OAAO,EAAE,EAAE;IACX,WAAW,EAAE,EAAE;IACf,QAAQ,EAAE,EAAE;IACZ,KAAK,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE;IACrB,EAAE,EAAE,EAAE;IACN,MAAM,EAAE,EAAE;IACV,cAAc,EAAE,CAAC;IACjB,cAAc,EAAE,CAAC;IACjB,IAAI,EAAE,EAAE;IACR,QAAQ,EAAE,EAAE;IACZ,QAAQ,EAAE,EAAE;IACZ,QAAQ,EAAE,EAAE;CACb,CAAC"}
*/ .highlight .m { color: #00D; font-weight: bold } /* Literal.Number */ .highlight .s { color: #D20; background-color: #FFF0F0 } /* Literal.String */ .highlight .na { color: #369 } /* Name.Attribute */ .highlight .nb { color: #038 } /* Name.Builtin */ .highlight .nc { color: #B06; font-weight: bold } /* Name.Class */ .highlight .no { color: #036; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555 } /* Name.Decorator */ .highlight .ne { color: #B06; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #06B; font-weight: bold } /* Name.Function */ .highlight .nl { color: #369; font-style: italic } /* Name.Label */ .highlight .nn { color: #B06; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #369; font-weight: bold } /* Name.Property */ .highlight .nt { color: #B06; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #369 } /* Name.Variable */ .highlight .ow { color: #080 } /* Operator.Word */ .highlight .w { color: #BBB } /* Text.Whitespace */ .highlight .mb { color: #00D; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #00D; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #00D; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #00D; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #00D; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #D20; background-color: #FFF0F0 } /* Literal.String.Affix */ .highlight .sb { color: #D20; background-color: #FFF0F0 } /* Literal.String.Backtick */ .highlight .sc { color: #D20; background-color: #FFF0F0 } /* Literal.String.Char */ .highlight .dl { color: #D20; background-color: #FFF0F0 } /* Literal.String.Delimiter */ .highlight .sd { color: #D20; background-color: #FFF0F0 } /* Literal.String.Doc */ .highlight .s2 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Double */ .highlight .se { color: #04D; background-color: #FFF0F0 } /* Literal.String.Escape */ .highlight .sh { color: #D20; background-color: #FFF0F0 } /* Literal.String.Heredoc */ .highlight .si { color: #33B; background-color: #FFF0F0 } /* Literal.String.Interpol */ .highlight .sx { color: #2B2; background-color: #F0FFF0 } /* Literal.String.Other */ .highlight .sr { color: #080; background-color: #FFF0FF } /* Literal.String.Regex */ .highlight .s1 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Single */ .highlight .ss { color: #A60; background-color: #FFF0F0 } /* Literal.String.Symbol */ .highlight .bp { color: #038 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #369 } /* Name.Variable.Class */ .highlight .vg { color: #D70 } /* Name.Variable.Global */ .highlight .vi { color: #33B } /* Name.Variable.Instance */ .highlight .vm { color: #369 } /* Name.Variable.Magic */ .highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
import { err, warn, l, line } from "$lib/logging";
import { types } from "$lib/payments";
import { archive, db, g, ga, s } from "$lib/db";
import { wait } from "$lib/utils";

let queued = {};
let migrating = {};
async function migrate(id) {
  if (id) id = id.replace(/\s/g, "").toLowerCase();

  let user = await g(`user:${id}`);
  if (typeof user === "string") user = await g(`user:${user}`);
  if (user) {
    delete migrating[id];
    return user;
  }

  user = await ga(`user:${id}`);

  if (typeof user === "string") {
    if (await g(`user:${user}`)) {
      warn("user already migrated", id);
      s(`user:${id}`, user);
    }

    id = user;
    user = await ga(`user:${id}`);
  }

  if (user) {
    if (migrating[id]) return;
    migrating[id] = true;

    let { username } = user;
    username = username.replace(/\s/g, "").toLowerCase();
    l("migrating", username);

    try {
      let multi = await db
        .multi()
        .set(`user:${username}`, id)
        .set(`user:${user.pubkey}`, id)
        .set(`user:${id}`, JSON.stringify(user));

      await multi.exec();

      await archive.del(`user:${username}`);
      await archive.del(`user:${user.pubkey}`);
      await archive.del(`user:${id}`);

      await s(`${id}:contacts`, (await ga(`${id}:contacts`)) || []);
      await archive.del(`${id}:contacts`);

      await s(`credit:bitcoin:${id}`, (await ga(`credit:bitcoin:${id}`)) || 0);
      await archive.del(`credit:bitcoin:${id}`);

      await s(
        `credit:lightning:${id}`,
        (await ga(`credit:lightning:${id}`)) || 0,
      );
      await archive.del(`credit:lightning:${id}`);

      await s(`${id}:lastlen`, (await ga(`${id}:lastlen`)) || 0);
      await archive.del(`${id}:lastlen`);
      await s(`${id}:cindex`, (await ga(`${id}:cindex`)) || 0);
      await archive.del(`${id}:cindex`);
      await s(`balance:${id}`, (await ga(`balance:${id}`)) || 0);
      await archive.del(`balance:${id}`);
      await s(`pending:${id}`, (await ga(`pending:${id}`)) || 0);
      await archive.del(`pending:${id}`);

      let pid;
      while ((pid = await archive.rPop(`${id}:payments`))) {
        let p = await ga(`payment:${pid}`);
        if (!p) continue;
        if (p.type === types.internal && p.ref) {
          let recipient = await g(`user:${p.ref}`);
          if (!recipient) queued[p.ref] = true;
        }

        await multi
          .lPush(`${id}:payments`, pid)
          .set(`payment:${pid}`, JSON.stringify(p))
          .exec();

        await archive.del(`payment:${pid}`);
      }
      await archive.del(`${id}:payments`);

      let iid;
      while ((iid = await archive.rPop(`${id}:invoices`))) {
        let inv = await ga(`invoice:${iid}`);
        if (!inv) continue;
        await multi
          .lPush(`${id}:invoices`, iid)
          .set(`invoice:${iid}`, JSON.stringify(inv))
          .exec();

        await archive.del(`invoice:${iid}`);
      }
      await archive.del(`${id}:invoices`);

      l("done migrating", username);
    } catch (e) {
      warn("failed to migrate", username, e.message, line());
    }

    delete migrating[id];
    return user;
  }
}

let keepMigrating = async () => {
  try {
    if (Object.keys(queued).length) {
      await migrate(Object.keys(queued)[0]);
    }
  } catch (e) {
    err("problem migrating", e.message);
  }

  setTimeout(keepMigrating, 1000);
};

keepMigrating();

export default migrate;