Resource notifications

rsc_delete

Resource will be deleted. This notification is part of the delete transaction, it’s purpose is to clean up associated data.

Type:
notify

Return:

#rsc_delete{} properties:
  • id: unknown
  • is_a: unknown

rsc_get

Resource is read, opportunity to add computed fields Used in a foldr with the read properties as accumulator.

Type:
first

Return:

#rsc_get{} properties:
  • id: unknown

rsc_insert

Foldr for an resource insert, modify the insertion properties.

Type:
foldr

Return:

#rsc_insert{} properties: none

rsc_merge

Map to signal merging two resources. Move any information from the looser to the winner. The looser will be deleted.

Type:
first

Return:

#rsc_merge{} properties:
  • winner_id: integer
  • looser_id: integer

rsc_pivot_done

Signal that a resource pivot has been done.

Type:
notify

Return:

#rsc_pivot_done{} properties:
  • id: m_rsc:resource()
  • is_a: list

rsc_property

Possibility to overrule a property of a resource (currently only the title)

Type:
first

Return:

#rsc_property{} properties:
  • id: m_rsc:resource()
  • property: atom
  • value: term

rsc_query_item

Send a notification that the resource ‘id’ is added to the query query_id.

Type:
notify

Return:

#rsc_query_item{} properties:
  • query_id: unknown
  • match_id: unknown

rsc_update

An updated resource is about to be persisted. Observe this notification to change the resource properties before they are persisted. The props are the resource’s props _before_ the update. The folded value is {IsChanged, UpdateProps} for the update itself. Set IsChanged to true if you modify the UpdateProps.

Type:
foldr
Return:
{true, ChangedProps} or {false, Props}
#rsc_update{} properties:
  • action: insert|update
  • id: m_rsc:resource()
  • props: list

An updated resource is about to be persisted. Observe this notification to change the resource properties before they are persisted.

Arguments

#rsc_update
action
Either insert or update.
id
Id of the resource.
props
List of resource properties.
{IsChanged, UpdateProps}
And/remove resource properties before the update is persisted. Set IsChanged to true if you want to modify UpdateProps.
Context
Site context

Example

Add a property before the resource is persisted:

observe_rsc_update(#rsc_update{action = insert, id = Id}, {Modified, Props}, Context) ->
    %% Set an extra property
    {true, Props ++ [{extra_property, <<"special value!">>}].

rsc_update_done

An updated resource has just been persisted. Observe this notification to execute follow-up actions for a resource update.

Type:
notify
Return:
return value is ignored
#rsc_update_done{} properties:
  • action: insert|update|delete
  • id: m_rsc:resource()
  • pre_is_a: list
  • post_is_a: list
  • pre_props: list
  • post_props: list
pre_is_a
List of resource categories before the update.
post_is_a
List of resource categories after the update.
pre_props
List of properties before the update.
post_props
List of properties after the update.

Example

Add some default edges when a resource is created:

observe_rsc_update_done(#rsc_update_done{action = insert, id = Id, post_is_a = PostIsA, post_props = Props}, Context) ->
    case lists:member(activity, PostIsA) of
        false ->
            ok;
        true ->
            m_my_rsc:create_default_edges(Id, Context),
            ok
    end;
observe_rsc_update_done(#rsc_update_done{}, _Context) ->
    %% Fall through
    ok.

rsc_upload

Upload and replace the the resource with the given data. The data is in the given format. Return {ok, Id} or {error, Reason}, return {error, badarg} when the data is corrupt.

Type:
first

Return:

#rsc_upload{} properties:
  • id: unknown
  • format: json|bert
  • data: unknown

edge_delete

An edge has been deleted

Type:
notify
Return:
return value is ignored
#edge_delete{} properties:
  • subject_id: m_rsc:resource()
  • predicate: atom
  • object_id: m_rsc:resource()
  • edge_id: pos_integer

Example

Perform some action when an edge is deleted:

-include_lib("zotonic_core/include/zotonic.hrl").
-export([observe_edge_delete/2]).

observe_edge_delete(#edge_delete{edge_id = Id}, Context) ->
    %% Consult the edge_log table to get the late edge's details
    Edge = z_db:assoc_row("select * from edge_log where edge_id = $1", [Id], Context)),

    ?DEBUG(Edge),
    %% logged is when the deletion was logged; created is when the edge was
    %% originally created
    %% [{id,11},{op,<<"DELETE">>},{edge_id,25},{subject_id,341},{predicate_id,300},{predicate,<<"about">>},{object_id,338},{seq,1000000},{logged,{{2016,10,13},{10,23,21}}},{created,{{2016,10,13},{10,23,13}}}]

    %% Do something...

    ok.

edge_insert

An edge has been inserted

Type:
notify
Return:
return value is ignored
#edge_insert{} properties:
  • subject_id: m_rsc:resource()
  • predicate: atom
  • object_id: m_rsc:resource()
  • edge_id: pos_integer

edge_update

An edge has been updated

Type:
notify
Return:
return value is ignored
#edge_update{} properties:
  • subject_id: m_rsc:resource()
  • predicate: atom
  • object_id: m_rsc:resource()
  • edge_id: pos_integer