openvk/Web/Models/Repositories/Conversations.php

54 lines
1.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-07 19:04:43 +03:00
namespace openvk\Web\Models\Repositories;
2020-06-07 19:04:43 +03:00
use openvk\Web\Models\Entities\{Messages as M, User};
use Chandler\Database\DatabaseConnection as DB;
use Nette\Database\Table\ActiveRow;
class Conversations
{
private $context;
private $convos;
public function __construct()
2020-06-07 19:04:43 +03:00
{
$this->context = DB::i()->getContext();
$this->convos = $this->context->table("conversations");
}
2020-06-07 19:04:43 +03:00
private function toConversation(?ActiveRow $ar): ?M\AbstractConversation
{
if (is_null($ar)) {
return null;
} elseif ($ar->is_pm) {
2020-06-07 19:04:43 +03:00
return new M\PrivateConversation($ar);
} else {
2020-06-07 19:04:43 +03:00
return new M\Conversation($ar);
}
2020-06-07 19:04:43 +03:00
}
public function get(int $id): ?M\AbstractConversation
2020-06-07 19:04:43 +03:00
{
return $this->toConversation($this->convos->get($id));
}
public function getConversationsByUser(User $user, int $page = 1, ?int $perPage = null): \Traversable
2020-06-07 19:04:43 +03:00
{
$rels = $this->context->table("conversation_members")->where([
"deleted" => false,
"user" => $user->getId(),
])->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE);
foreach ($rels as $rel) {
2020-06-07 19:04:43 +03:00
yield $this->get($rel->conversation);
}
2020-06-07 19:04:43 +03:00
}
public function getPrivateConversation(User $user, int $peer): M\PrivateConversation
2020-06-07 19:04:43 +03:00
{
;
}
}