/src/privacy/mod_privacy_riak.erl

https://github.com/esl/MongooseIM · Erlang · 173 lines · 117 code · 22 blank · 34 comment · 0 complexity · 0bd5dad57c9a4765212678ef3fbaa3b0 MD5 · raw file

  1. %%==============================================================================
  2. %% Copyright 2015 Guillaume Bour.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%==============================================================================
  16. %
  17. % NOTES:
  18. % user default privacy is stored in
  19. % bucket {<<"privacy_defaults">>, LServer}, key LUser@LServer
  20. % user privacy lists names are stored in
  21. % set {<<"privacy_lists_names">>, <<"LServer">>}, key LUser@LServer
  22. % user privacy lists content are stored in
  23. % bucket {<<"privacy_lists">>, LServer}, key LUser@LServer/ListName
  24. -module(mod_privacy_riak).
  25. -author('guillaume@bour.cc').
  26. -behaviour(mod_privacy_backend).
  27. -export([init/2,
  28. get_default_list/3,
  29. get_list_names/3,
  30. get_privacy_list/4,
  31. set_default_list/4,
  32. forget_default_list/3,
  33. remove_privacy_list/4,
  34. replace_privacy_list/5,
  35. remove_user/3,
  36. remove_domain/2]).
  37. -include("mongoose.hrl").
  38. -include("jlib.hrl").
  39. -include("mod_privacy.hrl").
  40. -include_lib("riakc/include/riakc.hrl").
  41. get_bucket_name(HostType, Opt) ->
  42. {gen_mod:get_module_opt(HostType, mod_privacy, [riak, Opt]), HostType}.
  43. -define(BKT_DEFAULT_LIST(HostType),
  44. get_bucket_name(HostType, defaults_bucket_type)).
  45. -define(BKT_LISTS_NAMES(HostType),
  46. get_bucket_name(HostType, names_bucket_type)).
  47. -define(BKT_LISTS(HostType),
  48. get_bucket_name(HostType, bucket_type)).
  49. init(_HostType, _Opts) ->
  50. ok.
  51. get_default_list(HostType, LUser, LServer) ->
  52. case get_default_list_name(HostType, LUser, LServer) of
  53. none ->
  54. {error, not_found};
  55. Default ->
  56. case get_privacy_list(HostType, LUser, LServer, Default) of
  57. {ok, List} ->
  58. {ok, {Default, List}};
  59. {error, Reason} ->
  60. {error, Reason}
  61. end
  62. end.
  63. get_list_names(HostType, LUser, LServer) ->
  64. Default = get_default_list_name(HostType, LUser, LServer),
  65. Names = get_list_names_only(HostType, LUser, LServer),
  66. {ok, {Default, Names}}.
  67. -spec get_default_list_name(mongooseim:host_type(), jid:luser(), jid:lserver()) -> binary() | none.
  68. get_default_list_name(HostType, LUser, LServer) ->
  69. case mongoose_riak:get(?BKT_DEFAULT_LIST(HostType), key(LUser, LServer)) of
  70. {ok, Obj} ->
  71. riakc_obj:get_value(Obj);
  72. _ -> none
  73. end.
  74. -spec get_list_names_only(mongooseim:host_type(), jid:luser(), jid:lserver()) -> list(binary()).
  75. get_list_names_only(HostType, LUser, LServer) ->
  76. case mongoose_riak:fetch_type(?BKT_LISTS_NAMES(HostType), key(LUser, LServer)) of
  77. {ok, Set} ->
  78. riakc_set:value(Set);
  79. {error, {notfound, set}} ->
  80. [];
  81. Err ->
  82. ?LOG_ERROR(#{what => privacy_get_list_names_only_failed,
  83. user => LUser, server => LServer, reason => Err}),
  84. []
  85. end.
  86. get_privacy_list(HostType, LUser, LServer, Name) ->
  87. case mongoose_riak:get(?BKT_LISTS(HostType), key(LUser, LServer, Name)) of
  88. {ok, Obj} ->
  89. Val = binary_to_term(riakc_obj:get_value(Obj)),
  90. {ok, Val};
  91. {error, notfound} ->
  92. {error, not_found};
  93. Err ->
  94. ?LOG_ERROR(#{what => privacy_get_list_names_only_failed,
  95. user => LUser, server => LServer, list_name => Name,
  96. reason => Err}),
  97. Err
  98. end.
  99. forget_default_list(HostType, LUser, LServer) ->
  100. mongoose_riak:delete(?BKT_DEFAULT_LIST(HostType), key(LUser, LServer)).
  101. set_default_list(HostType, LUser, LServer, Name) ->
  102. case mongoose_riak:get(?BKT_LISTS(HostType), key(LUser, LServer, Name)) of
  103. {ok, _} ->
  104. % create or update
  105. Obj = riakc_obj:new(?BKT_DEFAULT_LIST(HostType), key(LUser, LServer), Name),
  106. mongoose_riak:put(Obj);
  107. % in case list name is not found
  108. {error, notfound} ->
  109. {error, not_found};
  110. Err -> Err
  111. end.
  112. remove_privacy_list(HostType, LUser, LServer, Name) ->
  113. mongoose_riak:delete(?BKT_LISTS(HostType), key(LUser, LServer, Name)),
  114. case mongoose_riak:fetch_type(?BKT_LISTS_NAMES(HostType), key(LUser, LServer)) of
  115. {ok, S1} ->
  116. S2 = riakc_set:del_element(Name, S1),
  117. mongoose_riak:update_type(?BKT_LISTS_NAMES(HostType), key(LUser, LServer), riakc_set:to_op(S2));
  118. Err ->
  119. ?LOG_ERROR(#{what => privacy_remove_privacy_list_failed,
  120. user => LUser, server => LServer, list_name => Name,
  121. reason => Err}),
  122. Err
  123. end.
  124. replace_privacy_list(HostType, LUser, LServer, Name, List) ->
  125. % store privacy-list content
  126. BinaryList = term_to_binary(List),
  127. Obj = riakc_obj:new(?BKT_LISTS(HostType), key(LUser, LServer, Name), BinaryList),
  128. mongoose_riak:put(Obj),
  129. % add new list name to user privacy-lists set
  130. S = riakc_set:add_element(Name, riakc_set:new()),
  131. mongoose_riak:update_type(?BKT_LISTS_NAMES(HostType), key(LUser, LServer), riakc_set:to_op(S)).
  132. remove_user(HostType, LUser, LServer) ->
  133. forget_default_list(HostType, LUser, LServer),
  134. lists:foreach(
  135. fun(Name) -> remove_privacy_list(HostType, LUser, LServer, Name) end,
  136. get_list_names_only(HostType, LUser, LServer)
  137. ),
  138. mongoose_riak:delete(?BKT_LISTS_NAMES(HostType), key(LUser, LServer)).
  139. %% TODO
  140. remove_domain(_HostType, _LServer) ->
  141. ok.
  142. % forget_default_list(HostType, <<>>, LServer),
  143. % lists:foreach(
  144. % fun(Name) -> remove_privacy_list(HostType, <<>>, LServer, Name) end,
  145. % get_list_names_only(HostType, <<>>, LServer)
  146. % ),
  147. % mongoose_riak:delete(?BKT_LISTS_NAMES(HostType), key(<<>>, LServer)).
  148. key(LUser, LServer) ->
  149. jid:to_binary({LUser, LServer}).
  150. key(LUser, LServer, Name) ->
  151. jid:to_binary({LUser, LServer, Name}).