Skip to content

Commit

Permalink
Merge 49c9bb3 into 310ab99
Browse files Browse the repository at this point in the history
  • Loading branch information
jdarwood007 committed Oct 26, 2018
2 parents 310ab99 + 49c9bb3 commit 42d21f5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
52 changes: 48 additions & 4 deletions Sources/Profile-Modify.php
Expand Up @@ -2254,20 +2254,23 @@ function alert_delete($toDelete, $memID = false)

/**
* Counts how many alerts a user has - either unread or all depending on $unread
* We can't use db_num_rows here, as we have to determine what boards the user can see
* Possibly in future versions as database support for json is mainstream, we can simplify this.
*
* @param int $memID The user ID.
* @param bool $unread Whether to only count unread alerts.
* @return int The number of requested alerts
*/
function alert_count($memID, $unread = false)
{
global $smcFunc;
global $smcFunc, $user_info;

if (empty($memID))
return false;

// We have to do this the slow way as to iterate over all possible boards the user can see.
$request = $smcFunc['db_query']('', '
SELECT id_alert
SELECT id_alert, extra
FROM {db_prefix}user_alerts
WHERE id_member = {int:id_member}
'.($unread ? '
Expand All @@ -2277,10 +2280,51 @@ function alert_count($memID, $unread = false)
)
);

$count = $smcFunc['db_num_rows']($request);
// First we dump alerts and possible boards information out.
$alerts = array();
$boards = array();
$possible_boards = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$alerts[$row['id_alert']] = !empty($row['extra']) ? $smcFunc['json_decode']($row['extra'], true) : array();

// Only add to possible boards ones that are not empty and that we haven't set before.
if (!empty($alerts[$row['id_alert']]['board']) && !isset($possible_boards[$alerts[$row['id_alert']]['board']]))
$possible_boards[$alerts[$row['id_alert']]['board']] = $alerts[$row['id_alert']]['board'];
}
$smcFunc['db_free_result']($request);

return $count;
// If this isn't the current user, get their boards.
if (isset($user_info) && $user_info['id'] != $memID)
{
$query_see_board = build_query_board($memID);
$query_see_board = $query_see_board['query_see_board'];
}

// Find only the boards they can see.
if (!empty($possible_boards))
{
$request = $smcFunc['db_query']('', '
SELECT id_board
FROM {db_prefix}boards AS b
WHERE ' . (!empty($query_see_board) ? '{raw:query_see_board}' : '{query_see_board}') . '
AND id_board IN ({array_int:boards})',
array(
'boards' => array_keys($possible_boards),
'query_see_board' => $query_see_board
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$boards[$row['id_board']] = $row['id_board'];
}
unset($possible_boards);

// Now check alerts again and remove any they can't see.
foreach ($alerts as $id_alert => $extra)
if (!isset($boards[$extra['board']]))
unset($alerts[$id_alert]);

return count($alerts);
}

/**
Expand Down
23 changes: 8 additions & 15 deletions Sources/Profile-View.php
Expand Up @@ -229,15 +229,6 @@ function fetch_alerts($memID, $all = false, $counter = 0, $pagination = array(),
$query_see_board = build_query_board($memID);
$query_see_board = $query_see_board['query_see_board'];

$user_old = $user_info;
// are we someone else?
if (empty($user_info) || $user_info['id'] != $memID)
{
if (empty($user_profile[$memID]))
loadMemberData($memID, false, 'profile');
$user_info = $user_profile[$memID];
}

$alerts = array();
$request = $smcFunc['db_query']('', '
SELECT id_alert, alert_time, mem.id_member AS sender_id, COALESCE(mem.real_name, ua.member_name) AS sender_name,
Expand Down Expand Up @@ -270,7 +261,7 @@ function fetch_alerts($memID, $all = false, $counter = 0, $pagination = array(),
}
$smcFunc['db_free_result']($request);

if($withSender)
if ($withSender)
{
$senders = loadMemberData($senders);
foreach ($senders as $member)
Expand Down Expand Up @@ -386,17 +377,19 @@ function fetch_alerts($memID, $all = false, $counter = 0, $pagination = array(),
$extra = $alerts[$id_alert]['extra'];
$search = array('{member_link}', '{scripturl}');
$repl = array(!empty($alert['sender_id']) ? '<a href="' . $scripturl . '?action=profile;u=' . $alert['sender_id'] . '">' . $alert['sender_name'] . '</a>' : $alert['sender_name'], $scripturl);
foreach ($extra as $k => $v)

if (is_array($extra))
{
$search[] = '{' . $k . '}';
$repl[] = $v;
foreach ($extra as $k => $v)
{
$search[] = '{' . $k . '}';
$repl[] = $v;
}
}
$alerts[$id_alert]['text'] = str_replace($search, $repl, $txt[$string]);
}
}

$user_info = $user_old;

return $alerts;
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/Subs.php
Expand Up @@ -391,15 +391,12 @@ function updateMemberData($members, $data)
{
$val = 'CASE ';
foreach ($members as $k => $v)
$val .= 'WHEN id_member = ' . $v . ' THEN '. count(fetch_alerts($v, false, 0, array(), false)) . ' ';
$val .= 'WHEN id_member = ' . $v . ' THEN '. alert_count($v, false) . ' ';
$val = $val . ' END';
$type = 'raw';
}
else
{
$blub = fetch_alerts($members, false, 0, array(), false);
$val = count($blub);
}
$val = alert_count($members, false);
}
else if ($type == 'int' && ($val === '+' || $val === '-'))
{
Expand Down Expand Up @@ -6375,7 +6372,8 @@ function build_query_board($userid)
$mod_cache;
$ignoreboards;

if ($user_info['id'] == $userid)
// If we come from cron, we can't have a $user_info.
if (isset($user_info['id']) && $user_info['id'] == $userid)
{
$groups = $user_info['groups'];
$is_admin = $user_info['is_admin'];
Expand Down

0 comments on commit 42d21f5

Please sign in to comment.