Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Actions\Analytics\DispatchAccountAnalytics;
use App\Actions\SocialAccount\ListPinterestBoards;
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Jobs\PostHog\IdentifyConnectedPlatforms;
use App\Jobs\PostHog\SyncAccountUsage;
use App\Jobs\PostHog\TrackSocialAccountConnectionLost;
use App\Models\SocialAccount;
use App\Services\PostHogService;
use App\Services\Repurpose\RepurposeAccountSync;
use Illuminate\Support\Facades\DB;
class SocialAccountObserver
{
public function creating(SocialAccount $socialAccount): void
{
if ($socialAccount->position !== null) {
return;
}
$last = SocialAccount::withoutGlobalScopes()
->where('workspace_id', $socialAccount->workspace_id)
->max('position');
$socialAccount->position = $last === null ? 0 : ((int) $last) + 1;
}
public function created(SocialAccount $socialAccount): void
{
$this->syncUsageAndIdentify($socialAccount);
app(DispatchAccountAnalytics::class)->handle($socialAccount);
}
public function deleted(SocialAccount $socialAccount): void
{
$this->syncUsageAndIdentify($socialAccount);
$this->forgetPinterestBoards($socialAccount);
}
public function deleting(SocialAccount $socialAccount): void
{
app(RepurposeAccountSync::class)->accountRemoved($socialAccount);
}
public function updated(SocialAccount $socialAccount): void
{
app(RepurposeAccountSync::class)->accountChanged($socialAccount);
if ($socialAccount->wasChanged('access_token')) {
$this->forgetPinterestBoards($socialAccount);
}
$wasConnected = $socialAccount->getRawOriginal('status') === Status::Connected->value;
$isConnected = $socialAccount->status === Status::Connected;
$connectionChanged = $socialAccount->wasChanged('status') && $wasConnected !== $isConnected;
if ($connectionChanged) {
$this->identifyConnectedPlatforms($socialAccount);
}
if ($connectionChanged && $isConnected) {
app(DispatchAccountAnalytics::class)->handle($socialAccount);
}
if ($connectionChanged && $wasConnected) {
$status = $socialAccount->status;
DB::afterCommit(function () use ($socialAccount, $status): void {
rescue(function () use ($socialAccount, $status): void {
TrackSocialAccountConnectionLost::dispatchIf(PostHogService::isEnabled(), (string) $socialAccount->id, $status);
});
});
}
}
private function forgetPinterestBoards(SocialAccount $socialAccount): void
{
if ($socialAccount->platform === Platform::Pinterest) {
ListPinterestBoards::forget($socialAccount->id);
}
}
private function syncUsageAndIdentify(SocialAccount $socialAccount): void
{
$this->syncUsage($socialAccount);
$this->identifyConnectedPlatforms($socialAccount);
}
private function identifyConnectedPlatforms(SocialAccount $socialAccount): void
{
if (! PostHogService::isEnabled()) {
return;
}
IdentifyConnectedPlatforms::dispatch((string) $socialAccount->workspace_id);
}
private function syncUsage(SocialAccount $socialAccount): void
{
if (PostHogService::isEnabled()) {
SyncAccountUsage::dispatch(
(string) $socialAccount->workspace->account_id,
(string) $socialAccount->workspace_id,
);
}
}
}