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
- id:
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
- id:
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
- winner_id:
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
- id:
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
- id:
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
- query_id:
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
- action:
An updated resource is about to be persisted. Observe this notification to change the resource properties before they are persisted.
Arguments¶
#rsc_updateaction- Either
insertorupdate. id- Id of the resource.
props- List of resource properties.
{IsChanged, UpdateProps}- And/remove resource properties before the update is persisted. Set
IsChangedtotrueif you want to modifyUpdateProps. 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
- action:
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
- id:
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
- subject_id:
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.