%%%------------------------------------------------------------------- %%% File : avatar_trust_helper.erl %%% Author : Gordon Guthrie %%% Description : %%% %%% Created : 16 Jan 2004 by Gordon Guthrie %%% Revision(HB): 03/08/05 Added try messages handling %%% Revision(HB): 04/08/05 Added trust history support %%% Revision(HB): 08/08/05 Fixed untrust message handling %%% Modified timestamps for history messages %%%------------------------------------------------------------------- -module(avatar_trust_helper). -copyright('Gordon Guthrie (c) 2003'). -include("avatar_records.hrl"). %%% This function is only for use in module avatar -export([handletrusts/5]). %% ******************************************************************** %% %% handletrusts is called by the main avatar module to handle all the %% trust type messages: %% + trust %% + trusted by %% + untrust %% + untrusted by %% + distrust %% + undistrust %% + distrusted by %% + undistrusted by %% + block %% + blocked by %% + unblock %% + unblocked by %% + untry %% + untried by %% %% This function returns a message {ok, Newstate} or exits %% Newstate is the new version of the state of the avatar %% %% ******************************************************************** handletrusts(Message, MyName, Trusts, Notify, AdminMsgs) -> %% %% First the active voice - message originating from the avatar's owner %% case Message of {"trust", Name} -> Relationship = {"trust", Name, "about", "everything"}, {AddHistory, NewTrusts2} = apply_trust(Relationship, MyName, Trusts), NewNotify=Notify, NewAdminMsgs = AdminMsgs, if AddHistory == true -> addhistory(MyName, Relationship); true -> ok end; {"trust", Name, "about", Topic} -> Relationship = {"trust", Name, "about", Topic}, {AddHistory, NewTrusts2} = apply_trust(Relationship, MyName, Trusts), NewNotify=Notify, NewAdminMsgs = AdminMsgs, if AddHistory == true -> addhistory(MyName, Relationship); true -> ok end; {"untrust", Name} -> NewTrusts2 = lists:delete({"trust", Name, "about", "everything"}, Trusts), addhistory(MyName, {"untrust",Name,"about","everything"}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"untrusted by", MyName, "about", "everything"}); {"untrust", Name, "about", Topic} -> NewTrusts2 = lists:delete({"trust", Name, "about", Topic}, Trusts), addhistory(MyName, {"untrust",Name,"about",Topic}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"untrusted by", MyName, "about", Topic}); {"distrust", Name} -> Relationship = {"distrust", Name, "about", "everything"}, NewTrusts = removetrust(Trusts, {false, Name, "about", "everything"}), NewTrusts2 = addtrust(NewTrusts, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, case NewTrusts of Trusts -> avatar:trust_message(Name, {"distrusted by", MyName, "about", "everything"}), addhistory(MyName, Relationship); _ -> ok end; {"distrust", Name, "about", Topic} -> Relationship = {"distrust", Name, "about", Topic}, NewTrusts = removetrust(Trusts, {false, Name, "about", Topic}), NewTrusts2 = addtrust(NewTrusts, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, case NewTrusts of Trusts -> avatar:trust_message(Name,{"distrusted by",MyName,"about",Topic}), addhistory(MyName, Relationship); _ -> ok end; {"undistrust", Name} -> NewTrusts2 = removetrust(Trusts,{"distrust",Name,"about","everything"}), addhistory(MyName, {"undistrust", Name, "about", "everything"}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"undistrusted by",MyName,"about","everything"}); {"undistrust", Name, "about", Topic} -> NewTrusts2 = lists:delete({"distrust", Name, "about", Topic}, Trusts), addhistory(MyName, {"undistrust", Name, "about", Topic}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name,{"undistrusted by",MyName,"about",Topic}); {"block", Name} -> Relationship = {"block", Name, "about", "everything"}, NewTrusts = removetrust(Trusts, {false, Name, "about", "everything"}), NewTrusts2 = addtrust(NewTrusts, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, case NewTrusts of Trusts -> avatar:trust_message(Name,{"blocked by",MyName,"about","everything"}), addhistory(MyName, Relationship); _ -> ok end; {"block", Name, "about", Topic} -> Relationship = {"block", Name, "about", Topic}, NewTrusts = removetrust(Trusts, {false, Name, "about", Topic}), NewTrusts2 = addtrust(NewTrusts, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, case NewTrusts of Trusts -> avatar:trust_message(Name,{"blocked by",MyName,"about",Topic}), addhistory(MyName, Relationship); _ -> ok end; {"unblock", Name} -> NewTrusts2 = lists:delete({"block", Name, "about", "everything"},Trusts), addhistory(MyName, {"unblock",Name,"about","everything"}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"unblocked by",MyName,"about","everything"}); {"unblock", Name, "about", Topic} -> NewTrusts2 = lists:delete({"block", Name, "about", Topic}, Trusts), addhistory(MyName, {"unblock", Name, "about", Topic}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"unblocked by",MyName,"about",Topic}); {"try", Name} -> Relationship = {"try", Name, "about", "everything", "for", "3"}, NewTrusts2 = addtrust(Trusts, Relationship), addhistory(MyName, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"tried by",MyName,"about","everything","for","3"}); {"try", Name, "for", Number} -> Relationship = {"try", Name, "about", "everything", "for", Number}, NewTrusts2 = addtrust(Trusts, Relationship), addhistory(MyName, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"tried by",MyName,"about","everything","for",Number}); {"try", Name, "about", Topic} -> Relationship = {"try", Name, "about", Topic, "for", "3"}, NewTrusts2 = addtrust(Trusts, Relationship), addhistory(MyName, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"tried by",MyName,"about",Topic,"for","3"}); {"try", Name, "about", Topic, "for", Number} -> Relationship = {"try", Name, "about", Topic, "for", Number}, NewTrusts2 = addtrust(Trusts, Relationship), addhistory(MyName, Relationship), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"tried by",MyName,"about",Topic,"for",Number}); {"untry", Name} -> TryRelation = getrelation("try", Name, "everything", Trusts), NewTrusts2 = lists:delete(TryRelation, Trusts), addhistory(MyName, {"untry", Name, "about", "everything"}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"untried by", MyName, "about", "everything"}); {"untry", Name, "about", Topic} -> TryRelation = getrelation("try", Name, Topic, Trusts), NewTrusts2 = lists:delete(TryRelation, Trusts), addhistory(MyName, {"untry", Name, "about", Topic}), NewNotify=Notify, NewAdminMsgs = AdminMsgs, avatar:trust_message(Name, {"untried by", MyName, "about", Topic}); %% %% Now the passive voice, messages originating from other avatars %% {"trusted by", Name, "about", Topic} -> Relationship = {"trusted by", Name , "about", Topic}, NewTrusts = removetrust(Trusts, {true, Name, "about", Topic}), NewTrusts2 = addtrust(NewTrusts, Relationship), addhistory(MyName, Relationship), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"untrusted by", Name, "about", Topic} -> Relationship = {"untrusted by", Name, "about", Topic}, NewTrusts2 = lists:delete({"trusted by", Name, "about", Topic}, Trusts), addhistory(MyName, Relationship), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"distrusted by", Name, "about", Topic} -> Relationship = {"distrusted by", Name, "about", Topic}, NewTrusts = removetrust(Trusts, {true, Name, "about", Topic}), NewTrusts2 = addtrust(NewTrusts, Relationship), addhistory(MyName, Relationship), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"undistrusted by", Name, "about", Topic} -> Relationship = {"undistrusted by", Name, "about", Topic}, NewTrusts2 = lists:delete({"distrusted by", Name, "about", Topic}, Trusts), addhistory(MyName, {"undistrusted by", Name, "about", Topic}), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"blocked by", Name, "about", Topic} -> Relationship = {"blocked by", Name, "about", Topic}, NewTrusts = removetrust(Trusts, {true, Name, "about", Topic}), NewTrusts2 = addtrust(NewTrusts, Relationship), addhistory(MyName, Relationship), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"unblocked by", Name, "about", Topic} -> Relationship = {"unblocked by", Name, "about", Topic}, NewTrusts2 = lists:delete({"blocked by", Name, "about", Topic}, Trusts), addhistory(MyName, {"unblocked by", Name, "about", Topic}), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"tried by", Name, "about", Topic, "for", Number} -> Relationship = {"tried by", Name, "about", Topic, "for", Number}, NewTrusts2 = addtrust(Trusts, Relationship), addhistory(MyName, Relationship), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); {"untried by", Name, "about", Topic} -> Relationship = {"untried by", Name, "about", Topic}, TriedRelation = getrelation("tried by", Name, Topic, Trusts), NewTrusts2 = lists:delete(TriedRelation, Trusts), addhistory(MyName, {"untried by", Name, "about", Topic}), {ok, NewNotify, NewAdminMsgs} = avatar_notification_helper:handlenotifications(MyName,Relationship,Notify,AdminMsgs); %% %% Anything else we just die %% _Else -> NewTrusts2 = scratch, NewHistory = scratch, NewNotify=scratch, NewAdminMsgs=scratch, exit("unexpected trust message being handled", Message) end, {ok,NewTrusts2,NewNotify,NewAdminMsgs}. % Reply %% ******************************************************************** %% %% addtrust adds a trust to the trust list %% %% the trust is passed in in the variable Relationship which is a tuple %% with the following structure %% %% * the name of the avatar %% + the name of the relationship, eg: %% + trust %% + trusted by %% + distrust %% + distrusted by %% + block %% + blocked by %% + try %% + tried by %% + the topic (or "everything") %% %% ******************************************************************** addtrust(Trusts, Relationship) -> [Relationship|Trusts]. % case lists:member(Relationship, Trusts) of % false -> [Relationship | Trusts]; % true -> Trusts % end. %% ******************************************************************** %% %% removetrust removes a trust from the trust list with this profile %% %% Trust type is the suffix i.e. "trust" -> "" and "trusted by" -> "by" %% (Used to overwrite trusts of the same type). %% %% ******************************************************************** removetrust(Trusts, {Suffix, Person, "about", Topic}) -> R = fun(X) -> case {Suffix, X} of {true, {Full, Person, "about", Topic}} -> not lists:suffix(" by", Full); {false, {Full, Person, "about", Topic}} -> lists:suffix(" by", Full); {true, {"tried by", Person, "about", Topic, "for", _N}} -> false; {false, {"try", Person, "about", Topic, "for", _N}} -> false; _Else -> true end end, lists:filter(R, Trusts). %% ******************************************************************** %% %% apply_trust is a work around that gets us away from a compiler %% error (no it isn't says conrad...) %% %% ******************************************************************** apply_trust({"trust", Name, "about", Topic}, MyName, Trusts) -> NewTrusts = removetrust(Trusts, {false, Name, "about", Topic}), case NewTrusts of Trusts -> avatar:trust_message(Name, {"trusted by", MyName, "about", Topic}), {true, addtrust(NewTrusts, {"trust", Name , "about", Topic})}; _ -> {false, addtrust(NewTrusts, {"trust", Name , "about", Topic})} end. %% ******************************************************************** %% %% Gets a type of relationship based on the name and topic. %% %% ******************************************************************** getrelation(Type, Name, Topic, Relationships) -> TryTuples = avatar_utilities:filtertrustsbytype(Type, Relationships), NameTuples = avatar_utilities:filtertrustsbyname(Name, TryTuples), TopicTuples = avatar_utilities:filtertrustsbytopic(Topic, NameTuples), if length(TopicTuples) > 0 -> lists:nth(1, TopicTuples); true -> null end. %% ******************************************************************** %% %% Adds a new trust in the Trust History. %% %% Takes a normal relationship, adds a timestamp to it and adds it to %% the current history. %% %% ******************************************************************** addhistory(Avatar, Relationship) -> NewHistory = {Relationship, "on", calendar:now_to_datetime(now())}, socket:call(data_access,write_history,{Avatar,NewHistory}).