mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-10-01 11:49:51 -04:00
Add a cache for getting the presence list for a user
This commit is contained in:
parent
36317f3dad
commit
1a9a9abcc7
@ -521,20 +521,26 @@ class PresenceHandler(BaseHandler):
|
|||||||
if not self.hs.is_mine(observer_user):
|
if not self.hs.is_mine(observer_user):
|
||||||
raise SynapseError(400, "User is not hosted on this Home Server")
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
||||||
|
|
||||||
presence = yield self.store.get_presence_list(
|
presence_list = yield self.store.get_presence_list(
|
||||||
observer_user.localpart, accepted=accepted
|
observer_user.localpart, accepted=accepted
|
||||||
)
|
)
|
||||||
|
|
||||||
for p in presence:
|
results = []
|
||||||
observed_user = UserID.from_string(p.pop("observed_user_id"))
|
for row in presence_list:
|
||||||
p["observed_user"] = observed_user
|
observed_user = UserID.from_string(row["observed_user_id"])
|
||||||
p.update(self._get_or_offline_usercache(observed_user).get_state())
|
result = {
|
||||||
if "last_active" in p:
|
"observed_user": observed_user, "accepted": row["accepted"]
|
||||||
p["last_active_ago"] = int(
|
}
|
||||||
self.clock.time_msec() - p.pop("last_active")
|
result.update(
|
||||||
|
self._get_or_offline_usercache(observed_user).get_state()
|
||||||
)
|
)
|
||||||
|
if "last_active" in result:
|
||||||
|
result["last_active_ago"] = int(
|
||||||
|
self.clock.time_msec() - result.pop("last_active")
|
||||||
|
)
|
||||||
|
results.append(result)
|
||||||
|
|
||||||
defer.returnValue(presence)
|
defer.returnValue(results)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
@log_function
|
@log_function
|
||||||
|
@ -13,7 +13,9 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from ._base import SQLBaseStore
|
from ._base import SQLBaseStore, cached
|
||||||
|
|
||||||
|
from twisted.internet import defer
|
||||||
|
|
||||||
|
|
||||||
class PresenceStore(SQLBaseStore):
|
class PresenceStore(SQLBaseStore):
|
||||||
@ -87,16 +89,22 @@ class PresenceStore(SQLBaseStore):
|
|||||||
desc="add_presence_list_pending",
|
desc="add_presence_list_pending",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
def set_presence_list_accepted(self, observer_localpart, observed_userid):
|
def set_presence_list_accepted(self, observer_localpart, observed_userid):
|
||||||
return self._simple_update_one(
|
result = yield self._simple_update_one(
|
||||||
table="presence_list",
|
table="presence_list",
|
||||||
keyvalues={"user_id": observer_localpart,
|
keyvalues={"user_id": observer_localpart,
|
||||||
"observed_user_id": observed_userid},
|
"observed_user_id": observed_userid},
|
||||||
updatevalues={"accepted": True},
|
updatevalues={"accepted": True},
|
||||||
desc="set_presence_list_accepted",
|
desc="set_presence_list_accepted",
|
||||||
)
|
)
|
||||||
|
self.get_presence_list_accepted.invalidate(observer_localpart)
|
||||||
|
defer.returnValue(result)
|
||||||
|
|
||||||
def get_presence_list(self, observer_localpart, accepted=None):
|
def get_presence_list(self, observer_localpart, accepted=None):
|
||||||
|
if accepted:
|
||||||
|
return self.get_presence_list_accepted(observer_localpart)
|
||||||
|
else:
|
||||||
keyvalues = {"user_id": observer_localpart}
|
keyvalues = {"user_id": observer_localpart}
|
||||||
if accepted is not None:
|
if accepted is not None:
|
||||||
keyvalues["accepted"] = accepted
|
keyvalues["accepted"] = accepted
|
||||||
@ -108,10 +116,21 @@ class PresenceStore(SQLBaseStore):
|
|||||||
desc="get_presence_list",
|
desc="get_presence_list",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@cached()
|
||||||
|
def get_presence_list_accepted(self, observer_localpart):
|
||||||
|
return self._simple_select_list(
|
||||||
|
table="presence_list",
|
||||||
|
keyvalues={"user_id": observer_localpart, "accepted": True},
|
||||||
|
retcols=["observed_user_id", "accepted"],
|
||||||
|
desc="get_presence_list_accepted",
|
||||||
|
)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
def del_presence_list(self, observer_localpart, observed_userid):
|
def del_presence_list(self, observer_localpart, observed_userid):
|
||||||
return self._simple_delete_one(
|
yield self._simple_delete_one(
|
||||||
table="presence_list",
|
table="presence_list",
|
||||||
keyvalues={"user_id": observer_localpart,
|
keyvalues={"user_id": observer_localpart,
|
||||||
"observed_user_id": observed_userid},
|
"observed_user_id": observed_userid},
|
||||||
desc="del_presence_list",
|
desc="del_presence_list",
|
||||||
)
|
)
|
||||||
|
self.get_presence_list_accepted.invalidate(observer_localpart)
|
||||||
|
Loading…
Reference in New Issue
Block a user