test_presence.py 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Optional
  15. from unittest.mock import Mock, call
  16. from signedjson.key import generate_signing_key
  17. from synapse.api.constants import EventTypes, Membership, PresenceState
  18. from synapse.api.presence import UserPresenceState
  19. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  20. from synapse.events.builder import EventBuilder
  21. from synapse.federation.sender import FederationSender
  22. from synapse.handlers.presence import (
  23. EXTERNAL_PROCESS_EXPIRY,
  24. FEDERATION_PING_INTERVAL,
  25. FEDERATION_TIMEOUT,
  26. IDLE_TIMER,
  27. LAST_ACTIVE_GRANULARITY,
  28. SYNC_ONLINE_TIMEOUT,
  29. handle_timeout,
  30. handle_update,
  31. )
  32. from synapse.rest import admin
  33. from synapse.rest.client import room
  34. from synapse.types import UserID, get_domain_from_id
  35. from tests import unittest
  36. class PresenceUpdateTestCase(unittest.HomeserverTestCase):
  37. servlets = [admin.register_servlets]
  38. def prepare(self, reactor, clock, homeserver):
  39. self.store = homeserver.get_datastore()
  40. def test_offline_to_online(self):
  41. wheel_timer = Mock()
  42. user_id = "@foo:bar"
  43. now = 5000000
  44. prev_state = UserPresenceState.default(user_id)
  45. new_state = prev_state.copy_and_replace(
  46. state=PresenceState.ONLINE, last_active_ts=now
  47. )
  48. state, persist_and_notify, federation_ping = handle_update(
  49. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  50. )
  51. self.assertTrue(persist_and_notify)
  52. self.assertTrue(state.currently_active)
  53. self.assertEquals(new_state.state, state.state)
  54. self.assertEquals(new_state.status_msg, state.status_msg)
  55. self.assertEquals(state.last_federation_update_ts, now)
  56. self.assertEquals(wheel_timer.insert.call_count, 3)
  57. wheel_timer.insert.assert_has_calls(
  58. [
  59. call(now=now, obj=user_id, then=new_state.last_active_ts + IDLE_TIMER),
  60. call(
  61. now=now,
  62. obj=user_id,
  63. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
  64. ),
  65. call(
  66. now=now,
  67. obj=user_id,
  68. then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY,
  69. ),
  70. ],
  71. any_order=True,
  72. )
  73. def test_online_to_online(self):
  74. wheel_timer = Mock()
  75. user_id = "@foo:bar"
  76. now = 5000000
  77. prev_state = UserPresenceState.default(user_id)
  78. prev_state = prev_state.copy_and_replace(
  79. state=PresenceState.ONLINE, last_active_ts=now, currently_active=True
  80. )
  81. new_state = prev_state.copy_and_replace(
  82. state=PresenceState.ONLINE, last_active_ts=now
  83. )
  84. state, persist_and_notify, federation_ping = handle_update(
  85. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  86. )
  87. self.assertFalse(persist_and_notify)
  88. self.assertTrue(federation_ping)
  89. self.assertTrue(state.currently_active)
  90. self.assertEquals(new_state.state, state.state)
  91. self.assertEquals(new_state.status_msg, state.status_msg)
  92. self.assertEquals(state.last_federation_update_ts, now)
  93. self.assertEquals(wheel_timer.insert.call_count, 3)
  94. wheel_timer.insert.assert_has_calls(
  95. [
  96. call(now=now, obj=user_id, then=new_state.last_active_ts + IDLE_TIMER),
  97. call(
  98. now=now,
  99. obj=user_id,
  100. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
  101. ),
  102. call(
  103. now=now,
  104. obj=user_id,
  105. then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY,
  106. ),
  107. ],
  108. any_order=True,
  109. )
  110. def test_online_to_online_last_active_noop(self):
  111. wheel_timer = Mock()
  112. user_id = "@foo:bar"
  113. now = 5000000
  114. prev_state = UserPresenceState.default(user_id)
  115. prev_state = prev_state.copy_and_replace(
  116. state=PresenceState.ONLINE,
  117. last_active_ts=now - LAST_ACTIVE_GRANULARITY - 10,
  118. currently_active=True,
  119. )
  120. new_state = prev_state.copy_and_replace(
  121. state=PresenceState.ONLINE, last_active_ts=now
  122. )
  123. state, persist_and_notify, federation_ping = handle_update(
  124. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  125. )
  126. self.assertFalse(persist_and_notify)
  127. self.assertTrue(federation_ping)
  128. self.assertTrue(state.currently_active)
  129. self.assertEquals(new_state.state, state.state)
  130. self.assertEquals(new_state.status_msg, state.status_msg)
  131. self.assertEquals(state.last_federation_update_ts, now)
  132. self.assertEquals(wheel_timer.insert.call_count, 3)
  133. wheel_timer.insert.assert_has_calls(
  134. [
  135. call(now=now, obj=user_id, then=new_state.last_active_ts + IDLE_TIMER),
  136. call(
  137. now=now,
  138. obj=user_id,
  139. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
  140. ),
  141. call(
  142. now=now,
  143. obj=user_id,
  144. then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY,
  145. ),
  146. ],
  147. any_order=True,
  148. )
  149. def test_online_to_online_last_active(self):
  150. wheel_timer = Mock()
  151. user_id = "@foo:bar"
  152. now = 5000000
  153. prev_state = UserPresenceState.default(user_id)
  154. prev_state = prev_state.copy_and_replace(
  155. state=PresenceState.ONLINE,
  156. last_active_ts=now - LAST_ACTIVE_GRANULARITY - 1,
  157. currently_active=True,
  158. )
  159. new_state = prev_state.copy_and_replace(state=PresenceState.ONLINE)
  160. state, persist_and_notify, federation_ping = handle_update(
  161. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  162. )
  163. self.assertTrue(persist_and_notify)
  164. self.assertFalse(state.currently_active)
  165. self.assertEquals(new_state.state, state.state)
  166. self.assertEquals(new_state.status_msg, state.status_msg)
  167. self.assertEquals(state.last_federation_update_ts, now)
  168. self.assertEquals(wheel_timer.insert.call_count, 2)
  169. wheel_timer.insert.assert_has_calls(
  170. [
  171. call(now=now, obj=user_id, then=new_state.last_active_ts + IDLE_TIMER),
  172. call(
  173. now=now,
  174. obj=user_id,
  175. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
  176. ),
  177. ],
  178. any_order=True,
  179. )
  180. def test_remote_ping_timer(self):
  181. wheel_timer = Mock()
  182. user_id = "@foo:bar"
  183. now = 5000000
  184. prev_state = UserPresenceState.default(user_id)
  185. prev_state = prev_state.copy_and_replace(
  186. state=PresenceState.ONLINE, last_active_ts=now
  187. )
  188. new_state = prev_state.copy_and_replace(state=PresenceState.ONLINE)
  189. state, persist_and_notify, federation_ping = handle_update(
  190. prev_state, new_state, is_mine=False, wheel_timer=wheel_timer, now=now
  191. )
  192. self.assertFalse(persist_and_notify)
  193. self.assertFalse(federation_ping)
  194. self.assertFalse(state.currently_active)
  195. self.assertEquals(new_state.state, state.state)
  196. self.assertEquals(new_state.status_msg, state.status_msg)
  197. self.assertEquals(wheel_timer.insert.call_count, 1)
  198. wheel_timer.insert.assert_has_calls(
  199. [
  200. call(
  201. now=now,
  202. obj=user_id,
  203. then=new_state.last_federation_update_ts + FEDERATION_TIMEOUT,
  204. )
  205. ],
  206. any_order=True,
  207. )
  208. def test_online_to_offline(self):
  209. wheel_timer = Mock()
  210. user_id = "@foo:bar"
  211. now = 5000000
  212. prev_state = UserPresenceState.default(user_id)
  213. prev_state = prev_state.copy_and_replace(
  214. state=PresenceState.ONLINE, last_active_ts=now, currently_active=True
  215. )
  216. new_state = prev_state.copy_and_replace(state=PresenceState.OFFLINE)
  217. state, persist_and_notify, federation_ping = handle_update(
  218. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  219. )
  220. self.assertTrue(persist_and_notify)
  221. self.assertEquals(new_state.state, state.state)
  222. self.assertEquals(state.last_federation_update_ts, now)
  223. self.assertEquals(wheel_timer.insert.call_count, 0)
  224. def test_online_to_idle(self):
  225. wheel_timer = Mock()
  226. user_id = "@foo:bar"
  227. now = 5000000
  228. prev_state = UserPresenceState.default(user_id)
  229. prev_state = prev_state.copy_and_replace(
  230. state=PresenceState.ONLINE, last_active_ts=now, currently_active=True
  231. )
  232. new_state = prev_state.copy_and_replace(state=PresenceState.UNAVAILABLE)
  233. state, persist_and_notify, federation_ping = handle_update(
  234. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  235. )
  236. self.assertTrue(persist_and_notify)
  237. self.assertEquals(new_state.state, state.state)
  238. self.assertEquals(state.last_federation_update_ts, now)
  239. self.assertEquals(new_state.state, state.state)
  240. self.assertEquals(new_state.status_msg, state.status_msg)
  241. self.assertEquals(wheel_timer.insert.call_count, 1)
  242. wheel_timer.insert.assert_has_calls(
  243. [
  244. call(
  245. now=now,
  246. obj=user_id,
  247. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
  248. )
  249. ],
  250. any_order=True,
  251. )
  252. def test_persisting_presence_updates(self):
  253. """Tests that the latest presence state for each user is persisted correctly"""
  254. # Create some test users and presence states for them
  255. presence_states = []
  256. for i in range(5):
  257. user_id = self.register_user(f"user_{i}", "password")
  258. presence_state = UserPresenceState(
  259. user_id=user_id,
  260. state="online",
  261. last_active_ts=1,
  262. last_federation_update_ts=1,
  263. last_user_sync_ts=1,
  264. status_msg="I'm online!",
  265. currently_active=True,
  266. )
  267. presence_states.append(presence_state)
  268. # Persist these presence updates to the database
  269. self.get_success(self.store.update_presence(presence_states))
  270. # Check that each update is present in the database
  271. db_presence_states = self.get_success(
  272. self.store.get_all_presence_updates(
  273. instance_name="master",
  274. last_id=0,
  275. current_id=len(presence_states) + 1,
  276. limit=len(presence_states),
  277. )
  278. )
  279. # Extract presence update user ID and state information into lists of tuples
  280. db_presence_states = [(ps[0], ps[1]) for _, ps in db_presence_states[0]]
  281. presence_states = [(ps.user_id, ps.state) for ps in presence_states]
  282. # Compare what we put into the storage with what we got out.
  283. # They should be identical.
  284. self.assertEqual(presence_states, db_presence_states)
  285. class PresenceTimeoutTestCase(unittest.TestCase):
  286. """Tests different timers and that the timer does not change `status_msg` of user."""
  287. def test_idle_timer(self):
  288. user_id = "@foo:bar"
  289. status_msg = "I'm here!"
  290. now = 5000000
  291. state = UserPresenceState.default(user_id)
  292. state = state.copy_and_replace(
  293. state=PresenceState.ONLINE,
  294. last_active_ts=now - IDLE_TIMER - 1,
  295. last_user_sync_ts=now,
  296. status_msg=status_msg,
  297. )
  298. new_state = handle_timeout(state, is_mine=True, syncing_user_ids=set(), now=now)
  299. self.assertIsNotNone(new_state)
  300. self.assertEquals(new_state.state, PresenceState.UNAVAILABLE)
  301. self.assertEquals(new_state.status_msg, status_msg)
  302. def test_busy_no_idle(self):
  303. """
  304. Tests that a user setting their presence to busy but idling doesn't turn their
  305. presence state into unavailable.
  306. """
  307. user_id = "@foo:bar"
  308. status_msg = "I'm here!"
  309. now = 5000000
  310. state = UserPresenceState.default(user_id)
  311. state = state.copy_and_replace(
  312. state=PresenceState.BUSY,
  313. last_active_ts=now - IDLE_TIMER - 1,
  314. last_user_sync_ts=now,
  315. status_msg=status_msg,
  316. )
  317. new_state = handle_timeout(state, is_mine=True, syncing_user_ids=set(), now=now)
  318. self.assertIsNotNone(new_state)
  319. self.assertEquals(new_state.state, PresenceState.BUSY)
  320. self.assertEquals(new_state.status_msg, status_msg)
  321. def test_sync_timeout(self):
  322. user_id = "@foo:bar"
  323. status_msg = "I'm here!"
  324. now = 5000000
  325. state = UserPresenceState.default(user_id)
  326. state = state.copy_and_replace(
  327. state=PresenceState.ONLINE,
  328. last_active_ts=0,
  329. last_user_sync_ts=now - SYNC_ONLINE_TIMEOUT - 1,
  330. status_msg=status_msg,
  331. )
  332. new_state = handle_timeout(state, is_mine=True, syncing_user_ids=set(), now=now)
  333. self.assertIsNotNone(new_state)
  334. self.assertEquals(new_state.state, PresenceState.OFFLINE)
  335. self.assertEquals(new_state.status_msg, status_msg)
  336. def test_sync_online(self):
  337. user_id = "@foo:bar"
  338. status_msg = "I'm here!"
  339. now = 5000000
  340. state = UserPresenceState.default(user_id)
  341. state = state.copy_and_replace(
  342. state=PresenceState.ONLINE,
  343. last_active_ts=now - SYNC_ONLINE_TIMEOUT - 1,
  344. last_user_sync_ts=now - SYNC_ONLINE_TIMEOUT - 1,
  345. status_msg=status_msg,
  346. )
  347. new_state = handle_timeout(
  348. state, is_mine=True, syncing_user_ids={user_id}, now=now
  349. )
  350. self.assertIsNotNone(new_state)
  351. self.assertEquals(new_state.state, PresenceState.ONLINE)
  352. self.assertEquals(new_state.status_msg, status_msg)
  353. def test_federation_ping(self):
  354. user_id = "@foo:bar"
  355. status_msg = "I'm here!"
  356. now = 5000000
  357. state = UserPresenceState.default(user_id)
  358. state = state.copy_and_replace(
  359. state=PresenceState.ONLINE,
  360. last_active_ts=now,
  361. last_user_sync_ts=now,
  362. last_federation_update_ts=now - FEDERATION_PING_INTERVAL - 1,
  363. status_msg=status_msg,
  364. )
  365. new_state = handle_timeout(state, is_mine=True, syncing_user_ids=set(), now=now)
  366. self.assertIsNotNone(new_state)
  367. self.assertEquals(state, new_state)
  368. def test_no_timeout(self):
  369. user_id = "@foo:bar"
  370. now = 5000000
  371. state = UserPresenceState.default(user_id)
  372. state = state.copy_and_replace(
  373. state=PresenceState.ONLINE,
  374. last_active_ts=now,
  375. last_user_sync_ts=now,
  376. last_federation_update_ts=now,
  377. )
  378. new_state = handle_timeout(state, is_mine=True, syncing_user_ids=set(), now=now)
  379. self.assertIsNone(new_state)
  380. def test_federation_timeout(self):
  381. user_id = "@foo:bar"
  382. status_msg = "I'm here!"
  383. now = 5000000
  384. state = UserPresenceState.default(user_id)
  385. state = state.copy_and_replace(
  386. state=PresenceState.ONLINE,
  387. last_active_ts=now,
  388. last_user_sync_ts=now,
  389. last_federation_update_ts=now - FEDERATION_TIMEOUT - 1,
  390. status_msg=status_msg,
  391. )
  392. new_state = handle_timeout(
  393. state, is_mine=False, syncing_user_ids=set(), now=now
  394. )
  395. self.assertIsNotNone(new_state)
  396. self.assertEquals(new_state.state, PresenceState.OFFLINE)
  397. self.assertEquals(new_state.status_msg, status_msg)
  398. def test_last_active(self):
  399. user_id = "@foo:bar"
  400. status_msg = "I'm here!"
  401. now = 5000000
  402. state = UserPresenceState.default(user_id)
  403. state = state.copy_and_replace(
  404. state=PresenceState.ONLINE,
  405. last_active_ts=now - LAST_ACTIVE_GRANULARITY - 1,
  406. last_user_sync_ts=now,
  407. last_federation_update_ts=now,
  408. status_msg=status_msg,
  409. )
  410. new_state = handle_timeout(state, is_mine=True, syncing_user_ids=set(), now=now)
  411. self.assertIsNotNone(new_state)
  412. self.assertEquals(state, new_state)
  413. class PresenceHandlerTestCase(unittest.HomeserverTestCase):
  414. def prepare(self, reactor, clock, hs):
  415. self.presence_handler = hs.get_presence_handler()
  416. self.clock = hs.get_clock()
  417. def test_external_process_timeout(self):
  418. """Test that if an external process doesn't update the records for a while
  419. we time out their syncing users presence.
  420. """
  421. process_id = 1
  422. user_id = "@test:server"
  423. # Notify handler that a user is now syncing.
  424. self.get_success(
  425. self.presence_handler.update_external_syncs_row(
  426. process_id, user_id, True, self.clock.time_msec()
  427. )
  428. )
  429. # Check that if we wait a while without telling the handler the user has
  430. # stopped syncing that their presence state doesn't get timed out.
  431. self.reactor.advance(EXTERNAL_PROCESS_EXPIRY / 2)
  432. state = self.get_success(
  433. self.presence_handler.get_state(UserID.from_string(user_id))
  434. )
  435. self.assertEqual(state.state, PresenceState.ONLINE)
  436. # Check that if the external process timeout fires, then the syncing
  437. # user gets timed out
  438. self.reactor.advance(EXTERNAL_PROCESS_EXPIRY)
  439. state = self.get_success(
  440. self.presence_handler.get_state(UserID.from_string(user_id))
  441. )
  442. self.assertEqual(state.state, PresenceState.OFFLINE)
  443. def test_user_goes_offline_by_timeout_status_msg_remain(self):
  444. """Test that if a user doesn't update the records for a while
  445. users presence goes `OFFLINE` because of timeout and `status_msg` remains.
  446. """
  447. user_id = "@test:server"
  448. status_msg = "I'm here!"
  449. # Mark user as online
  450. self._set_presencestate_with_status_msg(
  451. user_id, PresenceState.ONLINE, status_msg
  452. )
  453. # Check that if we wait a while without telling the handler the user has
  454. # stopped syncing that their presence state doesn't get timed out.
  455. self.reactor.advance(SYNC_ONLINE_TIMEOUT / 2)
  456. state = self.get_success(
  457. self.presence_handler.get_state(UserID.from_string(user_id))
  458. )
  459. self.assertEqual(state.state, PresenceState.ONLINE)
  460. self.assertEqual(state.status_msg, status_msg)
  461. # Check that if the timeout fires, then the syncing user gets timed out
  462. self.reactor.advance(SYNC_ONLINE_TIMEOUT)
  463. state = self.get_success(
  464. self.presence_handler.get_state(UserID.from_string(user_id))
  465. )
  466. # status_msg should remain even after going offline
  467. self.assertEqual(state.state, PresenceState.OFFLINE)
  468. self.assertEqual(state.status_msg, status_msg)
  469. def test_user_goes_offline_manually_with_no_status_msg(self):
  470. """Test that if a user change presence manually to `OFFLINE`
  471. and no status is set, that `status_msg` is `None`.
  472. """
  473. user_id = "@test:server"
  474. status_msg = "I'm here!"
  475. # Mark user as online
  476. self._set_presencestate_with_status_msg(
  477. user_id, PresenceState.ONLINE, status_msg
  478. )
  479. # Mark user as offline
  480. self.get_success(
  481. self.presence_handler.set_state(
  482. UserID.from_string(user_id), {"presence": PresenceState.OFFLINE}
  483. )
  484. )
  485. state = self.get_success(
  486. self.presence_handler.get_state(UserID.from_string(user_id))
  487. )
  488. self.assertEqual(state.state, PresenceState.OFFLINE)
  489. self.assertEqual(state.status_msg, None)
  490. def test_user_goes_offline_manually_with_status_msg(self):
  491. """Test that if a user change presence manually to `OFFLINE`
  492. and a status is set, that `status_msg` appears.
  493. """
  494. user_id = "@test:server"
  495. status_msg = "I'm here!"
  496. # Mark user as online
  497. self._set_presencestate_with_status_msg(
  498. user_id, PresenceState.ONLINE, status_msg
  499. )
  500. # Mark user as offline
  501. self._set_presencestate_with_status_msg(
  502. user_id, PresenceState.OFFLINE, "And now here."
  503. )
  504. def test_user_reset_online_with_no_status(self):
  505. """Test that if a user set again the presence manually
  506. and no status is set, that `status_msg` is `None`.
  507. """
  508. user_id = "@test:server"
  509. status_msg = "I'm here!"
  510. # Mark user as online
  511. self._set_presencestate_with_status_msg(
  512. user_id, PresenceState.ONLINE, status_msg
  513. )
  514. # Mark user as online again
  515. self.get_success(
  516. self.presence_handler.set_state(
  517. UserID.from_string(user_id), {"presence": PresenceState.ONLINE}
  518. )
  519. )
  520. state = self.get_success(
  521. self.presence_handler.get_state(UserID.from_string(user_id))
  522. )
  523. # status_msg should remain even after going offline
  524. self.assertEqual(state.state, PresenceState.ONLINE)
  525. self.assertEqual(state.status_msg, None)
  526. def test_set_presence_with_status_msg_none(self):
  527. """Test that if a user set again the presence manually
  528. and status is `None`, that `status_msg` is `None`.
  529. """
  530. user_id = "@test:server"
  531. status_msg = "I'm here!"
  532. # Mark user as online
  533. self._set_presencestate_with_status_msg(
  534. user_id, PresenceState.ONLINE, status_msg
  535. )
  536. # Mark user as online and `status_msg = None`
  537. self._set_presencestate_with_status_msg(user_id, PresenceState.ONLINE, None)
  538. def _set_presencestate_with_status_msg(
  539. self, user_id: str, state: PresenceState, status_msg: Optional[str]
  540. ):
  541. """Set a PresenceState and status_msg and check the result.
  542. Args:
  543. user_id: User for that the status is to be set.
  544. PresenceState: The new PresenceState.
  545. status_msg: Status message that is to be set.
  546. """
  547. self.get_success(
  548. self.presence_handler.set_state(
  549. UserID.from_string(user_id),
  550. {"presence": state, "status_msg": status_msg},
  551. )
  552. )
  553. new_state = self.get_success(
  554. self.presence_handler.get_state(UserID.from_string(user_id))
  555. )
  556. self.assertEqual(new_state.state, state)
  557. self.assertEqual(new_state.status_msg, status_msg)
  558. class PresenceFederationQueueTestCase(unittest.HomeserverTestCase):
  559. def prepare(self, reactor, clock, hs):
  560. self.presence_handler = hs.get_presence_handler()
  561. self.clock = hs.get_clock()
  562. self.instance_name = hs.get_instance_name()
  563. self.queue = self.presence_handler.get_federation_queue()
  564. def test_send_and_get(self):
  565. state1 = UserPresenceState.default("@user1:test")
  566. state2 = UserPresenceState.default("@user2:test")
  567. state3 = UserPresenceState.default("@user3:test")
  568. prev_token = self.queue.get_current_token(self.instance_name)
  569. self.queue.send_presence_to_destinations((state1, state2), ("dest1", "dest2"))
  570. self.queue.send_presence_to_destinations((state3,), ("dest3",))
  571. now_token = self.queue.get_current_token(self.instance_name)
  572. rows, upto_token, limited = self.get_success(
  573. self.queue.get_replication_rows("master", prev_token, now_token, 10)
  574. )
  575. self.assertEqual(upto_token, now_token)
  576. self.assertFalse(limited)
  577. expected_rows = [
  578. (1, ("dest1", "@user1:test")),
  579. (1, ("dest2", "@user1:test")),
  580. (1, ("dest1", "@user2:test")),
  581. (1, ("dest2", "@user2:test")),
  582. (2, ("dest3", "@user3:test")),
  583. ]
  584. self.assertCountEqual(rows, expected_rows)
  585. now_token = self.queue.get_current_token(self.instance_name)
  586. rows, upto_token, limited = self.get_success(
  587. self.queue.get_replication_rows("master", upto_token, now_token, 10)
  588. )
  589. self.assertEqual(upto_token, now_token)
  590. self.assertFalse(limited)
  591. self.assertCountEqual(rows, [])
  592. def test_send_and_get_split(self):
  593. state1 = UserPresenceState.default("@user1:test")
  594. state2 = UserPresenceState.default("@user2:test")
  595. state3 = UserPresenceState.default("@user3:test")
  596. prev_token = self.queue.get_current_token(self.instance_name)
  597. self.queue.send_presence_to_destinations((state1, state2), ("dest1", "dest2"))
  598. now_token = self.queue.get_current_token(self.instance_name)
  599. self.queue.send_presence_to_destinations((state3,), ("dest3",))
  600. rows, upto_token, limited = self.get_success(
  601. self.queue.get_replication_rows("master", prev_token, now_token, 10)
  602. )
  603. self.assertEqual(upto_token, now_token)
  604. self.assertFalse(limited)
  605. expected_rows = [
  606. (1, ("dest1", "@user1:test")),
  607. (1, ("dest2", "@user1:test")),
  608. (1, ("dest1", "@user2:test")),
  609. (1, ("dest2", "@user2:test")),
  610. ]
  611. self.assertCountEqual(rows, expected_rows)
  612. now_token = self.queue.get_current_token(self.instance_name)
  613. rows, upto_token, limited = self.get_success(
  614. self.queue.get_replication_rows("master", upto_token, now_token, 10)
  615. )
  616. self.assertEqual(upto_token, now_token)
  617. self.assertFalse(limited)
  618. expected_rows = [
  619. (2, ("dest3", "@user3:test")),
  620. ]
  621. self.assertCountEqual(rows, expected_rows)
  622. def test_clear_queue_all(self):
  623. state1 = UserPresenceState.default("@user1:test")
  624. state2 = UserPresenceState.default("@user2:test")
  625. state3 = UserPresenceState.default("@user3:test")
  626. prev_token = self.queue.get_current_token(self.instance_name)
  627. self.queue.send_presence_to_destinations((state1, state2), ("dest1", "dest2"))
  628. self.queue.send_presence_to_destinations((state3,), ("dest3",))
  629. self.reactor.advance(10 * 60 * 1000)
  630. now_token = self.queue.get_current_token(self.instance_name)
  631. rows, upto_token, limited = self.get_success(
  632. self.queue.get_replication_rows("master", prev_token, now_token, 10)
  633. )
  634. self.assertEqual(upto_token, now_token)
  635. self.assertFalse(limited)
  636. self.assertCountEqual(rows, [])
  637. prev_token = self.queue.get_current_token(self.instance_name)
  638. self.queue.send_presence_to_destinations((state1, state2), ("dest1", "dest2"))
  639. self.queue.send_presence_to_destinations((state3,), ("dest3",))
  640. now_token = self.queue.get_current_token(self.instance_name)
  641. rows, upto_token, limited = self.get_success(
  642. self.queue.get_replication_rows("master", prev_token, now_token, 10)
  643. )
  644. self.assertEqual(upto_token, now_token)
  645. self.assertFalse(limited)
  646. expected_rows = [
  647. (3, ("dest1", "@user1:test")),
  648. (3, ("dest2", "@user1:test")),
  649. (3, ("dest1", "@user2:test")),
  650. (3, ("dest2", "@user2:test")),
  651. (4, ("dest3", "@user3:test")),
  652. ]
  653. self.assertCountEqual(rows, expected_rows)
  654. def test_partially_clear_queue(self):
  655. state1 = UserPresenceState.default("@user1:test")
  656. state2 = UserPresenceState.default("@user2:test")
  657. state3 = UserPresenceState.default("@user3:test")
  658. prev_token = self.queue.get_current_token(self.instance_name)
  659. self.queue.send_presence_to_destinations((state1, state2), ("dest1", "dest2"))
  660. self.reactor.advance(2 * 60 * 1000)
  661. self.queue.send_presence_to_destinations((state3,), ("dest3",))
  662. self.reactor.advance(4 * 60 * 1000)
  663. now_token = self.queue.get_current_token(self.instance_name)
  664. rows, upto_token, limited = self.get_success(
  665. self.queue.get_replication_rows("master", prev_token, now_token, 10)
  666. )
  667. self.assertEqual(upto_token, now_token)
  668. self.assertFalse(limited)
  669. expected_rows = [
  670. (2, ("dest3", "@user3:test")),
  671. ]
  672. self.assertCountEqual(rows, [])
  673. prev_token = self.queue.get_current_token(self.instance_name)
  674. self.queue.send_presence_to_destinations((state1, state2), ("dest1", "dest2"))
  675. self.queue.send_presence_to_destinations((state3,), ("dest3",))
  676. now_token = self.queue.get_current_token(self.instance_name)
  677. rows, upto_token, limited = self.get_success(
  678. self.queue.get_replication_rows("master", prev_token, now_token, 10)
  679. )
  680. self.assertEqual(upto_token, now_token)
  681. self.assertFalse(limited)
  682. expected_rows = [
  683. (3, ("dest1", "@user1:test")),
  684. (3, ("dest2", "@user1:test")),
  685. (3, ("dest1", "@user2:test")),
  686. (3, ("dest2", "@user2:test")),
  687. (4, ("dest3", "@user3:test")),
  688. ]
  689. self.assertCountEqual(rows, expected_rows)
  690. class PresenceJoinTestCase(unittest.HomeserverTestCase):
  691. """Tests remote servers get told about presence of users in the room when
  692. they join and when new local users join.
  693. """
  694. user_id = "@test:server"
  695. servlets = [room.register_servlets]
  696. def make_homeserver(self, reactor, clock):
  697. hs = self.setup_test_homeserver(
  698. "server",
  699. federation_http_client=None,
  700. federation_sender=Mock(spec=FederationSender),
  701. )
  702. return hs
  703. def default_config(self):
  704. config = super().default_config()
  705. config["send_federation"] = True
  706. return config
  707. def prepare(self, reactor, clock, hs):
  708. self.federation_sender = hs.get_federation_sender()
  709. self.event_builder_factory = hs.get_event_builder_factory()
  710. self.federation_event_handler = hs.get_federation_event_handler()
  711. self.presence_handler = hs.get_presence_handler()
  712. # self.event_builder_for_2 = EventBuilderFactory(hs)
  713. # self.event_builder_for_2.hostname = "test2"
  714. self.store = hs.get_datastore()
  715. self.state = hs.get_state_handler()
  716. self._event_auth_handler = hs.get_event_auth_handler()
  717. # We don't actually check signatures in tests, so lets just create a
  718. # random key to use.
  719. self.random_signing_key = generate_signing_key("ver")
  720. def test_remote_joins(self):
  721. # We advance time to something that isn't 0, as we use 0 as a special
  722. # value.
  723. self.reactor.advance(1000000000000)
  724. # Create a room with two local users
  725. room_id = self.helper.create_room_as(self.user_id)
  726. self.helper.join(room_id, "@test2:server")
  727. # Mark test2 as online, test will be offline with a last_active of 0
  728. self.get_success(
  729. self.presence_handler.set_state(
  730. UserID.from_string("@test2:server"), {"presence": PresenceState.ONLINE}
  731. )
  732. )
  733. self.reactor.pump([0]) # Wait for presence updates to be handled
  734. #
  735. # Test that a new server gets told about existing presence
  736. #
  737. self.federation_sender.reset_mock()
  738. # Add a new remote server to the room
  739. self._add_new_user(room_id, "@alice:server2")
  740. # When new server is joined we send it the local users presence states.
  741. # We expect to only see user @test2:server, as @test:server is offline
  742. # and has a zero last_active_ts
  743. expected_state = self.get_success(
  744. self.presence_handler.current_state_for_user("@test2:server")
  745. )
  746. self.assertEqual(expected_state.state, PresenceState.ONLINE)
  747. self.federation_sender.send_presence_to_destinations.assert_called_once_with(
  748. destinations={"server2"}, states=[expected_state]
  749. )
  750. #
  751. # Test that only the new server gets sent presence and not existing servers
  752. #
  753. self.federation_sender.reset_mock()
  754. self._add_new_user(room_id, "@bob:server3")
  755. self.federation_sender.send_presence_to_destinations.assert_called_once_with(
  756. destinations={"server3"}, states=[expected_state]
  757. )
  758. def test_remote_gets_presence_when_local_user_joins(self):
  759. # We advance time to something that isn't 0, as we use 0 as a special
  760. # value.
  761. self.reactor.advance(1000000000000)
  762. # Create a room with one local users
  763. room_id = self.helper.create_room_as(self.user_id)
  764. # Mark test as online
  765. self.get_success(
  766. self.presence_handler.set_state(
  767. UserID.from_string("@test:server"), {"presence": PresenceState.ONLINE}
  768. )
  769. )
  770. # Mark test2 as online, test will be offline with a last_active of 0.
  771. # Note we don't join them to the room yet
  772. self.get_success(
  773. self.presence_handler.set_state(
  774. UserID.from_string("@test2:server"), {"presence": PresenceState.ONLINE}
  775. )
  776. )
  777. # Add servers to the room
  778. self._add_new_user(room_id, "@alice:server2")
  779. self._add_new_user(room_id, "@bob:server3")
  780. self.reactor.pump([0]) # Wait for presence updates to be handled
  781. #
  782. # Test that when a local join happens remote servers get told about it
  783. #
  784. self.federation_sender.reset_mock()
  785. # Join local user to room
  786. self.helper.join(room_id, "@test2:server")
  787. self.reactor.pump([0]) # Wait for presence updates to be handled
  788. # We expect to only send test2 presence to server2 and server3
  789. expected_state = self.get_success(
  790. self.presence_handler.current_state_for_user("@test2:server")
  791. )
  792. self.assertEqual(expected_state.state, PresenceState.ONLINE)
  793. self.federation_sender.send_presence_to_destinations.assert_called_once_with(
  794. destinations={"server2", "server3"}, states=[expected_state]
  795. )
  796. def _add_new_user(self, room_id, user_id):
  797. """Add new user to the room by creating an event and poking the federation API."""
  798. hostname = get_domain_from_id(user_id)
  799. room_version = self.get_success(self.store.get_room_version_id(room_id))
  800. builder = EventBuilder(
  801. state=self.state,
  802. event_auth_handler=self._event_auth_handler,
  803. store=self.store,
  804. clock=self.clock,
  805. hostname=hostname,
  806. signing_key=self.random_signing_key,
  807. room_version=KNOWN_ROOM_VERSIONS[room_version],
  808. room_id=room_id,
  809. type=EventTypes.Member,
  810. sender=user_id,
  811. state_key=user_id,
  812. content={"membership": Membership.JOIN},
  813. )
  814. prev_event_ids = self.get_success(
  815. self.store.get_latest_event_ids_in_room(room_id)
  816. )
  817. event = self.get_success(
  818. builder.build(prev_event_ids=prev_event_ids, auth_event_ids=None)
  819. )
  820. self.get_success(self.federation_event_handler.on_receive_pdu(hostname, event))
  821. # Check that it was successfully persisted.
  822. self.get_success(self.store.get_event(event.event_id))
  823. self.get_success(self.store.get_event(event.event_id))