2016-03-31 22:01:57 +02:00
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
#
|
2017-01-02 21:41:53 +01:00
|
|
|
# ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2016-03-31 22:01:57 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 2 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
# ----------------------------------------------------------------------
|
2016-03-31 13:53:31 +02:00
|
|
|
|
|
|
|
defmodule ModLastMock do
|
|
|
|
|
|
|
|
require Record
|
2016-03-31 22:01:57 +02:00
|
|
|
Record.defrecord :session, Record.extract(:session, from_lib: "ejabberd/include/ejabberd_sm.hrl")
|
|
|
|
Record.defrecord :jid, Record.extract(:jid, from_lib: "ejabberd/include/jlib.hrl")
|
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
@author "jsautret@process-one.net"
|
|
|
|
@agent __MODULE__
|
|
|
|
|
|
|
|
def init do
|
|
|
|
try do
|
|
|
|
Agent.stop(@agent)
|
|
|
|
catch
|
|
|
|
:exit, _e -> :ok
|
|
|
|
end
|
2016-03-31 22:01:57 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
{:ok, _pid} = Agent.start_link(fn -> %{} end, name: @agent)
|
2016-03-31 22:01:57 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
mock(:mod_last, :get_last_info,
|
|
|
|
fn (user, domain) ->
|
|
|
|
Agent.get(@agent, fn last ->
|
|
|
|
case Map.get(last, {user, domain}, :not_found) do
|
|
|
|
{ts, status} -> {:ok, ts, status}
|
|
|
|
result -> result
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
2016-03-31 22:01:57 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
def set_last(user, domain, status) do
|
|
|
|
set_last(user, domain, status, now)
|
|
|
|
end
|
2016-03-31 22:01:57 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
def set_last(user, domain, status, timestamp) do
|
|
|
|
Agent.update(@agent, fn last ->
|
|
|
|
Map.put(last, {user, domain}, {timestamp, status})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
# Helpers
|
|
|
|
####################################################################
|
|
|
|
def now() do
|
|
|
|
{megasecs, secs, _microsecs} = :os.timestamp
|
|
|
|
megasecs * 1000000 + secs
|
|
|
|
end
|
2016-03-31 22:01:57 +02:00
|
|
|
|
2016-03-31 13:53:31 +02:00
|
|
|
# TODO refactor: Move to ejabberd_test_mock
|
|
|
|
def mock(module, function, fun) do
|
|
|
|
try do
|
|
|
|
:meck.new(module)
|
|
|
|
catch
|
|
|
|
:error, {:already_started, _pid} -> :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
:meck.expect(module, function, fun)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|