Blog plugin events

v0.0.3

The blog fires mishka_installer events at the points below, so a plugin can react to a change, adjust a query, rewrite content or veto an action without editing the blog. This page lists those events, what a plugin receives and what it returns, and the events of the CMS that the blog's own plugins listen on. A plugin is a module that uses MishkaInstaller.Event.Hook and is started in a supervision tree, as the blog does for its own.

Event names

A blog event is named blog:<resource>:<phase>_<action>. The resource is the last part of the module name, in snake case: MishkaBlog.BlogPost is blog_post, MishkaBlog.PostRating is post_rating. The action is the Ash action that ran, such as publish. The action of a master, such as master_publish, fires its own event.

After events

Fired after the action's transaction commits. The plugin receives the saved record and a read-only meta of %{actor:, site_id:}. The reply does not change the action.

EventFired when
blog:<resource>:after_<action>Any action of the resource runs, such as blog:blog_post:after_publish, blog:comment:after_create or blog:post_rating:after_remove.
blog:<resource>:after_saveAny create or update action of the resource runs, including a master's. It fires after the event of the action itself.

These resources fire them: blog_post, collection, tag, blogger, team, comment, post_rating, post_bookmark, comment_vote, blogger_follower, team_follower, post_blogger, post_collection, post_tag, team_member and team_permission.

Before events

Fired before the action runs. The plugin receives the changeset. It returns the changeset, changed or not, or the changeset with Ash.Changeset.add_error/2 to reject the action.

EventRuns on
blog:blog_post:before_createA site admin creating a post. A master's create does not fire it.
blog:comment:before_create_publicA reader writing a comment.

Query events

Fired before a read runs. The plugin receives the Ash.Query and returns it, usually with an added filter or sort.

EventRead
blog:blog_post:query_publishedThe published posts of a site admin.
blog:blogger:query_public_list, blog:blogger:query_public_by_slugThe public bloggers.
blog:team:query_public_list, blog:team:query_public_by_slugThe public teams.
blog:comment:query_public_listThe public comments.

Render events

Fired when the rendered_content calculation of a post or comment is loaded, which blog-post-detail and blog-post-comments do. The plugin receives %{html: raw, record: record} and returns %{html: changed}.

EventContent
blog:blog_post:render_contentA post's body.
blog:comment:render_contentA comment's text.

Events of the blog's own

These events are not tied to one action. Unless the table says otherwise, a plugin receives the map and answers the same map, changed or not.

EventFired whenThe map
blog:post:publishedA post becomes published, by publish, master_publish, a create or an update that saves it as published, or the job that publishes scheduled posts. It fires once, after the write is committed. Saving a post that was already published fires nothing. The reply changes nothing.%{post:, site_id:, actor:}, with actor nil for the job.
blog:post:can_publishA write by an actor makes a post published or scheduled, before the write is kept. The job that publishes scheduled posts, which has no actor, does not fire it.%{allowed?:, actor:, site_id:, blogger:, changeset:}. allowed? starts true for a master, an editor with no blogger profile, a blogger who can publish directly and an account that holds the publish name. Change it to allow or refuse the publish.
blog:post:count_viewA post's page is shown to a connected reader, before the view counts.%{count?:, site_id:, post_id:, user_agent:, current_user:}. count? starts false for a user agent that names a bot, a crawler, a script or a headless browser. Change it to count or not.
blog:post:viewedThe counted views are written, about once a minute. It fires once for the batch, never for one view.%{views: [%{site_id:, post_id:, day:, views:, unique_readers:}]}
blog:dashboard:actionsThe Blog dashboard draws its header.%{actions: [], current_user:}. Answer %{actions: [...]}, each action with an id, label, icon, priority and either a path: or a js: command. An action missing a field, with an id already taken, or with both or neither of path and js is dropped. Low priority comes first.
blog:analytics:panelsThe Analytics screen opens.%{panels:, current_user:}. Add, remove or reorder the panels and their tabs.
blog:analytics:filtersThe Analytics screen opens.%{filters:, current_user:}. A filter's value reaches every panel in period.filters.
blog:analytics:panelEach panel's data is read, the built-in ones included.%{panel:, period:, current_user:, data:}. Change data to put another source's numbers in its place.

A post published at its scheduled time fires blog:post:published like any other publish. A plugin on that event is where a post is shared to other channels.

Example: reject a comment

This plugin refuses a reader's comment that contains a phrase. Start it in your application's supervision tree.

elixir
defmodule MyApp.Plugins.RejectComment do
  use MishkaInstaller.Event.Hook, event: "blog:comment:before_create_public"

  @impl true
  def call(changeset, _meta) do
    content = Ash.Changeset.get_attribute(changeset, :content) || ""

    if String.contains?(content, "buy now") do
      {:reply, Ash.Changeset.add_error(changeset, field: :content, message: "Rejected")}
    else
      {:reply, changeset}
    end
  end
end

An after event listens by name too. MishkaCmsCore.Runtime.Resources.Notifiers.PluginHook.event_name/2 builds the name, so the plugin cannot misspell it.

elixir
alias MishkaCmsCore.Runtime.Resources.Notifiers.PluginHook

use MishkaInstaller.Event.Hook,
  event: PluginHook.event_name(MishkaBlog.BlogPost, :publish)

Events the blog listens on

The blog starts 31 plugins of its own. These are the events they listen on.

EventWhat the blog does
sidebar:menusAdds the Blog entry and the blog's screens to the admin sidebar.
dashboard:widgetsAdds the five dashboard widgets.
settings:registerRegisters the widget sizes, the notification toggle, the set-up version and active switch, the feed settings, the days daily views are kept and the Post address.
installer:extension:installSets the blog up on a site when an install is asked for, and hands any other request on untouched.
seo:sitemap:urlsAdds posts, collections, bloggers and teams to the sitemap, and the tags that ask to be indexed.
runtime:page:recordsNames the kinds of record a page can show: post, collection, tag, blogger and team, and the two list pages, Blog home and People.
runtime:page:sourceReads the rows a blog page shows once, as the visitor, for the page's head, its frame and its blocks.
runtime:page:frameHands the site layout the blog's bar and the languages of the record the page shows.
runtime:page:seoBuilds the head of a page that shows one of those records, from the rows the page read once.
runtime:translations:used_keysNames the words the blog's bar looks up, so Remove unused keeps them.
runtime:compile:content_componentsTells the site's stylesheet which icons and classes the published posts use, and which components. A component name reaches the stylesheet only when a template of the site draws a post's body, as blog-post-detail does.
runtime:share_card:targetsNames every share card of a site, for a redraw of all of them.
RootHtml.MetaTagsAdds the blog's RSS, Atom and JSON feed links to the head of the site's pages, and a term's feeds on its own page.
cms_core:setting:after_savePuts the site's post page where its Post address setting says.
runtime:page:movedSends a post asked for at an address that is not its own to its own address with a 301.
RuntimeLive.OnMount.AfterCompileKeeps the reader's user agent and session token on a connected page, for counting a view.
RuntimeLive.HandleParams.AfterCompileCounts a view of the post a connected reader's page shows.
agent:operationsNames the blog's writes an agent receipt can be kept for: a post's create, update, publish, unpublish, schedule, archive, unarchive, permanent delete and version restore, and a category's and a tag's create, update, archive, unarchive and permanent delete.
blog:<resource>:after_save for posts, collections, tags, bloggers and teamsDraws the record's share image again when what it shows has changed.
blog:blog_post:after_publish, blog:blog_post:after_master_publishNotifies the holders of admin:dashboard:blog:post, except the publisher.

Plugins that read and frame a page

Three plugins read a blog page once and frame it. A request runs PageSource first, then PageFrame, then the blog's PageSeo, which builds the head from the same rows. PageSource and PageFrame answer only for their own pages and hand every other page back untouched before they read anything. TranslationKeys runs when the CMS works out which translation keys a site still uses. All three run at priority 50.

PluginEventAnswers forReturns
MishkaBlog.Plugins.PageSourceruntime:page:sourceA page whose record is blog:post, blog:category, blog:tag, blog:author, blog:team or blog:home, the blog's front page, whatever its section.{:reply, :halt, given} with source set to a MishkaBlog.PageSource struct, or to the core's missing answer. Any other page: {:reply, given}.
MishkaBlog.Plugins.PageFrameruntime:page:frameA page of the section blog: translations and section_bar. A page of another section that shows one of the five records: translations only, with no bar.{:reply, :halt, frame}. Any other page: {:reply, frame}.
MishkaBlog.Plugins.TranslationKeysruntime:translations:used_keysEvery call.{:reply, data} with the bar's five words added to keys.

What the source holds

The source is read in handle_params/3, as the signed-in visitor or as a guest, through the same public actions the blocks read with. A record that visitor may not open is nil in the struct.

  • A post is read with its text as the site draws it, its readers' rating, its count of approved comments, its categories and tags, its authors with their teams, and its other editions.
  • A category and a tag are read with the number of posts their own page lists and their other editions. The page of their post list that the address names is read with them.
  • A post is named by its slug, or by its number where the page's path captures :post_id, with the date and category the path may also name. A category named by a chain, design/accessibility, is read by its last segment, and the chain must be its own.
  • An address that names a post, category, tag, blogger or team the site does not have is a missing source: the next page that matches the address is tried, and the site's 404 comes last. A record the site has but the visitor may not open is a source with no record, and its page answers with its lock.
  • A blogger is read with their teams. A team is read with its members and how many posts each has.
  • The front page holds the featured posts of its strip, how many posts the visitor may open in the page's language, and the page of the list that the address names.

A list the reader narrows with a search, or on the front page with a tag, reads for itself. A live patch that stays on the same record, such as another page of a list, takes from the read of the address before it what the new address left unchanged. A block takes rows from the source only when it declares the page_source attribute and the source was read for the same site, visitor, language and page. See Page source: read a page's rows once.

What the frame holds

  • The bar shows the blog's name, which leads to the front page, and four places (Posts, Categories, Tags and People). The place the reader is in is marked. A post, a category and a tag are in Posts, and a blogger and a team are in People. A site that moved the front page or the people page, and marked it Blog home or People, has the bar lead there.
  • The bar's words are the site's translations in the response's language. A word nobody translated shows as its key.
  • For a post, a category and a tag, the languages are every published edition the visitor may open, each at its own slug. A blogger and a team are one record in every language, so each language the site publishes in is offered at the same slug. A list page keeps the layout's own list: the same path in each language.

See Page frame: languages and the section bar for the shape of both entries.

What the translation keys name

The five words are Blog, Posts, Categories, Tags and People. No template holds them, because a plugin draws the bar. Without TranslationKeys, Remove unused on the Translations screen would delete them and what a translator wrote for them. Setting the blog up registers the same words for each language of the site. See Translate an extension's words.