Changes
This commit is contained in:
@@ -21,9 +21,10 @@ class PublishPostAction
|
||||
* Публикует черновик поста
|
||||
*
|
||||
* @param Post $post Пост для публикации
|
||||
* @param bool $publishToVk Публиковать ли в VK
|
||||
* @return Post Опубликованный пост
|
||||
*/
|
||||
public function run(Post $post): Post
|
||||
public function run(Post $post, bool $publishToVk = true): Post
|
||||
{
|
||||
// Обновляем данные поста
|
||||
$updatedData = $this->postDataProcessor->processUpdate([
|
||||
@@ -40,8 +41,10 @@ class PublishPostAction
|
||||
// Отправляем уведомления
|
||||
$this->sendNotifications($post);
|
||||
|
||||
// Публикуем в VK
|
||||
$this->publishToVk($post);
|
||||
// Публикуем в VK, если указано
|
||||
if ($publishToVk) {
|
||||
$this->publishToVk($post);
|
||||
}
|
||||
|
||||
// Обновляем слайд, если есть
|
||||
$this->updateSlide($post);
|
||||
|
||||
@@ -95,9 +95,12 @@ class CreatePostFromAiDataTask
|
||||
*/
|
||||
private function preparePostData(array $newsData, array $content, ?string $previewPath, array $imagesPaths): array
|
||||
{
|
||||
$baseSlug = Str::slug($newsData['title'] ?? '');
|
||||
$slug = $this->generateUniqueSlug($baseSlug);
|
||||
|
||||
return [
|
||||
'title' => $newsData['title'] ?? 'Без названия',
|
||||
'slug' => Str::slug($newsData['title'] ?? '') . '-' . time(),
|
||||
'slug' => $slug,
|
||||
'content' => $content,
|
||||
'authors' => $newsData['authors'] ?? [],
|
||||
'category_id' => $newsData['category_id'] ?? null,
|
||||
@@ -113,6 +116,22 @@ class CreatePostFromAiDataTask
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Генерирует уникальный slug
|
||||
*/
|
||||
private function generateUniqueSlug(string $baseSlug): string
|
||||
{
|
||||
$slug = $baseSlug;
|
||||
$count = 1;
|
||||
|
||||
while (Post::where('slug', $slug)->exists()) {
|
||||
$slug = $baseSlug . '-' . $count;
|
||||
$count++;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Сохраняет теги поста
|
||||
*/
|
||||
|
||||
@@ -27,7 +27,7 @@ class PublishPostController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
$publishedPost = $this->publishPostAction->run($post);
|
||||
$publishedPost = $this->publishPostAction->run($post, $request->input('publish_to_vk', true));
|
||||
|
||||
return back()->with([
|
||||
'success' => 'Новость успешно опубликована: ' . $publishedPost->title,
|
||||
|
||||
@@ -26,7 +26,7 @@ class CreateVkPost implements ShouldQueue
|
||||
readonly private array $images = [],
|
||||
readonly private int $post_id,
|
||||
readonly private int|null $publish_date = null,
|
||||
|
||||
readonly private string $primary_attachments_mode = 'carousel',
|
||||
)
|
||||
{}
|
||||
|
||||
@@ -34,7 +34,13 @@ class CreateVkPost implements ShouldQueue
|
||||
{
|
||||
try {
|
||||
$vkService = new VkService();
|
||||
$vk_post = $vkService->createPost($this->title, $this->text, $this->images, $this->publish_date);
|
||||
$vk_post = $vkService->createPost(
|
||||
$this->title,
|
||||
$this->text,
|
||||
$this->images,
|
||||
$this->publish_date,
|
||||
$this->primary_attachments_mode
|
||||
);
|
||||
DB::table('posts_vk_posts')->insert(
|
||||
[
|
||||
'post_id' => $this->post_id,
|
||||
|
||||
@@ -28,9 +28,10 @@ class CreateVkPostJob implements ShouldQueue
|
||||
protected $videos;
|
||||
protected $publish_date;
|
||||
protected $public_id;
|
||||
protected $primary_attachments_mode;
|
||||
|
||||
public function __construct(
|
||||
int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null
|
||||
int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null, string $primary_attachments_mode = 'carousel'
|
||||
)
|
||||
{
|
||||
$this->post_id = $post_id;
|
||||
@@ -40,6 +41,7 @@ class CreateVkPostJob implements ShouldQueue
|
||||
$this->videos = $videos;
|
||||
$this->publish_date = $publish_date;
|
||||
$this->public_id = config('services.vk.public_id');
|
||||
$this->primary_attachments_mode = $primary_attachments_mode;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
@@ -96,7 +98,7 @@ class CreateVkPostJob implements ShouldQueue
|
||||
$attachmentString = implode(',', $selected_attachments);
|
||||
Log::info("{$logPrefix} Финальная строка вложений: '{$attachmentString}'");
|
||||
|
||||
$vk_post = $wallService->createPost($this->message, $from_group, $attachmentString, $this->publish_date);
|
||||
$vk_post = $wallService->createPost($this->message, $from_group, $attachmentString, $this->publish_date, $this->primary_attachments_mode);
|
||||
|
||||
if (isset($vk_post['post_id'])) {
|
||||
Log::info("{$logPrefix} Пост успешно создан ID: {$vk_post['post_id']}");
|
||||
|
||||
@@ -28,9 +28,10 @@ class UpdateVkPostJob implements ShouldQueue
|
||||
protected $videos;
|
||||
protected $publish_date;
|
||||
protected $public_id;
|
||||
protected $primary_attachments_mode;
|
||||
|
||||
public function __construct(
|
||||
int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null
|
||||
int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null, string $primary_attachments_mode = 'carousel'
|
||||
)
|
||||
{
|
||||
$this->post_id = $post_id;
|
||||
@@ -40,6 +41,7 @@ class UpdateVkPostJob implements ShouldQueue
|
||||
$this->videos = $videos;
|
||||
$this->publish_date = $publish_date;
|
||||
$this->public_id = config('services.vk.public_id');
|
||||
$this->primary_attachments_mode = $primary_attachments_mode;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
|
||||
@@ -53,7 +53,7 @@ class VkService
|
||||
// return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date);
|
||||
// }
|
||||
|
||||
public function createPost(string $title, string $message, array $images = [], array $videos = [], int|null $publish_date = null)
|
||||
public function createPost(string $title, string $message, array $images = [], array $videos = [], int|null $publish_date = null, string $primary_attachments_mode = 'carousel')
|
||||
{
|
||||
$from_group = 1;
|
||||
$attachments = [];
|
||||
@@ -76,7 +76,7 @@ class VkService
|
||||
|
||||
$attachmentString = implode(',', $attachments);
|
||||
|
||||
return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date);
|
||||
return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date, $primary_attachments_mode);
|
||||
}
|
||||
|
||||
public function updatePost(int $id, string $title, string $message, array $images = [], int|null $publish_date = null)
|
||||
|
||||
@@ -48,7 +48,7 @@ class VkWallService
|
||||
}
|
||||
}
|
||||
|
||||
public function createPost(string $message, int $from_group, string $attachments = '', int|null $publish_date = null)
|
||||
public function createPost(string $message, int $from_group, string $attachments = '', int|null $publish_date = null, string $primary_attachments_mode = 'carousel')
|
||||
{
|
||||
$token = $this->vkAuthService->getToken()->access_token;
|
||||
$params = [
|
||||
@@ -58,6 +58,7 @@ class VkWallService
|
||||
'attachments' => $attachments,
|
||||
'access_token' => $token,
|
||||
'publish_date' => $publish_date,
|
||||
'primary_attachments_mode' => $primary_attachments_mode,
|
||||
'v' => '5.131',
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user