Skip to content

Commit

Permalink
GH-126 Add recipient data fetchers, by uid & by name
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Sep 23, 2020
1 parent a70e259 commit 41e73c6
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/messages/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
include($includePath . './utils/batchMessageUpdates.utils.php');
include($includePath . './utils/createReplyMessageSubject.utils.php');
include($includePath . './utils/fetchFormDataForReply.utils.php');
include($includePath . './utils/fetchRecipientDataByUserId.utils.php');
include($includePath . './utils/fetchRecipientDataByUsername.utils.php');
include($includePath . './utils/getMessageCopyId.utils.php');
include($includePath . './utils/sendMessage.utils.php');
include($includePath . './validators/validateWithIgnoreSystem.validators.php');
Expand Down
55 changes: 55 additions & 0 deletions modules/messages/utils/fetchRecipientDataByUserId.utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace UniEngine\Engine\Modules\Messages\Utils;

/**
* Fetches necessary data of a message recipient.
*
* @param array $params
* @param string $params['userId']
*/
function fetchRecipientDataByUserId($params) {
$userId = intval($params['userId']);

if (!($userId > 0)) {
return [
'isSuccess' => false,
'errors' => [
'isUserIdInvalid' => true,
],
];
}

$query_fetchRecipientData = (
"SELECT `id`, `username`, `authlevel` " .
"FROM {{table}} " .
"WHERE `id` = '{$userId}' " .
"LIMIT 1 " .
";"
);
$result_fetchRecipientData = doquery($query_fetchRecipientData, 'users', true);

if (!$result_fetchRecipientData) {
return [
'isSuccess' => false,
'errors' => [
'notFound' => true,
],
];

return;
}

$recipientData = [
'id' => $result_fetchRecipientData['id'],
'username' => $result_fetchRecipientData['username'],
'authlevel' => $result_fetchRecipientData['authlevel'],
];

return [
'isSuccess' => true,
'payload' => $recipientData,
];
}

?>
55 changes: 55 additions & 0 deletions modules/messages/utils/fetchRecipientDataByUsername.utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace UniEngine\Engine\Modules\Messages\Utils;

/**
* Fetches necessary data of a message recipient.
*
* @param array $params
* @param string $params['username']
*/
function fetchRecipientDataByUsername($params) {
$username = trim($params['username']);

if (!preg_match(REGEXP_USERNAME_ABSOLUTE, $username)) {
return [
'isSuccess' => false,
'errors' => [
'isUsernameInvalid' => true,
],
];
}

$query_fetchRecipientData = (
"SELECT `id`, `authlevel` " .
"FROM {{table}} " .
"WHERE `username` = '{$username}' " .
"LIMIT 1 " .
";"
);
$result_fetchRecipientData = doquery($query_fetchRecipientData, 'users', true);

if (!$result_fetchRecipientData) {
return [
'isSuccess' => false,
'errors' => [
'notFound' => true,
],
];

return;
}

$recipientData = [
'id' => $result_fetchRecipientData['id'],
'username' => $username,
'authlevel' => $result_fetchRecipientData['authlevel'],
];

return [
'isSuccess' => true,
'payload' => $recipientData,
];
}

?>

0 comments on commit 41e73c6

Please sign in to comment.