mirror of
https://github.com/NamelessMC/Nameless
synced 2026-08-18 06:29:04 -04:00
169 lines
6 KiB
PHP
169 lines
6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Provides access to create & get alerts for a user, as well as their PMs.
|
|
*
|
|
* @package NamelessMC\Core
|
|
* @author Samerton
|
|
* @version 2.0.0-pr8
|
|
* @license MIT
|
|
*/
|
|
class Alert
|
|
{
|
|
/**
|
|
* Creates an alert for the specified user.
|
|
*
|
|
* @deprecated Use Alert::send instead
|
|
*
|
|
* @param int $user_id Contains the ID of the user who we are creating the alert for.
|
|
* @param string $type Contains the alert type, eg 'tag' for user tagging.
|
|
* @param array $text_short Contains the alert text in short form for the dropdown.
|
|
* @param array $text Contains full information about the alert.
|
|
* @param ?string $link Contains link to view the alert, defaults to #.
|
|
* @param ?string $content Optional alert content.
|
|
*/
|
|
public static function create(int $user_id, string $type, array $text_short, array $text, ?string $link = '#', ?string $content = null): void
|
|
{
|
|
$db = DB::getInstance();
|
|
|
|
$language = $db->query('SELECT nl2_languages.short_code AS `short_code` FROM nl2_users LEFT JOIN nl2_languages ON nl2_languages.id = nl2_users.language_id WHERE nl2_users.id = ?', [$user_id]);
|
|
|
|
if (!$language->count()) {
|
|
return;
|
|
}
|
|
|
|
$language = new Language($text_short['path'], $language->first()->short_code);
|
|
|
|
$text_short = $text_short['content'] ?? str_replace($text_short['replace'] ?? '', $text_short['replace_with'] ?? '', $language->get($text_short['file'], $text_short['term']));
|
|
$text = $text['content'] ?? str_replace($text['replace'] ?? '', $text['replace_with'] ?? '', $language->get($text['file'], $text['term']));
|
|
|
|
$db->insert('alerts', [
|
|
'user_id' => $user_id,
|
|
'type' => $type,
|
|
'url' => $link,
|
|
'content_short' => $text_short,
|
|
'content' => $text,
|
|
'content_rich' => $content,
|
|
'created' => date('U'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Post a new alert to a user.
|
|
*
|
|
* @param int $userId
|
|
* @param string $title
|
|
* @param string|null $content
|
|
* @param string|null $link Optional link to redirect the user to on click
|
|
* @param bool $skipPurify If true the content will not be purified before displaying to user - use with care
|
|
* @return void
|
|
*/
|
|
public static function send(int $userId, string $title, ?string $content, ?string $link = '', bool $skipPurify = false)
|
|
{
|
|
DB::getInstance()->insert('alerts', [
|
|
'user_id' => $userId,
|
|
'type' => 'alert',
|
|
'url' => $link ?? '',
|
|
'content_short' => $title, // Column maintained for legacy reasons
|
|
'content' => $title,
|
|
'content_rich' => $content ?? '',
|
|
'created' => date('U'),
|
|
'bypass_purify' => $skipPurify,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get user alerts.
|
|
*
|
|
* @param int $user_id Contains the ID of the user who we are getting alerts for.
|
|
* @param bool $all Do we want to get all alerts (including read), or not; defaults to false).
|
|
*
|
|
* @return array All their alerts.
|
|
*/
|
|
public static function getAlerts(int $user_id, bool $all = false): array
|
|
{
|
|
$db = DB::getInstance();
|
|
|
|
if ($all == true) {
|
|
$alerts = $db->get('alerts', ['user_id', $user_id])->results();
|
|
} else {
|
|
$alerts = $db->query('SELECT * FROM nl2_alerts WHERE user_id = ? AND `read` = 0', [$user_id])->results();
|
|
}
|
|
|
|
return array_map(
|
|
static fn($alert) => [
|
|
...(array) $alert,
|
|
'content' => Output::getClean($alert->content),
|
|
'content_short' => Output::getClean($alert->content_short),
|
|
'content_rich' => $alert->bypass_purify ? $alert->content_rich : Output::getPurified($alert->content_rich, false, false),
|
|
],
|
|
$alerts
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get a users unread messages.
|
|
*
|
|
* @param int $user_id The ID of the user who we are getting messages for.
|
|
* @param bool $all Get all alerts (including read), or not. Defaults to false.
|
|
*
|
|
* @return array All their messages matching the $all filter.
|
|
*/
|
|
public static function getPMs(int $user_id, bool $all = false): array
|
|
{
|
|
$db = DB::getInstance();
|
|
|
|
if ($all == true) {
|
|
$pms_access = $db->get('private_messages_users', ['user_id', $user_id])->results();
|
|
$pms = [];
|
|
|
|
foreach ($pms_access as $pm) {
|
|
// Get actual PM information
|
|
$pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results();
|
|
|
|
if (!count($pm_full)) {
|
|
continue;
|
|
}
|
|
|
|
$pm_full = $pm_full[0];
|
|
|
|
$pms[] = [
|
|
'id' => $pm_full->id,
|
|
'title' => Output::getClean($pm_full->title),
|
|
'created' => $pm_full->created,
|
|
'author_id' => $pm_full->author_id,
|
|
'last_reply_user' => $pm_full->last_reply_user,
|
|
'last_reply_date' => $pm_full->last_reply_date,
|
|
];
|
|
}
|
|
|
|
return $pms;
|
|
}
|
|
|
|
$pms = $db->get('private_messages_users', ['user_id', $user_id])->results();
|
|
$unread = [];
|
|
|
|
foreach ($pms as $pm) {
|
|
if ($pm->read == 0) {
|
|
$pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results();
|
|
|
|
if (!count($pm_full)) {
|
|
continue;
|
|
}
|
|
|
|
$pm_full = $pm_full[0];
|
|
|
|
$unread[] = [
|
|
'id' => $pm_full->id,
|
|
'title' => Output::getClean($pm_full->title),
|
|
'created' => $pm_full->created,
|
|
'author_id' => $pm_full->author_id,
|
|
'last_reply_user' => $pm_full->last_reply_user,
|
|
'last_reply_date' => $pm_full->last_reply_date,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $unread;
|
|
}
|
|
}
|