matrix_sdk/event_cache/room/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! All event cache types for a single room.
use std::{
collections::BTreeMap,
fmt,
ops::{Deref, DerefMut},
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use events::{sort_positions_descending, Gap};
use eyeball::SharedObservable;
use eyeball_im::VectorDiff;
use matrix_sdk_base::{
deserialized_responses::{AmbiguityChange, TimelineEvent},
sync::{JoinedRoomUpdate, LeftRoomUpdate, Timeline},
};
use ruma::{
events::{relation::RelationType, AnyRoomAccountDataEvent, AnySyncEphemeralRoomEvent},
serde::Raw,
EventId, OwnedEventId, OwnedRoomId,
};
use tokio::sync::{
broadcast::{Receiver, Sender},
mpsc, Notify, RwLock,
};
use tracing::{error, instrument, trace, warn};
use super::{
deduplicator::DeduplicationOutcome, AllEventsCache, AutoShrinkChannelPayload, EventsOrigin,
Result, RoomEventCacheUpdate, RoomPagination, RoomPaginationStatus,
};
use crate::{client::WeakClient, room::WeakRoom};
pub(super) mod events;
/// A subset of an event cache, for a room.
///
/// Cloning is shallow, and thus is cheap to do.
#[derive(Clone)]
pub struct RoomEventCache {
pub(super) inner: Arc<RoomEventCacheInner>,
}
impl fmt::Debug for RoomEventCache {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RoomEventCache").finish_non_exhaustive()
}
}
/// Thin wrapper for a room event cache listener, so as to trigger side-effects
/// when all listeners are gone.
#[allow(missing_debug_implementations)]
pub struct RoomEventCacheListener {
/// Underlying receiver of the room event cache's updates.
recv: Receiver<RoomEventCacheUpdate>,
/// To which room are we listening?
room_id: OwnedRoomId,
/// Sender to the auto-shrink channel.
auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
/// Shared instance of the auto-shrinker.
listener_count: Arc<AtomicUsize>,
}
impl Drop for RoomEventCacheListener {
fn drop(&mut self) {
let previous_listener_count = self.listener_count.fetch_sub(1, Ordering::SeqCst);
trace!("dropping a room event cache listener; previous count: {previous_listener_count}");
if previous_listener_count == 1 {
// We were the last instance of the listener; let the auto-shrinker know by
// notifying it of our room id.
let mut room_id = self.room_id.clone();
// Try to send without waiting for channel capacity, and restart in a spin-loop
// if it failed (until a maximum number of attempts is reached, or
// the send was successful). The channel shouldn't be super busy in
// general, so this should resolve quickly enough.
let mut num_attempts = 0;
while let Err(err) = self.auto_shrink_sender.try_send(room_id) {
num_attempts += 1;
if num_attempts > 1024 {
// If we've tried too many times, just give up with a warning; after all, this
// is only an optimization.
warn!("couldn't send notification to the auto-shrink channel after 1024 attempts; giving up");
return;
}
match err {
mpsc::error::TrySendError::Full(stolen_room_id) => {
room_id = stolen_room_id;
}
mpsc::error::TrySendError::Closed(_) => return,
}
}
trace!("sent notification to the parent channel that we were the last listener");
}
}
}
impl Deref for RoomEventCacheListener {
type Target = Receiver<RoomEventCacheUpdate>;
fn deref(&self) -> &Self::Target {
&self.recv
}
}
impl DerefMut for RoomEventCacheListener {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.recv
}
}
impl RoomEventCache {
/// Create a new [`RoomEventCache`] using the given room and store.
pub(super) fn new(
client: WeakClient,
state: RoomEventCacheState,
pagination_status: SharedObservable<RoomPaginationStatus>,
room_id: OwnedRoomId,
all_events_cache: Arc<RwLock<AllEventsCache>>,
auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
) -> Self {
Self {
inner: Arc::new(RoomEventCacheInner::new(
client,
state,
pagination_status,
room_id,
all_events_cache,
auto_shrink_sender,
)),
}
}
/// Subscribe to this room updates, after getting the initial list of
/// events.
pub async fn subscribe(&self) -> (Vec<TimelineEvent>, RoomEventCacheListener) {
let state = self.inner.state.read().await;
let events = state.events().events().map(|(_position, item)| item.clone()).collect();
let previous_listener_count = state.listener_count.fetch_add(1, Ordering::SeqCst);
trace!("added a room event cache listener; new count: {}", previous_listener_count + 1);
let recv = self.inner.sender.subscribe();
let listener = RoomEventCacheListener {
recv,
room_id: self.inner.room_id.clone(),
auto_shrink_sender: self.inner.auto_shrink_sender.clone(),
listener_count: state.listener_count.clone(),
};
(events, listener)
}
/// Return a [`RoomPagination`] API object useful for running
/// back-pagination queries in the current room.
pub fn pagination(&self) -> RoomPagination {
RoomPagination { inner: self.inner.clone() }
}
/// Try to find an event by id in this room.
pub async fn event(&self, event_id: &EventId) -> Option<TimelineEvent> {
// Search in all loaded or stored events.
let Ok(maybe_position_and_event) = self.inner.state.read().await.find_event(event_id).await
else {
error!("Failed to find the event");
return None;
};
// Search in `AllEventsCache` for known events that are not stored.
if let Some(event) = maybe_position_and_event.map(|(_location, _position, event)| event) {
Some(event)
} else if let Some((room_id, event)) =
self.inner.all_events.read().await.events.get(event_id).cloned()
{
(room_id == self.inner.room_id).then_some(event)
} else {
None
}
}
/// Try to find an event by id in this room, along with its related events.
///
/// You can filter which types of related events to retrieve using
/// `filter`. `None` will retrieve related events of any type.
pub async fn event_with_relations(
&self,
event_id: &EventId,
filter: Option<Vec<RelationType>>,
) -> Option<(TimelineEvent, Vec<TimelineEvent>)> {
let cache = self.inner.all_events.read().await;
if let Some((_, event)) = cache.events.get(event_id) {
let related_events = cache.collect_related_events(event_id, filter.as_deref());
Some((event.clone(), related_events))
} else {
None
}
}
/// Clear all the storage for this [`RoomEventCache`].
///
/// This will get rid of all the events from the linked chunk and persisted
/// storage.
pub async fn clear(&self) -> Result<()> {
// Clear the linked chunk and persisted storage.
let updates_as_vector_diffs = self.inner.state.write().await.reset().await?;
// Clear the (temporary) events mappings.
self.inner.all_events.write().await.clear();
// Notify observers about the update.
let _ = self.inner.sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
diffs: updates_as_vector_diffs,
origin: EventsOrigin::Sync,
});
Ok(())
}
/// Save a single event in the event cache, for further retrieval with
/// [`Self::event`].
// TODO: This doesn't insert the event into the linked chunk. In the future
// there'll be no distinction between the linked chunk and the separate
// cache. There is a discussion in https://github.com/matrix-org/matrix-rust-sdk/issues/3886.
pub(crate) async fn save_event(&self, event: TimelineEvent) {
if let Some(event_id) = event.event_id() {
let mut cache = self.inner.all_events.write().await;
cache.append_related_event(&event);
cache.events.insert(event_id, (self.inner.room_id.clone(), event));
} else {
warn!("couldn't save event without event id in the event cache");
}
}
/// Save some events in the event cache, for further retrieval with
/// [`Self::event`]. This function will save them using a single lock,
/// as opposed to [`Self::save_event`].
// TODO: This doesn't insert the event into the linked chunk. In the future
// there'll be no distinction between the linked chunk and the separate
// cache. There is a discussion in https://github.com/matrix-org/matrix-rust-sdk/issues/3886.
pub(crate) async fn save_events(&self, events: impl IntoIterator<Item = TimelineEvent>) {
let mut cache = self.inner.all_events.write().await;
for event in events {
if let Some(event_id) = event.event_id() {
cache.append_related_event(&event);
cache.events.insert(event_id, (self.inner.room_id.clone(), event));
} else {
warn!("couldn't save event without event id in the event cache");
}
}
}
/// Return a nice debug string (a vector of lines) for the linked chunk of
/// events for this room.
pub async fn debug_string(&self) -> Vec<String> {
self.inner.state.read().await.events().debug_string()
}
}
/// The (non-cloneable) details of the `RoomEventCache`.
pub(super) struct RoomEventCacheInner {
/// The room id for this room.
room_id: OwnedRoomId,
pub weak_room: WeakRoom,
/// Sender part for subscribers to this room.
pub sender: Sender<RoomEventCacheUpdate>,
/// State for this room's event cache.
pub state: RwLock<RoomEventCacheState>,
/// See comment of [`super::EventCacheInner::all_events`].
///
/// This is shared between the [`super::EventCacheInner`] singleton and all
/// [`RoomEventCacheInner`] instances.
all_events: Arc<RwLock<AllEventsCache>>,
/// A notifier that we received a new pagination token.
pub pagination_batch_token_notifier: Notify,
pub pagination_status: SharedObservable<RoomPaginationStatus>,
/// Sender to the auto-shrink channel.
///
/// See doc comment around [`EventCache::auto_shrink_linked_chunk_task`] for
/// more details.
auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
}
impl RoomEventCacheInner {
/// Creates a new cache for a room, and subscribes to room updates, so as
/// to handle new timeline events.
fn new(
client: WeakClient,
state: RoomEventCacheState,
pagination_status: SharedObservable<RoomPaginationStatus>,
room_id: OwnedRoomId,
all_events_cache: Arc<RwLock<AllEventsCache>>,
auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
) -> Self {
let sender = Sender::new(32);
let weak_room = WeakRoom::new(client, room_id);
Self {
room_id: weak_room.room_id().to_owned(),
weak_room,
state: RwLock::new(state),
all_events: all_events_cache,
sender,
pagination_batch_token_notifier: Default::default(),
auto_shrink_sender,
pagination_status,
}
}
fn handle_account_data(&self, account_data: Vec<Raw<AnyRoomAccountDataEvent>>) {
if account_data.is_empty() {
return;
}
let mut handled_read_marker = false;
trace!("Handling account data");
for raw_event in account_data {
match raw_event.deserialize() {
Ok(AnyRoomAccountDataEvent::FullyRead(ev)) => {
// If duplicated, do not forward read marker multiple times
// to avoid clutter the update channel.
if handled_read_marker {
continue;
}
handled_read_marker = true;
// Propagate to observers. (We ignore the error if there aren't any.)
let _ = self.sender.send(RoomEventCacheUpdate::MoveReadMarkerTo {
event_id: ev.content.event_id,
});
}
Ok(_) => {
// We're not interested in other room account data updates,
// at this point.
}
Err(e) => {
let event_type = raw_event.get_field::<String>("type").ok().flatten();
warn!(event_type, "Failed to deserialize account data: {e}");
}
}
}
}
#[instrument(skip_all, fields(room_id = %self.room_id))]
pub(super) async fn handle_joined_room_update(
&self,
has_storage: bool,
updates: JoinedRoomUpdate,
) -> Result<()> {
self.handle_timeline(
has_storage,
updates.timeline,
updates.ephemeral.clone(),
updates.ambiguity_changes,
)
.await?;
self.handle_account_data(updates.account_data);
Ok(())
}
async fn handle_timeline(
&self,
has_storage: bool,
timeline: Timeline,
ephemeral_events: Vec<Raw<AnySyncEphemeralRoomEvent>>,
ambiguity_changes: BTreeMap<OwnedEventId, AmbiguityChange>,
) -> Result<()> {
if !has_storage && timeline.limited {
// Ideally we'd try to reconcile existing events against those received in the
// timeline, but we're not there yet. In the meanwhile, clear the
// items from the room. TODO: implement Smart Matching™.
trace!("limited timeline, clearing all previous events and pushing new events");
self.replace_all_events_by(
timeline.events,
timeline.prev_batch,
ephemeral_events,
ambiguity_changes,
)
.await?;
} else {
// Add all the events to the backend.
trace!("adding new events");
// If we have storage, only keep the previous-batch token if we have a limited
// timeline. Otherwise, we know about all the events, and we don't need to
// back-paginate, so we wouldn't make use of the given previous-batch token.
//
// If we don't have storage, even if the timeline isn't limited, we may not have
// saved the previous events in any cache, so we should always be
// able to retrieve those.
let prev_batch =
if has_storage && !timeline.limited { None } else { timeline.prev_batch };
let mut state = self.state.write().await;
self.append_events_locked(
&mut state,
timeline.events,
prev_batch,
ephemeral_events,
ambiguity_changes,
)
.await?;
}
Ok(())
}
#[instrument(skip_all, fields(room_id = %self.room_id))]
pub(super) async fn handle_left_room_update(
&self,
has_storage: bool,
updates: LeftRoomUpdate,
) -> Result<()> {
self.handle_timeline(has_storage, updates.timeline, Vec::new(), updates.ambiguity_changes)
.await?;
Ok(())
}
/// Remove existing events, and append a set of events to the room cache and
/// storage, notifying observers.
pub(super) async fn replace_all_events_by(
&self,
timeline_events: Vec<TimelineEvent>,
prev_batch: Option<String>,
ephemeral_events: Vec<Raw<AnySyncEphemeralRoomEvent>>,
ambiguity_changes: BTreeMap<OwnedEventId, AmbiguityChange>,
) -> Result<()> {
// Acquire the lock.
let mut state = self.state.write().await;
// Reset the room's state.
let updates_as_vector_diffs = state.reset().await?;
// Propagate to observers.
let _ = self.sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
diffs: updates_as_vector_diffs,
origin: EventsOrigin::Sync,
});
// Push the new events.
self.append_events_locked(
&mut state,
timeline_events,
prev_batch.clone(),
ephemeral_events,
ambiguity_changes,
)
.await?;
Ok(())
}
/// Append a set of events to the room cache and storage, notifying
/// observers.
///
/// This is a private implementation. It must not be exposed publicly.
async fn append_events_locked(
&self,
state: &mut RoomEventCacheState,
timeline_events: Vec<TimelineEvent>,
prev_batch: Option<String>,
ephemeral_events: Vec<Raw<AnySyncEphemeralRoomEvent>>,
ambiguity_changes: BTreeMap<OwnedEventId, AmbiguityChange>,
) -> Result<()> {
if timeline_events.is_empty()
&& prev_batch.is_none()
&& ephemeral_events.is_empty()
&& ambiguity_changes.is_empty()
{
return Ok(());
}
let (
DeduplicationOutcome {
all_events: events,
in_memory_duplicated_event_ids,
in_store_duplicated_event_ids,
},
all_duplicates,
) = state.collect_valid_and_duplicated_events(timeline_events).await?;
// During a sync, when a duplicated event is found, the old event is removed and
// the new event is added.
//
// Let's remove the old events that are duplicated.
let timeline_event_diffs = if all_duplicates {
// No new events, thus no need to change the room events.
vec![]
} else {
// Remove the old duplicated events.
//
// We don't have to worry the removals can change the position of the
// existing events, because we are pushing all _new_
// `events` at the back.
let mut timeline_event_diffs = state
.remove_events(in_memory_duplicated_event_ids, in_store_duplicated_event_ids)
.await?;
// Add the previous back-pagination token (if present), followed by the timeline
// events themselves.
let new_timeline_event_diffs = state
.with_events_mut(|room_events| {
// If we only received duplicated events, we don't need to store the gap: if
// there was a gap, we'd have received an unknown event at the tail of
// the room's timeline (unless the server reordered sync events since the last
// time we sync'd).
if !all_duplicates {
if let Some(prev_token) = &prev_batch {
// As a tiny optimization: remove the last chunk if it's an empty event
// one, as it's not useful to keep it before a gap.
let prev_chunk_to_remove =
room_events.rchunks().next().and_then(|chunk| {
(chunk.is_items() && chunk.num_items() == 0)
.then_some(chunk.identifier())
});
room_events.push_gap(Gap { prev_token: prev_token.clone() });
if let Some(prev_chunk_to_remove) = prev_chunk_to_remove {
room_events.remove_empty_chunk_at(prev_chunk_to_remove).expect(
"we just checked the chunk is there, and it's an empty item chunk",
);
}
}
}
room_events.push_events(events.clone());
events.clone()
})
.await?;
timeline_event_diffs.extend(new_timeline_event_diffs);
if prev_batch.is_some() && !all_duplicates {
// If there was a previous batch token, and there's at least one non-duplicated
// new event, unload the chunks so it only contains the last
// one; otherwise, there might be a valid gap in between, and
// observers may not render it (yet).
//
// We must do this *after* the above call to `.with_events_mut`, so the new
// events and gaps are properly persisted to storage.
if let Some(diffs) = state.shrink_to_last_chunk().await? {
// Override the diffs with the new ones, as per `shrink_to_last_chunk`'s API
// contract.
timeline_event_diffs = diffs;
}
}
{
// Fill the AllEventsCache.
let mut all_events_cache = self.all_events.write().await;
for event in events {
if let Some(event_id) = event.event_id() {
all_events_cache.append_related_event(&event);
all_events_cache
.events
.insert(event_id.to_owned(), (self.room_id.clone(), event));
}
}
}
timeline_event_diffs
};
// Now that all events have been added, we can trigger the
// `pagination_token_notifier`.
if prev_batch.is_some() {
self.pagination_batch_token_notifier.notify_one();
}
// The order of `RoomEventCacheUpdate`s is **really** important here.
{
if !timeline_event_diffs.is_empty() {
let _ = self.sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
diffs: timeline_event_diffs,
origin: EventsOrigin::Sync,
});
}
if !ephemeral_events.is_empty() {
let _ = self
.sender
.send(RoomEventCacheUpdate::AddEphemeralEvents { events: ephemeral_events });
}
if !ambiguity_changes.is_empty() {
let _ = self.sender.send(RoomEventCacheUpdate::UpdateMembers { ambiguity_changes });
}
}
Ok(())
}
}
/// Internal type to represent the output of
/// [`RoomEventCacheState::load_more_events_backwards`].
#[derive(Debug)]
pub(super) enum LoadMoreEventsBackwardsOutcome {
/// A gap has been inserted.
Gap {
/// The previous batch token to be used as the "end" parameter in the
/// back-pagination request.
prev_token: Option<String>,
},
/// The start of the timeline has been reached.
StartOfTimeline,
/// Events have been inserted.
Events {
events: Vec<TimelineEvent>,
timeline_event_diffs: Vec<VectorDiff<TimelineEvent>>,
reached_start: bool,
},
/// The caller must wait for the initial previous-batch token, and retry.
WaitForInitialPrevToken,
}
// Use a private module to hide `events` to this parent module.
mod private {
use std::sync::{atomic::AtomicUsize, Arc};
use eyeball::SharedObservable;
use eyeball_im::VectorDiff;
use matrix_sdk_base::{
apply_redaction,
deserialized_responses::{TimelineEvent, TimelineEventKind},
event_cache::{store::EventCacheStoreLock, Event, Gap},
linked_chunk::{lazy_loader, ChunkContent, ChunkIdentifierGenerator, Position, Update},
};
use matrix_sdk_common::executor::spawn;
use once_cell::sync::OnceCell;
use ruma::{
events::{
room::redaction::SyncRoomRedactionEvent, AnySyncTimelineEvent, MessageLikeEventType,
},
serde::Raw,
EventId, OwnedEventId, OwnedRoomId, RoomVersionId,
};
use tracing::{debug, error, instrument, trace, warn};
use super::{
super::{
deduplicator::{DeduplicationOutcome, Deduplicator},
EventCacheError,
},
events::RoomEvents,
sort_positions_descending, EventLocation, LoadMoreEventsBackwardsOutcome,
};
use crate::event_cache::RoomPaginationStatus;
/// State for a single room's event cache.
///
/// This contains all the inner mutable states that ought to be updated at
/// the same time.
pub struct RoomEventCacheState {
/// The room this state relates to.
room: OwnedRoomId,
/// The room version for this room.
room_version: RoomVersionId,
/// Reference to the underlying backing store.
///
/// Set to none if the room shouldn't read the linked chunk from
/// storage, and shouldn't store updates to storage.
store: Arc<OnceCell<EventCacheStoreLock>>,
/// The events of the room.
events: RoomEvents,
/// The events deduplicator instance to help finding duplicates.
deduplicator: Deduplicator,
/// Have we ever waited for a previous-batch-token to come from sync, in
/// the context of pagination? We do this at most once per room,
/// the first time we try to run backward pagination. We reset
/// that upon clearing the timeline events.
pub waited_for_initial_prev_token: bool,
pagination_status: SharedObservable<RoomPaginationStatus>,
/// An atomic count of the current number of listeners of the
/// [`super::RoomEventCache`].
pub(super) listener_count: Arc<AtomicUsize>,
}
impl RoomEventCacheState {
/// Create a new state, or reload it from storage if it's been enabled.
///
/// Not all events are going to be loaded. Only a portion of them. The
/// [`RoomEvents`] relies on a [`LinkedChunk`] to store all events. Only
/// the last chunk will be loaded. It means the events are loaded from
/// the most recent to the oldest. To load more events, see
/// [`Self::load_more_events_backwards`].
///
/// [`LinkedChunk`]: matrix_sdk_common::linked_chunk::LinkedChunk
pub async fn new(
room_id: OwnedRoomId,
room_version: RoomVersionId,
store: Arc<OnceCell<EventCacheStoreLock>>,
pagination_status: SharedObservable<RoomPaginationStatus>,
) -> Result<Self, EventCacheError> {
let (events, deduplicator) = if let Some(store) = store.get() {
let store_lock = store.lock().await?;
let linked_chunk = match store_lock
.load_last_chunk(&room_id)
.await
.map_err(EventCacheError::from)
.and_then(|(last_chunk, chunk_identifier_generator)| {
lazy_loader::from_last_chunk(last_chunk, chunk_identifier_generator)
.map_err(EventCacheError::from)
}) {
Ok(linked_chunk) => linked_chunk,
Err(err) => {
error!("error when reloading a linked chunk from memory: {err}");
// Clear storage for this room.
store_lock
.handle_linked_chunk_updates(&room_id, vec![Update::Clear])
.await?;
// Restart with an empty linked chunk.
None
}
};
(
RoomEvents::with_initial_linked_chunk(linked_chunk),
Deduplicator::new_store_based(room_id.clone(), store.clone()),
)
} else {
(RoomEvents::default(), Deduplicator::new_memory_based())
};
Ok(Self {
room: room_id,
room_version,
store,
events,
deduplicator,
waited_for_initial_prev_token: false,
listener_count: Default::default(),
pagination_status,
})
}
/// Deduplicate `events` considering all events in `Self::events`.
///
/// The returned tuple contains:
/// - all events (duplicated or not) with an ID
/// - all the duplicated event IDs with their position,
/// - a boolean indicating all events (at least one) are duplicates.
///
/// This last boolean is useful to know whether we need to store a
/// previous-batch token (gap) we received from a server-side
/// request (sync or back-pagination), or if we should
/// *not* store it.
///
/// Since there can be empty back-paginations with a previous-batch
/// token (that is, they don't contain any events), we need to
/// make sure that there is *at least* one new event that has
/// been added. Otherwise, we might conclude something wrong
/// because a subsequent back-pagination might
/// return non-duplicated events.
///
/// If we had already seen all the duplicated events that we're trying
/// to add, then it would be wasteful to store a previous-batch
/// token, or even touch the linked chunk: we would repeat
/// back-paginations for events that we have already seen, and
/// possibly misplace them. And we should not be missing
/// events either: the already-known events would have their own
/// previous-batch token (it might already be consumed).
pub async fn collect_valid_and_duplicated_events(
&mut self,
events: Vec<Event>,
) -> Result<(DeduplicationOutcome, bool), EventCacheError> {
let deduplication_outcome =
self.deduplicator.filter_duplicate_events(events, &self.events).await?;
let number_of_events = deduplication_outcome.all_events.len();
let number_of_deduplicated_events =
deduplication_outcome.in_memory_duplicated_event_ids.len()
+ deduplication_outcome.in_store_duplicated_event_ids.len();
let all_duplicates =
number_of_events > 0 && number_of_events == number_of_deduplicated_events;
Ok((deduplication_outcome, all_duplicates))
}
/// Given a fully-loaded linked chunk with no gaps, return the
/// [`LoadMoreEventsBackwardsOutcome`] expected for this room's cache.
fn conclude_load_more_for_fully_loaded_chunk(&mut self) -> LoadMoreEventsBackwardsOutcome {
// If we never received events for this room, this means we've never
// received a sync for that room, because every room must have at least a
// room creation event. Otherwise, we have reached the start of the
// timeline.
if self.events.events().next().is_some() {
// If there's at least one event, this means we've reached the start of the
// timeline, since the chunk is fully loaded.
LoadMoreEventsBackwardsOutcome::StartOfTimeline
} else if !self.waited_for_initial_prev_token {
// There's no events. Since we haven't yet, wait for an initial previous-token.
LoadMoreEventsBackwardsOutcome::WaitForInitialPrevToken
} else {
// Otherwise, we've already waited, *and* received no previous-batch token from
// the sync, *and* there are still no events in the fully-loaded
// chunk: start back-pagination from the end of the room.
LoadMoreEventsBackwardsOutcome::Gap { prev_token: None }
}
}
/// Load more events backwards if the last chunk is **not** a gap.
pub(in super::super) async fn load_more_events_backwards(
&mut self,
) -> Result<LoadMoreEventsBackwardsOutcome, EventCacheError> {
let Some(store) = self.store.get() else {
// No store to reload events from. Pretend the caller has to act as if a gap was
// present. Limited syncs will always clear and push a gap, in this mode.
// There's no lazy-loading.
// Look for a gap in the in-memory chunk, iterating in reverse so as to get the
// most recent one.
if let Some(prev_token) = self.events.rgap().map(|gap| gap.prev_token) {
return Ok(LoadMoreEventsBackwardsOutcome::Gap {
prev_token: Some(prev_token),
});
}
return Ok(self.conclude_load_more_for_fully_loaded_chunk());
};
// If any in-memory chunk is a gap, don't load more events, and let the caller
// resolve the gap.
if let Some(prev_token) = self.events.rgap().map(|gap| gap.prev_token) {
return Ok(LoadMoreEventsBackwardsOutcome::Gap { prev_token: Some(prev_token) });
}
// Because `first_chunk` is `not `Send`, get this information before the
// `.await` point, so that this `Future` can implement `Send`.
let first_chunk_identifier =
self.events.chunks().next().expect("a linked chunk is never empty").identifier();
let store = store.lock().await?;
// The first chunk is not a gap, we can load its previous chunk.
let new_first_chunk =
match store.load_previous_chunk(&self.room, first_chunk_identifier).await {
Ok(Some(new_first_chunk)) => {
// All good, let's continue with this chunk.
new_first_chunk
}
Ok(None) => {
// There's no previous chunk. The chunk is now fully-loaded. Conclude.
return Ok(self.conclude_load_more_for_fully_loaded_chunk());
}
Err(err) => {
error!("error when loading the previous chunk of a linked chunk: {err}");
// Clear storage for this room.
store.handle_linked_chunk_updates(&self.room, vec![Update::Clear]).await?;
// Return the error.
return Err(err.into());
}
};
let chunk_content = new_first_chunk.content.clone();
// We've reached the start on disk, if and only if, there was no chunk prior to
// the one we just loaded.
let reached_start = new_first_chunk.previous.is_none();
if let Err(err) = self.events.insert_new_chunk_as_first(new_first_chunk) {
error!("error when inserting the previous chunk into its linked chunk: {err}");
// Clear storage for this room.
store.handle_linked_chunk_updates(&self.room, vec![Update::Clear]).await?;
// Return the error.
return Err(err.into());
};
// ⚠️ Let's not propagate the updates to the store! We already have these data
// in the store! Let's drain them.
let _ = self.events.store_updates().take();
// However, we want to get updates as `VectorDiff`s.
let timeline_event_diffs = self.events.updates_as_vector_diffs();
Ok(match chunk_content {
ChunkContent::Gap(gap) => {
LoadMoreEventsBackwardsOutcome::Gap { prev_token: Some(gap.prev_token) }
}
ChunkContent::Items(events) => LoadMoreEventsBackwardsOutcome::Events {
events,
timeline_event_diffs,
reached_start,
},
})
}
/// If storage is enabled, unload all the chunks, then reloads only the
/// last one.
///
/// If storage's enabled, return a diff update that starts with a clear
/// of all events; as a result, the caller may override any
/// pending diff updates with the result of this function.
///
/// Otherwise, returns `None`.
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
pub(super) async fn shrink_to_last_chunk(
&mut self,
) -> Result<Option<Vec<VectorDiff<TimelineEvent>>>, EventCacheError> {
let Some(store) = self.store.get() else {
// No need to do anything if there's no storage; we'll already reset the
// timeline after a limited response.
return Ok(None);
};
let store_lock = store.lock().await?;
// Attempt to load the last chunk.
let (last_chunk, chunk_identifier_generator) = match store_lock
.load_last_chunk(&self.room)
.await
{
Ok(pair) => pair,
Err(err) => {
// If loading the last chunk failed, clear the entire linked chunk.
error!("error when reloading a linked chunk from memory: {err}");
// Clear storage for this room.
store_lock.handle_linked_chunk_updates(&self.room, vec![Update::Clear]).await?;
// Restart with an empty linked chunk.
(None, ChunkIdentifierGenerator::new_from_scratch())
}
};
debug!("unloading the linked chunk, and resetting it to its last chunk");
// Remove all the chunks from the linked chunks, except for the last one, and
// updates the chunk identifier generator.
if let Err(err) = self.events.replace_with(last_chunk, chunk_identifier_generator) {
error!("error when replacing the linked chunk: {err}");
return self.reset().await.map(Some);
}
// Let pagination observers know that we may have not reached the start of the
// timeline.
// TODO: likely need to cancel any ongoing pagination.
self.pagination_status.set(RoomPaginationStatus::Idle { hit_timeline_start: false });
// Don't propagate those updates to the store; this is only for the in-memory
// representation that we're doing this. Let's drain those store updates.
let _ = self.events.store_updates().take();
// However, we want to get updates as `VectorDiff`s, for the external listeners.
// Check we're respecting the contract defined in the doc comment.
let diffs = self.events.updates_as_vector_diffs();
assert!(matches!(diffs[0], VectorDiff::Clear));
Ok(Some(diffs))
}
/// Automatically shrink the room if there are no listeners, as
/// indicated by the atomic number of active listeners.
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
pub(crate) async fn auto_shrink_if_no_listeners(
&mut self,
) -> Result<Option<Vec<VectorDiff<TimelineEvent>>>, EventCacheError> {
let listener_count = self.listener_count.load(std::sync::atomic::Ordering::SeqCst);
trace!(listener_count, "received request to auto-shrink");
if listener_count == 0 {
// If we are the last strong reference to the auto-shrinker, we can shrink the
// events data structure to its last chunk.
self.shrink_to_last_chunk().await
} else {
Ok(None)
}
}
/// Removes the bundled relations from an event, if they were present.
///
/// Only replaces the present if it contained bundled relations.
fn strip_relations_if_present<T>(event: &mut Raw<T>) {
// We're going to get rid of the `unsigned`/`m.relations` field, if it's
// present.
// Use a closure that returns an option so we can quickly short-circuit.
let mut closure = || -> Option<()> {
let mut val: serde_json::Value = event.deserialize_as().ok()?;
let unsigned = val.get_mut("unsigned")?;
let unsigned_obj = unsigned.as_object_mut()?;
if unsigned_obj.remove("m.relations").is_some() {
*event = Raw::new(&val).ok()?.cast();
}
None
};
let _ = closure();
}
fn strip_relations_from_event(ev: &mut TimelineEvent) {
match &mut ev.kind {
TimelineEventKind::Decrypted(decrypted) => {
// Remove all information about encryption info for
// the bundled events.
decrypted.unsigned_encryption_info = None;
// Remove the `unsigned`/`m.relations` field, if needs be.
Self::strip_relations_if_present(&mut decrypted.event);
}
TimelineEventKind::UnableToDecrypt { event, .. }
| TimelineEventKind::PlainText { event } => {
Self::strip_relations_if_present(event);
}
}
}
/// Strips the bundled relations from a collection of events.
fn strip_relations_from_events(items: &mut [TimelineEvent]) {
for ev in items.iter_mut() {
Self::strip_relations_from_event(ev);
}
}
/// Remove events by their position, in `RoomEvents` and in
/// `EventCacheStore`.
///
/// This method is purposely isolated because it must ensure that
/// positions are sorted appropriately or it can be disastrous.
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
pub(crate) async fn remove_events(
&mut self,
in_memory_events: Vec<(OwnedEventId, Position)>,
in_store_events: Vec<(OwnedEventId, Position)>,
) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError> {
// In-store events.
{
let mut positions = in_store_events
.into_iter()
.map(|(_event_id, position)| position)
.collect::<Vec<_>>();
sort_positions_descending(&mut positions);
self.send_updates_to_store(
positions
.into_iter()
.map(|position| Update::RemoveItem { at: position })
.collect(),
)
.await?;
}
// In-memory events.
let timeline_event_diffs = self
.with_events_mut(|room_events| {
// `remove_events_by_position` sorts the positions by itself.
room_events
.remove_events_by_position(
in_memory_events
.into_iter()
.map(|(_event_id, position)| position)
.collect(),
)
.expect("failed to remove an event");
vec![]
})
.await?;
Ok(timeline_event_diffs)
}
/// Propagate changes to the underlying storage.
#[instrument(skip_all)]
async fn propagate_changes(&mut self) -> Result<(), EventCacheError> {
let updates = self.events.store_updates().take();
self.send_updates_to_store(updates).await
}
pub async fn send_updates_to_store(
&mut self,
mut updates: Vec<Update<TimelineEvent, Gap>>,
) -> Result<(), EventCacheError> {
let Some(store) = self.store.get() else {
return Ok(());
};
if updates.is_empty() {
return Ok(());
}
// Strip relations from updates which insert or replace items.
for update in updates.iter_mut() {
match update {
Update::PushItems { items, .. } => Self::strip_relations_from_events(items),
Update::ReplaceItem { item, .. } => Self::strip_relations_from_event(item),
// Other update kinds don't involve adding new events.
Update::NewItemsChunk { .. }
| Update::NewGapChunk { .. }
| Update::RemoveChunk(_)
| Update::RemoveItem { .. }
| Update::DetachLastItems { .. }
| Update::StartReattachItems
| Update::EndReattachItems
| Update::Clear => {}
}
}
// Spawn a task to make sure that all the changes are effectively forwarded to
// the store, even if the call to this method gets aborted.
//
// The store cross-process locking involves an actual mutex, which ensures that
// storing updates happens in the expected order.
let store = store.clone();
let room_id = self.room.clone();
spawn(async move {
let store = store.lock().await?;
trace!(%room_id, ?updates, "sending linked chunk updates to the store");
store.handle_linked_chunk_updates(&room_id, updates).await?;
trace!("linked chunk updates applied");
super::Result::Ok(())
})
.await
.expect("joining failed")?;
Ok(())
}
/// Reset this data structure as if it were brand new.
///
/// Return a single diff update that is a clear of all events; as a
/// result, the caller may override any pending diff updates
/// with the result of this function.
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
pub async fn reset(&mut self) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError> {
self.events.reset();
self.propagate_changes().await?;
// Reset the pagination state too: pretend we never waited for the initial
// prev-batch token, and indicate that we're not at the start of the
// timeline, since we don't know about that anymore.
self.waited_for_initial_prev_token = false;
// TODO: likely must cancel any ongoing back-paginations too
self.pagination_status.set(RoomPaginationStatus::Idle { hit_timeline_start: false });
let diff_updates = self.events.updates_as_vector_diffs();
// Ensure the contract defined in the doc comment is true:
assert_eq!(diff_updates.len(), 1);
assert!(matches!(diff_updates[0], VectorDiff::Clear));
Ok(diff_updates)
}
/// Returns a read-only reference to the underlying events.
pub fn events(&self) -> &RoomEvents {
&self.events
}
/// Find a single event in this room.
///
/// It starts by looking into loaded events in `RoomEvents` before
/// looking inside the storage if it is enabled.
pub async fn find_event(
&self,
event_id: &EventId,
) -> Result<Option<(EventLocation, Position, TimelineEvent)>, EventCacheError> {
let room_id = self.room.as_ref();
// There are supposedly fewer events loaded in memory than in the store. Let's
// start by looking up in the `RoomEvents`.
for (position, event) in self.events().revents() {
if event.event_id().as_deref() == Some(event_id) {
return Ok(Some((EventLocation::Memory, position, event.clone())));
}
}
let Some(store) = self.store.get() else {
// No store, event is not present.
return Ok(None);
};
let store = store.lock().await?;
Ok(store
.find_event(room_id, event_id)
.await?
.map(|(position, event)| (EventLocation::Store, position, event)))
}
/// Gives a temporary mutable handle to the underlying in-memory events,
/// and will propagate changes to the storage once done.
///
/// Returns the updates to the linked chunk, as vector diffs, so the
/// caller may propagate such updates, if needs be.
///
/// The function `func` takes a mutable reference to `RoomEvents`. It
/// returns a set of events that will be post-processed. At the time of
/// writing, all these events are passed to
/// `Self::maybe_apply_new_redaction`.
#[must_use = "Updates as `VectorDiff` must probably be propagated via `RoomEventCacheUpdate`"]
pub async fn with_events_mut<F>(
&mut self,
func: F,
) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError>
where
F: FnOnce(&mut RoomEvents) -> Vec<TimelineEvent>,
{
let events_to_post_process = func(&mut self.events);
// Update the store before doing the post-processing.
self.propagate_changes().await?;
for event in &events_to_post_process {
self.maybe_apply_new_redaction(event).await?;
}
// If we've never waited for an initial previous-batch token, and we now have at
// least one gap in the chunk, no need to wait for a previous-batch token later.
if !self.waited_for_initial_prev_token
&& self.events.chunks().any(|chunk| chunk.is_gap())
{
self.waited_for_initial_prev_token = true;
}
let updates_as_vector_diffs = self.events.updates_as_vector_diffs();
Ok(updates_as_vector_diffs)
}
/// If the given event is a redaction, try to retrieve the
/// to-be-redacted event in the chunk, and replace it by the
/// redacted form.
#[instrument(skip_all)]
async fn maybe_apply_new_redaction(
&mut self,
event: &Event,
) -> Result<(), EventCacheError> {
let raw_event = event.raw();
// Do not deserialise the entire event if we aren't certain it's a
// `m.room.redaction`. It saves a non-negligible amount of computations.
let Ok(Some(MessageLikeEventType::RoomRedaction)) =
raw_event.get_field::<MessageLikeEventType>("type")
else {
return Ok(());
};
// It is a `m.room.redaction`! We can deserialize it entirely.
let Ok(AnySyncTimelineEvent::MessageLike(
ruma::events::AnySyncMessageLikeEvent::RoomRedaction(redaction),
)) = event.raw().deserialize()
else {
return Ok(());
};
let Some(event_id) = redaction.redacts(&self.room_version) else {
warn!("missing target event id from the redaction event");
return Ok(());
};
// Replace the redacted event by a redacted form, if we knew about it.
if let Some((location, position, target_event)) = self.find_event(event_id).await? {
// Don't redact already redacted events.
if let Ok(deserialized) = target_event.raw().deserialize() {
match deserialized {
AnySyncTimelineEvent::MessageLike(ev) => {
if ev.is_redacted() {
return Ok(());
}
}
AnySyncTimelineEvent::State(ev) => {
if ev.is_redacted() {
return Ok(());
}
}
}
}
if let Some(redacted_event) = apply_redaction(
target_event.raw(),
event.raw().cast_ref::<SyncRoomRedactionEvent>(),
&self.room_version,
) {
let mut copy = target_event.clone();
// It's safe to cast `redacted_event` here:
// - either the event was an `AnyTimelineEvent` cast to `AnySyncTimelineEvent`
// when calling .raw(), so it's still one under the hood.
// - or it wasn't, and it's a plain `AnySyncTimelineEvent` in this case.
copy.replace_raw(redacted_event.cast());
match location {
EventLocation::Memory => {
self.events
.replace_event_at(position, copy)
.expect("should have been a valid position of an item");
}
EventLocation::Store => {
self.send_updates_to_store(vec![Update::ReplaceItem {
at: position,
item: copy,
}])
.await?;
}
}
}
} else {
trace!("redacted event is missing from the linked chunk");
}
// TODO: remove all related events too!
Ok(())
}
}
}
/// An enum representing where an event has been found.
pub(super) enum EventLocation {
/// Event lives in memory (and likely in the store!).
Memory,
/// Event lives in the store only, it has not been loaded in memory yet.
Store,
}
pub(super) use private::RoomEventCacheState;
#[cfg(test)]
mod tests {
use std::sync::Arc;
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use matrix_sdk_base::{
event_cache::{
store::{EventCacheStore as _, MemoryStore},
Gap,
},
linked_chunk::{ChunkContent, ChunkIdentifier, Position, Update},
store::StoreConfig,
sync::{JoinedRoomUpdate, Timeline},
};
use matrix_sdk_common::deserialized_responses::TimelineEvent;
use matrix_sdk_test::{async_test, event_factory::EventFactory, ALICE, BOB};
use ruma::{
event_id,
events::{
relation::RelationType, room::message::RoomMessageEventContentWithoutRelation,
AnySyncMessageLikeEvent, AnySyncTimelineEvent,
},
room_id, user_id, RoomId,
};
use crate::test_utils::{client::MockClientBuilder, logged_in_client};
#[async_test]
async fn test_event_with_redaction_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.text_msg("Original event").event_id(original_id).into(),
f.redaction(original_id).event_id(related_id).into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_edit_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.text_msg("Original event").event_id(original_id).into(),
f.text_msg("* An edited event")
.edit(
original_id,
RoomMessageEventContentWithoutRelation::text_plain("And edited event"),
)
.event_id(related_id)
.into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_reply_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.text_msg("Original event").event_id(original_id).into(),
f.text_msg("A reply").reply_to(original_id).event_id(related_id).into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_thread_reply_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.text_msg("Original event").event_id(original_id).into(),
f.text_msg("A reply").in_thread(original_id, related_id).event_id(related_id).into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_reaction_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.text_msg("Original event").event_id(original_id).into(),
f.reaction(original_id, ":D").event_id(related_id).into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_poll_response_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.poll_start("Poll start event", "A poll question", vec!["An answer"])
.event_id(original_id)
.into(),
f.poll_response(vec!["1"], original_id).event_id(related_id).into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_poll_end_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
assert_relations(
room_id,
f.poll_start("Poll start event", "A poll question", vec!["An answer"])
.event_id(original_id)
.into(),
f.poll_end("Poll ended", original_id).event_id(related_id).into(),
f,
)
.await;
}
#[async_test]
async fn test_event_with_filtered_relationships() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let associated_related_id = event_id!("$recursive_related");
let room_id = room_id!("!galette:saucisse.bzh");
let event_factory = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let original_event = event_factory.text_msg("Original event").event_id(original_id).into();
let related_event = event_factory
.text_msg("* Edited event")
.edit(original_id, RoomMessageEventContentWithoutRelation::text_plain("Edited event"))
.event_id(related_id)
.into();
let associated_related_event =
event_factory.redaction(related_id).event_id(associated_related_id).into();
let client = logged_in_client(None).await;
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Save the original event.
room_event_cache.save_event(original_event).await;
// Save the related event.
room_event_cache.save_event(related_event).await;
// Save the associated related event, which redacts the related event.
room_event_cache.save_event(associated_related_event).await;
let filter = Some(vec![RelationType::Replacement]);
let (event, related_events) =
room_event_cache.event_with_relations(original_id, filter).await.unwrap();
// Fetched event is the right one.
let cached_event_id = event.event_id().unwrap();
assert_eq!(cached_event_id, original_id);
// There are both the related id and the associatively related id
assert_eq!(related_events.len(), 2);
let related_event_id = related_events[0].event_id().unwrap();
assert_eq!(related_event_id, related_id);
let related_event_id = related_events[1].event_id().unwrap();
assert_eq!(related_event_id, associated_related_id);
// Now we'll filter threads instead, there should be no related events
let filter = Some(vec![RelationType::Thread]);
let (event, related_events) =
room_event_cache.event_with_relations(original_id, filter).await.unwrap();
// Fetched event is the right one.
let cached_event_id = event.event_id().unwrap();
assert_eq!(cached_event_id, original_id);
// No Thread related events found
assert!(related_events.is_empty());
}
#[async_test]
async fn test_event_with_recursive_relation() {
let original_id = event_id!("$original");
let related_id = event_id!("$related");
let associated_related_id = event_id!("$recursive_related");
let room_id = room_id!("!galette:saucisse.bzh");
let event_factory = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let original_event = event_factory.text_msg("Original event").event_id(original_id).into();
let related_event = event_factory
.text_msg("* Edited event")
.edit(original_id, RoomMessageEventContentWithoutRelation::text_plain("Edited event"))
.event_id(related_id)
.into();
let associated_related_event =
event_factory.redaction(related_id).event_id(associated_related_id).into();
let client = logged_in_client(None).await;
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Save the original event.
room_event_cache.save_event(original_event).await;
// Save the related event.
room_event_cache.save_event(related_event).await;
// Save the associated related event, which redacts the related event.
room_event_cache.save_event(associated_related_event).await;
let (event, related_events) =
room_event_cache.event_with_relations(original_id, None).await.unwrap();
// Fetched event is the right one.
let cached_event_id = event.event_id().unwrap();
assert_eq!(cached_event_id, original_id);
// There are both the related id and the associatively related id
assert_eq!(related_events.len(), 2);
let related_event_id = related_events[0].event_id().unwrap();
assert_eq!(related_event_id, related_id);
let related_event_id = related_events[1].event_id().unwrap();
assert_eq!(related_event_id, associated_related_id);
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_write_to_storage() {
use matrix_sdk_base::linked_chunk::lazy_loader::from_all_chunks;
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let event_cache_store = Arc::new(MemoryStore::new());
let client = MockClientBuilder::new("http://localhost".to_owned())
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
.build()
.await;
let event_cache = client.event_cache();
// Don't forget to subscribe and like^W enable storage!
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Propagate an update for a message and a prev-batch token.
let timeline = Timeline {
limited: true,
prev_batch: Some("raclette".to_owned()),
events: vec![f.text_msg("hey yo").sender(*ALICE).into_event()],
};
room_event_cache
.inner
.handle_joined_room_update(true, JoinedRoomUpdate { timeline, ..Default::default() })
.await
.unwrap();
let linked_chunk =
from_all_chunks::<3, _, _>(event_cache_store.load_all_chunks(room_id).await.unwrap())
.unwrap()
.unwrap();
assert_eq!(linked_chunk.chunks().count(), 2);
let mut chunks = linked_chunk.chunks();
// We start with the gap.
assert_matches!(chunks.next().unwrap().content(), ChunkContent::Gap(gap) => {
assert_eq!(gap.prev_token, "raclette");
});
// Then we have the stored event.
assert_matches!(chunks.next().unwrap().content(), ChunkContent::Items(events) => {
assert_eq!(events.len(), 1);
let deserialized = events[0].raw().deserialize().unwrap();
assert_let!(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = deserialized);
assert_eq!(msg.as_original().unwrap().content.body(), "hey yo");
});
// That's all, folks!
assert!(chunks.next().is_none());
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_write_to_storage_strips_bundled_relations() {
use matrix_sdk_base::linked_chunk::lazy_loader::from_all_chunks;
use ruma::events::BundledMessageLikeRelations;
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let event_cache_store = Arc::new(MemoryStore::new());
let client = MockClientBuilder::new("http://localhost".to_owned())
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
.build()
.await;
let event_cache = client.event_cache();
// Don't forget to subscribe and like^W enable storage!
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Propagate an update for a message with bundled relations.
let mut relations = BundledMessageLikeRelations::new();
relations.replace =
Some(Box::new(f.text_msg("Hello, Kind Sir").sender(*ALICE).into_raw_sync()));
let ev = f.text_msg("hey yo").sender(*ALICE).bundled_relations(relations).into_event();
let timeline = Timeline { limited: false, prev_batch: None, events: vec![ev] };
room_event_cache
.inner
.handle_joined_room_update(true, JoinedRoomUpdate { timeline, ..Default::default() })
.await
.unwrap();
// The in-memory linked chunk keeps the bundled relation.
{
let (events, _) = room_event_cache.subscribe().await;
assert_eq!(events.len(), 1);
let ev = events[0].raw().deserialize().unwrap();
assert_let!(
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = ev
);
let original = msg.as_original().unwrap();
assert_eq!(original.content.body(), "hey yo");
assert!(original.unsigned.relations.replace.is_some());
}
// The one in storage does not.
let linked_chunk =
from_all_chunks::<3, _, _>(event_cache_store.load_all_chunks(room_id).await.unwrap())
.unwrap()
.unwrap();
assert_eq!(linked_chunk.chunks().count(), 1);
let mut chunks = linked_chunk.chunks();
assert_matches!(chunks.next().unwrap().content(), ChunkContent::Items(events) => {
assert_eq!(events.len(), 1);
let ev = events[0].raw().deserialize().unwrap();
assert_let!(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = ev);
let original = msg.as_original().unwrap();
assert_eq!(original.content.body(), "hey yo");
assert!(original.unsigned.relations.replace.is_none());
});
// That's all, folks!
assert!(chunks.next().is_none());
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_clear() {
use eyeball_im::VectorDiff;
use matrix_sdk_base::linked_chunk::lazy_loader::from_all_chunks;
use crate::{assert_let_timeout, event_cache::RoomEventCacheUpdate};
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let event_cache_store = Arc::new(MemoryStore::new());
let event_id1 = event_id!("$1");
let event_id2 = event_id!("$2");
let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(event_id1).into_event();
let ev2 = f.text_msg("how's it going").sender(*BOB).event_id(event_id2).into_event();
// Prefill the store with some data.
event_cache_store
.handle_linked_chunk_updates(
room_id,
vec![
// An empty items chunk.
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(0),
next: None,
},
// A gap chunk.
Update::NewGapChunk {
previous: Some(ChunkIdentifier::new(0)),
// Chunk IDs aren't supposed to be ordered, so use a random value here.
new: ChunkIdentifier::new(42),
next: None,
gap: Gap { prev_token: "comté".to_owned() },
},
// Another items chunk, non-empty this time.
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(42)),
new: ChunkIdentifier::new(1),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(1), 0),
items: vec![ev1.clone()],
},
// And another items chunk, non-empty again.
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(1)),
new: ChunkIdentifier::new(2),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(2), 0),
items: vec![ev2.clone()],
},
],
)
.await
.unwrap();
let client = MockClientBuilder::new("http://localhost".to_owned())
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
.build()
.await;
let event_cache = client.event_cache();
// Don't forget to subscribe and like^W enable storage!
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
let (items, mut stream) = room_event_cache.subscribe().await;
// The rooms knows about all cached events.
{
assert!(room_event_cache.event(event_id1).await.is_some());
assert!(room_event_cache.event(event_id2).await.is_some());
}
// But only part of events are loaded from the store
{
// The room must contain only one event because only one chunk has been loaded.
assert_eq!(items.len(), 1);
assert_eq!(items[0].event_id().unwrap(), event_id2);
assert!(stream.is_empty());
}
// Let's load more chunks to load all events.
{
room_event_cache.pagination().run_backwards_once(20).await.unwrap();
assert_let_timeout!(
Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
);
assert_eq!(diffs.len(), 1);
assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value: event } => {
// Here you are `event_id1`!
assert_eq!(event.event_id().unwrap(), event_id1);
});
assert!(stream.is_empty());
}
// After clearing,…
room_event_cache.clear().await.unwrap();
//… we get an update that the content has been cleared.
assert_let_timeout!(
Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
);
assert_eq!(diffs.len(), 1);
assert_let!(VectorDiff::Clear = &diffs[0]);
// The room event cache has forgotten about the events.
assert!(room_event_cache.event(event_id1).await.is_none());
let (items, _) = room_event_cache.subscribe().await;
assert!(items.is_empty());
// The event cache store too.
let linked_chunk =
from_all_chunks::<3, _, _>(event_cache_store.load_all_chunks(room_id).await.unwrap())
.unwrap()
.unwrap();
// Note: while the event cache store could return `None` here, clearing it will
// reset it to its initial form, maintaining the invariant that it
// contains a single items chunk that's empty.
assert_eq!(linked_chunk.num_items(), 0);
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_load_from_storage() {
use eyeball_im::VectorDiff;
use super::RoomEventCacheUpdate;
use crate::assert_let_timeout;
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let event_cache_store = Arc::new(MemoryStore::new());
let event_id1 = event_id!("$1");
let event_id2 = event_id!("$2");
let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(event_id1).into_event();
let ev2 = f.text_msg("how's it going").sender(*BOB).event_id(event_id2).into_event();
// Prefill the store with some data.
event_cache_store
.handle_linked_chunk_updates(
room_id,
vec![
// An empty items chunk.
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(0),
next: None,
},
// A gap chunk.
Update::NewGapChunk {
previous: Some(ChunkIdentifier::new(0)),
// Chunk IDs aren't supposed to be ordered, so use a random value here.
new: ChunkIdentifier::new(42),
next: None,
gap: Gap { prev_token: "cheddar".to_owned() },
},
// Another items chunk, non-empty this time.
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(42)),
new: ChunkIdentifier::new(1),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(1), 0),
items: vec![ev1.clone()],
},
// And another items chunk, non-empty again.
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(1)),
new: ChunkIdentifier::new(2),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(2), 0),
items: vec![ev2.clone()],
},
],
)
.await
.unwrap();
let client = MockClientBuilder::new("http://localhost".to_owned())
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
.build()
.await;
let event_cache = client.event_cache();
// Don't forget to subscribe and like^W enable storage!
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
let (items, mut stream) = room_event_cache.subscribe().await;
// The initial items contain one event because only the last chunk is loaded by
// default.
assert_eq!(items.len(), 1);
assert_eq!(items[0].event_id().unwrap(), event_id2);
assert!(stream.is_empty());
// The event cache knows only all events though, even if they aren't loaded.
assert!(room_event_cache.event(event_id1).await.is_some());
assert!(room_event_cache.event(event_id2).await.is_some());
// Let's paginate to load more events.
room_event_cache.pagination().run_backwards_once(20).await.unwrap();
assert_let_timeout!(
Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
);
assert_eq!(diffs.len(), 1);
assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value: event } => {
assert_eq!(event.event_id().unwrap(), event_id1);
});
assert!(stream.is_empty());
// A new update with one of these events leads to deduplication.
let timeline = Timeline { limited: false, prev_batch: None, events: vec![ev2] };
room_event_cache
.inner
.handle_joined_room_update(true, JoinedRoomUpdate { timeline, ..Default::default() })
.await
.unwrap();
// The stream doesn't report these changes *yet*. Use the items vector given
// when subscribing, to check that the items correspond to their new
// positions. The duplicated item is removed (so it's not the first
// element anymore), and it's added to the back of the list.
let (items, _stream) = room_event_cache.subscribe().await;
assert_eq!(items.len(), 2);
assert_eq!(items[0].event_id().unwrap(), event_id1);
assert_eq!(items[1].event_id().unwrap(), event_id2);
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_load_from_storage_resilient_to_failure() {
let room_id = room_id!("!fondue:patate.ch");
let event_cache_store = Arc::new(MemoryStore::new());
let event = EventFactory::new()
.room(room_id)
.sender(user_id!("@ben:saucisse.bzh"))
.text_msg("foo")
.event_id(event_id!("$42"))
.into_event();
// Prefill the store with invalid data: two chunks that form a cycle.
event_cache_store
.handle_linked_chunk_updates(
room_id,
vec![
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(0),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(0), 0),
items: vec![event],
},
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(0)),
new: ChunkIdentifier::new(1),
next: Some(ChunkIdentifier::new(0)),
},
],
)
.await
.unwrap();
let client = MockClientBuilder::new("http://localhost".to_owned())
.store_config(
StoreConfig::new("holder".to_owned()).event_cache_store(event_cache_store.clone()),
)
.build()
.await;
let event_cache = client.event_cache();
// Don't forget to subscribe and like^W enable storage!
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
let (items, _stream) = room_event_cache.subscribe().await;
// Because the persisted content was invalid, the room store is reset: there are
// no events in the cache.
assert!(items.is_empty());
// Storage doesn't contain anything. It would also be valid that it contains a
// single initial empty items chunk.
let raw_chunks = event_cache_store.load_all_chunks(room_id).await.unwrap();
assert!(raw_chunks.is_empty());
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_no_useless_gaps() {
use crate::event_cache::room::LoadMoreEventsBackwardsOutcome;
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
let has_storage = true; // for testing purposes only
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
let f = EventFactory::new().room(room_id).sender(*ALICE);
// Propagate an update including a limited timeline with one message and a
// prev-batch token.
room_event_cache
.inner
.handle_joined_room_update(
has_storage,
JoinedRoomUpdate {
timeline: Timeline {
limited: true,
prev_batch: Some("raclette".to_owned()),
events: vec![f.text_msg("hey yo").into_event()],
},
..Default::default()
},
)
.await
.unwrap();
{
let mut state = room_event_cache.inner.state.write().await;
let mut num_gaps = 0;
let mut num_events = 0;
for c in state.events().chunks() {
match c.content() {
ChunkContent::Items(items) => num_events += items.len(),
ChunkContent::Gap(_) => num_gaps += 1,
}
}
// The limited sync unloads the chunk, so it will appear as if there are only
// the events.
assert_eq!(num_gaps, 0);
assert_eq!(num_events, 1);
// But if I manually reload more of the chunk, the gap will be present.
assert_matches!(
state.load_more_events_backwards().await.unwrap(),
LoadMoreEventsBackwardsOutcome::Gap { .. }
);
num_gaps = 0;
num_events = 0;
for c in state.events().chunks() {
match c.content() {
ChunkContent::Items(items) => num_events += items.len(),
ChunkContent::Gap(_) => num_gaps += 1,
}
}
// The gap must have been stored.
assert_eq!(num_gaps, 1);
assert_eq!(num_events, 1);
}
// Now, propagate an update for another message, but the timeline isn't limited
// this time.
room_event_cache
.inner
.handle_joined_room_update(
has_storage,
JoinedRoomUpdate {
timeline: Timeline {
limited: false,
prev_batch: Some("fondue".to_owned()),
events: vec![f.text_msg("sup").into_event()],
},
..Default::default()
},
)
.await
.unwrap();
{
let state = room_event_cache.inner.state.read().await;
let mut num_gaps = 0;
let mut num_events = 0;
for c in state.events().chunks() {
match c.content() {
ChunkContent::Items(items) => num_events += items.len(),
ChunkContent::Gap(gap) => {
assert_eq!(gap.prev_token, "raclette");
num_gaps += 1;
}
}
}
// There's only the previous gap, no new ones.
assert_eq!(num_gaps, 1);
assert_eq!(num_events, 2);
}
}
async fn assert_relations(
room_id: &RoomId,
original_event: TimelineEvent,
related_event: TimelineEvent,
event_factory: EventFactory,
) {
let client = logged_in_client(None).await;
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Save the original event.
let original_event_id = original_event.event_id().unwrap();
room_event_cache.save_event(original_event).await;
// Save an unrelated event to check it's not in the related events list.
let unrelated_id = event_id!("$2");
room_event_cache
.save_event(event_factory.text_msg("An unrelated event").event_id(unrelated_id).into())
.await;
// Save the related event.
let related_id = related_event.event_id().unwrap();
room_event_cache.save_event(related_event).await;
let (event, related_events) =
room_event_cache.event_with_relations(&original_event_id, None).await.unwrap();
// Fetched event is the right one.
let cached_event_id = event.event_id().unwrap();
assert_eq!(cached_event_id, original_event_id);
// There is only the actually related event in the related ones
assert_eq!(related_events.len(), 1);
let related_event_id = related_events[0].event_id().unwrap();
assert_eq!(related_event_id, related_id);
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_shrink_to_last_chunk() {
use eyeball_im::VectorDiff;
use crate::{assert_let_timeout, event_cache::RoomEventCacheUpdate};
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let f = EventFactory::new().room(room_id);
let evid1 = event_id!("$1");
let evid2 = event_id!("$2");
let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(evid1).into_event();
let ev2 = f.text_msg("howdy").sender(*BOB).event_id(evid2).into_event();
// Fill the event cache store with an initial linked chunk with 2 events chunks.
{
let store = client.event_cache_store();
let store = store.lock().await.unwrap();
store
.handle_linked_chunk_updates(
room_id,
vec![
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(0),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(0), 0),
items: vec![ev1],
},
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(0)),
new: ChunkIdentifier::new(1),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(1), 0),
items: vec![ev2],
},
],
)
.await
.unwrap();
}
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Sanity check: lazily loaded, so only includes one item at start.
let (events, mut stream) = room_event_cache.subscribe().await;
assert_eq!(events.len(), 1);
assert_eq!(events[0].event_id().as_deref(), Some(evid2));
assert!(stream.is_empty());
// Force loading the full linked chunk by back-paginating.
let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
assert_eq!(outcome.events.len(), 1);
assert_eq!(outcome.events[0].event_id().as_deref(), Some(evid1));
assert!(outcome.reached_start);
// We also get an update about the loading from the store.
assert_let_timeout!(
Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
);
assert_eq!(diffs.len(), 1);
assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value } => {
assert_eq!(value.event_id().as_deref(), Some(evid1));
});
assert!(stream.is_empty());
// Shrink the linked chunk to the last chunk.
let diffs = room_event_cache
.inner
.state
.write()
.await
.shrink_to_last_chunk()
.await
.expect("shrinking should succeed")
.unwrap();
// We receive updates about the changes to the linked chunk.
assert_eq!(diffs.len(), 2);
assert_matches!(&diffs[0], VectorDiff::Clear);
assert_matches!(&diffs[1], VectorDiff::Append { values} => {
assert_eq!(values.len(), 1);
assert_eq!(values[0].event_id().as_deref(), Some(evid2));
});
assert!(stream.is_empty());
// When reading the events, we do get only the last one.
let (events, _) = room_event_cache.subscribe().await;
assert_eq!(events.len(), 1);
assert_eq!(events[0].event_id().as_deref(), Some(evid2));
// But if we back-paginate, we don't need access to network to find out about
// the previous event.
let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
assert_eq!(outcome.events.len(), 1);
assert_eq!(outcome.events[0].event_id().as_deref(), Some(evid1));
assert!(outcome.reached_start);
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_auto_shrink_after_all_subscribers_are_gone() {
use eyeball_im::VectorDiff;
use tokio::task::yield_now;
use crate::{assert_let_timeout, event_cache::RoomEventCacheUpdate};
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let f = EventFactory::new().room(room_id);
let evid1 = event_id!("$1");
let evid2 = event_id!("$2");
let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(evid1).into_event();
let ev2 = f.text_msg("howdy").sender(*BOB).event_id(evid2).into_event();
// Fill the event cache store with an initial linked chunk with 2 events chunks.
{
let store = client.event_cache_store();
let store = store.lock().await.unwrap();
store
.handle_linked_chunk_updates(
room_id,
vec![
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(0),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(0), 0),
items: vec![ev1],
},
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(0)),
new: ChunkIdentifier::new(1),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(1), 0),
items: vec![ev2],
},
],
)
.await
.unwrap();
}
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
let room = client.get_room(room_id).unwrap();
let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
// Sanity check: lazily loaded, so only includes one item at start.
let (events1, mut stream1) = room_event_cache.subscribe().await;
assert_eq!(events1.len(), 1);
assert_eq!(events1[0].event_id().as_deref(), Some(evid2));
assert!(stream1.is_empty());
// Force loading the full linked chunk by back-paginating.
let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
assert_eq!(outcome.events.len(), 1);
assert_eq!(outcome.events[0].event_id().as_deref(), Some(evid1));
assert!(outcome.reached_start);
// We also get an update about the loading from the store. Ignore it, for this
// test's sake.
assert_let_timeout!(
Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream1.recv()
);
assert_eq!(diffs.len(), 1);
assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value } => {
assert_eq!(value.event_id().as_deref(), Some(evid1));
});
assert!(stream1.is_empty());
// Have another listener subscribe to the event cache.
// Since it's not the first one, and the previous one loaded some more events,
// the second listener seems them all.
let (events2, stream2) = room_event_cache.subscribe().await;
assert_eq!(events2.len(), 2);
assert_eq!(events2[0].event_id().as_deref(), Some(evid1));
assert_eq!(events2[1].event_id().as_deref(), Some(evid2));
assert!(stream2.is_empty());
// Drop the first stream, and wait a bit.
drop(stream1);
yield_now().await;
// The second stream remains undisturbed.
assert!(stream2.is_empty());
// Now drop the second stream, and wait a bit.
drop(stream2);
yield_now().await;
// The linked chunk must have auto-shrunk by now.
{
// Check the inner state: there's no more shared auto-shrinker.
let state = room_event_cache.inner.state.read().await;
assert_eq!(state.listener_count.load(std::sync::atomic::Ordering::SeqCst), 0);
}
// Getting the events will only give us the latest chunk.
let (events3, _stream2) = room_event_cache.subscribe().await;
assert_eq!(events3.len(), 1);
assert_eq!(events3[0].event_id().as_deref(), Some(evid2));
}
}