summaryrefslogtreecommitdiff
path: root/src/service/rooms/event_handler/mod.rs
blob: b01a282833a2ff626b6020df58413f8238a42d81 (plain)
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
/// An async function that can recursively call itself.
type AsyncRecursiveType<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;

use ruma::{
    api::federation::discovery::{get_remote_server_keys, get_server_keys},
    CanonicalJsonObject, CanonicalJsonValue, OwnedServerName, OwnedServerSigningKeyId,
    RoomVersionId,
};
use std::{
    collections::{hash_map, BTreeMap, HashMap, HashSet},
    pin::Pin,
    sync::{Arc, RwLock, RwLockWriteGuard},
    time::{Duration, Instant, SystemTime},
};
use tokio::sync::Semaphore;

use futures_util::{stream::FuturesUnordered, Future, StreamExt};
use ruma::{
    api::{
        client::error::ErrorKind,
        federation::{
            discovery::get_remote_server_keys_batch::{self, v2::QueryCriteria},
            event::{get_event, get_room_state_ids},
            membership::create_join_event,
        },
    },
    events::{
        room::{create::RoomCreateEventContent, server_acl::RoomServerAclEventContent},
        StateEventType,
    },
    int,
    serde::Base64,
    state_res::{self, RoomVersion, StateMap},
    uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName,
};
use serde_json::value::RawValue as RawJsonValue;
use tracing::{debug, error, info, trace, warn};

use crate::{service::*, services, Error, PduEvent, Result};

pub struct Service;

impl Service {
    /// When receiving an event one needs to:
    /// 0. Check the server is in the room
    /// 1. Skip the PDU if we already know about it
    /// 1.1. Remove unsigned field
    /// 2. Check signatures, otherwise drop
    /// 3. Check content hash, redact if doesn't match
    /// 4. Fetch any missing auth events doing all checks listed here starting at 1. These are not
    ///    timeline events
    /// 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are
    ///    also rejected "due to auth events"
    /// 6. Reject "due to auth events" if the event doesn't pass auth based on the auth events
    /// 7. Persist this event as an outlier
    /// 8. If not timeline event: stop
    /// 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline
    ///    events
    /// 10. Fetch missing state and auth chain events by calling /state_ids at backwards extremities
    ///     doing all the checks in this list starting at 1. These are not timeline events
    /// 11. Check the auth of the event passes based on the state of the event
    /// 12. Ensure that the state is derived from the previous current state (i.e. we calculated by
    ///     doing state res where one of the inputs was a previously trusted set of state, don't just
    ///     trust a set of state we got from a remote)
    /// 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail"
    ///     it
    /// 14. Use state resolution to find new room state
    // We use some AsyncRecursiveType hacks here so we can call this async funtion recursively
    #[tracing::instrument(skip(self, value, is_timeline_event, pub_key_map))]
    pub(crate) async fn handle_incoming_pdu<'a>(
        &self,
        origin: &'a ServerName,
        event_id: &'a EventId,
        room_id: &'a RoomId,
        value: BTreeMap<String, CanonicalJsonValue>,
        is_timeline_event: bool,
        pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> Result<Option<Vec<u8>>> {
        // 0. Check the server is in the room
        if !services().rooms.metadata.exists(room_id)? {
            return Err(Error::BadRequest(
                ErrorKind::NotFound,
                "Room is unknown to this server",
            ));
        }

        if services().rooms.metadata.is_disabled(room_id)? {
            return Err(Error::BadRequest(
                ErrorKind::Forbidden,
                "Federation of this room is currently disabled on this server.",
            ));
        }

        // 1. Skip the PDU if we already have it as a timeline event
        if let Some(pdu_id) = services().rooms.timeline.get_pdu_id(event_id)? {
            return Ok(Some(pdu_id.to_vec()));
        }

        let create_event = services()
            .rooms
            .state_accessor
            .room_state_get(room_id, &StateEventType::RoomCreate, "")?
            .ok_or_else(|| Error::bad_database("Failed to find create event in db."))?;

        let create_event_content: RoomCreateEventContent =
            serde_json::from_str(create_event.content.get()).map_err(|e| {
                error!("Invalid create event: {}", e);
                Error::BadDatabase("Invalid create event in db")
            })?;
        let room_version_id = &create_event_content.room_version;

        let first_pdu_in_room = services()
            .rooms
            .timeline
            .first_pdu_in_room(room_id)?
            .ok_or_else(|| Error::bad_database("Failed to find first pdu in db."))?;

        let (incoming_pdu, val) = self
            .handle_outlier_pdu(origin, &create_event, event_id, room_id, value, pub_key_map)
            .await?;

        // 8. if not timeline event: stop
        if !is_timeline_event {
            return Ok(None);
        }

        // Skip old events
        if incoming_pdu.origin_server_ts < first_pdu_in_room.origin_server_ts {
            return Ok(None);
        }

        // 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline events
        let (sorted_prev_events, mut eventid_info) = self
            .fetch_unknown_prev_events(
                origin,
                &create_event,
                room_id,
                room_version_id,
                pub_key_map,
                incoming_pdu.prev_events.clone(),
            )
            .await?;

        let mut errors = 0;
        debug!(events = ?sorted_prev_events, "Got previous events");
        for prev_id in sorted_prev_events {
            // Check for disabled again because it might have changed
            if services().rooms.metadata.is_disabled(room_id)? {
                return Err(Error::BadRequest(
                    ErrorKind::Forbidden,
                    "Federation of this room is currently disabled on this server.",
                ));
            }

            if let Some((time, tries)) = services()
                .globals
                .bad_event_ratelimiter
                .read()
                .unwrap()
                .get(&*prev_id)
            {
                // Exponential backoff
                let mut min_elapsed_duration = Duration::from_secs(5 * 60) * (*tries) * (*tries);
                if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
                    min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
                }

                if time.elapsed() < min_elapsed_duration {
                    info!("Backing off from {}", prev_id);
                    continue;
                }
            }

            if errors >= 5 {
                break;
            }

            if let Some((pdu, json)) = eventid_info.remove(&*prev_id) {
                // Skip old events
                if pdu.origin_server_ts < first_pdu_in_room.origin_server_ts {
                    continue;
                }

                let start_time = Instant::now();
                services()
                    .globals
                    .roomid_federationhandletime
                    .write()
                    .unwrap()
                    .insert(room_id.to_owned(), ((*prev_id).to_owned(), start_time));

                if let Err(e) = self
                    .upgrade_outlier_to_timeline_pdu(
                        pdu,
                        json,
                        &create_event,
                        origin,
                        room_id,
                        pub_key_map,
                    )
                    .await
                {
                    errors += 1;
                    warn!("Prev event {} failed: {}", prev_id, e);
                    match services()
                        .globals
                        .bad_event_ratelimiter
                        .write()
                        .unwrap()
                        .entry((*prev_id).to_owned())
                    {
                        hash_map::Entry::Vacant(e) => {
                            e.insert((Instant::now(), 1));
                        }
                        hash_map::Entry::Occupied(mut e) => {
                            *e.get_mut() = (Instant::now(), e.get().1 + 1)
                        }
                    }
                }
                let elapsed = start_time.elapsed();
                services()
                    .globals
                    .roomid_federationhandletime
                    .write()
                    .unwrap()
                    .remove(&room_id.to_owned());
                warn!(
                    "Handling prev event {} took {}m{}s",
                    prev_id,
                    elapsed.as_secs() / 60,
                    elapsed.as_secs() % 60
                );
            }
        }

        // Done with prev events, now handling the incoming event

        let start_time = Instant::now();
        services()
            .globals
            .roomid_federationhandletime
            .write()
            .unwrap()
            .insert(room_id.to_owned(), (event_id.to_owned(), start_time));
        let r = services()
            .rooms
            .event_handler
            .upgrade_outlier_to_timeline_pdu(
                incoming_pdu,
                val,
                &create_event,
                origin,
                room_id,
                pub_key_map,
            )
            .await;
        services()
            .globals
            .roomid_federationhandletime
            .write()
            .unwrap()
            .remove(&room_id.to_owned());

        r
    }

    #[tracing::instrument(skip(self, create_event, value, pub_key_map))]
    fn handle_outlier_pdu<'a>(
        &'a self,
        origin: &'a ServerName,
        create_event: &'a PduEvent,
        event_id: &'a EventId,
        room_id: &'a RoomId,
        mut value: BTreeMap<String, CanonicalJsonValue>,
        pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> AsyncRecursiveType<'a, Result<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>> {
        Box::pin(async move {
            // 1.1. Remove unsigned field
            value.remove("unsigned");

            // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json

            // We go through all the signatures we see on the value and fetch the corresponding signing
            // keys
            self.fetch_required_signing_keys(&value, pub_key_map)
                .await?;

            // 2. Check signatures, otherwise drop
            // 3. check content hash, redact if doesn't match
            let create_event_content: RoomCreateEventContent =
                serde_json::from_str(create_event.content.get()).map_err(|e| {
                    error!("Invalid create event: {}", e);
                    Error::BadDatabase("Invalid create event in db")
                })?;

            let room_version_id = &create_event_content.room_version;
            let room_version =
                RoomVersion::new(room_version_id).expect("room version is supported");

            let mut val = match ruma::signatures::verify_event(
                &pub_key_map.read().expect("RwLock is poisoned."),
                &value,
                room_version_id,
            ) {
                Err(e) => {
                    // Drop
                    warn!("Dropping bad event {}: {}", event_id, e);
                    return Err(Error::BadRequest(
                        ErrorKind::InvalidParam,
                        "Signature verification failed",
                    ));
                }
                Ok(ruma::signatures::Verified::Signatures) => {
                    // Redact
                    warn!("Calculated hash does not match: {}", event_id);
                    match ruma::canonical_json::redact(value, room_version_id, None) {
                        Ok(obj) => obj,
                        Err(_) => {
                            return Err(Error::BadRequest(
                                ErrorKind::InvalidParam,
                                "Redaction failed",
                            ))
                        }
                    }
                }
                Ok(ruma::signatures::Verified::All) => value,
            };

            // Now that we have checked the signature and hashes we can add the eventID and convert
            // to our PduEvent type
            val.insert(
                "event_id".to_owned(),
                CanonicalJsonValue::String(event_id.as_str().to_owned()),
            );
            let incoming_pdu = serde_json::from_value::<PduEvent>(
                serde_json::to_value(&val).expect("CanonicalJsonObj is a valid JsonValue"),
            )
            .map_err(|_| Error::bad_database("Event is not a valid PDU."))?;

            // 4. fetch any missing auth events doing all checks listed here starting at 1. These are not timeline events
            // 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are also rejected "due to auth events"
            // NOTE: Step 5 is not applied anymore because it failed too often
            debug!(event_id = ?incoming_pdu.event_id, "Fetching auth events");
            self.fetch_and_handle_outliers(
                origin,
                &incoming_pdu
                    .auth_events
                    .iter()
                    .map(|x| Arc::from(&**x))
                    .collect::<Vec<_>>(),
                create_event,
                room_id,
                room_version_id,
                pub_key_map,
            )
            .await;

            // 6. Reject "due to auth events" if the event doesn't pass auth based on the auth events
            info!(
                "Auth check for {} based on auth events",
                incoming_pdu.event_id
            );

            // Build map of auth events
            let mut auth_events = HashMap::new();
            for id in &incoming_pdu.auth_events {
                let auth_event = match services().rooms.timeline.get_pdu(id)? {
                    Some(e) => e,
                    None => {
                        warn!("Could not find auth event {}", id);
                        continue;
                    }
                };

                match auth_events.entry((
                    auth_event.kind.to_string().into(),
                    auth_event
                        .state_key
                        .clone()
                        .expect("all auth events have state keys"),
                )) {
                    hash_map::Entry::Vacant(v) => {
                        v.insert(auth_event);
                    }
                    hash_map::Entry::Occupied(_) => {
                        return Err(Error::BadRequest(
                            ErrorKind::InvalidParam,
                            "Auth event's type and state_key combination exists multiple times.",
                        ));
                    }
                }
            }

            // The original create event must be in the auth events
            if !matches!(
                auth_events
                    .get(&(StateEventType::RoomCreate, "".to_owned()))
                    .map(|a| a.as_ref()),
                Some(_) | None
            ) {
                return Err(Error::BadRequest(
                    ErrorKind::InvalidParam,
                    "Incoming event refers to wrong create event.",
                ));
            }

            if !state_res::event_auth::auth_check(
                &room_version,
                &incoming_pdu,
                None::<PduEvent>, // TODO: third party invite
                |k, s| auth_events.get(&(k.to_string().into(), s.to_owned())),
            )
            .map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed"))?
            {
                return Err(Error::BadRequest(
                    ErrorKind::InvalidParam,
                    "Auth check failed",
                ));
            }

            info!("Validation successful.");

            // 7. Persist the event as an outlier.
            services()
                .rooms
                .outlier
                .add_pdu_outlier(&incoming_pdu.event_id, &val)?;

            info!("Added pdu as outlier.");

            Ok((Arc::new(incoming_pdu), val))
        })
    }

    #[tracing::instrument(skip(self, incoming_pdu, val, create_event, pub_key_map))]
    pub async fn upgrade_outlier_to_timeline_pdu(
        &self,
        incoming_pdu: Arc<PduEvent>,
        val: BTreeMap<String, CanonicalJsonValue>,
        create_event: &PduEvent,
        origin: &ServerName,
        room_id: &RoomId,
        pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> Result<Option<Vec<u8>>> {
        // Skip the PDU if we already have it as a timeline event
        if let Ok(Some(pduid)) = services().rooms.timeline.get_pdu_id(&incoming_pdu.event_id) {
            return Ok(Some(pduid));
        }

        if services()
            .rooms
            .pdu_metadata
            .is_event_soft_failed(&incoming_pdu.event_id)?
        {
            return Err(Error::BadRequest(
                ErrorKind::InvalidParam,
                "Event has been soft failed",
            ));
        }

        info!("Upgrading {} to timeline pdu", incoming_pdu.event_id);

        let create_event_content: RoomCreateEventContent =
            serde_json::from_str(create_event.content.get()).map_err(|e| {
                warn!("Invalid create event: {}", e);
                Error::BadDatabase("Invalid create event in db")
            })?;

        let room_version_id = &create_event_content.room_version;
        let room_version = RoomVersion::new(room_version_id).expect("room version is supported");

        // 10. Fetch missing state and auth chain events by calling /state_ids at backwards extremities
        //     doing all the checks in this list starting at 1. These are not timeline events.

        // TODO: if we know the prev_events of the incoming event we can avoid the request and build
        // the state from a known point and resolve if > 1 prev_event

        info!("Requesting state at event");
        let mut state_at_incoming_event = None;

        if incoming_pdu.prev_events.len() == 1 {
            let prev_event = &*incoming_pdu.prev_events[0];
            let prev_event_sstatehash = services()
                .rooms
                .state_accessor
                .pdu_shortstatehash(prev_event)?;

            let state = if let Some(shortstatehash) = prev_event_sstatehash {
                Some(
                    services()
                        .rooms
                        .state_accessor
                        .state_full_ids(shortstatehash)
                        .await,
                )
            } else {
                None
            };

            if let Some(Ok(mut state)) = state {
                info!("Using cached state");
                let prev_pdu = services()
                    .rooms
                    .timeline
                    .get_pdu(prev_event)
                    .ok()
                    .flatten()
                    .ok_or_else(|| {
                        Error::bad_database("Could not find prev event, but we know the state.")
                    })?;

                if let Some(state_key) = &prev_pdu.state_key {
                    let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
                        &prev_pdu.kind.to_string().into(),
                        state_key,
                    )?;

                    state.insert(shortstatekey, Arc::from(prev_event));
                    // Now it's the state after the pdu
                }

                state_at_incoming_event = Some(state);
            }
        } else {
            info!("Calculating state at event using state res");
            let mut extremity_sstatehashes = HashMap::new();

            let mut okay = true;
            for prev_eventid in &incoming_pdu.prev_events {
                let prev_event =
                    if let Ok(Some(pdu)) = services().rooms.timeline.get_pdu(prev_eventid) {
                        pdu
                    } else {
                        okay = false;
                        break;
                    };

                let sstatehash = if let Ok(Some(s)) = services()
                    .rooms
                    .state_accessor
                    .pdu_shortstatehash(prev_eventid)
                {
                    s
                } else {
                    okay = false;
                    break;
                };

                extremity_sstatehashes.insert(sstatehash, prev_event);
            }

            if okay {
                let mut fork_states = Vec::with_capacity(extremity_sstatehashes.len());
                let mut auth_chain_sets = Vec::with_capacity(extremity_sstatehashes.len());

                for (sstatehash, prev_event) in extremity_sstatehashes {
                    let mut leaf_state: HashMap<_, _> = services()
                        .rooms
                        .state_accessor
                        .state_full_ids(sstatehash)
                        .await?;

                    if let Some(state_key) = &prev_event.state_key {
                        let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
                            &prev_event.kind.to_string().into(),
                            state_key,
                        )?;
                        leaf_state.insert(shortstatekey, Arc::from(&*prev_event.event_id));
                        // Now it's the state after the pdu
                    }

                    let mut state = StateMap::with_capacity(leaf_state.len());
                    let mut starting_events = Vec::with_capacity(leaf_state.len());

                    for (k, id) in leaf_state {
                        if let Ok((ty, st_key)) = services().rooms.short.get_statekey_from_short(k)
                        {
                            // FIXME: Undo .to_string().into() when StateMap
                            //        is updated to use StateEventType
                            state.insert((ty.to_string().into(), st_key), id.clone());
                        } else {
                            warn!("Failed to get_statekey_from_short.");
                        }
                        starting_events.push(id);
                    }

                    auth_chain_sets.push(
                        services()
                            .rooms
                            .auth_chain
                            .get_auth_chain(room_id, starting_events)
                            .await?
                            .collect(),
                    );

                    fork_states.push(state);
                }

                let lock = services().globals.stateres_mutex.lock();

                let result =
                    state_res::resolve(room_version_id, &fork_states, auth_chain_sets, |id| {
                        let res = services().rooms.timeline.get_pdu(id);
                        if let Err(e) = &res {
                            error!("LOOK AT ME Failed to fetch event: {}", e);
                        }
                        res.ok().flatten()
                    });
                drop(lock);

                state_at_incoming_event = match result {
                    Ok(new_state) => Some(
                        new_state
                            .into_iter()
                            .map(|((event_type, state_key), event_id)| {
                                let shortstatekey =
                                    services().rooms.short.get_or_create_shortstatekey(
                                        &event_type.to_string().into(),
                                        &state_key,
                                    )?;
                                Ok((shortstatekey, event_id))
                            })
                            .collect::<Result<_>>()?,
                    ),
                    Err(e) => {
                        warn!("State resolution on prev events failed, either an event could not be found or deserialization: {}", e);
                        None
                    }
                }
            }
        }

        if state_at_incoming_event.is_none() {
            info!("Calling /state_ids");
            // Call /state_ids to find out what the state at this pdu is. We trust the server's
            // response to some extend, but we still do a lot of checks on the events
            match services()
                .sending
                .send_federation_request(
                    origin,
                    get_room_state_ids::v1::Request {
                        room_id: room_id.to_owned(),
                        event_id: (*incoming_pdu.event_id).to_owned(),
                    },
                )
                .await
            {
                Ok(res) => {
                    info!("Fetching state events at event.");
                    let state_vec = self
                        .fetch_and_handle_outliers(
                            origin,
                            &res.pdu_ids
                                .iter()
                                .map(|x| Arc::from(&**x))
                                .collect::<Vec<_>>(),
                            create_event,
                            room_id,
                            room_version_id,
                            pub_key_map,
                        )
                        .await;

                    let mut state: HashMap<_, Arc<EventId>> = HashMap::new();
                    for (pdu, _) in state_vec {
                        let state_key = pdu.state_key.clone().ok_or_else(|| {
                            Error::bad_database("Found non-state pdu in state events.")
                        })?;

                        let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
                            &pdu.kind.to_string().into(),
                            &state_key,
                        )?;

                        match state.entry(shortstatekey) {
                            hash_map::Entry::Vacant(v) => {
                                v.insert(Arc::from(&*pdu.event_id));
                            }
                            hash_map::Entry::Occupied(_) => return Err(
                                Error::bad_database("State event's type and state_key combination exists multiple times."),
                            ),
                        }
                    }

                    // The original create event must still be in the state
                    let create_shortstatekey = services()
                        .rooms
                        .short
                        .get_shortstatekey(&StateEventType::RoomCreate, "")?
                        .expect("Room exists");

                    if state.get(&create_shortstatekey).map(|id| id.as_ref())
                        != Some(&create_event.event_id)
                    {
                        return Err(Error::bad_database(
                            "Incoming event refers to wrong create event.",
                        ));
                    }

                    state_at_incoming_event = Some(state);
                }
                Err(e) => {
                    warn!("Fetching state for event failed: {}", e);
                    return Err(e);
                }
            };
        }

        let state_at_incoming_event =
            state_at_incoming_event.expect("we always set this to some above");

        info!("Starting auth check");
        // 11. Check the auth of the event passes based on the state of the event
        let check_result = state_res::event_auth::auth_check(
            &room_version,
            &incoming_pdu,
            None::<PduEvent>, // TODO: third party invite
            |k, s| {
                services()
                    .rooms
                    .short
                    .get_shortstatekey(&k.to_string().into(), s)
                    .ok()
                    .flatten()
                    .and_then(|shortstatekey| state_at_incoming_event.get(&shortstatekey))
                    .and_then(|event_id| services().rooms.timeline.get_pdu(event_id).ok().flatten())
            },
        )
        .map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed."))?;

        if !check_result {
            return Err(Error::bad_database(
                "Event has failed auth check with state at the event.",
            ));
        }
        info!("Auth check succeeded");

        // We start looking at current room state now, so lets lock the room

        let mutex_state = Arc::clone(
            services()
                .globals
                .roomid_mutex_state
                .write()
                .unwrap()
                .entry(room_id.to_owned())
                .or_default(),
        );
        let state_lock = mutex_state.lock().await;

        // Now we calculate the set of extremities this room has after the incoming event has been
        // applied. We start with the previous extremities (aka leaves)
        info!("Calculating extremities");
        let mut extremities = services().rooms.state.get_forward_extremities(room_id)?;

        // Remove any forward extremities that are referenced by this incoming event's prev_events
        for prev_event in &incoming_pdu.prev_events {
            if extremities.contains(prev_event) {
                extremities.remove(prev_event);
            }
        }

        // Only keep those extremities were not referenced yet
        extremities.retain(|id| {
            !matches!(
                services()
                    .rooms
                    .pdu_metadata
                    .is_event_referenced(room_id, id),
                Ok(true)
            )
        });

        info!("Compressing state at event");
        let state_ids_compressed = state_at_incoming_event
            .iter()
            .map(|(shortstatekey, id)| {
                services()
                    .rooms
                    .state_compressor
                    .compress_state_event(*shortstatekey, id)
            })
            .collect::<Result<_>>()?;

        // 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it
        info!("Starting soft fail auth check");

        let auth_events = services().rooms.state.get_auth_events(
            room_id,
            &incoming_pdu.kind,
            &incoming_pdu.sender,
            incoming_pdu.state_key.as_deref(),
            &incoming_pdu.content,
        )?;

        let soft_fail = !state_res::event_auth::auth_check(
            &room_version,
            &incoming_pdu,
            None::<PduEvent>,
            |k, s| auth_events.get(&(k.clone(), s.to_owned())),
        )
        .map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed."))?;

        if soft_fail {
            services().rooms.timeline.append_incoming_pdu(
                &incoming_pdu,
                val,
                extremities.iter().map(|e| (**e).to_owned()).collect(),
                state_ids_compressed,
                soft_fail,
                &state_lock,
            )?;

            // Soft fail, we keep the event as an outlier but don't add it to the timeline
            warn!("Event was soft failed: {:?}", incoming_pdu);
            services()
                .rooms
                .pdu_metadata
                .mark_event_soft_failed(&incoming_pdu.event_id)?;
            return Err(Error::BadRequest(
                ErrorKind::InvalidParam,
                "Event has been soft failed",
            ));
        }

        if incoming_pdu.state_key.is_some() {
            info!("Loading current room state ids");
            let current_sstatehash = services()
                .rooms
                .state
                .get_room_shortstatehash(room_id)?
                .expect("every room has state");

            let current_state_ids = services()
                .rooms
                .state_accessor
                .state_full_ids(current_sstatehash)
                .await?;

            info!("Preparing for stateres to derive new room state");
            let mut extremity_sstatehashes = HashMap::new();

            info!(?extremities, "Loading extremities");
            for id in &extremities {
                match services().rooms.timeline.get_pdu(id)? {
                    Some(leaf_pdu) => {
                        extremity_sstatehashes.insert(
                            services()
                                .rooms
                                .state_accessor
                                .pdu_shortstatehash(&leaf_pdu.event_id)?
                                .ok_or_else(|| {
                                    error!(
                                        "Found extremity pdu with no statehash in db: {:?}",
                                        leaf_pdu
                                    );
                                    Error::bad_database("Found pdu with no statehash in db.")
                                })?,
                            leaf_pdu,
                        );
                    }
                    _ => {
                        error!("Missing state snapshot for {:?}", id);
                        return Err(Error::BadDatabase("Missing state snapshot."));
                    }
                }
            }

            let mut fork_states = Vec::new();

            // 12. Ensure that the state is derived from the previous current state (i.e. we calculated
            //     by doing state res where one of the inputs was a previously trusted set of state,
            //     don't just trust a set of state we got from a remote).

            // We do this by adding the current state to the list of fork states
            extremity_sstatehashes.remove(&current_sstatehash);
            fork_states.push(current_state_ids);

            // We also add state after incoming event to the fork states
            let mut state_after = state_at_incoming_event.clone();
            if let Some(state_key) = &incoming_pdu.state_key {
                let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
                    &incoming_pdu.kind.to_string().into(),
                    state_key,
                )?;

                state_after.insert(shortstatekey, Arc::from(&*incoming_pdu.event_id));
            }
            fork_states.push(state_after);

            let mut update_state = false;
            // 14. Use state resolution to find new room state
            let new_room_state = if fork_states.is_empty() {
                panic!("State is empty");
            } else if fork_states.iter().skip(1).all(|f| &fork_states[0] == f) {
                info!("State resolution trivial");
                // There was only one state, so it has to be the room's current state (because that is
                // always included)
                fork_states[0]
                    .iter()
                    .map(|(k, id)| {
                        services()
                            .rooms
                            .state_compressor
                            .compress_state_event(*k, id)
                    })
                    .collect::<Result<_>>()?
            } else {
                info!("Loading auth chains");
                // We do need to force an update to this room's state
                update_state = true;

                let mut auth_chain_sets = Vec::new();
                for state in &fork_states {
                    auth_chain_sets.push(
                        services()
                            .rooms
                            .auth_chain
                            .get_auth_chain(
                                room_id,
                                state.iter().map(|(_, id)| id.clone()).collect(),
                            )
                            .await?
                            .collect(),
                    );
                }

                info!("Loading fork states");

                let fork_states: Vec<_> = fork_states
                    .into_iter()
                    .map(|map| {
                        map.into_iter()
                            .filter_map(|(k, id)| {
                                services()
                                    .rooms
                                    .short
                                    .get_statekey_from_short(k)
                                    .map(|(ty, st_key)| ((ty.to_string().into(), st_key), id))
                                    .ok()
                            })
                            .collect::<StateMap<_>>()
                    })
                    .collect();

                info!("Resolving state");

                let lock = services().globals.stateres_mutex.lock();
                let state = match state_res::resolve(
                    room_version_id,
                    &fork_states,
                    auth_chain_sets,
                    |id| {
                        let res = services().rooms.timeline.get_pdu(id);
                        if let Err(e) = &res {
                            error!("LOOK AT ME Failed to fetch event: {}", e);
                        }
                        res.ok().flatten()
                    },
                ) {
                    Ok(new_state) => new_state,
                    Err(_) => {
                        return Err(Error::bad_database("State resolution failed, either an event could not be found or deserialization"));
                    }
                };

                drop(lock);

                info!("State resolution done. Compressing state");

                state
                    .into_iter()
                    .map(|((event_type, state_key), event_id)| {
                        let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
                            &event_type.to_string().into(),
                            &state_key,
                        )?;
                        services()
                            .rooms
                            .state_compressor
                            .compress_state_event(shortstatekey, &event_id)
                    })
                    .collect::<Result<_>>()?
            };

            // Set the new room state to the resolved state
            if update_state {
                info!("Forcing new room state");
                let (sstatehash, new, removed) = services()
                    .rooms
                    .state_compressor
                    .save_state(room_id, new_room_state)?;
                services()
                    .rooms
                    .state
                    .force_state(room_id, sstatehash, new, removed, &state_lock)
                    .await?;
            }
        }

        info!("Appending pdu to timeline");
        extremities.insert(incoming_pdu.event_id.clone());

        // Now that the event has passed all auth it is added into the timeline.
        // We use the `state_at_event` instead of `state_after` so we accurately
        // represent the state for this event.

        let pdu_id = services().rooms.timeline.append_incoming_pdu(
            &incoming_pdu,
            val,
            extremities.iter().map(|e| (**e).to_owned()).collect(),
            state_ids_compressed,
            soft_fail,
            &state_lock,
        )?;

        info!("Appended incoming pdu");

        // Event has passed all auth/stateres checks
        drop(state_lock);
        Ok(pdu_id)
    }

    /// Find the event and auth it. Once the event is validated (steps 1 - 8)
    /// it is appended to the outliers Tree.
    ///
    /// Returns pdu and if we fetched it over federation the raw json.
    ///
    /// a. Look in the main timeline (pduid_pdu tree)
    /// b. Look at outlier pdu tree
    /// c. Ask origin server over federation
    /// d. TODO: Ask other servers over federation?
    #[tracing::instrument(skip_all)]
    pub(crate) fn fetch_and_handle_outliers<'a>(
        &'a self,
        origin: &'a ServerName,
        events: &'a [Arc<EventId>],
        create_event: &'a PduEvent,
        room_id: &'a RoomId,
        room_version_id: &'a RoomVersionId,
        pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> AsyncRecursiveType<'a, Vec<(Arc<PduEvent>, Option<BTreeMap<String, CanonicalJsonValue>>)>>
    {
        Box::pin(async move {
            let back_off = |id| match services()
                .globals
                .bad_event_ratelimiter
                .write()
                .unwrap()
                .entry(id)
            {
                hash_map::Entry::Vacant(e) => {
                    e.insert((Instant::now(), 1));
                }
                hash_map::Entry::Occupied(mut e) => *e.get_mut() = (Instant::now(), e.get().1 + 1),
            };

            let mut pdus = vec![];
            for id in events {
                if let Some((time, tries)) = services()
                    .globals
                    .bad_event_ratelimiter
                    .read()
                    .unwrap()
                    .get(&**id)
                {
                    // Exponential backoff
                    let mut min_elapsed_duration =
                        Duration::from_secs(5 * 60) * (*tries) * (*tries);
                    if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
                        min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
                    }

                    if time.elapsed() < min_elapsed_duration {
                        info!("Backing off from {}", id);
                        continue;
                    }
                }

                // a. Look in the main timeline (pduid_pdu tree)
                // b. Look at outlier pdu tree
                // (get_pdu_json checks both)
                if let Ok(Some(local_pdu)) = services().rooms.timeline.get_pdu(id) {
                    trace!("Found {} in db", id);
                    pdus.push((local_pdu, None));
                    continue;
                }

                // c. Ask origin server over federation
                // We also handle its auth chain here so we don't get a stack overflow in
                // handle_outlier_pdu.
                let mut todo_auth_events = vec![Arc::clone(id)];
                let mut events_in_reverse_order = Vec::new();
                let mut events_all = HashSet::new();
                let mut i = 0;
                while let Some(next_id) = todo_auth_events.pop() {
                    if events_all.contains(&next_id) {
                        continue;
                    }

                    i += 1;
                    if i % 100 == 0 {
                        tokio::task::yield_now().await;
                    }

                    if let Ok(Some(_)) = services().rooms.timeline.get_pdu(&next_id) {
                        trace!("Found {} in db", id);
                        continue;
                    }

                    info!("Fetching {} over federation.", next_id);
                    match services()
                        .sending
                        .send_federation_request(
                            origin,
                            get_event::v1::Request {
                                event_id: (*next_id).to_owned(),
                            },
                        )
                        .await
                    {
                        Ok(res) => {
                            info!("Got {} over federation", next_id);
                            let (calculated_event_id, value) =
                                match pdu::gen_event_id_canonical_json(&res.pdu, room_version_id) {
                                    Ok(t) => t,
                                    Err(_) => {
                                        back_off((*next_id).to_owned());
                                        continue;
                                    }
                                };

                            if calculated_event_id != *next_id {
                                warn!("Server didn't return event id we requested: requested: {}, we got {}. Event: {:?}",
                                    next_id, calculated_event_id, &res.pdu);
                            }

                            if let Some(auth_events) =
                                value.get("auth_events").and_then(|c| c.as_array())
                            {
                                for auth_event in auth_events {
                                    if let Ok(auth_event) =
                                        serde_json::from_value(auth_event.clone().into())
                                    {
                                        let a: Arc<EventId> = auth_event;
                                        todo_auth_events.push(a);
                                    } else {
                                        warn!("Auth event id is not valid");
                                    }
                                }
                            } else {
                                warn!("Auth event list invalid");
                            }

                            events_in_reverse_order.push((next_id.clone(), value));
                            events_all.insert(next_id);
                        }
                        Err(_) => {
                            warn!("Failed to fetch event: {}", next_id);
                            back_off((*next_id).to_owned());
                        }
                    }
                }

                for (next_id, value) in events_in_reverse_order.iter().rev() {
                    match self
                        .handle_outlier_pdu(
                            origin,
                            create_event,
                            next_id,
                            room_id,
                            value.clone(),
                            pub_key_map,
                        )
                        .await
                    {
                        Ok((pdu, json)) => {
                            if next_id == id {
                                pdus.push((pdu, Some(json)));
                            }
                        }
                        Err(e) => {
                            warn!("Authentication of event {} failed: {:?}", next_id, e);
                            back_off((**next_id).to_owned());
                        }
                    }
                }
            }
            pdus
        })
    }

    async fn fetch_unknown_prev_events(
        &self,
        origin: &ServerName,
        create_event: &PduEvent,
        room_id: &RoomId,
        room_version_id: &RoomVersionId,
        pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
        initial_set: Vec<Arc<EventId>>,
    ) -> Result<(
        Vec<Arc<EventId>>,
        HashMap<Arc<EventId>, (Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>,
    )> {
        let mut graph: HashMap<Arc<EventId>, _> = HashMap::new();
        let mut eventid_info = HashMap::new();
        let mut todo_outlier_stack: Vec<Arc<EventId>> = initial_set;

        let first_pdu_in_room = services()
            .rooms
            .timeline
            .first_pdu_in_room(room_id)?
            .ok_or_else(|| Error::bad_database("Failed to find first pdu in db."))?;

        let mut amount = 0;

        while let Some(prev_event_id) = todo_outlier_stack.pop() {
            if let Some((pdu, json_opt)) = self
                .fetch_and_handle_outliers(
                    origin,
                    &[prev_event_id.clone()],
                    create_event,
                    room_id,
                    room_version_id,
                    pub_key_map,
                )
                .await
                .pop()
            {
                if amount > services().globals.max_fetch_prev_events() {
                    // Max limit reached
                    warn!("Max prev event limit reached!");
                    graph.insert(prev_event_id.clone(), HashSet::new());
                    continue;
                }

                if let Some(json) = json_opt.or_else(|| {
                    services()
                        .rooms
                        .outlier
                        .get_outlier_pdu_json(&prev_event_id)
                        .ok()
                        .flatten()
                }) {
                    if pdu.origin_server_ts > first_pdu_in_room.origin_server_ts {
                        amount += 1;
                        for prev_prev in &pdu.prev_events {
                            if !graph.contains_key(prev_prev) {
                                todo_outlier_stack.push(prev_prev.clone());
                            }
                        }

                        graph.insert(
                            prev_event_id.clone(),
                            pdu.prev_events.iter().cloned().collect(),
                        );
                    } else {
                        // Time based check failed
                        graph.insert(prev_event_id.clone(), HashSet::new());
                    }

                    eventid_info.insert(prev_event_id.clone(), (pdu, json));
                } else {
                    // Get json failed, so this was not fetched over federation
                    graph.insert(prev_event_id.clone(), HashSet::new());
                }
            } else {
                // Fetch and handle failed
                graph.insert(prev_event_id.clone(), HashSet::new());
            }
        }

        let sorted = state_res::lexicographical_topological_sort(&graph, |event_id| {
            // This return value is the key used for sorting events,
            // events are then sorted by power level, time,
            // and lexically by event_id.
            Ok((
                int!(0),
                MilliSecondsSinceUnixEpoch(
                    eventid_info
                        .get(event_id)
                        .map_or_else(|| uint!(0), |info| info.0.origin_server_ts),
                ),
            ))
        })
        .map_err(|_| Error::bad_database("Error sorting prev events"))?;

        Ok((sorted, eventid_info))
    }

    #[tracing::instrument(skip_all)]
    pub(crate) async fn fetch_required_signing_keys(
        &self,
        event: &BTreeMap<String, CanonicalJsonValue>,
        pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> Result<()> {
        let signatures = event
            .get("signatures")
            .ok_or(Error::BadServerResponse(
                "No signatures in server response pdu.",
            ))?
            .as_object()
            .ok_or(Error::BadServerResponse(
                "Invalid signatures object in server response pdu.",
            ))?;

        // We go through all the signatures we see on the value and fetch the corresponding signing
        // keys
        for (signature_server, signature) in signatures {
            let signature_object = signature.as_object().ok_or(Error::BadServerResponse(
                "Invalid signatures content object in server response pdu.",
            ))?;

            let signature_ids = signature_object.keys().cloned().collect::<Vec<_>>();

            let fetch_res = self
                .fetch_signing_keys(
                    signature_server.as_str().try_into().map_err(|_| {
                        Error::BadServerResponse(
                            "Invalid servername in signatures of server response pdu.",
                        )
                    })?,
                    signature_ids,
                )
                .await;

            let keys = match fetch_res {
                Ok(keys) => keys,
                Err(_) => {
                    warn!("Signature verification failed: Could not fetch signing key.",);
                    continue;
                }
            };

            pub_key_map
                .write()
                .map_err(|_| Error::bad_database("RwLock is poisoned."))?
                .insert(signature_server.clone(), keys);
        }

        Ok(())
    }

    // Gets a list of servers for which we don't have the signing key yet. We go over
    // the PDUs and either cache the key or add it to the list that needs to be retrieved.
    fn get_server_keys_from_cache(
        &self,
        pdu: &RawJsonValue,
        servers: &mut BTreeMap<OwnedServerName, BTreeMap<OwnedServerSigningKeyId, QueryCriteria>>,
        room_version: &RoomVersionId,
        pub_key_map: &mut RwLockWriteGuard<'_, BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> Result<()> {
        let value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
            error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
            Error::BadServerResponse("Invalid PDU in server response")
        })?;

        let event_id = format!(
            "${}",
            ruma::signatures::reference_hash(&value, room_version)
                .expect("ruma can calculate reference hashes")
        );
        let event_id = <&EventId>::try_from(event_id.as_str())
            .expect("ruma's reference hashes are valid event ids");

        if let Some((time, tries)) = services()
            .globals
            .bad_event_ratelimiter
            .read()
            .unwrap()
            .get(event_id)
        {
            // Exponential backoff
            let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
            if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
                min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
            }

            if time.elapsed() < min_elapsed_duration {
                debug!("Backing off from {}", event_id);
                return Err(Error::BadServerResponse("bad event, still backing off"));
            }
        }

        let signatures = value
            .get("signatures")
            .ok_or(Error::BadServerResponse(
                "No signatures in server response pdu.",
            ))?
            .as_object()
            .ok_or(Error::BadServerResponse(
                "Invalid signatures object in server response pdu.",
            ))?;

        for (signature_server, signature) in signatures {
            let signature_object = signature.as_object().ok_or(Error::BadServerResponse(
                "Invalid signatures content object in server response pdu.",
            ))?;

            let signature_ids = signature_object.keys().cloned().collect::<Vec<_>>();

            let contains_all_ids = |keys: &BTreeMap<String, Base64>| {
                signature_ids.iter().all(|id| keys.contains_key(id))
            };

            let origin = <&ServerName>::try_from(signature_server.as_str()).map_err(|_| {
                Error::BadServerResponse("Invalid servername in signatures of server response pdu.")
            })?;

            if servers.contains_key(origin) || pub_key_map.contains_key(origin.as_str()) {
                continue;
            }

            trace!("Loading signing keys for {}", origin);

            let result: BTreeMap<_, _> = services()
                .globals
                .signing_keys_for(origin)?
                .into_iter()
                .map(|(k, v)| (k.to_string(), v.key))
                .collect();

            if !contains_all_ids(&result) {
                trace!("Signing key not loaded for {}", origin);
                servers.insert(origin.to_owned(), BTreeMap::new());
            }

            pub_key_map.insert(origin.to_string(), result);
        }

        Ok(())
    }

    pub(crate) async fn fetch_join_signing_keys(
        &self,
        event: &create_join_event::v2::Response,
        room_version: &RoomVersionId,
        pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
    ) -> Result<()> {
        let mut servers: BTreeMap<
            OwnedServerName,
            BTreeMap<OwnedServerSigningKeyId, QueryCriteria>,
        > = BTreeMap::new();

        {
            let mut pkm = pub_key_map
                .write()
                .map_err(|_| Error::bad_database("RwLock is poisoned."))?;

            // Try to fetch keys, failure is okay
            // Servers we couldn't find in the cache will be added to `servers`
            for pdu in &event.room_state.state {
                let _ = self.get_server_keys_from_cache(pdu, &mut servers, room_version, &mut pkm);
            }
            for pdu in &event.room_state.auth_chain {
                let _ = self.get_server_keys_from_cache(pdu, &mut servers, room_version, &mut pkm);
            }

            drop(pkm);
        }

        if servers.is_empty() {
            info!("We had all keys locally");
            return Ok(());
        }

        for server in services().globals.trusted_servers() {
            info!("Asking batch signing keys from trusted server {}", server);
            if let Ok(keys) = services()
                .sending
                .send_federation_request(
                    server,
                    get_remote_server_keys_batch::v2::Request {
                        server_keys: servers.clone(),
                    },
                )
                .await
            {
                trace!("Got signing keys: {:?}", keys);
                let mut pkm = pub_key_map
                    .write()
                    .map_err(|_| Error::bad_database("RwLock is poisoned."))?;
                for k in keys.server_keys {
                    let k = match k.deserialize() {
                        Ok(key) => key,
                        Err(e) => {
                            warn!(
                                "Received error {} while fetching keys from trusted server {}",
                                e, server
                            );
                            warn!("{}", k.into_json());
                            continue;
                        }
                    };

                    // TODO: Check signature from trusted server?
                    servers.remove(&k.server_name);

                    let result = services()
                        .globals
                        .add_signing_key(&k.server_name, k.clone())?
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.key))
                        .collect::<BTreeMap<_, _>>();

                    pkm.insert(k.server_name.to_string(), result);
                }
            }

            if servers.is_empty() {
                info!("Trusted server supplied all signing keys");
                return Ok(());
            }
        }

        info!("Asking individual servers for signing keys: {servers:?}");
        let mut futures: FuturesUnordered<_> = servers
            .into_keys()
            .map(|server| async move {
                (
                    services()
                        .sending
                        .send_federation_request(&server, get_server_keys::v2::Request::new())
                        .await,
                    server,
                )
            })
            .collect();

        while let Some(result) = futures.next().await {
            info!("Received new result");
            if let (Ok(get_keys_response), origin) = result {
                info!("Result is from {origin}");
                if let Ok(key) = get_keys_response.server_key.deserialize() {
                    let result: BTreeMap<_, _> = services()
                        .globals
                        .add_signing_key(&origin, key)?
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.key))
                        .collect();
                    pub_key_map
                        .write()
                        .map_err(|_| Error::bad_database("RwLock is poisoned."))?
                        .insert(origin.to_string(), result);
                }
            }
            info!("Done handling result");
        }

        info!("Search for signing keys done");

        Ok(())
    }

    /// Returns Ok if the acl allows the server
    pub fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> {
        let acl_event = match services().rooms.state_accessor.room_state_get(
            room_id,
            &StateEventType::RoomServerAcl,
            "",
        )? {
            Some(acl) => acl,
            None => return Ok(()),
        };

        let acl_event_content: RoomServerAclEventContent =
            match serde_json::from_str(acl_event.content.get()) {
                Ok(content) => content,
                Err(_) => {
                    warn!("Invalid ACL event");
                    return Ok(());
                }
            };

        if acl_event_content.is_allowed(server_name) {
            Ok(())
        } else {
            Err(Error::BadRequest(
                ErrorKind::Forbidden,
                "Server was denied by ACL",
            ))
        }
    }

    /// Search the DB for the signing keys of the given server, if we don't have them
    /// fetch them from the server and save to our DB.
    #[tracing::instrument(skip_all)]
    pub async fn fetch_signing_keys(
        &self,
        origin: &ServerName,
        signature_ids: Vec<String>,
    ) -> Result<BTreeMap<String, Base64>> {
        let contains_all_ids =
            |keys: &BTreeMap<String, Base64>| signature_ids.iter().all(|id| keys.contains_key(id));

        let permit = services()
            .globals
            .servername_ratelimiter
            .read()
            .unwrap()
            .get(origin)
            .map(|s| Arc::clone(s).acquire_owned());

        let permit = match permit {
            Some(p) => p,
            None => {
                let mut write = services().globals.servername_ratelimiter.write().unwrap();
                let s = Arc::clone(
                    write
                        .entry(origin.to_owned())
                        .or_insert_with(|| Arc::new(Semaphore::new(1))),
                );

                s.acquire_owned()
            }
        }
        .await;

        let back_off = |id| match services()
            .globals
            .bad_signature_ratelimiter
            .write()
            .unwrap()
            .entry(id)
        {
            hash_map::Entry::Vacant(e) => {
                e.insert((Instant::now(), 1));
            }
            hash_map::Entry::Occupied(mut e) => *e.get_mut() = (Instant::now(), e.get().1 + 1),
        };

        if let Some((time, tries)) = services()
            .globals
            .bad_signature_ratelimiter
            .read()
            .unwrap()
            .get(&signature_ids)
        {
            // Exponential backoff
            let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
            if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
                min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
            }

            if time.elapsed() < min_elapsed_duration {
                debug!("Backing off from {:?}", signature_ids);
                return Err(Error::BadServerResponse("bad signature, still backing off"));
            }
        }

        trace!("Loading signing keys for {}", origin);

        let mut result: BTreeMap<_, _> = services()
            .globals
            .signing_keys_for(origin)?
            .into_iter()
            .map(|(k, v)| (k.to_string(), v.key))
            .collect();

        if contains_all_ids(&result) {
            return Ok(result);
        }

        debug!("Fetching signing keys for {} over federation", origin);

        if let Some(server_key) = services()
            .sending
            .send_federation_request(origin, get_server_keys::v2::Request::new())
            .await
            .ok()
            .and_then(|resp| resp.server_key.deserialize().ok())
        {
            services()
                .globals
                .add_signing_key(origin, server_key.clone())?;

            result.extend(
                server_key
                    .verify_keys
                    .into_iter()
                    .map(|(k, v)| (k.to_string(), v.key)),
            );
            result.extend(
                server_key
                    .old_verify_keys
                    .into_iter()
                    .map(|(k, v)| (k.to_string(), v.key)),
            );

            if contains_all_ids(&result) {
                return Ok(result);
            }
        }

        for server in services().globals.trusted_servers() {
            debug!("Asking {} for {}'s signing key", server, origin);
            if let Some(server_keys) = services()
                .sending
                .send_federation_request(
                    server,
                    get_remote_server_keys::v2::Request::new(
                        origin.to_owned(),
                        MilliSecondsSinceUnixEpoch::from_system_time(
                            SystemTime::now()
                                .checked_add(Duration::from_secs(3600))
                                .expect("SystemTime to large"),
                        )
                        .expect("time is valid"),
                    ),
                )
                .await
                .ok()
                .map(|resp| {
                    resp.server_keys
                        .into_iter()
                        .filter_map(|e| e.deserialize().ok())
                        .collect::<Vec<_>>()
                })
            {
                trace!("Got signing keys: {:?}", server_keys);
                for k in server_keys {
                    services().globals.add_signing_key(origin, k.clone())?;
                    result.extend(
                        k.verify_keys
                            .into_iter()
                            .map(|(k, v)| (k.to_string(), v.key)),
                    );
                    result.extend(
                        k.old_verify_keys
                            .into_iter()
                            .map(|(k, v)| (k.to_string(), v.key)),
                    );
                }

                if contains_all_ids(&result) {
                    return Ok(result);
                }
            }
        }

        drop(permit);

        back_off(signature_ids);

        warn!("Failed to find public key for server: {}", origin);
        Err(Error::BadServerResponse(
            "Failed to find public key for server",
        ))
    }
}