From 772d46c5a828860a3a11e0b7146bf24792fec90b Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Thu, 30 May 2024 18:57:26 -0700 Subject: [PATCH] Add migration to set all users some push notifications Signed-off-by: Matt Friedman --- migrations/update_user_notifications.php | 93 ++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 migrations/update_user_notifications.php diff --git a/migrations/update_user_notifications.php b/migrations/update_user_notifications.php new file mode 100644 index 0000000..085d30e --- /dev/null +++ b/migrations/update_user_notifications.php @@ -0,0 +1,93 @@ + + * @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; + } +}