Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #226 from matrix-org/erikj/room_presence
Browse files Browse the repository at this point in the history
Add and use cached batched storage.get_state function.
  • Loading branch information
erikjohnston committed Aug 18, 2015
2 parents 8899df1 + 83eb627 commit dc2a105
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 15 deletions.
18 changes: 6 additions & 12 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,20 +460,14 @@ def room_initial_sync(self, user_id, room_id, pagin_config=None,

@defer.inlineCallbacks
def get_presence():
presence_defs = yield defer.DeferredList(
[
presence_handler.get_state(
target_user=UserID.from_string(m.user_id),
auth_user=auth_user,
as_event=True,
check_auth=False,
)
for m in room_members
],
consumeErrors=True,
states = yield presence_handler.get_states(
target_users=[UserID.from_string(m.user_id) for m in room_members],
auth_user=auth_user,
as_event=True,
check_auth=False,
)

defer.returnValue([p for success, p in presence_defs if success])
defer.returnValue(states.values())

receipts_handler = self.hs.get_handlers().receipts_handler

Expand Down
87 changes: 87 additions & 0 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ def is_presence_visible(self, observer_user, observed_user):

@defer.inlineCallbacks
def get_state(self, target_user, auth_user, as_event=False, check_auth=True):
"""Get the current presence state of the given user.
Args:
target_user (UserID): The user whose presence we want
auth_user (UserID): The user requesting the presence, used for
checking if said user is allowed to see the persence of the
`target_user`
as_event (bool): Format the return as an event or not?
check_auth (bool): Perform the auth checks or not?
Returns:
dict: The presence state of the `target_user`, whose format depends
on the `as_event` argument.
"""
if self.hs.is_mine(target_user):
if check_auth:
visible = yield self.is_presence_visible(
Expand Down Expand Up @@ -232,6 +246,79 @@ def get_state(self, target_user, auth_user, as_event=False, check_auth=True):
else:
defer.returnValue(state)

@defer.inlineCallbacks
def get_states(self, target_users, auth_user, as_event=False, check_auth=True):
"""A batched version of the `get_state` method that accepts a list of
`target_users`
Args:
target_users (list): The list of UserID's whose presence we want
auth_user (UserID): The user requesting the presence, used for
checking if said user is allowed to see the persence of the
`target_users`
as_event (bool): Format the return as an event or not?
check_auth (bool): Perform the auth checks or not?
Returns:
dict: A mapping from user -> presence_state
"""
local_users, remote_users = partitionbool(
target_users,
lambda u: self.hs.is_mine(u)
)

if check_auth:
for user in local_users:
visible = yield self.is_presence_visible(
observer_user=auth_user,
observed_user=user
)

if not visible:
raise SynapseError(404, "Presence information not visible")

results = {}
if local_users:
for user in local_users:
if user in self._user_cachemap:
results[user] = self._user_cachemap[user].get_state()

local_to_user = {u.localpart: u for u in local_users}

states = yield self.store.get_presence_states(
[u.localpart for u in local_users if u not in results]
)

for local_part, state in states.items():
res = {"presence": state["state"]}
if "status_msg" in state and state["status_msg"]:
res["status_msg"] = state["status_msg"]
results[local_to_user[local_part]] = res

for user in remote_users:
# TODO(paul): Have remote server send us permissions set
results[user] = self._get_or_offline_usercache(user).get_state()

for state in results.values():
if "last_active" in state:
state["last_active_ago"] = int(
self.clock.time_msec() - state.pop("last_active")
)

if as_event:
for user, state in results.items():
content = state
content["user_id"] = user.to_string()

if "last_active" in content:
content["last_active_ago"] = int(
self._clock.time_msec() - content.pop("last_active")
)

results[user] = {"type": "m.presence", "content": content}

defer.returnValue(results)

@defer.inlineCallbacks
@log_function
def set_state(self, target_user, auth_user, state):
Expand Down
32 changes: 29 additions & 3 deletions synapse/storage/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
# limitations under the License.

from ._base import SQLBaseStore
from synapse.util.caches.descriptors import cached
from synapse.util.caches.descriptors import cached, cachedList

from twisted.internet import defer


class PresenceStore(SQLBaseStore):
def create_presence(self, user_localpart):
return self._simple_insert(
res = self._simple_insert(
table="presence",
values={"user_id": user_localpart},
desc="create_presence",
)

self.get_presence_state.invalidate((user_localpart,))
return res

def has_presence_state(self, user_localpart):
return self._simple_select_one(
table="presence",
Expand All @@ -36,6 +39,7 @@ def has_presence_state(self, user_localpart):
desc="has_presence_state",
)

@cached()
def get_presence_state(self, user_localpart):
return self._simple_select_one(
table="presence",
Expand All @@ -44,8 +48,27 @@ def get_presence_state(self, user_localpart):
desc="get_presence_state",
)

@cachedList(get_presence_state.cache, list_name="user_localparts")
def get_presence_states(self, user_localparts):
def f(txn):
results = {}
for user_localpart in user_localparts:
res = self._simple_select_one_txn(
txn,
table="presence",
keyvalues={"user_id": user_localpart},
retcols=["state", "status_msg", "mtime"],
allow_none=True,
)
if res:
results[user_localpart] = res

return results

return self.runInteraction("get_presence_states", f)

def set_presence_state(self, user_localpart, new_state):
return self._simple_update_one(
res = self._simple_update_one(
table="presence",
keyvalues={"user_id": user_localpart},
updatevalues={"state": new_state["state"],
Expand All @@ -54,6 +77,9 @@ def set_presence_state(self, user_localpart, new_state):
desc="set_presence_state",
)

self.get_presence_state.invalidate((user_localpart,))
return res

def allow_presence_visible(self, observed_localpart, observer_userid):
return self._simple_insert(
table="presence_allow_inbound",
Expand Down

0 comments on commit dc2a105

Please sign in to comment.