View Source Bonfire.Social.Activities (Bonfire v0.9.11-social-beta.6)

Helpers to create or query (though that's usually done through Bonfire.Social.FeedActivities) activities, preload relevant associations, and otherwise massage activity-related data.

This is the context for Bonfire.Data.Social.Activity, which has these fields:

  • id: primary key (which when the verb is Create usually matches the related Object)
  • subject: references the who (eg. a user)
  • verb: what kind of action (eg. references Like or Follow in Bonfire.Data.AccessControl.Verb)
  • object: references the what (eg. a specific post)

Summary

Functions

Applies preloads to a query or or post-loads to object(s) with the specified options. See activity_preloads/3 for what preload options you can specify.

Applies preloads to a query or or post-loads to object(s) with the specified preloads and options.

Processes and structures activity data within a media.

Processes and structures activity data within an object.

Outputs the names of all object verbs for localization, for the purpose of adding to the localisation strings, as long as the output is piped through to localise_strings/1 at compile time.

Retrieves additional verb names with various formats for localization.

Filters a query to include only permitted objects.

Casts a changeset with the provided verb, creator and options.

Counts the total number of activities.

Create an Activity. NOTE: you will usually want to use cast/3 instead or maybe Objects.publish/5

Deletes activities by ID or struct, or using specific filters.

Deletes an activity by object ID.

Deletes an activity by subject, verb, and object.

Retrieves the cursor value for pagination based on field or data structure.

Gets an activity by its ID.

Loads an object based on its ID or pointer.

Optionally joins the creator.

Optimizes the query to optionally include the subject data.

Retrieves or constructs the object from an activity.

Preloads the activity for an object and verb.

Preloads the creation activity for an object.

Provides cursor fields for ordering based on sort criteria.

Provides pagination options for ordering.

Constructs a query based on filters and optional user context.

Preloads activity for objects in a query using the specified verb and object ID field.

Preloads creation activity for objects in a query.

Orders query results based on a specified field and sort order.

Retrieves an activity based on a query and options.

Constructs a query for reading activities based on input.

Returns a localized and formatted display name for a verb.

Retrieves or constructs an ID for a verb based on its name or identifier.

Optionally modifies the verb based on activity context.

Returns the name of a verb based on its slug or identifier.

Functions

activity_preloads(query_or_object_or_objects, opts)

Applies preloads to a query or or post-loads to object(s) with the specified options. See activity_preloads/3 for what preload options you can specify.

Examples

> activity_preloads(query, preload: [])
# Query with applied activity preloads

activity_preloads(query_or_object_or_objects, preloads, opts)

Applies preloads to a query or or post-loads to object(s) with the specified preloads and options.

Examples

> activity_preloads(query, [], [])
# Original query, with no extra joins/preloads

> activity_preloads(object, [], [])
# Original object, with no extra assocs preloads

> activity_preloads(object_or_query, [:with_creator], [])
# Object or query with extra assocs preloads

> activity_preloads(object_or_query, [:feed, :with_reply_to, :with_media, :with_object_more, :maybe_with_labelled])
# Object or query several extra assoc preloads

Other possible preloads:

:default
:all
:feed
:feed_postload
:feed_metadata
:feed_by_subject
:feed_by_creator
:notifications
:object_with_creator
:posts
:posts_with_thread
:posts_with_reply_to
:with_creator
:with_subject
:with_verb
:with_object
:with_post_content
:with_object_more
:with_replied
:with_thread_name
:with_parent
:with_reply_to
:with_seen
:with_media
:per_media
:tags
:maybe_with_labelled

activity_under_media(top_object)

Processes and structures activity data within a media.

Examples

iex> %{id: 1, activity: %{id: 2}} = activity_under_media(%{activity: %Bonfire.Data.Social.Activity{id: 2, media: %{id: 1}}})

activity_under_media(activity, media)

activity_under_object(top_object)

Processes and structures activity data within an object.

Examples

iex> %{id: 1, activity: %{id: 2}} = activity_under_object(%{activity: %Bonfire.Data.Social.Activity{id: 2, object: %{id: 1}}})

activity_under_object(activity, object)

all_verb_names()

Outputs the names of all object verbs for localization, for the purpose of adding to the localisation strings, as long as the output is piped through to localise_strings/1 at compile time.

all_verb_names_extra()

Retrieves additional verb names with various formats for localization.

as_permitted_for(q, opts \\ [], verbs \\ [:see, :read])

Filters a query to include only permitted objects.

Examples

> as_permitted_for(query, [])
# Filtered query

build_assoc(thing, verb, subject)

build_assoc(thing, verb, subject, object)

by_subject_verb_object_q(subject, verb, object)

cast(changeset, verb, creator, opts)

Casts a changeset with the provided verb, creator and options.

Examples

> cast(changeset, :like, %User{}, feed_ids: [])
# Changeset with associations set

count_total()

Counts the total number of activities.

create(subject, verb, object, activity_id \\ nil)

Create an Activity. NOTE: you will usually want to use cast/3 instead or maybe Objects.publish/5

Examples

> create(%User{id: "1"}, :like, %Post{id: "1"})
{:ok, %Activity{}}

delete(id)

Deletes activities by ID or struct, or using specific filters.

Examples

> delete(activity)
# Number of deleted activities

> delete("1")
# Number of deleted activities

> delete([id: "1"])
# Number of deleted activities

delete_by_object(id)

Deletes an activity by object ID.

Examples

> delete_by_object("1")
# Number of deleted objects

delete_by_subject_verb_object(subject, verb, object)

Deletes an activity by subject, verb, and object.

Examples

> delete_by_subject_verb_object(%User{id: "1"}, :like, %Post{id: "1"})
# Number of deleted activities

fetch_cursor_value_fun(replied, list)

Retrieves the cursor value for pagination based on field or data structure.

Examples

> fetch_cursor_value_fun(%{nested_replies_count: 5}, :num_replies)
# returns the cursor value based on the number of replies

get(id, opts)

Gets an activity by its ID.

Examples

> get("activity_id", [])

load_object(id_or_pointer, opts \\ [])

Loads an object based on its ID or pointer.

Examples

> load_object("object_id")

maybe_filter(query, filters, opts \\ [])

maybe_join_creator(query, skip_loading_user_ids)

Optionally joins the creator.

Performs a query optimization: only includes the creator if different from the subject or current user.

If skip_loading_user_ids is empty, the creator is always included. Otherwise, it is included only if it is different from the users in skip_loading_user_ids.

Examples

> maybe_join_creator(query, [])
# returns query with creator preloaded if different from the subject

> maybe_join_creator(query, [1, 2, 3])
# returns query with creator included only if creator.id not in [1, 2, 3]

maybe_join_subject(query, skip_loading_user_ids)

Optimizes the query to optionally include the subject data.

If skip_loading_user_ids is empty, the subject is always included. Otherwise, it is included only if it is different from the users in skip_loading_user_ids.

Examples

> maybe_join_subject(query, [])
# returns query with subject preloaded

> maybe_join_subject(query, [1, 2, 3])
# returns query with subject included only if subject.id not in [1, 2, 3]

maybe_remove_for_deleters_feeds(id)

maybe_with_labelled()

object_from_activity(activity)

Retrieves or constructs the object from an activity.

Examples

iex> object_from_activity(%{object: %{id: 1}})
%{id: 1}

object_preload_activity(object, verb \\ :create)

Preloads the activity for an object and verb.

Examples

> object_preload_activity(%Post{}, :like)
# Object with preloaded activity

object_preload_create_activity(object)

Preloads the creation activity for an object.

Examples

> object_preload_create_activity(%Post{})
# Object with preloaded creation activity

object_under_activity(activity, object)

order_cursor_fields(arg1, sort_order)

Provides cursor fields for ordering based on sort criteria.

Examples

> order_cursor_fields(:num_likes, :asc)
# returns cursor fields for ordering by number of likes in ascending order

order_pagination_opts(sort_by, sort_order)

Provides pagination options for ordering.

Examples

> order_pagination_opts(:num_likes, :desc)
# returns pagination options for ordering by number of likes in descending order

put_assoc(changeset, verb, subject)

put_assoc(changeset, verb, subject, object)

query(filters \\ [], opts_or_current_user \\ [])

Constructs a query based on filters and optional user context.

Examples

> query(filters)

iex> query([my: :feed], [current_user: nil])
** (Bonfire.Fail.Auth) You need to log in first. 

query_module()

Callback implementation for Bonfire.Common.ContextModule.query_module/0.

query_object_preload_activity(q, verb \\ :create, object_id_field \\ :id, opts \\ [])

Preloads activity for objects in a query using the specified verb and object ID field.

Examples

> query_object_preload_activity(query, :like, :post_id, [])
# Query with preloaded activities

query_object_preload_create_activity(q, opts \\ [])

Preloads creation activity for objects in a query.

Examples

> query_object_preload_create_activity(query, [])
# Query with preloaded creation activities

query_order(query, sort_by, sort_order, with_pins? \\ false)

Orders query results based on a specified field and sort order.

Examples

> query_order(query, :num_replies, :asc)
# returns the query ordered by number of replies in ascending order

read(query, opts \\ [])

Retrieves an activity based on a query and options.

Examples

> read(query)
# returns an activity based on the provided query

> read(object_id)
# returns an activity for the provided object ID (usually a create activity)

read_query(query, opts \\ [])

Constructs a query for reading activities based on input.

Examples

> read_query(query, opts)

> read_query(object_id, opts)

reply_to_as_permitted_for(q, opts \\ [], verbs \\ [:see, :read])

schema_module()

Callback implementation for Bonfire.Common.ContextModule.schema_module/0.

verb_congugate(verb)

verb_display(verb)

Returns a localized and formatted display name for a verb.

Examples

iex> verb_display("create")

verb_id(verb)

Retrieves or constructs an ID for a verb based on its name or identifier.

Examples

iex> verb_id(:create)

verb_maybe_modify(verb, activity \\ nil)

Optionally modifies the verb based on activity context.

Examples

iex> verb_maybe_modify("Create", %{object: %{post_content: %{id: 1}}})
"Write"

verb_name(slug)

Returns the name of a verb based on its slug or identifier.

Examples

iex> verb_name(:create)
"Create"