Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Actions/Automation/Node/RunGenerateNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\DataTransferObjects\Automation\NodeRunResult;
use App\Enums\Ai\ContentStyle;
use App\Enums\Ai\GeneratorFormat;
use App\Enums\Post\CreatedVia;
use App\Enums\PostPlatform\ContentType;
use App\Models\AutomationRun;
use App\Models\SocialAccount;
Expand Down Expand Up @@ -145,6 +146,7 @@ public function __invoke(AutomationRun $run, array $config): NodeRunResult
'content' => $generated->content,
'media' => $generated->media,
'platforms' => $platforms,
'created_via' => CreatedVia::Automation,
]);

$run->update(['generated_post_id' => $post->id]);
Expand Down
9 changes: 6 additions & 3 deletions app/Actions/Post/CreatePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Actions\Post;

use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
use App\Events\PostCreated;
use App\Models\Post;
use App\Models\User;
use App\Models\Workspace;
Expand All @@ -26,11 +26,15 @@ class CreatePost
* `label_ids[]` are attached after creation so the same set of UUIDs
* works for REST, MCP, and web callers.
*
* `created_via` records which entry point created the post (web, mcp,
* api, or automation). Analytical only — null when omitted.
*
* @param array{
* content?: ?string,
* media?: array<int, mixed>,
* date?: ?string,
* scheduled_at?: ?string,
* created_via?: ?CreatedVia,
* platforms?: array<int, array{social_account_id: string, content_type?: string, meta?: array<string, mixed>}>,
* label_ids?: array<int, string>
* } $data
Expand All @@ -45,6 +49,7 @@ public static function execute(Workspace $workspace, User $user, array $data): P
'content' => data_get($data, 'content', ''),
'media' => data_get($data, 'media', []),
'status' => PostStatus::Draft,
'created_via' => data_get($data, 'created_via'),
'scheduled_at' => $scheduledAt,
]);

Expand Down Expand Up @@ -85,8 +90,6 @@ public static function execute(Workspace $workspace, User $user, array $data): P
return $post;
});

PostCreated::dispatch($post);

return $post;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Actions/Post/DuplicatePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Actions\Post;

use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
use App\Enums\PostPlatform\Status as PostPlatformStatus;
use App\Models\Post;
Expand All @@ -25,6 +26,7 @@ public static function execute(Post $original, User $user): Post
'content' => $original->content,
'media' => $original->media,
'status' => PostStatus::Draft,
'created_via' => CreatedVia::Web,
'scheduled_at' => null,
'published_at' => null,
]);
Expand Down
13 changes: 13 additions & 0 deletions app/Enums/Post/CreatedVia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Enums\Post;

enum CreatedVia: string
{
case Web = 'web';
case Mcp = 'mcp';
case Api = 'api';
case Automation = 'automation';
}
10 changes: 10 additions & 0 deletions app/Enums/PostHog/PostEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Enums\PostHog;

enum PostEvent: string
{
case Created = 'post.created';
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/Api/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Actions\Post\UpdatePost;
use App\Enums\Media\Type as MediaType;
use App\Enums\Post\Action as PostAction;
use App\Enums\Post\CreatedVia;
use App\Http\Requests\Api\Post\AttachMediaFromUrlRequest;
use App\Http\Requests\Api\Post\StoreMediaRequest;
use App\Http\Requests\Api\Post\StorePostRequest;
Expand Down Expand Up @@ -61,6 +62,8 @@ public function store(StorePostRequest $request): JsonResponse
);
}

$data['created_via'] = CreatedVia::Api;

$post = CreatePost::execute($workspace, $workspace->owner, $data);

$post->load(['postPlatforms.socialAccount']);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/App/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Ai\Templates\AiContentTemplate;
use App\Ai\Templates\AiTemplateRegistry;
use App\Enums\Post\Action as PostAction;
use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
use App\Enums\SocialAccount\Platform;
use App\Http\Requests\App\Post\StorePostRequest;
Expand Down Expand Up @@ -189,6 +190,7 @@ public function store(StorePostRequest $request): RedirectResponse|\Symfony\Comp
$post = CreatePost::execute($workspace, $request->user(), [
'date' => $request->input('date'),
'media' => $request->input('media', []),
'created_via' => CreatedVia::Web,
]);

return Inertia::location(route('app.posts.edit', $post));
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/App/PostTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Actions\Post\CreatePost;
use App\Enums\Media\Type as MediaType;
use App\Enums\Post\CreatedVia;
use App\Http\Requests\App\PostTemplate\ApplyPostTemplateRequest;
use App\Http\Requests\App\PostTemplate\IndexPostTemplateRequest;
use App\Http\Resources\App\PostTemplateResource;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function apply(ApplyPostTemplateRequest $request, string $slug, TemplateI
'content' => $content,
'media' => $media,
'date' => $request->input('date'),
'created_via' => CreatedVia::Web,
]);

return response()->json([
Expand Down
2 changes: 2 additions & 0 deletions app/Jobs/Ai/StreamPostCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Enums\Ai\GeneratorFormat;
use App\Enums\Notification\Channel as NotificationChannel;
use App\Enums\Notification\Type as NotificationType;
use App\Enums\Post\CreatedVia;
use App\Enums\PostPlatform\ContentType;
use App\Events\Ai\PostCreationReady;
use App\Jobs\SendNotification;
Expand Down Expand Up @@ -195,6 +196,7 @@ private function createPostFromGenerated(Workspace $workspace, GeneratedPost $ge
'content' => $generated->content,
'media' => $generated->media,
'date' => $this->date,
'created_via' => CreatedVia::Web,
]);

if ($generated->contentType && $socialAccount) {
Expand Down
67 changes: 67 additions & 0 deletions app/Jobs/PostHog/TrackPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace App\Jobs\PostHog;

use App\Enums\PostHog\PostEvent;
use App\Models\Post;
use App\Services\PostHogService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class TrackPost implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $tries = 3;

public int $timeout = 30;

public function __construct(public string $postId)
{
$this->onQueue('posthog');
}

public function handle(PostHogService $postHog): void
{
if (! PostHogService::isEnabled()) {
return;
}

$post = Post::query()
->with(['workspace.account.plan'])
->find($this->postId);

if (! $post) {
return;
}

$account = $post->workspace?->account;

if (! $account) {
return;
}

$distinctId = (string) ($post->user_id ?? $account->owner_id);

if ($distinctId === '') {
return;
}

$postHog->capture(
$distinctId,
PostEvent::Created->value,
[
'post_id' => (string) $post->id,
'workspace_id' => (string) $post->workspace_id,
'created_via' => $post->created_via?->value,
'status' => $post->status->value,
],
$account,
);
}
}
21 changes: 21 additions & 0 deletions app/Listeners/PostHog/TrackPostCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Listeners\PostHog;

use App\Events\PostCreated;
use App\Jobs\PostHog\TrackPost;
use App\Services\PostHogService;

class TrackPostCreated
{
public function handle(PostCreated $event): void
{
if (! PostHogService::isEnabled()) {
return;
}

TrackPost::dispatch((string) $event->post->id);
}
}
3 changes: 3 additions & 0 deletions app/Mcp/Tools/Post/CreatePostTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Mcp\Tools\Post;

use App\Actions\Post\CreatePost;
use App\Enums\Post\CreatedVia;
use App\Enums\PostPlatform\ContentType;
use App\Http\Resources\Api\PostResource;
use App\Rules\ContentTypeMatchesPlatform;
Expand Down Expand Up @@ -41,6 +42,8 @@ public function handle(Request $request): ResponseFactory
...PostPlatformMetaRules::rules(),
]);

$validated['created_via'] = CreatedVia::Mcp;

$post = CreatePost::execute($workspace, $request->user(), $validated);

$post->load(['postPlatforms.socialAccount', 'labels']);
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\DataTransferObjects\MediaItem;
use App\Enums\Media\Type;
use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
use App\Enums\SocialAccount\Platform;
use App\Observers\PostObserver;
Expand Down Expand Up @@ -34,6 +35,7 @@ class Post extends Model
'content',
'media',
'status',
'created_via',
'scheduled_at',
'published_at',
];
Expand All @@ -42,6 +44,7 @@ protected function casts(): array
{
return [
'status' => PostStatus::class,
'created_via' => CreatedVia::class,
'media' => 'array',
'scheduled_at' => 'datetime',
'published_at' => 'datetime',
Expand Down
7 changes: 7 additions & 0 deletions app/Observers/PostObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@

use App\Enums\Automation\Trigger\Type as TriggerType;
use App\Enums\Post\Status as PostStatus;
use App\Events\PostCreated;
use App\Jobs\Automation\DispatchPostTriggerAutomationsJob;
use App\Models\Post;
use Illuminate\Support\Facades\DB;

class PostObserver
{
public function created(Post $post): void
{
DB::afterCommit(fn () => PostCreated::dispatch($post));
}

public function saved(Post $post): void
{
if (! $post->wasChanged('status')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('posts', function (Blueprint $table) {
$table->string('created_via')->nullable()->after('status');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('created_via');
});
}
};
Loading