-module(blog_publish_digest). -export([build/1]). %% A motivating multi-step business flow for the fed-sx-triggers e2e: %% when an Article is published, decide a batch policy by category, %% (for newsletters) wait until morning, fetch the author's followers, %% build a digest email for each, and emit a DigestSent activity — the %% flow's own output, which a driver appends, closing the loop so it can %% trigger downstream flows. %% %% Demonstrates: a branch on an activity field (:category), a timer %% suspension (flow:wait/1, resumed by advancing the clock), an injected %% effect (fetch_followers), and a follow-up activity emit. %% %% Effect-as-data: a flow runs inside flow_store's drive, where a %% blocking call (e.g. into nx_kernel) would deadlock this scheduler, so %% the flow does NOT perform IO itself. It DESCRIBES the effects in its %% result — {digest_sent, Emails, DigestActivityObject} — and the driver %% (the fan-out caller) dispatches the emails and appends the DigestSent %% activity. fetch_followers is injected (the one external read) as a %% pure function so the e2e can supply a deterministic list. %% %% Input env (from flow_dispatch): [{activity, A}, {actor, Actor}, ...]. %% Result: {digest_sent, [Email], DigestObject} | skipped. build(Effects) -> FetchFollowers = field(fetch_followers, Effects), flow_spec:branch( fun (Env) -> is_article(Env) end, flow_spec:branch( fun (Env) -> category_is(Env, newsletter) end, %% newsletter: hold until morning, then send + emit flow_spec:sequence([flow:wait(morning), send_emit(FetchFollowers)]), flow_spec:branch( fun (Env) -> category_is(Env, urgent) end, %% urgent: send + emit now (no wait) send_emit(FetchFollowers), %% any other category: skip flow_spec:flow_const(skipped))), %% not an Article: skip flow_spec:flow_const(skipped)). %% send_emit(FetchFollowers) — the terminal step: build one digest email %% per follower and the DigestSent emit object. Pure given the injected %% follower list, so it is replay-safe (and it sits after the only %% suspend point, so it runs exactly once). send_emit(FetchFollowers) -> flow_spec:flow_node( fun (Env) -> Activity = env_activity(Env), Actor = env_actor(Env), ArtId = activity_id(Activity), Followers = FetchFollowers(Actor), Emails = [ [{to, F}, {article, ArtId}] || F <- Followers ], Digest = [{type, digest_sent}, {for, ArtId}, {follower_count, length(Followers)}], {digest_sent, Emails, Digest} end). %% ── predicates / accessors ────────────────────────────────────── is_article(Env) -> object_type(object_of(env_activity(Env))) =:= article. category_is(Env, Cat) -> object_category(object_of(env_activity(Env))) =:= Cat. env_activity(Env) -> field(activity, Env). env_actor(Env) -> field(actor, Env). object_of(Activity) -> field(object, Activity). object_type(Obj) -> field(type, Obj). object_category(Obj) -> field(category, Obj). activity_id(Activity) -> field(id, Activity). field(Key, Proplist) -> case envelope:get_field(Key, Proplist) of {ok, V} -> V; _ -> undefined end.