Software /
code /
prosody-modules
Comparison
mod_bookmarks/mod_bookmarks.lua @ 3229:e8963e328b26
mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 18 Aug 2018 13:48:45 +0100 |
child | 3230:ba0d444b64aa |
comparison
equal
deleted
inserted
replaced
3228:bada43f3a546 | 3229:e8963e328b26 |
---|---|
1 local st = require "util.stanza" | |
2 | |
3 local mod_pep = module:depends "pep"; | |
4 local private_storage = module:open_store("private", "map"); | |
5 | |
6 module:hook("account-disco-info", function (event) | |
7 event.reply:tag("feature", { var = "urn:xmpp:bookmarks-conversion:0" }):up(); | |
8 end); | |
9 | |
10 local function on_retrieve_private_xml(event) | |
11 local stanza, session = event.stanza, event.origin; | |
12 local query = stanza:get_child("query", "jabber:iq:private"); | |
13 if query == nil then | |
14 return; | |
15 end | |
16 | |
17 local bookmarks = query:get_child("storage", "storage:bookmarks"); | |
18 if bookmarks == nil then | |
19 return; | |
20 end | |
21 | |
22 module:log("debug", "Getting private %s", bookmarks); | |
23 | |
24 local username = session.username; | |
25 local service = mod_pep.get_pep_service(username); | |
26 module:log("debug", "%s", session.full_jid); | |
27 local ok, id = service:get_last_item("storage:bookmarks", session.full_jid); | |
28 if not ok then | |
29 module:log("error", "Failed to PEP bookmarks’ last id of %s: %s", username, id); | |
30 session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to retrive bookmarks from PEP")); | |
31 return; | |
32 end | |
33 | |
34 local ok, data = service:get_items("storage:bookmarks", session.full_jid, id); | |
35 if not ok then | |
36 module:log("error", "Failed to retrieve PEP bookmarks of %s: %s", username, data); | |
37 session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to retrive bookmarks from PEP")); | |
38 return; | |
39 end | |
40 | |
41 local item = data[id]; | |
42 local content = item.tags[1]; | |
43 module:log("debug", "Sending back private for %s: %s", username, content); | |
44 session.send(st.reply(stanza):query("jabber:iq:private"):add_child(content)); | |
45 return true; | |
46 end | |
47 | |
48 local function publish_to_pep(username, jid, bookmarks) | |
49 local service = mod_pep.get_pep_service(username); | |
50 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" }) | |
51 :add_child(bookmarks); | |
52 return service:publish("storage:bookmarks", jid, "current", item); | |
53 end | |
54 | |
55 -- Synchronise Private XML to PEP. | |
56 local function on_publish_private_xml(event) | |
57 local stanza, session = event.stanza, event.origin; | |
58 local query = stanza:get_child("query", "jabber:iq:private"); | |
59 if query == nil then | |
60 return; | |
61 end | |
62 | |
63 local bookmarks = query:get_child("storage", "storage:bookmarks"); | |
64 if bookmarks == nil then | |
65 return; | |
66 end | |
67 | |
68 module:log("debug", "Private bookmarks set by client, publishing to pep"); | |
69 local ok, err = publish_to_pep(session.username, session.full_jid, bookmarks); | |
70 if not ok then | |
71 module:log("error", "Failed to publish to PEP bookmarks for %s: %s", session.username, err); | |
72 session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP")); | |
73 return; | |
74 end | |
75 | |
76 session.send(st.reply(stanza)); | |
77 return true; | |
78 end | |
79 | |
80 local function on_resource_bind(event) | |
81 local session = event.session; | |
82 local username = session.username; | |
83 | |
84 local data, err = private_storage:get(username, "storage:storage:bookmarks"); | |
85 if not data then | |
86 module:log("debug", "No existing Private XML bookmarks for %s, migration already done: %s", username, err); | |
87 return; | |
88 end | |
89 local bookmarks = st.deserialize(data); | |
90 module:log("debug", "Got private bookmarks of %s: %s", username, bookmarks); | |
91 | |
92 module:log("debug", "Going to store PEP item for %s", username); | |
93 local ok, err = publish_to_pep(username, session.host, bookmarks); | |
94 if not ok then | |
95 module:log("error", "Failed to store bookmarks to PEP for %s, aborting migration: %s", username, err); | |
96 return; | |
97 end | |
98 module:log("debug", "Stored bookmarks to PEP for %s", username); | |
99 | |
100 local ok, err = private_storage:set(username, "storage:storage:bookmarks", nil); | |
101 if not ok then | |
102 module:log("error", "Failed to remove private bookmarks of %s: %s", username, err); | |
103 return; | |
104 end | |
105 module:log("debug", "Removed private bookmarks of %s, migration done!", username); | |
106 end | |
107 | |
108 module:hook("iq-get/bare/jabber:iq:private:query", on_retrieve_private_xml) | |
109 module:hook("iq-set/bare/jabber:iq:private:query", on_publish_private_xml) | |
110 module:hook("resource-bind", on_resource_bind) |