Skip to content

Commit

Permalink
GH-126 Move batch action handler to its own user command handling file
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Aug 20, 2020
1 parent 1bdc430 commit 1415264
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 227 deletions.
235 changes: 8 additions & 227 deletions messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,235 +387,16 @@
case 'delete':
// Delete or do other things with Selected/Nonselected/Add Messages

/**
* @param array $params
* @param array<number> $params['knownMessageTypes']
*/
$handler = function ($params) use ($_Lang, $Now, $_User) {
$createSuccess = function ($payload) {
return [
'isSuccess' => true,
'messages' => $payload,
];
};
$createFailure = function ($payload) {
return [
'isSuccess' => false,
'errors' => $payload,
];
};

$currentTimestamp = $Now;
$knownMessageTypes = $params['knownMessageTypes'];

$input = [
'command' => $_POST['deletemessages'],
'categoryID' => intval($_POST['category']),
'timeLimit' => round($_POST['time']),
];

$timeBasedCmds = [
'deleteall',
'deleteallcat',
'setallread',
'setcatread',
];

if (
in_array($input['command'], $timeBasedCmds) &&
(
$input['timeLimit'] <= 0 ||
$input['timeLimit'] > $currentTimestamp
)
) {
return $createFailure([ $_Lang['Delete_BadTimestamp'] ]);
}

switch ($input['command']) {
case 'deleteall':
$cmdResult = Messages\Commands\batchDeleteMessagesOlderThan([
'userID' => $_User['id'],
'untilTimestamp' => $input['timeLimit'],
]);

if ($cmdResult['deletedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_NoMsgsToDelete'] ]);
}

return $createSuccess([ $_Lang['Delete_AllMsgsDeleted'] ]);
break;

case 'deleteallcat':
if ($input['categoryID'] == 80) {
return $createFailure([ $_Lang['Delete_CannotDeleteAdminMsgsAtOnce'] ]);
}

if (
!in_array($input['categoryID'], $knownMessageTypes) ||
$input['categoryID'] == 100
) {
return $createFailure([ $_Lang['Delete_BadCatSelected'] ]);
}

$cmdResult = Messages\Commands\batchDeleteMessagesOlderThan([
'userID' => $_User['id'],
'messageTypeID' => $input['categoryID'],
'untilTimestamp' => $input['timeLimit'],
]);

if ($cmdResult['deletedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_NoMsgsToDelete'] ]);
}

return $createSuccess([ $_Lang['Delete_AllCatMsgsDeleted'] ]);
break;

case 'deletemarked':
$messagesIDs = [];

if (!empty($_POST['del_all'])) {
$matches = [];

preg_match_all('#([0-9]{1,})#si', $_POST['del_all'], $matches);

$messagesIDs = $matches[0];
} else {
foreach ($_POST as $key => $value) {
$matches = [];
$matchResult = preg_match("/^del([0-9]{1,})$/D", $key, $matches);

if (!$matchResult || $value != 'on') {
continue;
}

$messagesIDs[] = $matches[1];
}
}

if (empty($messagesIDs)) {
return $createFailure([ $_Lang['Delete_NoMsgsSelected'] ]);
}

$cmdResult = Messages\Commands\batchDeleteMessagesByID([
'messageIDs' => $messagesIDs,
'userID' => $_User['id'],
]);

if ($cmdResult['deletedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_BadSelections'] ]);
}

return $createSuccess([ $_Lang['Delete_SelectedDeleted'] ]);
break;

case 'deleteunmarked':
$messagesIDs = [];

if (!empty($_POST['sm_all'])) {
$matches = [];

preg_match_all('#([0-9]{1,})#si', $_POST['sm_all'], $matches);

$messagesIDs = $matches[0];
} else {
foreach ($_POST as $key => $value) {
$matches = [];
$matchResult = preg_match("/^sm([0-9]{1,})$/D", $key, $matches);

if (!$matchResult) {
continue;
}

$deleteKey = ('del' . $matches[1]);

if ($_POST[$deleteKey] == 'on') {
continue;
}

$messagesIDs[] = $matches[1];
}
}

if (empty($messagesIDs)) {
return $createFailure([ $_Lang['Delete_NoMsgsUnselected'] ]);
}

$cmdResult = Messages\Commands\batchDeleteMessagesByID([
'messageIDs' => $messagesIDs,
'userID' => $_User['id'],
]);

if ($cmdResult['deletedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_BadSelections'] ]);
}

return $createSuccess([ $_Lang['Delete_UnselectedDeleted'] ]);
break;

case 'setallread':
$cmdResult = Messages\Commands\batchMarkMessagesAsRead([
'userID' => $_User['id'],
'untilTimestamp' => $input['timeLimit'],
]);

if ($cmdResult['markedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_NoMsgsToRead'] ]);
}

return $createSuccess([ $_Lang['Delete_AllMsgsRead'] ]);
break;

case 'setcatread':
if ($input['categoryID'] == 80) {
return $createFailure([ $_Lang['Delete_CannotReadAdminMsgsAtOnce'] ]);
}

if (
!in_array($input['categoryID'], $knownMessageTypes) ||
$input['categoryID'] == 100
) {
return $createFailure([ $_Lang['Delete_BadCatSelected'] ]);
}

$cmdResult = Messages\Commands\batchMarkMessagesAsRead([
'userID' => $_User['id'],
'messageTypeID' => $input['categoryID'],
'untilTimestamp' => $input['timeLimit'],
]);

if ($cmdResult['markedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_NoMsgsToRead'] ]);
}

return $createSuccess([ $_Lang['Delete_CatMsgsRead'] ]);
break;

default:
if (empty($_POST['delid'])) {
return $createFailure([]);
}

$messageID = round(floatval($_POST['delid']));

$cmdResult = Messages\Commands\batchDeleteMessagesByID([
'messageIDs' => [ $messageID ],
'userID' => $_User['id'],
'ignoreExcludedMessageTypesRestriction' => true,
]);

if ($cmdResult['deletedMessagesCount'] <= 0) {
return $createFailure([ $_Lang['Delete_MsgNoExist'] ]);
}

return $createSuccess([ $_Lang['Delete_MsgDeleted'] ]);
}
};

$_ThisCategory = intval($_POST['category']);

$result = $handler([
'knownMessageTypes' => $MessageType,
]);
$result = Messages\Input\UserCommands\handleBatchAction(
$_POST,
[
'user' => &$_User,
'timestamp' => $Now,
'knownMessageTypes' => $MessageType,
]
);

if ($result['isSuccess'] && !empty($result['messages'])) {
foreach ($result['messages'] as $messageContent) {
Expand Down
1 change: 1 addition & 0 deletions modules/messages/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
include($includePath . './commands/batchDeleteMessagesByID.commands.php');
include($includePath . './commands/batchDeleteMessagesOlderThan.commands.php');
include($includePath . './commands/batchMarkMessagesAsRead.commands.php');
include($includePath . './input/batchActions.userCommands.php');
include($includePath . './utils/batchDeleteMessages.utils.php');
include($includePath . './utils/batchMessageUpdates.utils.php');

Expand Down
Loading

0 comments on commit 1415264

Please sign in to comment.