Changeset View
Changeset View
Standalone View
Standalone View
src/client/actions/sync.cpp
| Show First 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | ClientResult updateClient(ClientModel m, SyncAction) | ||||
| // Let initial sync return immediately | // Let initial sync return immediately | ||||
| isInitialSync ? 0 : m.syncTimeoutMs | isInitialSync ? 0 : m.syncTimeoutMs | ||||
| ) | ) | ||||
| .withData(json{{"is", isInitialSync ? "initial" : "incremental"}})); | .withData(json{{"is", isInitialSync ? "initial" : "incremental"}})); | ||||
| return { m, lager::noop }; | return { m, lager::noop }; | ||||
| } | } | ||||
| static KazvEventList loadRoomsFromSyncInPlace(ClientModel &m, SyncJob::Rooms rooms) | static KazvTriggerList loadRoomsFromSyncInPlace(ClientModel &m, SyncJob::Rooms rooms) | ||||
| { | { | ||||
| auto l = std::move(m.roomList); | auto l = std::move(m.roomList); | ||||
| auto eventsToEmit = KazvEventList{}.transient(); | auto eventsToEmit = KazvTriggerList{}.transient(); | ||||
| auto pushRules = PushRulesDesc(m.accountData["m.push_rules"]); | auto pushRules = PushRulesDesc(m.accountData["m.push_rules"]); | ||||
| auto updateRoomImpl = | auto updateRoomImpl = | ||||
| [&l](auto id, auto a) { | [&l](auto id, auto a) { | ||||
| l = RoomListModel::update( | l = RoomListModel::update( | ||||
| std::move(l), | std::move(l), | ||||
| UpdateRoomAction{std::move(id), std::move(a)}); | UpdateRoomAction{std::move(id), std::move(a)}); | ||||
| Show All 9 Lines | static KazvTriggerList loadRoomsFromSyncInPlace(ClientModel &m, SyncJob::Rooms rooms) | ||||
| intoImmer( | intoImmer( | ||||
| EventList{}, | EventList{}, | ||||
| zug::map([=](Event e) { | zug::map([=](Event e) { | ||||
| return Event::fromSync(e, id); | return Event::fromSync(e, id); | ||||
| }), | }), | ||||
| room.timeline.events); | room.timeline.events); | ||||
| eventsToEmit.append( | eventsToEmit.append( | ||||
| intoImmer( | intoImmer( | ||||
| KazvEventList{}, | KazvTriggerList{}, | ||||
| zug::map([=](Event e) -> KazvEvent { | zug::map([=](Event e) -> KazvTrigger { | ||||
| return ReceivingRoomTimelineEvent{std::move(e), id}; | return ReceivingRoomTimelineEvent{std::move(e), id}; | ||||
| }), | }), | ||||
| timelineEvents).transient()); | timelineEvents).transient()); | ||||
| updateRoomImpl(id, AddToTimelineAction{timelineEvents, | updateRoomImpl(id, AddToTimelineAction{timelineEvents, | ||||
| room.timeline.prevBatch, | room.timeline.prevBatch, | ||||
| room.timeline.limited, | room.timeline.limited, | ||||
| std::nullopt // we do not have a gapEventId | std::nullopt // we do not have a gapEventId | ||||
| }); | }); | ||||
| if (room.state) { | if (room.state) { | ||||
| eventsToEmit.append( | eventsToEmit.append( | ||||
| intoImmer( | intoImmer( | ||||
| KazvEventList{}, | KazvTriggerList{}, | ||||
| zug::map([=](Event e) -> KazvEvent { | zug::map([=](Event e) -> KazvTrigger { | ||||
| return ReceivingRoomStateEvent{std::move(e), id}; | return ReceivingRoomStateEvent{std::move(e), id}; | ||||
| }), | }), | ||||
| room.state.value().events).transient()); | room.state.value().events).transient()); | ||||
| updateRoomImpl(id, AddStateEventsAction{room.state.value().events}); | updateRoomImpl(id, AddStateEventsAction{room.state.value().events}); | ||||
| } | } | ||||
| // Process state events in timeline, which should have arrived later | // Process state events in timeline, which should have arrived later | ||||
| // than those in room.state . | // than those in room.state . | ||||
| updateRoomImpl(id, AddStateEventsAction{ | updateRoomImpl(id, AddStateEventsAction{ | ||||
| intoImmer(EventList{}, | intoImmer(EventList{}, | ||||
| zug::filter([=](Event e) { | zug::filter([=](Event e) { | ||||
| return e.isState(); | return e.isState(); | ||||
| }), | }), | ||||
| timelineEvents)}); | timelineEvents)}); | ||||
| if (room.accountData) { | if (room.accountData) { | ||||
| eventsToEmit.append( | eventsToEmit.append( | ||||
| intoImmer( | intoImmer( | ||||
| KazvEventList{}, | KazvTriggerList{}, | ||||
| zug::map([=](Event e) -> KazvEvent { | zug::map([=](Event e) -> KazvTrigger { | ||||
| return ReceivingRoomAccountDataEvent{std::move(e), id}; | return ReceivingRoomAccountDataEvent{std::move(e), id}; | ||||
| }), | }), | ||||
| room.accountData.value().events).transient()); | room.accountData.value().events).transient()); | ||||
| updateRoomImpl(id, AddAccountDataAction{room.accountData.value().events}); | updateRoomImpl(id, AddAccountDataAction{room.accountData.value().events}); | ||||
| } | } | ||||
| }; | }; | ||||
| auto updateRoomSummary = | auto updateRoomSummary = | ||||
| ▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | static KazvTriggerList loadRoomsFromSyncInPlace(ClientModel &m, SyncJob::Rooms rooms) | ||||
| for (const auto &[id, room]: rooms.leave) { | for (const auto &[id, room]: rooms.leave) { | ||||
| updateLeftRoom(id, room); | updateLeftRoom(id, room); | ||||
| } | } | ||||
| m.roomList = std::move(l); | m.roomList = std::move(l); | ||||
| return eventsToEmit.persistent(); | return eventsToEmit.persistent(); | ||||
| } | } | ||||
| static KazvEventList loadPresenceFromSyncInPlace(ClientModel &m, EventList presence) | static KazvTriggerList loadPresenceFromSyncInPlace(ClientModel &m, EventList presence) | ||||
| { | { | ||||
| auto eventsToEmit = intoImmer( | auto eventsToEmit = intoImmer( | ||||
| KazvEventList{}, | KazvTriggerList{}, | ||||
| zug::map([](Event e) { return ReceivingPresenceEvent{e}; }), | zug::map([](Event e) { return ReceivingPresenceEvent{e}; }), | ||||
| presence); | presence); | ||||
| m.presence = merge(std::move(m.presence), presence, keyOfPresence); | m.presence = merge(std::move(m.presence), presence, keyOfPresence); | ||||
| return eventsToEmit; | return eventsToEmit; | ||||
| } | } | ||||
| static KazvEventList loadAccountDataFromSyncInPlace(ClientModel &m, EventList accountData) | static KazvTriggerList loadAccountDataFromSyncInPlace(ClientModel &m, EventList accountData) | ||||
| { | { | ||||
| auto eventsToEmit = intoImmer( | auto eventsToEmit = intoImmer( | ||||
| KazvEventList{}, | KazvTriggerList{}, | ||||
| zug::map([](Event e) { return ReceivingPresenceEvent{e}; }), | zug::map([](Event e) { return ReceivingPresenceEvent{e}; }), | ||||
| accountData); | accountData); | ||||
| m.accountData = merge(std::move(m.accountData), accountData, keyOfAccountData); | m.accountData = merge(std::move(m.accountData), accountData, keyOfAccountData); | ||||
| return eventsToEmit; | return eventsToEmit; | ||||
| } | } | ||||
| static KazvEventList loadToDeviceFromSyncInPlace(ClientModel &m, JsonWrap toDevice) | static KazvTriggerList loadToDeviceFromSyncInPlace(ClientModel &m, JsonWrap toDevice) | ||||
| { | { | ||||
| if (toDevice.get().contains("events")) { | if (toDevice.get().contains("events")) { | ||||
| auto events = toDevice.get()["events"]; | auto events = toDevice.get()["events"]; | ||||
| auto msgs = intoImmer( | auto msgs = intoImmer( | ||||
| EventList{}, | EventList{}, | ||||
| zug::map([](json j) { | zug::map([](json j) { | ||||
| // Prevent malicious server from injecting a room id | // Prevent malicious server from injecting a room id | ||||
| j.erase("room_id"); | j.erase("room_id"); | ||||
| return Event(j); | return Event(j); | ||||
| }), | }), | ||||
| events); | events); | ||||
| m.toDevice = std::move(m.toDevice) + msgs; | m.toDevice = std::move(m.toDevice) + msgs; | ||||
| return intoImmer( | return intoImmer( | ||||
| KazvEventList{}, | KazvTriggerList{}, | ||||
| zug::map([](Event e) { return ReceivingToDeviceMessage{e}; }), | zug::map([](Event e) { return ReceivingToDeviceMessage{e}; }), | ||||
| msgs); | msgs); | ||||
| } | } | ||||
| return {}; | return {}; | ||||
| } | } | ||||
| ClientResult processResponse(ClientModel m, SyncResponse r) | ClientResult processResponse(ClientModel m, SyncResponse r) | ||||
| { | { | ||||
| if (! r.success()) { | if (! r.success()) { | ||||
| m.addTrigger(SyncFailed{}); | |||||
| kzo.client.dbg() << "Sync failed" << std::endl; | kzo.client.dbg() << "Sync failed" << std::endl; | ||||
| kzo.client.dbg() << r.statusCode << std::endl; | kzo.client.dbg() << r.statusCode << std::endl; | ||||
| if (isBodyJson(r.body)) { | if (isBodyJson(r.body)) { | ||||
| auto j = r.jsonBody(); | auto j = r.jsonBody(); | ||||
| kzo.client.dbg() << "Json says: " << j.get().dump() << std::endl; | kzo.client.dbg() << "Json says: " << j.get().dump() << std::endl; | ||||
| } else { | } else { | ||||
| kzo.client.dbg() << "Response body: " | kzo.client.dbg() << "Response body: " | ||||
| << std::get<BaseJob::BytesBody>(r.body) << std::endl; | << std::get<BaseJob::BytesBody>(r.body) << std::endl; | ||||
| ▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | ClientResult processResponse(ClientModel m, SyncResponse r) | ||||
| // deviceOneTimeKeysCount | // deviceOneTimeKeysCount | ||||
| m.withCrypto([&](auto &c) { c.setUploadedOneTimeKeysCount(r.deviceOneTimeKeysCount()); }); | m.withCrypto([&](auto &c) { c.setUploadedOneTimeKeysCount(r.deviceOneTimeKeysCount()); }); | ||||
| auto model = tryDecryptEvents(std::move(m)); | auto model = tryDecryptEvents(std::move(m)); | ||||
| m = std::move(model); | m = std::move(model); | ||||
| } | } | ||||
| m.addTrigger(SyncSuccessful{r.nextBatch()}); | |||||
| return { std::move(m), lager::noop }; | return { std::move(m), lager::noop }; | ||||
| } | } | ||||
| ClientResult updateClient(ClientModel m, SetShouldSyncAction a) | ClientResult updateClient(ClientModel m, SetShouldSyncAction a) | ||||
| { | { | ||||
| m.shouldSync = a.shouldSync; | m.shouldSync = a.shouldSync; | ||||
| return { std::move(m), lager::noop }; | return { std::move(m), lager::noop }; | ||||
| } | } | ||||
| Show All 27 Lines | namespace Kazv | ||||
| } | } | ||||
| ClientResult processResponse(ClientModel m, DefineFilterResponse r) | ClientResult processResponse(ClientModel m, DefineFilterResponse r) | ||||
| { | { | ||||
| auto is = r.dataStr("is"); | auto is = r.dataStr("is"); | ||||
| if (! r.success()) { | if (! r.success()) { | ||||
| m.syncing = false; | m.syncing = false; | ||||
| kzo.client.dbg() << "posting filter failed: " << r.errorCode() << r.errorMessage() << std::endl; | kzo.client.dbg() << "posting filter failed: " << r.errorCode() << r.errorMessage() << std::endl; | ||||
| m.addTrigger(PostInitialFiltersFailed{r.errorCode(), r.errorMessage()}); | |||||
| return { std::move(m), lager::noop }; | return { std::move(m), lager::noop }; | ||||
| } | } | ||||
| kzo.client.dbg() << "filter " << is << " is posted" << std::endl; | kzo.client.dbg() << "filter " << is << " is posted" << std::endl; | ||||
| if (is == "incrementalSyncFilter") { | if (is == "incrementalSyncFilter") { | ||||
| m.incrementalSyncFilterId = r.filterId(); | m.incrementalSyncFilterId = r.filterId(); | ||||
| m.addTrigger(PostInitialFiltersSuccessful{}); | |||||
| } else { | } else { | ||||
| m.initialSyncFilterId = r.filterId(); | m.initialSyncFilterId = r.filterId(); | ||||
| } | } | ||||
| return { std::move(m), lager::noop }; | return { std::move(m), lager::noop }; | ||||
| } | } | ||||
| } | } | ||||