• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 747

27 Jun 2024 01:43PM UTC coverage: 32.123% (+0.8%) from 31.276%
747

push

github

badlop
Set version to 24.06

14119 of 43953 relevant lines covered (32.12%)

614.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.0
/src/mod_last_sql.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_last_sql.erl
3
%%% Author  : Evgeny Khramtsov <ekhramtsov@process-one.net>
4
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
5
%%%
6
%%%
7
%%% ejabberd, Copyright (C) 2002-2024   ProcessOne
8
%%%
9
%%% This program is free software; you can redistribute it and/or
10
%%% modify it under the terms of the GNU General Public License as
11
%%% published by the Free Software Foundation; either version 2 of the
12
%%% License, or (at your option) any later version.
13
%%%
14
%%% This program is distributed in the hope that it will be useful,
15
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
16
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
%%% General Public License for more details.
18
%%%
19
%%% You should have received a copy of the GNU General Public License along
20
%%% with this program; if not, write to the Free Software Foundation, Inc.,
21
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
%%%
23
%%%----------------------------------------------------------------------
24

25
-module(mod_last_sql).
26

27
-behaviour(mod_last).
28

29

30
%% API
31
-export([init/2, get_last/2, store_last_info/4, remove_user/2,
32
         import/2, export/1]).
33
-export([sql_schemas/0]).
34

35
-include("mod_last.hrl").
36
-include("logger.hrl").
37
-include("ejabberd_sql_pt.hrl").
38

39
%%%===================================================================
40
%%% API
41
%%%===================================================================
42
init(Host, _Opts) ->
43
    ejabberd_sql_schema:update_schema(Host, ?MODULE, sql_schemas()),
3✔
44
    ok.
3✔
45

46
sql_schemas() ->
47
    [#sql_schema{
3✔
48
        version = 1,
49
        tables =
50
            [#sql_table{
51
                name = <<"last">>,
52
                columns =
53
                    [#sql_column{name = <<"username">>, type = text},
54
                     #sql_column{name = <<"server_host">>, type = text},
55
                     #sql_column{name = <<"seconds">>, type = text},
56
                     #sql_column{name = <<"state">>, type = text}],
57
                indices = [#sql_index{
58
                              columns = [<<"server_host">>, <<"username">>],
59
                              unique = true}]}]}].
60

61
get_last(LUser, LServer) ->
62
    case ejabberd_sql:sql_query(
×
63
           LServer,
64
           ?SQL("select @(seconds)d, @(state)s from last"
×
65
                " where username=%(LUser)s and %(LServer)H")) of
66
        {selected, []} ->
67
            error;
×
68
        {selected, [{TimeStamp, Status}]} ->
69
            {ok, {TimeStamp, Status}};
×
70
        _Reason ->
71
            {error, db_failure}
×
72
    end.
73

74
store_last_info(LUser, LServer, TimeStamp, Status) ->
75
    TS = integer_to_binary(TimeStamp),
69✔
76
    case ?SQL_UPSERT(LServer, "last",
69✔
77
                     ["!username=%(LUser)s",
78
                      "!server_host=%(LServer)s",
79
                      "seconds=%(TS)s",
80
                      "state=%(Status)s"]) of
81
        ok ->
82
            ok;
69✔
83
        _Err ->
84
            {error, db_failure}
×
85
    end.
86

87
remove_user(LUser, LServer) ->
88
    ejabberd_sql:sql_query(
6✔
89
      LServer,
90
      ?SQL("delete from last where username=%(LUser)s and %(LServer)H")).
6✔
91

92
export(_Server) ->
93
    [{last_activity,
×
94
      fun(Host, #last_activity{us = {LUser, LServer},
95
                               timestamp = TimeStamp, status = Status})
96
            when LServer == Host ->
97
              TS = integer_to_binary(TimeStamp),
×
98
              [?SQL("delete from last where username=%(LUser)s and %(LServer)H;"),
×
99
               ?SQL_INSERT("last",
×
100
                           ["username=%(LUser)s",
101
                            "server_host=%(LServer)s",
102
                            "seconds=%(TS)s",
103
                            "state=%(Status)s"])];
104
         (_Host, _R) ->
105
              []
×
106
      end}].
107

108
import(_LServer, _LA) ->
109
    pass.
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc