Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe Toggle in Notification Dropdown #41

Merged
merged 22 commits into from
Jun 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') . "'";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add AND notify = 1 as far as we're looking for enabled notification types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this I'm just looking to see if it exists yet, because if it does it means it's already been installed and people have started making settings already.

$result = $this->db->sql_query($sql);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need at least 1 positive result so probably $this->db->sql_query_limit($sql, 1).

$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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably limit user types to USER_NORMAL and USER_FOUNDER to skip unused accounts.

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;
}
}
Loading