|
36 | 36 | import synapse.metrics |
37 | 37 | from synapse.api.constants import EventContentFields, EventTypes, RelationTypes |
38 | 38 | from synapse.api.room_versions import RoomVersions |
39 | | -from synapse.events import EventBase # noqa: F401 |
40 | | -from synapse.events.snapshot import EventContext # noqa: F401 |
| 39 | +from synapse.events import EventBase, relation_from_event |
| 40 | +from synapse.events.snapshot import EventContext |
41 | 41 | from synapse.storage._base import db_to_json, make_in_list_sql_clause |
42 | 42 | from synapse.storage.database import ( |
43 | 43 | DatabasePool, |
@@ -1807,52 +1807,45 @@ def _handle_event_relations( |
1807 | 1807 | txn: The current database transaction. |
1808 | 1808 | event: The event which might have relations. |
1809 | 1809 | """ |
1810 | | - relation = event.content.get("m.relates_to") |
| 1810 | + relation = relation_from_event(event) |
1811 | 1811 | if not relation: |
1812 | | - # No relations |
| 1812 | + # No relation, nothing to do. |
1813 | 1813 | return |
1814 | 1814 |
|
1815 | | - # Relations must have a type and parent event ID. |
1816 | | - rel_type = relation.get("rel_type") |
1817 | | - if not isinstance(rel_type, str): |
1818 | | - return |
1819 | | - |
1820 | | - parent_id = relation.get("event_id") |
1821 | | - if not isinstance(parent_id, str): |
1822 | | - return |
1823 | | - |
1824 | | - # Annotations have a key field. |
1825 | | - aggregation_key = None |
1826 | | - if rel_type == RelationTypes.ANNOTATION: |
1827 | | - aggregation_key = relation.get("key") |
1828 | | - |
1829 | 1815 | self.db_pool.simple_insert_txn( |
1830 | 1816 | txn, |
1831 | 1817 | table="event_relations", |
1832 | 1818 | values={ |
1833 | 1819 | "event_id": event.event_id, |
1834 | | - "relates_to_id": parent_id, |
1835 | | - "relation_type": rel_type, |
1836 | | - "aggregation_key": aggregation_key, |
| 1820 | + "relates_to_id": relation.parent_id, |
| 1821 | + "relation_type": relation.rel_type, |
| 1822 | + "aggregation_key": relation.aggregation_key, |
1837 | 1823 | }, |
1838 | 1824 | ) |
1839 | 1825 |
|
1840 | | - txn.call_after(self.store.get_relations_for_event.invalidate, (parent_id,)) |
1841 | 1826 | txn.call_after( |
1842 | | - self.store.get_aggregation_groups_for_event.invalidate, (parent_id,) |
| 1827 | + self.store.get_relations_for_event.invalidate, (relation.parent_id,) |
| 1828 | + ) |
| 1829 | + txn.call_after( |
| 1830 | + self.store.get_aggregation_groups_for_event.invalidate, |
| 1831 | + (relation.parent_id,), |
1843 | 1832 | ) |
1844 | 1833 |
|
1845 | | - if rel_type == RelationTypes.REPLACE: |
1846 | | - txn.call_after(self.store.get_applicable_edit.invalidate, (parent_id,)) |
| 1834 | + if relation.rel_type == RelationTypes.REPLACE: |
| 1835 | + txn.call_after( |
| 1836 | + self.store.get_applicable_edit.invalidate, (relation.parent_id,) |
| 1837 | + ) |
1847 | 1838 |
|
1848 | | - if rel_type == RelationTypes.THREAD: |
1849 | | - txn.call_after(self.store.get_thread_summary.invalidate, (parent_id,)) |
| 1839 | + if relation.rel_type == RelationTypes.THREAD: |
| 1840 | + txn.call_after( |
| 1841 | + self.store.get_thread_summary.invalidate, (relation.parent_id,) |
| 1842 | + ) |
1850 | 1843 | # It should be safe to only invalidate the cache if the user has not |
1851 | 1844 | # previously participated in the thread, but that's difficult (and |
1852 | 1845 | # potentially error-prone) so it is always invalidated. |
1853 | 1846 | txn.call_after( |
1854 | 1847 | self.store.get_thread_participated.invalidate, |
1855 | | - (parent_id, event.sender), |
| 1848 | + (relation.parent_id, event.sender), |
1856 | 1849 | ) |
1857 | 1850 |
|
1858 | 1851 | def _handle_insertion_event( |
|
0 commit comments