aboutsummaryrefslogtreecommitdiff
path: root/src/mod_muc_sql.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mod_muc_sql.erl')
-rw-r--r--src/mod_muc_sql.erl34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/mod_muc_sql.erl b/src/mod_muc_sql.erl
index 03022e924..db829f6aa 100644
--- a/src/mod_muc_sql.erl
+++ b/src/mod_muc_sql.erl
@@ -38,6 +38,7 @@
register_online_user/4, unregister_online_user/4,
count_online_rooms_by_user/3, get_online_rooms_by_user/3,
get_subscribed_rooms/3, get_rooms_without_subscribers/2,
+ get_hibernated_rooms_older_than/3,
find_online_room_by_pid/2, remove_user/2]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]).
@@ -64,13 +65,19 @@ store_room(LServer, Host, Name, Opts, ChangesHints) ->
_ -> {[], Opts}
end,
SOpts = misc:term_to_expr(Opts2),
+ Timestamp = case lists:keyfind(hibernation_time, 1, Opts) of
+ false -> <<"1900-01-01 00:00:00">>;
+ {_, undefined} -> <<"1900-01-01 00:00:00">>;
+ {_, Time} -> usec_to_sql_timestamp(Time)
+ end,
F = fun () ->
?SQL_UPSERT_T(
"muc_room",
["!name=%(Name)s",
"!host=%(Host)s",
"server_host=%(LServer)s",
- "opts=%(SOpts)s"]),
+ "opts=%(SOpts)s",
+ "created_at=%(Timestamp)s"]),
case ChangesHints of
Changes when is_list(Changes) ->
[change_room(Host, Name, Change) || Change <- Changes];
@@ -179,6 +186,23 @@ get_rooms_without_subscribers(LServer, Host) ->
[]
end.
+get_hibernated_rooms_older_than(LServer, Host, Timestamp) ->
+ TimestampS = usec_to_sql_timestamp(Timestamp),
+ case catch ejabberd_sql:sql_query(
+ LServer,
+ ?SQL("select @(name)s, @(opts)s from muc_room"
+ " where host=%(Host)s and created_at < %(TimestampS)s and created_at > '1900-01-01 00:00:00'")) of
+ {selected, RoomOpts} ->
+ lists:map(
+ fun({Room, Opts}) ->
+ OptsD = ejabberd_sql:decode_term(Opts),
+ #muc_room{name_host = {Room, Host},
+ opts = mod_muc:opts_to_binary(OptsD)}
+ end, RoomOpts);
+ _Err ->
+ []
+ end.
+
get_rooms(LServer, Host) ->
case catch ejabberd_sql:sql_query(
LServer,
@@ -497,3 +521,11 @@ clean_tables(ServerHost) ->
?ERROR_MSG("Failed to clean 'muc_online_users' table: ~p", [Err2]),
Err2
end.
+
+usec_to_sql_timestamp(Timestamp) ->
+ case calendar:system_time_to_universal_time(Timestamp, microsecond) of
+ {{Year, Month, Day}, {Hour, Minute, Second}} ->
+ list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0B "
+ "~2..0B:~2..0B:~2..0B",
+ [Year, Month, Day, Hour, Minute, Second]))
+ end.