annotate mod_bookmarks/mod_bookmarks.lua @ 3308:7155cc1adf8f

mod_bookmarks: Handle iq:private queries before mod_private
author Kim Alvefur <zash@zash.se>
date Sun, 09 Sep 2018 14:10:22 +0200
parents 303b17ec8264
children fd889eb16541
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3238
786ba175f2e5 mod_bookmarks: Require forgotten util.jid module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3235
diff changeset
1 local st = require "util.stanza";
786ba175f2e5 mod_bookmarks: Require forgotten util.jid module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3235
diff changeset
2 local jid_split = require "util.jid".split;
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 local mod_pep = module:depends "pep";
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 local private_storage = module:open_store("private", "map");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
3294
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
7 local default_options = {
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
8 ["persist_items"] = true;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
9 ["access_model"] = "whitelist";
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
10 };
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
11
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 module:hook("account-disco-info", function (event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 event.reply:tag("feature", { var = "urn:xmpp:bookmarks-conversion:0" }):up();
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 end);
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 local function on_retrieve_private_xml(event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 local stanza, session = event.stanza, event.origin;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 local query = stanza:get_child("query", "jabber:iq:private");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 if query == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 local bookmarks = query:get_child("storage", "storage:bookmarks");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 if bookmarks == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
3230
ba0d444b64aa mod_bookmarks: Simplify last item retrieval thanks to Prosody b6ffd4f951b9.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3229
diff changeset
28 module:log("debug", "Getting private bookmarks: %s", bookmarks);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 local username = session.username;
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
31 local jid = username.."@"..session.host;
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 local service = mod_pep.get_pep_service(username);
3230
ba0d444b64aa mod_bookmarks: Simplify last item retrieval thanks to Prosody b6ffd4f951b9.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3229
diff changeset
33 local ok, id, item = service:get_last_item("storage:bookmarks", session.full_jid);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 if not ok then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
35 module:log("error", "Failed to retrieve PEP bookmarks of %s: %s", jid, id);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to retrive bookmarks from PEP"));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 end
3233
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3232
diff changeset
39 if not id or not item then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
40 module:log("debug", "Got no PEP bookmarks item for %s, returning empty private bookmarks", jid);
3233
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3232
diff changeset
41 session.send(st.reply(stanza):add_child(query));
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3232
diff changeset
42 return
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3232
diff changeset
43 end
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3232
diff changeset
44 module:log("debug", "Got item %s: %s", id, item);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 local content = item.tags[1];
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
47 module:log("debug", "Sending back private for %s: %s", jid, content);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 session.send(st.reply(stanza):query("jabber:iq:private"):add_child(content));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 return true;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
3235
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3234
diff changeset
52 function publish_to_pep(jid, bookmarks)
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3234
diff changeset
53 local service = mod_pep.get_pep_service(jid_split(jid));
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" })
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 :add_child(bookmarks);
3294
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
56 return service:publish("storage:bookmarks", jid, "current", item, default_options);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 -- Synchronise Private XML to PEP.
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 local function on_publish_private_xml(event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 local stanza, session = event.stanza, event.origin;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 local query = stanza:get_child("query", "jabber:iq:private");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 if query == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 local bookmarks = query:get_child("storage", "storage:bookmarks");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 if bookmarks == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 module:log("debug", "Private bookmarks set by client, publishing to pep");
3235
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3234
diff changeset
73 local ok, err = publish_to_pep(session.full_jid, bookmarks);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 if not ok then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
75 module:log("error", "Failed to publish to PEP bookmarks for %s@%s: %s", session.username, session.host, err);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP"));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 session.send(st.reply(stanza));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 return true;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 local function on_resource_bind(event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 local session = event.session;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 local username = session.username;
3290
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3282
diff changeset
87 local service = mod_pep.get_pep_service(username);
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
88 local jid = username.."@"..session.host;
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 local data, err = private_storage:get(username, "storage:storage:bookmarks");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 if not data then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
92 module:log("debug", "No existing Private XML bookmarks for %s, migration already done: %s", jid, err);
3234
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
93 local ok, id = service:get_last_item("storage:bookmarks", session.full_jid);
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
94 if not ok or not id then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
95 module:log("debug", "Additionally, no PEP bookmarks were existing for %s", jid);
3234
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
96 module:fire_event("bookmarks/empty", { session = session });
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
97 end
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 local bookmarks = st.deserialize(data);
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
101 module:log("debug", "Got private bookmarks of %s: %s", jid, bookmarks);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102
3290
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3282
diff changeset
103 -- We don’t care if deleting succeeds or not, we only want to start with a non-existent node.
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3282
diff changeset
104 module:log("debug", "Deleting possibly existing PEP item for %s", jid);
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3282
diff changeset
105 service:delete("storage:bookmarks", jid);
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3282
diff changeset
106
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
107 module:log("debug", "Going to store PEP item for %s", jid);
3235
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3234
diff changeset
108 local ok, err = publish_to_pep(session.full_jid, bookmarks);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 if not ok then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
110 module:log("error", "Failed to store bookmarks to PEP for %s, aborting migration: %s", jid, err);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 end
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
113 module:log("debug", "Stored bookmarks to PEP for %s", jid);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 local ok, err = private_storage:set(username, "storage:storage:bookmarks", nil);
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 if not ok then
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
117 module:log("error", "Failed to remove private bookmarks of %s: %s", jid, err);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 end
3282
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3253
diff changeset
120 module:log("debug", "Removed private bookmarks of %s, migration done!", jid);
3229
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122
3234
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
123 local function on_item_published(event)
3253
cf8ad9fd7f15 mod_bookmarks: Use the new item-published/<node> event.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
124 module:fire_event("bookmarks/updated", event);
3234
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
125 end
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
126
3294
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
127 local function on_node_created(event)
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
128 local service, node, actor = event.service, event.node, event.actor;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
129 if node ~= "storage:bookmarks" then
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
130 return;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
131 end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
132 local node_config = service.nodes[node].config;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
133 for config_field, value in pairs(default_options) do
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
134 node_config[config_field] = value;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
135 end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
136 end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3290
diff changeset
137
3308
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
138 module:hook("iq/bare/jabber:iq:private:query", function (event)
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
139 if event.stanza.attr.type == "get" then
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
140 return on_retrieve_private_xml(event);
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
141 else
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
142 return on_publish_private_xml(event);
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
143 end
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3307
diff changeset
144 end, 1);
3234
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
145 module:hook("resource-bind", on_resource_bind);
3253
cf8ad9fd7f15 mod_bookmarks: Use the new item-published/<node> event.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
146 module:hook("item-published/storage:bookmarks", on_item_published);
3307
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3294
diff changeset
147 module:handle_items("pep-service", function (event)
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3294
diff changeset
148 local service = event.item.service;
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3294
diff changeset
149 module:hook_object_event(service.events, "node-created", on_node_created);
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3294
diff changeset
150 end, function () end, true);