Changeset View
Changeset View
Standalone View
Standalone View
src/client/room/room-model.cpp
| Show First 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | RoomModel RoomModel::update(RoomModel r, Action a) | ||||
| const auto &e = *it; | const auto &e = *it; | ||||
| auto k = keyOfState(e); | auto k = keyOfState(e); | ||||
| if (!r.stateEvents.count(k)) { | if (!r.stateEvents.count(k)) { | ||||
| r.stateEvents = std::move(r.stateEvents).set(k, e); | r.stateEvents = std::move(r.stateEvents).set(k, e); | ||||
| } | } | ||||
| } | } | ||||
| return r; | return r; | ||||
| }, | }, | ||||
| [&](AddToTimelineAction a) { | [&](AddMessagesAction a) { | ||||
| auto eventIds = intoImmer(immer::flex_vector<std::string>(), | |||||
| zug::map(keyOfTimeline), a.events); | |||||
| auto oldMessages = r.messages; | |||||
| r.messages = merge(std::move(r.messages), a.events, keyOfTimeline); | r.messages = merge(std::move(r.messages), a.events, keyOfTimeline); | ||||
| auto exists = | |||||
| [=](auto eventId) -> bool { | |||||
| return !! oldMessages.find(eventId); | |||||
| }; | |||||
| auto key = | |||||
| [=](auto eventId) { | |||||
| // sort first by timestamp, then by id | |||||
| return sortKeyForTimelineEvent(r.messages[eventId]); | |||||
| }; | |||||
| auto handleRedaction = | auto handleRedaction = | ||||
| [&r](const auto &event) { | [&r](const auto &event) { | ||||
| if (event.type() == "m.room.redaction") { | if (event.type() == "m.room.redaction") { | ||||
| auto origJson = event.originalJson().get(); | auto origJson = event.originalJson().get(); | ||||
| if (origJson.contains("redacts") && origJson.at("redacts").is_string()) { | if (origJson.contains("redacts") && origJson.at("redacts").is_string()) { | ||||
| auto redactedEventId = origJson.at("redacts").template get<std::string>(); | auto redactedEventId = origJson.at("redacts").template get<std::string>(); | ||||
| if (r.messages.find(redactedEventId)) { | if (r.messages.find(redactedEventId)) { | ||||
| r.messages = std::move(r.messages).update(redactedEventId, [&origJson](const auto &eventToBeRedacted) { | r.messages = std::move(r.messages).update(redactedEventId, [&origJson](const auto &eventToBeRedacted) { | ||||
| auto newJson = eventToBeRedacted.originalJson().get(); | auto newJson = eventToBeRedacted.originalJson().get(); | ||||
| newJson.merge_patch(json{ | newJson.merge_patch(json{ | ||||
| {"unsigned", {{"redacted_because", std::move(origJson)}}}, | {"unsigned", {{"redacted_because", std::move(origJson)}}}, | ||||
| }); | }); | ||||
| newJson["content"] = json::object(); | newJson["content"] = json::object(); | ||||
| return Event(newJson); | return Event(newJson); | ||||
| }); | }); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| return event; | return event; | ||||
| }; | }; | ||||
| immer::for_each(a.events, handleRedaction); | immer::for_each(a.events, handleRedaction); | ||||
| r.timeline = sortedUniqueMerge(r.timeline, eventIds, exists, key); | // remove all local echoes that are received | ||||
| for (const auto &e : a.events) { | |||||
| auto jw = e.originalJson(); | |||||
| const auto &json = jw.get(); | |||||
| if (json.contains("unsigned") | |||||
| && json["unsigned"].contains("transaction_id") | |||||
| && json["unsigned"]["transaction_id"].is_string()) { | |||||
| r = update(std::move(r), RemoveLocalEchoAction{json["unsigned"]["transaction_id"].template get<std::string>()}); | |||||
| } | |||||
| } | |||||
| // If this is a pagination request, gapEventId | // calculate event relationships | ||||
| // should have value. If this is a sync request, | r.generateRelationships(a.events); | ||||
| // gapEventId does not have value. The pagination | |||||
| // request does not have the limited field, and | r.addToUndecryptedEvents(a.events); | ||||
| // whether it has more paginate back token is determined | |||||
| // by the presence of the prevBatch parameter. | return r; | ||||
| // In sync request, limited may not be specified, | }, | ||||
| // and thus, if limited does not have value, it means | [&](AddToTimelineAction a) { | ||||
| // it is not limited. | auto eventIds = intoImmer(immer::flex_vector<std::string>(), | ||||
| zug::map(keyOfTimeline), a.events); | |||||
| auto oldMessages = r.messages; | |||||
| auto next = RoomModel::update(std::move(r), AddMessagesAction{a.events}); | |||||
| r = std::move(next); | |||||
| auto key = | |||||
| [=](auto eventId) { | |||||
| // sort first by timestamp, then by id | |||||
| return sortKeyForTimelineEvent(r.messages[eventId]); | |||||
| }; | |||||
| // Things in messages do not always appear in the timeline. | |||||
| // Let `exists` function to always return false, so that it is checked for duplicates automatically. | |||||
| r.timeline = sortedUniqueMerge(r.timeline, eventIds, [](auto &&) { return false; }, key); | |||||
| // We have 3 possibilities for the source of calling this action: | |||||
| // pagination, sync, or load from storage. | |||||
| // | |||||
| // In pagination: `gapEventId.has_value() && !limited.has_value()` | |||||
| // If we can paginate back: `prevBatch.has_value()` | |||||
| // | |||||
| // In sync: `!gapEventId.has_value()` | |||||
| // If limited: `limited.has_value() && limited.value()` | |||||
| // If we can paginate back: `prevBatch.has_value()` (should always be present, but we do not add a Gap if it is not limited) | |||||
| // | |||||
| // In load from storage: `!gapEventId.has_value() && !limited.has_value() && !prevBatch.has_value()` (Because of actions/storage.cpp) | |||||
| // Only pagination and sync can add a Gap | |||||
| if (((a.limited.has_value() && a.limited.value()) | if (((a.limited.has_value() && a.limited.value()) | ||||
| || a.gapEventId.has_value()) | || a.gapEventId.has_value()) | ||||
| && a.prevBatch.has_value()) { | && a.prevBatch.has_value()) { | ||||
| // this sync is limited, add a Gap here | // this sync is limited, add a Gap here | ||||
| if (!eventIds.empty()) { | if (!eventIds.empty()) { | ||||
| r.timelineGaps = std::move(r.timelineGaps).set(eventIds[0], a.prevBatch.value()); | r.timelineGaps = std::move(r.timelineGaps).set(eventIds[0], a.prevBatch.value()); | ||||
| } | } | ||||
| } | } | ||||
| // Only pagination can remove Gaps | |||||
| // remove the original Gap, as it is resolved | // remove the original Gap, as it is resolved | ||||
| if (a.gapEventId.has_value()) { | if (a.gapEventId.has_value()) { | ||||
| r.timelineGaps = std::move(r.timelineGaps).erase(a.gapEventId.value()); | r.timelineGaps = std::move(r.timelineGaps).erase(a.gapEventId.value()); | ||||
| } | } | ||||
| // remove all Gaps between the gapped event and the first event in this batch | // remove all Gaps between the gapped event and the first event in this batch | ||||
| if (!eventIds.empty() && a.gapEventId.has_value()) { | if (!eventIds.empty() && a.gapEventId.has_value()) { | ||||
| auto cmp = [=](auto a, auto b) { | auto cmp = [=](auto a, auto b) { | ||||
| return key(a) < key(b); | return key(a) < key(b); | ||||
| }; | }; | ||||
| auto thisBatchStart = std::equal_range(r.timeline.begin(), r.timeline.end(), eventIds[0], cmp).first; | auto thisBatchStart = std::equal_range(r.timeline.begin(), r.timeline.end(), eventIds[0], cmp).first; | ||||
| auto origBatchStart = std::equal_range(thisBatchStart, r.timeline.end(), a.gapEventId.value(), cmp).first; | auto origBatchStart = std::equal_range(thisBatchStart, r.timeline.end(), a.gapEventId.value(), cmp).first; | ||||
| // Safety assert: we do not want to execute the for_each if the range is empty, | // Safety assert: we do not want to execute the for_each if the range is empty, | ||||
| // or it will go out of bounds. | // or it will go out of bounds. | ||||
| if (thisBatchStart.index() < origBatchStart.index()) { | if (thisBatchStart.index() < origBatchStart.index()) { | ||||
| std::for_each(thisBatchStart + 1, origBatchStart, | std::for_each(thisBatchStart + 1, origBatchStart, | ||||
| [&](auto eventId) { | [&](auto eventId) { | ||||
| r.timelineGaps = std::move(r.timelineGaps).erase(eventId); | r.timelineGaps = std::move(r.timelineGaps).erase(eventId); | ||||
| }); | }); | ||||
| } | } | ||||
| } | } | ||||
| // remove all local echoes that are received | |||||
| for (const auto &e : a.events) { | |||||
| auto jw = e.originalJson(); | |||||
| const auto &json = jw.get(); | |||||
| if (json.contains("unsigned") | |||||
| && json["unsigned"].contains("transaction_id") | |||||
| && json["unsigned"]["transaction_id"].is_string()) { | |||||
| r = update(std::move(r), RemoveLocalEchoAction{json["unsigned"]["transaction_id"].template get<std::string>()}); | |||||
| } | |||||
| } | |||||
| // calculate event relationships | |||||
| r.generateRelationships(a.events); | |||||
| r.addToUndecryptedEvents(a.events); | |||||
| return r; | return r; | ||||
| }, | }, | ||||
| [&](AddAccountDataAction a) { | [&](AddAccountDataAction a) { | ||||
| r.accountData = merge(std::move(r.accountData), a.events, keyOfAccountData); | r.accountData = merge(std::move(r.accountData), a.events, keyOfAccountData); | ||||
| return r; | return r; | ||||
| }, | }, | ||||
| [&](ChangeMembershipAction a) { | [&](ChangeMembershipAction a) { | ||||
| ▲ Show 20 Lines • Show All 492 Lines • Show Last 20 Lines | |||||