<commit_before>-module(event_controller).

-export([init/0, get/3, list/3, add/3]).

-include("uce.hrl").
-include("yaws_api.hrl").

init() ->
    [#uce_route{module="Events",
		method='GET',
		regexp="/event/([^/]+)/([^/]+)/([^/]+)",
		types=[any, any, event],
		callbacks=[{presence_controller, check,
			    ["uid", "sid"],
			    [required, required],
			    [string, string],
			    [user, presence]},
			   {?MODULE, get,
			    ["uid"],
			    [required],
			    [string],
			    [user]}]},

     #uce_route{module="Events",
		method='GET',
		regexp="/event/?([^/]+)?/?([^/]+)?/?",
		types=[org, meeting],
		callbacks=[{presence_controller, check,
			    ["uid", "sid"],
			    [required, required],
			    [string, string],
			    [user, presence]},
			   {?MODULE, list,
			    ["uid",
			     "search",
			     "type",
			     "from",
			     "start",
			     "end",
			     "count",
			     "page",
			     "order",
			     "parent",
			     "_async"],
			    [required, '_', '_', '_', 0, infinity, infinity, 1, asc, '_', "no"],
			    [string,
			     [string, atom],
			     [string, atom],
			     [string, atom],
			     integer,
			     [integer, atom],
			     [integer, atom],
			     integer,
			     atom,
			     [string, atom],
			     string],
			    [user, any, any, user, any, any, any, any, any, event, any]}]},
     
     #uce_route{module="Events",
		method='PUT',
		regexp="/event/?([^/]+)?/?([^/]+)?/?",
		types=[org, meeting],
		callbacks=[{presence_controller, check,
			    ["uid", "sid"],
			    [required, required],
			    [string, string],
			    [user, presence]},
			   {?MODULE, add,
			    ["uid", "type", "to", "parent", "metadata"],
			    [required, required, "all", "", []],
			    [string, string, string, string, dictionary],
			    [user, any, user, event, any]}]}].

get([_, _, Id], [Uid], _) ->
    case uce_acl:check(Uid, "event", "get", ["", ""], [{"id", Id}]) of
	false ->
	    {error, unauthorized};
	true ->
	    case uce_event:get(Id) of
		{error, Reason} ->
		    {error, Reason};
		#uce_event{to=To} = Event ->
		    if
			To == "all" ->
			    json_helpers:json(event_helpers:to_json(Event));
			To == Uid ->
			    json_helpers:json(event_helpers:to_json(Event));
			true ->
			    {error, unauthorized}
		    end
	    end
    end.

listen(Location, Uid, Search, Type, From, Socket) ->
    mnesia_pubsub:subscribe(Location, Uid, Search, Type, From, self()),
    Res = receive
	      {message, Id} ->
		  case uce_event:get(Id) of
		      {error, Reason} ->
			  ?DEBUG("Pubsub: unknown message ID: ~s (~p)~n", [Id, Reason]),
			  {error, Reason};
		      Event ->
			  JSONEvent = mochijson:encode({struct,
							[{result,
							  event_helpers:to_json(Event)}]}),
			  yaws_api:stream_process_deliver_final_chunk(Socket, list_to_binary(JSONEvent)),			  
			  ok
		  end;
	      {error, Reason} -><commit_msg>The event controller returned a simple event instead of a list of event when long-polling is in use.<commit_after>-module(event_controller).

-export([init/0, get/3, list/3, add/3]).

-include("uce.hrl").
-include("yaws_api.hrl").

init() ->
    [#uce_route{module="Events",
		method='GET',
		regexp="/event/([^/]+)/([^/]+)/([^/]+)",
		types=[any, any, event],
		callbacks=[{presence_controller, check,
			    ["uid", "sid"],
			    [required, required],
			    [string, string],
			    [user, presence]},
			   {?MODULE, get,
			    ["uid"],
			    [required],
			    [string],
			    [user]}]},

     #uce_route{module="Events",
		method='GET',
		regexp="/event/?([^/]+)?/?([^/]+)?/?",
		types=[org, meeting],
		callbacks=[{presence_controller, check,
			    ["uid", "sid"],
			    [required, required],
			    [string, string],
			    [user, presence]},
			   {?MODULE, list,
			    ["uid",
			     "search",
			     "type",
			     "from",
			     "start",
			     "end",
			     "count",
			     "page",
			     "order",
			     "parent",
			     "_async"],
			    [required, '_', '_', '_', 0, infinity, infinity, 1, asc, '_', "no"],
			    [string,
			     [string, atom],
			     [string, atom],
			     [string, atom],
			     integer,
			     [integer, atom],
			     [integer, atom],
			     integer,
			     atom,
			     [string, atom],
			     string],
			    [user, any, any, user, any, any, any, any, any, event, any]}]},
     
     #uce_route{module="Events",
		method='PUT',
		regexp="/event/?([^/]+)?/?([^/]+)?/?",
		types=[org, meeting],
		callbacks=[{presence_controller, check,
			    ["uid", "sid"],
			    [required, required],
			    [string, string],
			    [user, presence]},
			   {?MODULE, add,
			    ["uid", "type", "to", "parent", "metadata"],
			    [required, required, "all", "", []],
			    [string, string, string, string, dictionary],
			    [user, any, user, event, any]}]}].

get([_, _, Id], [Uid], _) ->
    case uce_acl:check(Uid, "event", "get", ["", ""], [{"id", Id}]) of
	false ->
	    {error, unauthorized};
	true ->
	    case uce_event:get(Id) of
		{error, Reason} ->
		    {error, Reason};
		#uce_event{to=To} = Event ->
		    if
			To == "all" ->
			    json_helpers:json(event_helpers:to_json(Event));
			To == Uid ->
			    json_helpers:json(event_helpers:to_json(Event));
			true ->
			    {error, unauthorized}
		    end
	    end
    end.

listen(Location, Uid, Search, Type, From, Socket) ->
    mnesia_pubsub:subscribe(Location, Uid, Search, Type, From, self()),
    Res = receive
	      {message, Id} ->
		  case uce_event:get(Id) of
		      {error, Reason} ->
			  ?DEBUG("Pubsub: unknown message ID: ~s (~p)~n", [Id, Reason]),
			  {error, Reason};
		      Event ->
			  JSONEvent = mochijson:encode({struct,
							[{result,
							  event_helpers:to_json([Event])}]}),
			  yaws_api:stream_process_deliver_final_chunk(Socket, list_to_binary(JSONEvent)),			  
			  ok
		  end;
	      {error, Reason} ->
{'fraction_non_alphanumeric': 0.20922654776196192, 'fraction_numerical': 0.0020579660435602813, 'mean_word_length': 2.3986013986013988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3852177', 'n_tokens_mistral': 2459, 'n_tokens_neox': 1985, 'n_words': 429}