Skip to content

Commit

Permalink
threads
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Jan 6, 2024
1 parent 02da924 commit c7c393f
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions ext/pm/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,86 @@ 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

0 comments on commit c7c393f

Please sign in to comment.