more fields

This commit is contained in:
Shish 2023-02-07 13:27:20 +00:00
parent 9b435f6fc3
commit ea29e7c954
3 changed files with 67 additions and 4 deletions

View file

@ -56,6 +56,7 @@ class Comment
public string $owner_class;
#[Field]
public string $comment;
#[Field]
public int $comment_id;
public int $image_id;
public string $poster_ip;
@ -96,7 +97,7 @@ class Comment
}
#[Field(extends: "Post", name: "comments", type: "[Comment!]!")]
public function get_comments(Image $post): array
public static function get_comments(Image $post): array
{
return CommentList::get_comments($post->id);
}

View file

@ -96,6 +96,24 @@ class PM
return $pms;
}
#[Field(extends: "User", name: "private_message_unread_count")]
public static function count_unread_pms(User $duser): ?int
{
global $database, $user;
if (!$user->can(Permissions::READ_PM)) {
return null;
}
if (($duser->id != $user->id) && !$user->can(Permissions::VIEW_OTHER_PMS)) {
return null;
}
return (int)$database->get_one(
"SELECT COUNT(*) FROM private_message WHERE to_id = :to_id AND is_read = :is_read",
["is_read" => false, "to_id" => $duser->id]
);
}
#[Mutation(name: "create_private_message")]
public static function send_pm(int $to_id, string $subject, string $message): bool
{

View file

@ -6,6 +6,10 @@ namespace Shimmie2;
require_once "events.php";
use GQLA\Field;
use GQLA\Type;
use GQLA\Mutation;
use MicroHTML\HTMLElement;
use MicroCRUD\ActionColumn;
use MicroCRUD\EnumColumn;
@ -75,6 +79,40 @@ class NullUserException extends SCoreException
{
}
#[Type(name: "LoginResult")]
class LoginResult
{
public function __construct(
#[Field]
public User $user,
#[Field]
public ?string $session = null,
#[Field]
public ?string $error = null,
) {
}
#[Mutation(name: "login")]
public static function login(string $name, string $pass): LoginResult
{
global $config;
$duser = User::by_name_and_pass($name, $pass);
if (!is_null($duser)) {
return new LoginResult(
$duser,
UserPage::get_session_id($duser->name),
null
);
} else {
$anon = User::by_id($config->get_int("anon_id", 0));
return new LoginResult(
$anon,
null,
"No user found"
);
}
}
}
class UserPage extends Extension
{
/** @var UserPageTheme $theme */
@ -554,12 +592,18 @@ class UserPage extends Extension
return $new_user;
}
public static function get_session_id(string $name): string
{
global $config;
$addr = get_session_ip($config);
$hash = User::by_name($name)->passhash;
return md5($hash.$addr);
}
private function set_login_cookie(string $name): void
{
global $config, $page;
$addr = get_session_ip($config);
$hash = User::by_name($name)->passhash;
$page->add_cookie(
"user",
@ -569,7 +613,7 @@ class UserPage extends Extension
);
$page->add_cookie(
"session",
md5($hash.$addr),
$this->get_session_id($name),
time()+60*60*24*$config->get_int('login_memory'),
'/'
);