2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
namespace openvk\Web\Models\Repositories;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
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;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function __construct()
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$this->context = DB::i()->getContext();
|
|
|
|
$this->convos = $this->context->table("conversations");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
private function toConversation(?ActiveRow $ar): ?M\AbstractConversation
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
if (is_null($ar)) {
|
|
|
|
return null;
|
|
|
|
} elseif ($ar->is_pm) {
|
2020-06-07 19:04:43 +03:00
|
|
|
return new M\PrivateConversation($ar);
|
2025-01-31 18:20:13 +03:00
|
|
|
} else {
|
2020-06-07 19:04:43 +03:00
|
|
|
return new M\Conversation($ar);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function get(int $id): ?M\AbstractConversation
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
return $this->toConversation($this->convos->get($id));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
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);
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($rels as $rel) {
|
2020-06-07 19:04:43 +03:00
|
|
|
yield $this->get($rel->conversation);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getPrivateConversation(User $user, int $peer): M\PrivateConversation
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|