Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM Threads #993

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions ext/pm/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,90 @@ public static function send_pm(int $to_user_id, string $subject, string $message
}
}

#[Type(name: "PMThread")]
class PMThread
{
public function __construct(
#[Field]
public User $user,
#[Field]
public bool $is_read,
#[Field]
public string $last_update,
#[Field(type: "[PrivateMessage!]!")]
public array $messages,
) {
}

#[Field(extends: "User")]
public static function thread(User $duser, string $other): PMThread
{
global $database, $user;

$ouser = User::by_name($other);

if (!$user->can(Permissions::READ_PM)) {
return null;
}
if (($duser->id != $user->id) && !$user->can(Permissions::VIEW_OTHER_PMS)) {
return null;
}

$rows = $database->get_all(
"SELECT *
FROM private_message
WHERE (to_id = :me AND from_id = :them) OR (to_id = :them AND from_id = :me)
ORDER BY sent_date ASC",
["me" => $duser->id, "them" => $ouser->id]
);
$messages = [];
$is_read = true;
$last_update = "";
foreach ($rows as $row) {
$pm = PM::from_row($row);
$messages[] = $pm;
if($pm->to_id == $duser->id && !$pm->is_read) {
$is_read = false;
}
$last_update = $pm->sent_date;
}
return new PMThread($ouser, $is_read, $last_update, $messages);
}

#[Field(extends: "User", type: "[PMThread!]!")]
public static function threads(User $duser): array
{
global $database, $user;

if (!$user->can(Permissions::READ_PM)) {
return null;
}
if (($duser->id != $user->id) && !$user->can(Permissions::VIEW_OTHER_PMS)) {
return null;
}

$rows = $database->get_all(
"SELECT *
FROM private_message
WHERE (to_id = :me) OR (from_id = :me)
ORDER BY sent_date ASC",
["me" => $duser->id]
);
$others = [];
foreach ($rows as $row) {
$others[] = ($row["to_id"] == $duser->id) ? $row["from_id"] : $row["to_id"];
}
$others = \array_unique($others);

$threads = [];
foreach ($others as $other) {
$ouser = User::by_id($other);
$threads[] = PMThread::thread($duser, $ouser->name);
}
return $threads;
}
}

class PrivMsg extends Extension
{
/** @var PrivMsgTheme */
Expand Down