Mercurial > prosody-modules
comparison mod_storage_mongodb/mod_storage_mongodb.lua @ 1324:853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 28 Feb 2014 15:36:06 +0100 |
parents | fd420237a5e4 |
children | b21236b6b8d8 |
comparison
equal
deleted
inserted
replaced
1323:c84ff82658cb | 1324:853a382c9bd6 |
---|---|
1 local next = next; | 1 local next = next; |
2 local setmetatable = setmetatable; | 2 local setmetatable = setmetatable; |
3 local set = require"util.set"; | |
4 local it = require"util.iterators"; | |
5 local array = require"util.array"; | |
3 | 6 |
4 local params = assert ( module:get_option("mongodb") , "mongodb configuration not found" ); | 7 local params = assert ( module:get_option("mongodb") , "mongodb configuration not found" ); |
5 | 8 |
6 prosody.unlock_globals(); | 9 prosody.unlock_globals(); |
7 local mongo = require "mongo"; | 10 local mongo = require "mongo"; |
44 else -- delete data | 47 else -- delete data |
45 return conn:remove ( namespace , v ); | 48 return conn:remove ( namespace , v ); |
46 end; | 49 end; |
47 end | 50 end |
48 | 51 |
52 local roster_store = {}; | |
53 roster_store.__index = roster_store; | |
54 | |
55 function roster_store:get(username) | |
56 local host = module.host or "_global"; | |
57 local store = self.store; | |
58 | |
59 -- The database name can't have a period in it (hence it can't be a host/ip) | |
60 local namespace = params.dbname .. "." .. host; | |
61 local v = { _id = { store = store ; username = username } }; | |
62 | |
63 local cursor , err = conn:query ( namespace , v ); | |
64 if not cursor then return nil , err end; | |
65 | |
66 local r , err = cursor:next ( ); | |
67 if not r then return nil , err end; | |
68 local roster = { | |
69 [false] = { | |
70 version = r.version; | |
71 }; | |
72 pending = set.new( r.pending )._items; | |
73 }; | |
74 local items = r.items; | |
75 for i = 1, #items do | |
76 local item = items[i]; | |
77 roster[item.jid] = { | |
78 subscription = item.subscription; | |
79 groups = set.new( item.groups )._items; | |
80 ask = item.ask; | |
81 name = item.name; | |
82 } | |
83 end | |
84 return roster; | |
85 end | |
86 | |
87 function roster_store:set(username, data) | |
88 local host = module.host or "_global"; | |
89 local store = self.store; | |
90 | |
91 -- The database name can't have a period in it (hence it can't be a host/ip) | |
92 local namespace = params.dbname .. "." .. host; | |
93 local v = { _id = { store = store ; username = username } }; | |
94 | |
95 if data == nil or next(data) == nil then -- delete data | |
96 return conn:remove ( namespace , v ); | |
97 end | |
98 | |
99 v.version = data[false].version | |
100 if data.pending then | |
101 v.pending = array(it.keys(v.pending)) | |
102 end | |
103 | |
104 local items = {} | |
105 for jid, item in pairs(data) do | |
106 if jid and jid ~= "pending" then | |
107 table.insert(items, { | |
108 jid = jid; | |
109 subscription = item.subscription; | |
110 groups = array(it.keys( item.groups )); | |
111 name = item.name; | |
112 ask = item.ask; | |
113 }); | |
114 end | |
115 end | |
116 v.items = items; | |
117 | |
118 return conn:insert ( namespace , v ); | |
119 end | |
120 | |
49 local driver = {}; | 121 local driver = {}; |
50 | 122 |
51 function driver:open(store, typ) | 123 function driver:open(store, typ) |
52 if not conn then | 124 if not conn then |
53 conn = assert ( mongo.Connection.New ( true ) ); | 125 conn = assert ( mongo.Connection.New ( true ) ); |
56 assert ( conn:auth ( params ) ); | 128 assert ( conn:auth ( params ) ); |
57 end | 129 end |
58 end | 130 end |
59 | 131 |
60 if not typ then -- default key-value store | 132 if not typ then -- default key-value store |
133 if store == "roster" then | |
134 return setmetatable({ store = store }, roster_store); | |
135 end | |
61 return setmetatable({ store = store }, keyval_store); | 136 return setmetatable({ store = store }, keyval_store); |
62 end; | 137 end; |
63 return nil, "unsupported-store"; | 138 return nil, "unsupported-store"; |
64 end | 139 end |
65 | 140 |