Bonfire.API.MastoCompat.InteractionHandler (Bonfire v1.0.1-social-alpha.28)

View Source

Handles user interactions with statuses (like/unlike/boost/unboost).

This module consolidates the common pattern for interaction mutations:

  1. Check authorization
  2. Perform the interaction via Bonfire context
  3. Fetch the updated activity with proper preloads
  4. Transform to Mastodon Status format
  5. Set the appropriate interaction flag

Previously, this pattern was duplicated across 4 functions (~178 lines). Now it's a single reusable function.

Usage

# Like a status
InteractionHandler.handle_interaction(
  conn,
  id,
  interaction_type: :like,
  context_fn: &Bonfire.Social.Likes.like/2,
  flag: "favourited",
  flag_value: true
)

# Boost a status
InteractionHandler.handle_interaction(
  conn,
  id,
  interaction_type: :boost,
  context_fn: &Bonfire.Social.Boosts.boost/2,
  flag: "reblogged",
  flag_value: true
)

Summary

Functions

Helper to get preload options for activity fetching. Exposed for testing and consistency.

Common handler for all status interactions.

Functions

activity_preload_opts()

Helper to get preload options for activity fetching. Exposed for testing and consistency.

handle_interaction(conn, id, opts)

Common handler for all status interactions.

Options

  • :interaction_type - Type of interaction (for logging): :like, :unlike, :boost, :unboost
  • :context_fn - The Bonfire context function to call (e.g., &Bonfire.Social.Likes.like/2)
  • :flag - The Mastodon status flag to set: "favourited" or "reblogged"
  • :flag_value - Value to set for the flag: true or false

Examples

iex> handle_interaction(conn, "123",
...>   interaction_type: :like,
...>   context_fn: &Bonfire.Social.Likes.like/2,
...>   flag: "favourited",
...>   flag_value: true
...> )
%Plug.Conn{...}