Skip to content

Commit

Permalink
PMs are sent by creating a conversation in an Inbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Spine committed Jan 1, 2024
1 parent dd5d8a6 commit 5c42da9
Show file tree
Hide file tree
Showing 54 changed files with 517 additions and 431 deletions.
9 changes: 7 additions & 2 deletions app/Applicant.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ public function saveNote(User $poster, string $body, string $visibility): int {
}
$noteId = $this->thread()->saveNote($poster, $body, $visibility);
if ($visibility == 'public' && $this->role()->isStaffViewer($poster)) {
(new Manager\User)->sendPM(
$this->userId(), 0,
/**
* We could send from the poster account, but that could
* direct the conversation away from the application page.
* Sending from System ensures the discussion is not fragmented
* between inboxes.
*/
(new User($this->userId()))->inbox()->createSystem(
"You have a reply to your {$this->role()->title()} application",
self::$twig->render('applicant/pm-reply.bbcode.twig', [
'applicant' => $this,
Expand Down
4 changes: 2 additions & 2 deletions app/Contest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ public function doPayout(Manager\User $userMan): int {
if (DEBUG_CONTEST_PAYOUT) {
continue;
}
$userMan->sendPM($p['ID'], 0,
$user->inbox()->createSystem(
"You have received " . number_format($totalGain, 2) . " bonus points!",
self::$twig->render('contest/payout-uploader.twig', [
self::$twig->render('contest/payout-uploader.bbcode.twig', [
'contest' => $this,
'contest_bonus' => $contestBonus,
'enabled_bonus' => $enabledUserBonus,
Expand Down
25 changes: 20 additions & 5 deletions app/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Login extends Base {
final public const NO_ERROR = 0;
final public const ERR_CREDENTIALS = 1;
final public const ERR_UNCONFIRMED = 2;
final public const FLOOD_COUNT = 'login_flood_total_%d';

protected int $error = self::NO_ERROR;
protected bool $persistent = false;
Expand Down Expand Up @@ -62,16 +63,30 @@ public function login(
if ($user) {
$this->watch->clearAttempts();
$user->toggleAttr('inactive-warning-sent', false);
self::$cache->delete_value(sprintf(self::FLOOD_COUNT, $user->id()));
} else {
// we might not have an authenticated user, but still have the id of the username
$this->watch->increment($this->userId, $this->username);
if ($this->watch->nrAttempts() > 10) {
$this->watch->ban($this->username);
(new Manager\User)->sendPM($this->userId, 0, "Too many login attempts on your account",
self::$twig->render('login/too-many-failures.twig', [
'ipaddr' => $this->ipaddr,
'username' => $this->username,
]));

$key = 'login_flood_' . $this->userId;
if (self::$cache->get_value($key) === false) {
self::$cache->cache_value($key, true, 86400);
// fake a user object temporarily to send them some email
(new User($this->userId))->inbox()->createSystem(
"Too many login attempts on your account",
self::$twig->render('login/too-many-failures.bbcode.twig', [
'ipaddr' => $this->ipaddr,
'username' => $this->username,
])
);
}
$key = sprintf(self::FLOOD_COUNT, $this->userId);
if (self::$cache->get_value($key) === false) {
self::$cache->cache_value($key, 0, 86400 * 7);
}
self::$cache->increment($key);
} elseif ($this->watch->nrBans() > 3) {
(new Manager\IPv4)->createBan(
$this->userId, $this->ipaddr, $this->ipaddr, 'Automated ban, too many failed login attempts'
Expand Down
3 changes: 2 additions & 1 deletion app/Manager/Recovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ public function boostUpload(): void {
--OPS Staff
END_MSG;
}
$userMan->sendPM($siteUserId, 0, "Your buffer stats have been updated", $Body);
(new \Gazelle\User($siteUserId))->inbox()
->createSystem("Your buffer stats have been updated", $Body);
}

/* insert this first to avoid a potential reallocation */
Expand Down
90 changes: 32 additions & 58 deletions app/Manager/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,45 +455,6 @@ public function flushEnabledUsersCount(): static {
return $this;
}

/**
* Sends a PM from $FromId to $ToId.
*
* @return int conversation Id
*/
public function sendPM(int $toId, int $fromId, string $subject, string $body): int {
if ($toId === 0 || $toId === $fromId) {
// Don't allow users to send messages to the system or themselves
return 0;
}

$qid = self::$db->get_query_id();
self::$db->begin_transaction();
self::$db->prepared_query("
INSERT INTO pm_conversations (Subject) VALUES (?)
", mb_substr($subject, 0, 255)
);
$convId = self::$db->inserted_id();

$placeholders = ["(?, ?, '1', '0', '1')"];
$args = [$toId, $convId];
if ($fromId !== 0) {
$placeholders[] = "(?, ?, '0', '1', '0')";
$args = array_merge($args, [$fromId, $convId]);
}

self::$db->prepared_query("
INSERT INTO pm_conversations_users
(UserID, ConvID, InInbox, InSentbox, UnRead)
VALUES
" . implode(', ', $placeholders), ...$args
);
$this->deliverPM($toId, $fromId, $subject, $body, $convId);
self::$db->commit();
self::$db->set_query_id($qid);

return $convId;
}

/**
* Send a reply from $FromId to $ToId.
*
Expand Down Expand Up @@ -565,7 +526,7 @@ public function sendCustomPM(\Gazelle\User $sender, string $subject, string $tem
continue;
}
$message = preg_replace('/%USERNAME%/', $user->username(), $template);
$this->sendPM($userId, $sender->id(), $subject, $message);
$user->inbox()->create($sender, $subject, $message);
$total++;
}
return $total;
Expand All @@ -579,7 +540,10 @@ public function sendSnatchPm(\Gazelle\User $viewer, \Gazelle\Torrent $torrent,

$snatchers = self::$db->collect(0, false);
foreach ($snatchers as $userId) {
$this->sendPM($userId, 0, $subject, $body);
$user = $this->findById($userId);
if ($user) {
$user->inbox()->createSystem($subject, $body);
}
}
$total = count($snatchers);
(new \Gazelle\Log)->general($viewer->username() . " sent a mass PM to $total snatcher" . plural($total)
Expand All @@ -600,7 +564,10 @@ public function sendRemovalPm(int $torrentId, int $uploaderId, string $name, str
. ".";

if ($pmUploader) {
$this->sendPM($uploaderId, 0, $subject, sprintf($message, 'you uploaded'));
$user = $this->findById($uploaderId);
if ($user) {
$user->inbox()->createSystem($subject, sprintf($message, 'you uploaded'));
}
}
$seen = [$uploaderId];

Expand All @@ -615,7 +582,10 @@ public function sendRemovalPm(int $torrentId, int $uploaderId, string $name, str
);
$ids = self::$db->collect('uid');
foreach ($ids as $userId) {
$this->sendPM($userId, 0, $subject, sprintf($message, 'you are seeding'));
$user = $this->findById($userId);
if ($user) {
$user->inbox()->createSystem($subject, sprintf($message, 'you are seeding'));
}
}
$seen = array_merge($seen, $ids);

Expand All @@ -630,7 +600,10 @@ public function sendRemovalPm(int $torrentId, int $uploaderId, string $name, str
);
$ids = self::$db->collect('uid');
foreach ($ids as $userId) {
$this->sendPM($userId, 0, $subject, sprintf($message, 'you have snatched'));
$user = $this->findById($userId);
if ($user) {
$user->inbox()->createSystem($subject, sprintf($message, 'you have snatched'));
}
}
$seen = array_merge($seen, $ids);

Expand All @@ -645,7 +618,10 @@ public function sendRemovalPm(int $torrentId, int $uploaderId, string $name, str
);
$ids = self::$db->collect('UserID');
foreach ($ids as $userId) {
$this->sendPM($userId, 0, $subject, sprintf($message, 'you have downloaded'));
$user = $this->findById($userId);
if ($user) {
$user->inbox()->createSystem($subject, sprintf($message, 'you have downloaded'));
}
}

return count(array_merge($seen, $ids));
Expand Down Expand Up @@ -989,7 +965,7 @@ public function promote(\Gazelle\Task $task = null, bool $commit = true): int {
$user->setField('PermissionID', $level['To'])
->addStaffNote("Class changed to $toClass by System")
->modify();
$this->sendPM($userId, 0,
$user->inbox()->createSystem(
"You have been promoted to $toClass",
"Congratulations on your promotion to $toClass!\n\nTo read more about "
. SITE_NAME
Expand Down Expand Up @@ -1046,7 +1022,7 @@ public function demote(\Gazelle\Task $task = null, bool $commit = true): int {
$user->setField('PermissionID', $level['From'])
->addStaffNote("Class changed to $toClass by System")
->modify();
$this->sendPM($userId, 0,
$user->inbox()->createSystem(
"You have been demoted to $toClass",
"You now only qualify for the \"$toClass\" user class.\n\nTo read more about "
. SITE_NAME
Expand Down Expand Up @@ -1371,13 +1347,13 @@ public function ratioWatchBlock(\Gazelle\Tracker $tracker, ?\Gazelle\Task $task
$user->setField('can_leech', 0)
->addStaffNote("Leeching privileges suspended by ratio watch system (required ratio: $ratio) for downloading more than 10 GBs on ratio watch.")
->modify();
$tracker->update_tracker('update_user', ['passkey' => $user->announceKey(), 'can_leech' => '0']);
$this->sendPM( $userId, 0,
$user->inbox()->createSystem(
'Your download privileges have been removed',
'You have downloaded more than 10 GB while on Ratio Watch. Your leeching privileges have been suspended. Please reread the rules and refer to this guide on [url=wiki.php?action=article&name=ratiotips]how to improve your ratio[/url]',
);
$processed++;
$tracker->update_tracker('update_user', ['passkey' => $user->announceKey(), 'can_leech' => '0']);
$task?->debug("Disabling leech for {$user->label()}", $userId);
$processed++;
}
self::$db->commit();
return $processed;
Expand Down Expand Up @@ -1409,13 +1385,12 @@ public function ratioWatchClear(\Gazelle\Tracker $tracker, ?\Gazelle\Task $task
if (is_null($user)) {
continue;
}
$user->flush();
$tracker->update_tracker('update_user', ['passkey' => $user->announceKey(), 'can_leech' => '1']);
$task?->debug("Taking {$user->label()} off ratio watch", $userId);
$this->sendPM($userId, 0,
$user->inbox()->createSystem(
'You have been taken off Ratio Watch',
"Congratulations! Feel free to begin downloading again.\n To ensure that you do not get put on ratio watch again, please read the rules located [url=rules.php?p=ratio]here[/url].\n"
);
$tracker->update_tracker('update_user', ['passkey' => $user->announceKey(), 'can_leech' => '1']);
$task?->debug("Taking {$user->label()} off ratio watch", $userId);
$processed++;
}
self::$db->commit();
Expand Down Expand Up @@ -1445,7 +1420,7 @@ public function ratioWatchEngage(\Gazelle\Tracker $tracker, \Gazelle\Task $task
->addStaffNote("Leeching ability suspended by ratio watch system (required ratio: $ratio)")
->modify();
$tracker->update_tracker('update_user', ['passkey' => $user->announceKey(), 'can_leech' => '0']);
$this->sendPM($userId, 0,
$user->inbox()->createSystem(
'Your downloading privileges have been suspended',
"As you did not raise your ratio in time, your downloading privileges have been revoked. You will not be able to download any torrents until your ratio is above your new required ratio."
);
Expand Down Expand Up @@ -1481,13 +1456,12 @@ public function ratioWatchSet(?\Gazelle\Task $task = null): int {
if (is_null($user)) {
continue;
}
$user->flush();
$this->sendPM($userId, 0,
$user->inbox()->createSystem(
'You have been put on Ratio Watch',
"This happens when your ratio falls below the requirements outlined in the rules located [url=rules.php?p=ratio]here[/url].\n For information about ratio watch, click the link above."
);
$processed++;
$task?->debug("Putting $userId on ratio watch", $userId);
$processed++;
}
self::$db->commit();
return $processed;
Expand Down
Loading

0 comments on commit 5c42da9

Please sign in to comment.