Commit 0661c86f authored by Mark Harding's avatar Mark Harding

(fix): add in repair job to change subscription ids

parent 34ddb380
No related merge requests found
Pipeline #69565103 failed with stages
in 3 minutes and 12 seconds
......@@ -7,8 +7,10 @@ use Minds\Core\Di\Di;
use Minds\Core\Events\Dispatcher;
use Minds\Core\Payments\Subscriptions\Manager;
use Minds\Core\Payments\Subscriptions\Queue;
use Minds\Core\Security\ACL;
use Minds\Helpers\Cql;
use Minds\Interfaces;
use Minds\Core\Util\BigNumber;
class Subscriptions extends Cli\Controller implements Interfaces\CliControllerInterface
{
......@@ -30,6 +32,8 @@ class Subscriptions extends Cli\Controller implements Interfaces\CliControllerIn
// Initialize events
\Minds\Core\Events\Defaults::_();
ACL::$ignore = true; // we need to save to channels
/** @var Manager $manager */
$manager = Di::_()->get('Payments\Subscriptions\Manager');
......@@ -42,6 +46,7 @@ class Subscriptions extends Cli\Controller implements Interfaces\CliControllerIn
foreach ($subscriptions as $subscription) {
$this->out("Subscription:`{$subscription->getId()}`");
$billing = date('d-m-Y', $subscription->getNextBilling());
$user_guid = $subscription->getUser()->guid;
$this->out("\t$billing | $user_guid");
......@@ -105,4 +110,92 @@ class Subscriptions extends Cli\Controller implements Interfaces\CliControllerIn
$this->out("Done");
}
/**
* Sometimes, plus doesn't hit the delegate so the badge
* doesn't apply. This is designed to run regularly via
* a cron job to fix that
* @return void
*/
public function fixPlusWires()
{
ACL::$ignore = true; // we need to save to channels
$delegate = new \Minds\Core\Wire\Delegates\Plus;
$usersLastPlus = [];
foreach ($this->getWires(false) as $wire) {
$sender_guid = $wire->getSender()->getGuid();
$friendly = date('d-m-Y', $wire->getTimestamp());
echo "\n$sender_guid";
if ($wire->getTimestamp() < $usersLastPlus[$sender_guid] ?? time()) {
echo " $friendly already given plus to this user";
continue;
}
$usersLastPlus[$sender_guid] = $wire->getTimestamp();
$friendly = date('d-m-Y', $wire->getTimestamp());
echo " $friendly sending plus update ({$wire->getAmount()})";
if ($delegate->onWire($wire, 'offchain') || $delegate->onWire($wire, '0x6f2548b1bee178a49c8ea09be6845f6aeaf3e8da')) {
echo " done";
}
}
}
public function getWires($onchain = false)
{
$cql = \Minds\Core\Di\Di::_()->get('Database\Cassandra\Cql');
$prepared = new \Minds\Core\Data\Cassandra\Prepared\Custom;
$statement = "SELECT * FROM blockchain_transactions_mainnet WHERE contract='offchain:wire' and user_guid=? ALLOW FILTERING";
if ($onchain) {
$statement = "SELECT * FROM blockchain_transactions_mainnet WHERE wallet_address=? ALLOW FILTERING";
} else {
$statement = "SELECT * FROM blockchain_transactions_mainnet WHERE user_guid=? and amount>=? ALLOW FILTERING";
}
$offset = "";
while (true) {
if ($onchain) {
$prepared->query($statement, [ '0x6f2548b1bee178a49c8ea09be6845f6aeaf3e8da' ]);
} else {
$prepared->query($statement, [ new \Cassandra\Varint(730071191229833224), new \Cassandra\Varint(5) ]);
}
$prepared->setOpts([
'paging_state_token' => $offset,
'page_size' => 100,
]);
try {
$result = $cql->request($prepared);
if (!$result) {
break;
}
$offset = $result->pagingStateToken();
} catch (\Exception $e) {
var_dump($e);
}
foreach ($result as $row) {
$data = json_decode($row['data'], true);
if ($row['timestamp']->time() < strtotime('35 days ago')) {
return; // Do not sync old
}
if (!$data['sender_guid']) {
var_dump($row);
}
$wire = new \Minds\Core\Wire\Wire();
$wire
->setSender(new \Minds\Entities\User($data['sender_guid']))
->setReceiver(new \Minds\Entities\User($data['receiver_guid']))
->setEntity(\Minds\Entities\Factory::build($data['entity_guid']))
->setAmount((string) $data['amount'])
->setTimestamp((int) $row['timestamp']->time());
yield $wire;
}
}
}
}
......@@ -55,6 +55,11 @@ class Plus
$user = $this->entitiesBuilder->single($user->getGuid(), [
'cache' => false,
]);
if (!$user) {
return $wire;
}
$user->setPlusExpires(strtotime('+30 days', $wire->getTimestamp()));
$user->save();
......@@ -62,4 +67,4 @@ class Plus
return $wire;
}
}
\ No newline at end of file
}
......@@ -14,6 +14,7 @@ use Minds\Core\Guid;
use Minds\Core\Util\BigNumber;
use Minds\Core\Wire\Exceptions\WalletNotSetupException;
use Minds\Core\Wire\Subscriptions\Manager as SubscriptionsManager;
use Minds\Common\Urn;
use Minds\Entities;
use Minds\Entities\User;
......@@ -328,7 +329,16 @@ class Manager
$receiver = new User($subscription->getEntity()->guid);
$amount = $subscription->getAmount();
if ($subscription->getId() === 'offchain') {
$id = $subscription->getId();
if (strpos($id, 'urn:', 0) !== 0) {
error_log("[wire][recurring]: $id was expecting a urn");
return false;
}
$urn = new Urn($id);
list ($address, $sender, $receiver) = explode('-', $urn->getNss());
if ($address === 'offchain') {
$this->setPayload([
'method' => 'offchain',
]);
......@@ -339,14 +349,14 @@ class Manager
'to' => $this->config->get('blockchain')['contracts']['wire']['contract_address'],
'gasLimit' => BigNumber::_(200000)->toHex(true),
'data' => $this->client->encodeContractMethod('wireFromDelegate(address,address,uint256)', [
$subscription->getId(),
$address,
$receiver->getEthWallet(),
BigNumber::_($this->token->toTokenUnit($amount))->toHex(true),
]),
]);
$this->setPayload([
'method' => 'onchain',
'address' => $subscription->getId(), //sender address
'address' => $address, //sender address
'receiver' => $receiver->getEthWallet(),
'txHash' => $txHash,
]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment