This commit is contained in:
f4ilji
2024-10-28 12:57:16 +05:00
parent e4678fc0d2
commit 6d516d1750
233 changed files with 6312 additions and 1314 deletions
+64 -5
View File
@@ -10,9 +10,12 @@ class VkService
{
protected VkWallService $wallService;
protected VkAlbumService $albumService;
public function __construct(VKApiClient $vk) {
protected int $public_id;
public function __construct() {
$vk = new VKApiClient();
$this->wallService = new VkWallService($vk);
$this->albumService = new VkAlbumService($vk);
$this->public_id = env('PUBLIC_ID');
}
public function getPosts(int $count = 10)
@@ -20,14 +23,70 @@ class VkService
return $this->wallService->getPosts($count);
}
public function createPost(string $message, int $from_group = 1)
public function getPostById(int $id)
{
return $this->wallService->createPost($message, $from_group);
return $this->wallService->getPostById($id);
}
public function createAlbum(string $title)
public function createPost(string $title, string $message, array $images = [], int $publish_date = null)
{
return $this->albumService->createAlbum($title);
$from_group = 1;
$album_attachment = '';
if ($images !== []) {
$album = $this->createAlbum($title, $images);
$album_attachment = $this->createAlbumAttachmentParam($album['id']);
}
return $this->wallService->createPost($message, $from_group, $album_attachment, $publish_date);
}
public function updatePost(int $id, string $title, string $message, array $images = [], int $publish_date = null)
{
$from_group = 1;
$vk_post = $this->wallService->getPostById($id);
$album_attachment = '';
if ($vk_post['attachments']) {
if ($this->getAlbumAttachment($vk_post['attachments'])) {
$album = $this->getAlbumAttachment($vk_post['attachments']);
$this->albumService->deleteAlbum($album['album']['id'], $this->public_id);
}
}
if ($images !== []) {
$album = $this->createAlbum($title, $images);
$album_attachment = $this->createAlbumAttachmentParam($album['id']);
}
return $this->wallService->updatePost($id, $message, $from_group, $album_attachment, $publish_date);
}
public function createAlbum(string $title, $images)
{
$album = $this->albumService->createAlbum($title);
$uploadServer = $this->albumService->getServerForUploadImages($album['id'], env('PUBLIC_ID'));
foreach (array_chunk($images, 4) as $images_slice) {
$images_data = $this->albumService->uploadImagesToUploadServer($uploadServer['upload_url'], $images_slice);
$this->albumService->saveImagesToUploadServer(
$images_data['aid'],
$images_data['server'],
$images_data['photos_list'],
$images_data['hash']
);
}
return $album;
}
private function createAlbumAttachmentParam(int $attachmentId)
{
return $this->wallService->generateAttachmentsParams('album', $attachmentId);
}
private function getAlbumAttachment(array $attachments)
{
$data = collect($attachments);
$filteredAttachment = $data->filter(function($attachment) {
return $attachment['type'] === 'album';
});
return (isset($filteredAttachment[0]) ? $filteredAttachment[0] : null);
}
}