Community程式設計與開發github.com

Taldres/laravel-waitlist

將電子報工具(如 Brevo、Mailchimp 或 Mailcoach)與 taldres/laravel-waitlist 的等候清單同步:按目的新增已確認的聯絡人,在撤回和刪除時移除他們,並透過 webhook 將在提供者端進行的取消訂閱帶回等候清單。

laravel-waitlist 是什麼?

此技能專為使用 `taldres/laravel-waitlist` 套件的 Laravel 應用程式設計,當這些應用程式透過外部電子報工具(如 Brevo、Mailchimp 或 Mailcoach)發送行銷活動時,必須確保工具的聯絡人清單與等候清單中的同意狀態保持同步。其主要目標是讓外部工具僅保留當前有效同意目的(purpose)的聯絡人,並確保任何一方的撤回動作都能即時反映到另一方,達成雙向同步。 技能支援在 Laravel 應用程式中實作事件監聽器,攔截等候清單的各個事件(如 EntryConfirmed、ConsentWithdrawn、EntryUnsubscribed、EntryForgotten 等),並將其對應到外部工具的 API 呼叫(如新增、移除或刪除聯絡人)。同時,它也涵蓋處理從外部電子報工具回傳的取消訂閱 webhook,將取消訂閱狀態同步回等候清單。 此技能提供完整的 PHP 監聽器範例程式碼,並強調避免常見的反模式,例如在雙重 opt-in 前同步、處理 EntryUnsubscribed 時忽略檢查其他清單,或在 EntryForgotten 時直接刪除聯絡人(可能影響其他清單中的地址)。整體而言,這是一個專為 Laravel 開發者設計、協助實現複雜的雙向通訊錄同步邏輯的實用指南。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Taldres/laravel-waitlist/tree/HEAD/resources/boost/skills/laravel-waitlist-provider-sync

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Laravel Waitlist:與電子郵件提供者同步

當使用 taldres/laravel-waitlist 的應用程式透過外部電子報工具發送行銷活動,且其聯絡人清單必須遵循等候清單的同意狀態時,使用此技能。

主要目標

  • 提供者僅持有其目的仍有效力的聯絡人,且任一方發生的撤回動作都能傳遞到另一方。

工作流程

1. 判斷是否需要同步

Laravel 應用程式自行發送的郵件(透過任何郵件程式)不需要同步:只需將它們發送給 Waitlist::recipients($purpose)。僅當行銷活動是從外部工具發送時,才需要同步。

2. 將事件對應到提供者呼叫

事件提供者動作
EntryConfirmed新增聯絡人,根據 $event->entry->purposes 中的每個目的,加入到對應的提供者清單
ConsentGranted一旦 $event->subscription->isConfirmed() 成立,將聯絡人加入到該目的的清單
ConsentWithdrawn從 $event->consent->purpose 的清單中移除,除非專案中仍有其他清單持有該目的
EntryUnsubscribed從該生命週期所持有的每個目的的清單中移除,除非其他清單仍持有該目的
EntryForgotten僅在該電子郵件地址完全沒有任何條目時,才刪除該聯絡人;否則,僅移除剩餘條目未持有的目的
  • 離開一個清單會終止其與清單上每個目的的關聯:在該清單上同意的電子報也會終止,且不會為此觸發 ConsentWithdrawn 事件。
  • EntryForgotten 會為每個被刪除的條目觸發,包括 forgetAll() 和對單一清單的保留期限清理,而該電子郵件地址可能仍在其他清單上。
  • 不要在 EntrySubscribed 事件上進行同步:採用雙重 opt-in 時,該地址尚未確認。

3. 一個佇列化、加密的監聽器類別

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Taldres\Waitlist\Events\ConsentGranted;
use Taldres\Waitlist\Events\ConsentWithdrawn;
use Taldres\Waitlist\Events\EntryConfirmed;
use Taldres\Waitlist\Events\EntryForgotten;
use Taldres\Waitlist\Events\EntryUnsubscribed;
use Taldres\Waitlist\Models\WaitlistEntry;

class SyncWaitlistToProvider implements ShouldQueue, ShouldBeEncrypted
{
    public function onConfirmed(EntryConfirmed $event): void
    {
        // 將 $event->entry->email 加入到 $event->entry->purposes 中每個目的對應的提供者清單
    }

    public function onGranted(ConsentGranted $event): void
    {
        if ($event->subscription->isConfirmed()) {
            // 將 $event->entry->email 加入到 $event->consent->purpose 對應的清單
        }
    }

    public function onWithdrawn(ConsentWithdrawn $event): void
    {
        $this->removeUnlessHeld($event->entry->project, $event->entry->email, $event->consent->purpose);
    }

    public function onUnsubscribed(EntryUnsubscribed $event): void
    {
        $event->subscription->consents()->pluck('purpose')->unique()->each(
            fn (string $purpose) => $this->removeUnlessHeld($event->entry->project, $event->entry->email, $purpose),
        );
    }

    public function onForgotten(EntryForgotten $event): void
    {
        if ($event->email === null) {   // 無法解密
            return;
        }

        if (! WaitlistEntry::query()->forEmail($event->email)->exists()) {
            // 刪除聯絡人 $event->email
            return;
        }

        foreach (['waitlist', 'newsletter'] as $purpose) {   // 你要同步的目的
            $this->removeUnlessHeld($event->project, $event->email, $purpose);
        }
    }

    private function removeUnlessHeld(string $project, string $email, string $purpose): void
    {
        // 事件在提交後觸發:只有當專案中其他清單仍持有該目的時,該目的才仍然有效。
        $held = WaitlistEntry::query()->inProject($project)->forEmail($email)->whereConsentedTo($purpose)->exists();

        if (! $held) {
            // 從 $purpose 的提供者清單中移除 $email
        }
    }
}

請手動註冊這些方法。它們故意不以 handle 開頭,以避免 Laravel 的事件發現機制再次註冊它們:

// AppServiceProvider::boot()
Event::listen(EntryConfirmed::class, [SyncWaitlistToProvider::class, 'onConfirmed']);
Event::listen(ConsentGranted::class, [SyncWaitlistToProvider::class, 'onGranted']);
Event::listen(ConsentWithdrawn::class, [SyncWaitlistToProvider::class, 'onWithdrawn']);
Event::listen(EntryUnsubscribed::class, [SyncWaitlistToProvider::class, 'onUnsubscribed']);
Event::listen(EntryForgotten::class, [SyncWaitlistToProvider::class, 'onForgotten']);

使用 Laravel 的 Http 用戶端來呼叫提供者 API;將憑證和每個目的對應的清單 ID 存放在 config/services.php 中。

4. 將提供者的取消訂閱帶回

從工具發送的行銷活動會包含其自身的取消訂閱連結。接收提供者的 webhook 並從該地址所有清單中撤回目的;重複執行不會有任何影響:

// routes/api.php,或 routes/web.php(將該路由排除在 CSRF 保護之外)
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Taldres\Waitlist\Facades\Waitlist;
use Taldres\Waitlist\Models\WaitlistEntry;

Route::post('/webhooks/newsletter', function (Request $request) {
    // 首先按照提供者的文件驗證 webhook
    $email = (string) $request->input('email'); // 欄位名稱取決於提供者

    Waitlist::findByEmail($email)->each(
        fn (WaitlistEntry $entry) => Waitlist::for($entry->list)->withdraw($email, 'newsletter'),
    );

    return response()->noContent();
});

多個專案:將提供者清單對應到其專案,並使用 Waitlist::project($project)->findByEmail() 和 Waitlist::project($project)->for($entry->list)->withdraw()。

規則、參考資料與範本

  • 每個專案和目的有一個提供者清單;切勿為可選目的使用每條等候清單一個清單的模式,因為可選目的是每個專案一個同意。
  • 共用主要目的的多個清單是獨立的等候清單:離開一個清單絕不會在另一個清單仍持有該目的時移除聯絡人。
  • php artisan waitlist:privacy 會將監聽器列為接收者;提供者是處理者,需要簽署協議。
  • 包含完整 Brevo 監聽器的完整指南:https://github.com/Taldres/laravel-waitlist/blob/main/docs/email-provider-sync.md

範例

  • "將已確認的電子報註冊同步到 Brevo":步驟 2 和 3,使用 config('services.brevo.lists.newsletter') 作為清單 ID。
  • "在 Mailchimp 中取消訂閱的人仍然收到我們的啟動郵件":步驟 4。

反模式

  • 在 EntrySubscribed 上同步,這發生在雙重 opt-in 確認之前。
  • 在 EntryUnsubscribed 時移除聯絡人,而未檢查專案中的其他清單,或僅移除清單的主要目的。
  • 在每次 EntryForgotten 時都刪除聯絡人:保留期限清理和 forgetAll() 會刪除一個清單的條目,而該地址可能仍在其他清單上。
  • 將監聽器方法命名為 handle* 並同時使用 Event::listen() 註冊它們:每次同步會執行兩次。
  • 僅從一個條目撤回的 Webhook,或未驗證請求就信任它。
  • 將已派送的 EntryForgotten 視為提供者已刪除聯絡人的證明;請監控失敗的任務。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

Taldres/laravel-waitlist

Use this skill when reviewing Laravel package compatibility across composer constraints, PHP versions, Laravel versions, Testbench versions, dependency stability lanes, Windows CI, or matrix-sensitive code and workflow changes.

Taldres/laravel-waitlist

Use this skill when creating or updating the bundled Laravel Boost skill under resources/boost/skills from the package implementation and package documentation. Trigger after public APIs, commands, config, routes, views, publish tags, README content, or examples change.

Taldres/laravel-waitlist

Use this skill when preparing Laravel package releases: CHANGELOG.md updates, generated release notes, GitHub release workflows, version checks, tags, release validation, or release automation changes. Never publish autonomously.

Taldres/laravel-waitlist

Use this skill when adding Laravel package capabilities or wiring them through the service provider: commands, migrations, routes, config merges, views, translations, assets, middleware, publish tags, workbench files, or console-only behavior.

Taldres/laravel-waitlist

Use this skill when writing, editing, fixing, or reviewing package tests with Pest 4/5 and Orchestra Testbench, including TDD, feature tests, unit tests, type coverage, arch tests, workbench behavior, commands, routes, config, migrations, and publishable resources.

Taldres/laravel-waitlist

Integrate taldres/laravel-waitlist in a Laravel application: define purposes, lists and fields, build the signup form, send the confirmation mail from events, add a preference page, and keep consent, retention and encryption intact.

Taldres/laravel-waitlist

Build the pages of a taldres/laravel-waitlist integration: the signup form with the registered wording, and the confirm, unsubscribe and preference pages, either in Laravel controllers (Blade, Livewire, Inertia) or in an SPA or static site over the package's JSON API, including CORS and bot protection.

Taldres/laravel-waitlist

Turn a taldres/laravel-waitlist waitlist into a launch: invite people in batches in signup order, let invited addresses register, announce the launch to everyone who agreed, and erase the list once its purpose is fulfilled.

Taldres/laravel-waitlist

Send the mails of a taldres/laravel-waitlist integration: the double opt-in confirmation, the preference page link, a welcome mail, launch mails and newsletters to the people who agreed, with the right unsubscribe links, one-click headers and a sender per project.

Taldres/laravel-waitlist

Operate a taldres/laravel-waitlist installation: answer access and erasure requests, withdraw a purpose on request, apply retention, erase a list once its purpose is fulfilled, rotate APP_KEY with waitlist:rekey, export a list, and check the setup before going live.

Taldres/laravel-waitlist

Run the waitlists of several products in one Laravel app with taldres/laravel-waitlist projects: define projects, scope facade calls with project(), resolve the project of HTTP signups, send mail per project, handle requests across all projects, or keep projects in a database for a central waitlist API.

Taldres/laravel-waitlist

Build waitlist statistics with taldres/laravel-waitlist: signups and confirmations per day for charts, totals and confirmation rates for a period, the confirmed count on any past day, and how many people are on a list right now, for dashboards, admin pages and API endpoints.

Taldres/laravel-waitlist

Write feature tests for an application's taldres/laravel-waitlist integration: get the plain tokens from events, assert the confirmation and other mails, test the signup, confirm and unsubscribe flows in PHP and over the package's HTTP routes, and bypass rate limits and bot checks in tests.

相關技能