Permalink
Browse files

Make ua vars available in m_req. Copy ua vars to z_context vars.

  • Loading branch information...
1 parent fc9eda9 commit 132d9e183b6b3f6ba866b414b1c0671f4253a360 @mworrell mworrell committed Apr 11, 2012
Showing with 40 additions and 6 deletions.
  1. +6 −1 src/models/m_req.erl
  2. +6 −2 src/support/z_session_manager.erl
  3. +28 −3 src/support/z_user_agent.erl
View
@@ -37,7 +37,7 @@
%% @doc Fetch the value for the key from a model source
%% @spec m_find_value(Key, Source, Context) -> term()
m_find_value(Key, #m{value=undefined}, Context) ->
- get(Key, z_context:get_reqdata(Context)).
+ get(Key, Context).
%% @doc Transform a m_config value to a list, used for template loops
%% @spec m_to_list(Source, Context) -> List
@@ -51,6 +51,11 @@ m_value(#m{value=undefined}, Context) ->
%% @doc Fetch the field from the wrq interface.
+get(ua_class, #context{} = Context) ->
+ z_user_agent:get_class(Context);
+get(ua_props, #context{} = Context) ->
+ z_user_agent:get_props(Context);
+
get(What, #context{} = Context) -> get(What, z_context:get_reqdata(Context));
get(_What, undefined) -> undefined;
get(method, RD) -> wrq:method(RD);
@@ -395,15 +395,19 @@ start_session(Action, CurrentSessionId, Context) ->
new ->
Props = [
{auth_user_id, z_acl:user(Context3)},
- {remote_ip, m_req:get(peer, Context3)}
+ {remote_ip, m_req:get(peer, Context3)},
+ {ua_class, z_user_agent:get_class(Context3)},
+ {ua_props, z_user_agent:get_props(Context3)}
],
z_session:set(Props, Context3),
z_notifier:notify(session_init, Context3),
z_notifier:foldl(session_init_fold, Context3, Context3);
restart ->
Props = [
{auth_user_id, z_acl:user(Context3)},
- {remote_ip, m_req:get(peer, Context3)}
+ {remote_ip, m_req:get(peer, Context3)},
+ {ua_class, z_user_agent:get_class(Context3)},
+ {ua_props, z_user_agent:get_props(Context3)}
],
z_session:set(Props, Context3),
Context3;
@@ -22,14 +22,18 @@
-export([
set_class/1,
get_class/1,
+ get_props/1,
filename_split_class/1,
order_class/2,
classes/0
]).
-include_lib("zotonic.hrl").
+-type ua_template_class() :: ua_classifier:device_type() | generic.
+
%% @doc Classify the user agent in the wm_reqdata.
+-spec set_class(#wm_reqdata{}) -> {ok, #wm_reqdata{}}.
set_class(#wm_reqdata{} = ReqData) ->
case wrq:get_req_header_lc("user-agent", ReqData) of
undefined ->
@@ -40,10 +44,14 @@ set_class(#wm_reqdata{} = ReqData) ->
UserAgent ->
case ua_classifier:classify(UserAgent) of
{ok, Props} ->
+ {ok, RD1} = webmachine_request:set_metadata(
+ ua_class,
+ ua_classifier:device_type(Props),
+ ReqData),
webmachine_request:set_metadata(
- ua_class,
- ua_classifier:device_type(Props),
- ReqData);
+ ua_props,
+ Props,
+ RD1);
{error, _Reason} ->
webmachine_request:set_metadata(
ua_class,
@@ -53,6 +61,7 @@ set_class(#wm_reqdata{} = ReqData) ->
end.
%% @doc Get the user agent class
+-spec get_class( #context{} | #wm_reqdata{} | undefined ) -> ua_classifier:device_type().
get_class(undefined) ->
desktop;
get_class(#context{} = Context) ->
@@ -63,7 +72,21 @@ get_class(#wm_reqdata{} = ReqData) ->
UAClass -> UAClass
end.
+%% @doc Get the user agent class
+-spec get_props( #context{} | #wm_reqdata{} | undefined ) -> [ {atom(), term()} ].
+get_props(undefined) ->
+ [{id, <<"desktop">>}];
+get_props(#context{} = Context) ->
+ get_props(z_context:get_reqdata(Context));
+get_props(#wm_reqdata{} = ReqData) ->
+ case webmachine_request:get_metadata(ua_props, ReqData) of
+ undefined -> [{id, <<"desktop">>}];
+ Props -> Props
+ end.
+
+
%% @doc Split a filename (with subdir) to a device class and a relative path.
+-spec filename_split_class(file:filename()) -> { ua_template_class(), file:filename() }.
filename_split_class(F) ->
case filename:split(F) of
["desktop"|T] -> {desktop, string:join(T, "/")};
@@ -75,6 +98,7 @@ filename_split_class(F) ->
%% @doc Ordering function for lists:sort/2. Orders by genericity. Most specific first.
+-spec order_class( ua_classifier:device_type(), ua_classifier:device_type() ) -> boolean().
order_class(A,A) -> true;
order_class(_, generic) -> false;
order_class(desktop, _) -> true;
@@ -85,5 +109,6 @@ order_class(_, _) -> false.
%% @doc Return all possible UA classes
+-spec classes() -> [ ua_classifier:device_type() ].
classes() ->
[ text, phone, tablet, desktop ].

0 comments on commit 132d9e1

Please sign in to comment.