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

Allows for each forum to select multiple webhooks. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 34 additions & 18 deletions acp/discord_notifications_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,16 @@ private function process_form_submit()
{
if ($url === '')
{
$sql = "DELETE FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($alias) . "'";
$this->db->sql_query($sql);
// Delete existing discord_webhook_forums
$webhook_id = $this->notification_service->query_webhook_id($alias);
if ($webhook_id)
{
$sql = "DELETE FROM {$table_prefix}discord_webhooks_forums WHERE discord_webhook_id = " . (int) $webhook_id;
$this->db->sql_query($sql);
}

$sql = "UPDATE " . FORUMS_TABLE . " SET discord_notifications = '' WHERE discord_notifications = '" . $this->db->sql_escape($alias) . "'";
// Delete the actual webhook.
$sql = "DELETE FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($alias) . "'";
$this->db->sql_query($sql);
} else
{
Expand All @@ -249,15 +255,24 @@ private function process_form_submit()
}

// Update configuration per forum
$forum_configuration = $this->request->variable('dn_forum', [0 => '']);
$forum_configuration = $this->request->variable('dn_forum', [0 => [0 => '']]);
foreach ($forum_configuration as $id => $value)
{
// Don't update deleted entries
if ($value === '' || $webhook_configuration[$value] !== '')
$sql = "DELETE FROM {$table_prefix}discord_webhooks_forums WHERE forum_id = " . (int) $id;
$this->db->sql_query($sql);

if (!is_array($value))
{
$sql = "UPDATE " . FORUMS_TABLE . " SET discord_notifications = '" . $this->db->sql_escape($value) .
"' WHERE forum_id = " . (int) $id;
$this->db->sql_query($sql);
$value = array($value);
}

foreach ($value as $val)
{
if (!empty($val))
{
$sql = "INSERT INTO {$table_prefix}discord_webhooks_forums (discord_webhook_id, forum_id) VALUES (" . ((int) $val) . ", " . ((int) $id) . ")";
$this->db->sql_query($sql);
}
}
}

Expand Down Expand Up @@ -309,14 +324,15 @@ private function generate_webhook_section()
{
global $table_prefix;

$sql = "SELECT alias, url FROM {$table_prefix}discord_webhooks ORDER BY alias";
$sql = "SELECT alias, url, discord_webhook_id FROM {$table_prefix}discord_webhooks ORDER BY alias";
$result = $this->db->sql_query($sql);

while ($row = $this->db->sql_fetchrow($result))
{
$tpl_row = array(
'ALIAS' => $row['alias'],
'URL' => $row['url'],
'DISCORD_WEBHOOK_ID' => $row['discord_webhook_id'],
'ALIAS' => $row['alias'],
'URL' => $row['url'],
);
$this->template->assign_block_vars('webhookrow', $tpl_row);
}
Expand All @@ -329,7 +345,7 @@ private function generate_webhook_section()
*/
private function generate_forum_section()
{
$sql = "SELECT forum_id, forum_type, forum_name, discord_notifications FROM " . FORUMS_TABLE . " ORDER BY left_id ASC";
$sql = "SELECT forum_id, forum_type, forum_name FROM " . FORUMS_TABLE . " ORDER BY left_id ASC";
$result = $this->db->sql_query($sql);

while ($row = $this->db->sql_fetchrow($result))
Expand All @@ -347,11 +363,11 @@ private function generate_forum_section()
{
// The labels for all the inputs are constructed based on the forum IDs to make it easy to know which
$tpl_row = array(
'S_IS_CAT' => false,
'FORUM_NAME' => $row['forum_name'],
'FORUM_ID' => $row['forum_id'],
'ALIAS' => $row['discord_notifications'],
);
'S_IS_CAT' => false,
'FORUM_NAME' => $row['forum_name'],
'FORUM_ID' => $row['forum_id'],
'DISCORD_WEBHOOK_IDS' => $this->notification_service->query_forum_webhooks($row['forum_id']),
);
$this->template->assign_block_vars('forumrow', $tpl_row);
}
// Other forum types (links) are ignored
Expand Down
4 changes: 2 additions & 2 deletions adm/style/acp_discord_notifications.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ <h1>{{ lang('ACP_DISCORD_NOTIFICATIONS') }}</h1>
<dl>
<dt><label for="dn_forum_{{ forum.FORUM_ID }}">{{ forum.FORUM_NAME }}</label></dt>
<dd>
<select id="dn_forum_{{ forum.FORUM_ID }}" name="dn_forum[{{ forum.FORUM_ID }}]">
<select id="dn_forum_{{ forum.FORUM_ID }}" name="dn_forum[{{ forum.FORUM_ID }}][]" multiple="multiple">
<option value="">({{ lang('DISABLED') }})</option>
{% for webhook in loops.webhookrow %}
<option value="{{ webhook.ALIAS }}"{% if forum.ALIAS == webhook.ALIAS %} selected{% endif %}>{{ webhook.ALIAS }}</option>
<option value="{{ webhook.DISCORD_WEBHOOK_ID }}"{% if webhook.DISCORD_WEBHOOK_ID in forum.DISCORD_WEBHOOK_IDS %} selected{% endif %}>{{ webhook.ALIAS }}</option>
{% endfor %}
</select>
</dd>
Expand Down
29 changes: 28 additions & 1 deletion migrations/create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function update_schema()
'add_tables' => array (
$this->table_prefix . 'discord_webhooks' => array (
'COLUMNS' => array (
'discord_webhook_id' => array (
'UINT',
NULL,
'auto_increment'
),
'alias' => array (
'VCHAR:255',
''
Expand All @@ -34,7 +39,28 @@ public function update_schema()
''
),
),
'PRIMARY_KEY' => 'alias',
'PRIMARY_KEY' => 'discord_webhook_id',
'alias' => array('INDEX', 'alias'),
),
$this->table_prefix . 'discord_webhooks_forums' => array (
'COLUMNS' => array (
'discord_webhook_forums_id' => array (
'UINT',
NULL,
'auto_increment'
),
'discord_webhook_id' => array (
'UINT',
0
),
'forum_id' => array (
'UINT',
0
),
),
'PRIMARY_KEY' => 'discord_webhook_forums_id',
'discord_webhook_id' => array('INDEX', 'discord_webhook_id'),
'forum_id' => array('INDEX', 'forum_id'),
),
),
);
Expand All @@ -49,6 +75,7 @@ public function revert_schema()
return array (
'drop_tables' => array (
$this->table_prefix . 'discord_webhooks',
$this->table_prefix . 'discord_webhooks_forums'
)
);
}
Expand Down
36 changes: 0 additions & 36 deletions migrations/extension_installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,6 @@ public function effectively_installed()
return isset($this->config['discord_notifications_enabled']);
}

/**
* Add the discord notification enabled column to the forums table.
* This setting determines whether or not activity on a specific forum will generate a
* notification transmitted to Discord.
*
* @return array Array of table schema
* @access public
*/
public function update_schema()
{
return array(
'add_columns' => array(
$this->table_prefix . 'forums' => array(
'discord_notifications' => array('VCHAR:255', ''),
),
),
);
}

/**
* Drop the discord notification enabled column from the users table.
*
* @return array Array of table schema
* @access public
*/
public function revert_schema()
{
return array(
'drop_columns' => array(
$this->table_prefix . 'forums' => array(
'discord_notifications',
),
),
);
}

/**
* Add Discord notification data to the database.
* Note: these changes will automatically get reverted by phpbb if the extension is uninstalled.
Expand Down
84 changes: 72 additions & 12 deletions notification_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ public function is_notification_type_enabled($notification_type)
* @param int $forum_id The ID of the forum to check
* @return string|false
*/

public function get_forum_notification_url($forum_id)
{
global $table_prefix;
$webhooks = array();

if (is_numeric($forum_id) == false)
{
Expand All @@ -81,22 +83,22 @@ public function get_forum_notification_url($forum_id)
}

// Query the forum table where forum notification settings are stored
$sql = "SELECT discord_notifications FROM " . FORUMS_TABLE . " WHERE forum_id = " . (int) $forum_id;
$sql = "SELECT url FROM {$table_prefix}discord_webhooks
INNER JOIN {$table_prefix}discord_webhooks_forums ON {$table_prefix}discord_webhooks_forums.discord_webhook_id = {$table_prefix}discord_webhooks.discord_webhook_id
WHERE {$table_prefix}discord_webhooks_forums.forum_id = " . (int) $forum_id;
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

if ($data['discord_notifications'])
while ($row = $this->db->sql_fetchrow($result))
{
$sql = "SELECT url FROM {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($data['discord_notifications']) . "'";
$result = $this->db->sql_query($sql);
$data2 = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

return $data2['url'];
if ($row['url'])
{
$webhooks[] = $row['url'];
}
}

return false;
$this->db->sql_freeresult($result);

return empty($webhooks) ? false : $webhooks;
}

/**
Expand Down Expand Up @@ -130,6 +132,37 @@ public function query_forum_name($forum_id)
return $data;
}


/**
* Retrieves the list of discord_webhook_ids for the forum
*
* @param int $forum_id The ID of the forum to query
* @return array|false The name of the forum, or false if not found
*/
public function query_forum_webhooks($forum_id)
{
global $table_prefix;

if (is_numeric($forum_id) == false)
{
return null;
}

$webhooks = array();

$sql = "SELECT discord_webhook_id from {$table_prefix}discord_webhooks_forums WHERE forum_id = " . (int) $forum_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
if ($row['discord_webhook_id'])
{
$webhooks[] = $row['discord_webhook_id'];
}
}
$this->db->sql_freeresult($result);
return $webhooks;
}

/**
* Retrieves the subject of a post from the database when given an ID
*
Expand Down Expand Up @@ -217,6 +250,21 @@ public function query_user_name($user_id)
return $data;
}

/**
* Retrieves the ID of the discord webhook.
*
* @param string $alias The alias of the discord webhook.
* @return int|false The name of the user, or false if not found
*/
public function query_webhook_id($alias)
{
$sql = "SELECT discord_webhook_id from {$table_prefix}discord_webhooks WHERE alias = '" . $this->db->sql_escape($alias) . "'";
$result = $this->db->sql_query($sql);
$data = $this->db->sql_fetchfield('discord_webhook_id');
$this->db->sql_freeresult($result);
return $data;
}

/**
* Sends a notification message to Discord. This function checks the master switch configuration for the extension,
* but does no further checks. The caller is responsible for performing full validation of the notification prior
Expand Down Expand Up @@ -249,7 +297,19 @@ public function send_discord_notification($color, $message, $footer = null, $web
$webhook_url = $data['url'];
}
}
$this->execute_discord_webhook($webhook_url, $color, $message, $footer);


if (!empty($webhook_url) && is_array($webhook_url))
{
foreach ($webhook_url as $url)
{
$this->execute_discord_webhook($url, $color, $message, $footer);
}
}
else
{
$this->execute_discord_webhook($webhook_url, $color, $message, $footer);
}
}

/**
Expand Down