Merge 0.10->trunk
[prosody.git] / util / presence.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local t_insert = table.insert;
10
11 local function select_top_resources(user)
12         local priority = 0;
13         local recipients = {};
14         for _, session in pairs(user.sessions) do -- find resource with greatest priority
15                 if session.presence then
16                         -- TODO check active privacy list for session
17                         local p = session.priority;
18                         if p > priority then
19                                 priority = p;
20                                 recipients = {session};
21                         elseif p == priority then
22                                 t_insert(recipients, session);
23                         end
24                 end
25         end
26         return recipients;
27 end
28 local function recalc_resource_map(user)
29         if user then
30                 user.top_resources = select_top_resources(user);
31                 if #user.top_resources == 0 then user.top_resources = nil; end
32         end
33 end
34
35 return {
36         select_top_resources = select_top_resources;
37         recalc_resource_map = recalc_resource_map;
38 }