|
27 | 27 |
|
28 | 28 | -behaviour(mod_muc). |
29 | 29 | -behaviour(mod_muc_room). |
| 30 | +-behaviour(ejabberd_db_serialize). |
30 | 31 |
|
31 | 32 | %% API |
32 | 33 | -export([init/2, store_room/5, store_changes/4, |
|
43 | 44 | -export([set_affiliation/6, set_affiliations/4, get_affiliation/5, |
44 | 45 | get_affiliations/3, search_affiliation/4]). |
45 | 46 | -export([sql_schemas/0]). |
| 47 | +-export([serialize/3, deserialize_start/1, deserialize/2]). |
46 | 48 |
|
47 | 49 | -include_lib("xmpp/include/jid.hrl"). |
48 | 50 | -include("mod_muc.hrl"). |
49 | 51 | -include("logger.hrl"). |
50 | 52 | -include("ejabberd_sql_pt.hrl"). |
| 53 | +-include("ejabberd_db_serialize.hrl"). |
51 | 54 |
|
52 | 55 | %%%=================================================================== |
53 | 56 | %%% API |
@@ -608,6 +611,102 @@ export(_Server) -> |
608 | 611 | import(_, _, _) -> |
609 | 612 | ok. |
610 | 613 |
|
| 614 | +serialize(LServer, BatchSize, undefined) -> |
| 615 | + serialize(LServer, BatchSize, {rooms, 0}); |
| 616 | +serialize(LServer, BatchSize, {rooms, Offset}) -> |
| 617 | + Records = |
| 618 | + case ejabberd_sql:sql_query( |
| 619 | + LServer, |
| 620 | + ?SQL("select @(name)s, @(host)s, @(opts)s from muc_room " |
| 621 | + "where %(LServer)H " |
| 622 | + "order by host, name " |
| 623 | + "limit %(BatchSize)d offset %(Offset)d")) of |
| 624 | + {selected, Data} -> |
| 625 | + lists:foldl( |
| 626 | + fun(_, {error, _} = Err) -> Err; |
| 627 | + ({Name, Host, Opts}, Acc) -> |
| 628 | + case ejabberd_sql:sql_query( |
| 629 | + LServer, |
| 630 | + ?SQL("select @(jid)s, @(nick)s, @(nodes)s from muc_room_subscribers where room=%(Name)s" |
| 631 | + " and host=%(Host)s and %(LServer)H")) of |
| 632 | + {selected, Subs} -> |
| 633 | + SubData = lists:map( |
| 634 | + fun({Jid, Nick, Nodes}) -> |
| 635 | + {jid:decode(Jid), Nick, ejabberd_sql:decode_term(Nodes)} |
| 636 | + end, Subs), |
| 637 | + OptsD = ejabberd_sql:decode_term(Opts), |
| 638 | + Opts2 = lists:keystore(subscribers, 1, OptsD, {subscribers, SubData}), |
| 639 | + [#serialize_muc_room_v1{serverhost = LServer, host = Host, name = Name, |
| 640 | + options = mod_muc:opts_to_binary(Opts2)} | Acc]; |
| 641 | + _ -> |
| 642 | + {error, io_lib:format("Error when retrieving list of muc subscribers for room ~s@~s", |
| 643 | + [Name, Host])} |
| 644 | + end |
| 645 | + end, [], Data); |
| 646 | + _ -> |
| 647 | + {error, io_lib:format("Error when retrieving list of muc rooms", |
| 648 | + [])} |
| 649 | + end, |
| 650 | + case length(Records) of |
| 651 | + Val when Val < BatchSize -> |
| 652 | + case serialize(LServer, BatchSize - Val, {registrations, 0}) of |
| 653 | + {ok, Records2, Next} -> |
| 654 | + {ok, Records ++ Records2, Next}; |
| 655 | + Err -> Err |
| 656 | + end; |
| 657 | + Val -> |
| 658 | + {ok, Records, {rooms, Offset + Val}} |
| 659 | + end; |
| 660 | +serialize(LServer, BatchSize, {registrations, Offset}) -> |
| 661 | + Records = |
| 662 | + case ejabberd_sql:sql_query( |
| 663 | + LServer, |
| 664 | + ?SQL("select @(jid)s, @(host)s, @(nick)s from muc_registered " |
| 665 | + "where %(LServer)H " |
| 666 | + "order by host, jid " |
| 667 | + "limit %(BatchSize)d offset %(Offset)d")) of |
| 668 | + {selected, Data} -> |
| 669 | + lists:map( |
| 670 | + fun({Jid, Host, Nick}) -> |
| 671 | + #serialize_muc_registrations_v1{serverhost = LServer, host = Host, jid = Jid, nick = Nick} |
| 672 | + end, Data); |
| 673 | + _ -> |
| 674 | + {error, io_lib:format("Error when retrieving list of muc registered nicks", |
| 675 | + [])} |
| 676 | + end, |
| 677 | + {ok, Records, {registrations, Offset + length(Records)}}. |
| 678 | + |
| 679 | +deserialize_start(LServer) -> |
| 680 | + ejabberd_sql:sql_query( |
| 681 | + LServer, |
| 682 | + ?SQL("delete from muc_room where %(LServer)H")), |
| 683 | + ejabberd_sql:sql_query( |
| 684 | + LServer, |
| 685 | + ?SQL("delete from muc_registered where %(LServer)H")). |
| 686 | + |
| 687 | +deserialize(LServer, Batch) -> |
| 688 | + lists:foldl( |
| 689 | + fun(_, {error, _} = Err) -> |
| 690 | + Err; |
| 691 | + (#serialize_muc_room_v1{name = Name, host = Host, options = Opts}, _) -> |
| 692 | + case store_room(LServer, Host, Name, Opts, undefined) of |
| 693 | + {atomic, _} -> |
| 694 | + ok; |
| 695 | + _ -> {error, io_lib:format("Error when writing muc room data", [])} |
| 696 | + end; |
| 697 | + (#serialize_muc_registrations_v1{host = Host, jid = Jid, nick = Nick}, _) -> |
| 698 | + case ejabberd_sql:sql_query(LServer, |
| 699 | + ?SQL_INSERT( |
| 700 | + "muc_registered", |
| 701 | + ["jid=%(Jid)s", |
| 702 | + "host=%(Host)s", |
| 703 | + "server_host=%(LServer)s", |
| 704 | + "nick=%(Nick)s"])) of |
| 705 | + {updated, _} -> ok; |
| 706 | + _ -> {error, io_lib:format("Error when writing muc registration data", [])} |
| 707 | + end |
| 708 | + end, ok, Batch). |
| 709 | + |
611 | 710 | get_subscribed_rooms(LServer, Host, Jid) -> |
612 | 711 | JidS = jid:encode(Jid), |
613 | 712 | case ejabberd_sql:sql_query( |
|
0 commit comments