Skip to content

Commit

Permalink
Add migration to set all users some push notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed May 31, 2024
1 parent 0ef563d commit 9310948
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions migrations/update_user_notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
*
* phpBB Browser Push Notifications. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2024, phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\webpushnotifications\migrations;

use phpbb\db\migration\migration;

class update_user_notifications extends migration
{
/**
* @inheritDoc
*/
public static function depends_on()
{
return ['\phpbb\webpushnotifications\migrations\add_webpush'];
}

/**
* @inheritDoc
*/
public function effectively_installed()
{
$sql = 'SELECT method
FROM ' . $this->table_prefix . 'user_notifications
WHERE method = ' . $this->db->sql_escape('notification.method.phpbb.wpn.webpush');
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

return $row !== false;
}

/**
* @inheritDoc
*/
public function update_data()
{
return [
['custom', [[$this, 'update_notifications']]],
];
}

/**
* Add default push notifications for users in chunks
*
* @param $start int Start value for the update
* @return int|true Next start value or true if complete
*/
public function update_notifications($start)
{
$start = (int) $start;
$limit = 500;
$updated = 0;

$sql_ary = [];

$sql = 'SELECT user_id
FROM ' . $this->table_prefix . 'users
ORDER BY user_id ASC';
$result = $this->db->sql_query_limit($sql, $limit, $start);

while ($row = $this->db->sql_fetchrow($result))
{
$sql_ary[] = [
'item_type' => 'notification.type.pm',
'item_id' => 0,
'user_id' => (int) $row['user_id'],
'notify' => 1,
'method' => 'notification.method.phpbb.wpn.webpush',
];
$sql_ary[] = [
'item_type' => 'notification.type.quote',
'item_id' => 0,
'user_id' => (int) $row['user_id'],
'notify' => 1,
'method' => 'notification.method.phpbb.wpn.webpush',
];
$updated++;
}
$this->db->sql_freeresult($result);

$this->db->sql_multi_insert($this->table_prefix . 'user_notifications', $sql_ary);

return ($updated === $limit) ? $start + $limit : true;
}
}

0 comments on commit 9310948

Please sign in to comment.