#include "screens/notifications/NotificationsModel.hpp"
#include <algorithm>
#include "core/Log.hpp"
#include "screens/components/AlertPresenter.hpp"
#include "spec/SocialSpec.hpp"
#include "store/AppState.hpp"
namespace dp {
NotificationsModel::NotificationsModel(DevPlaceApi& api, AlertSink alertSink)
: api_(api), alertSink_(alertSink ? std::move(alertSink) : [](const AlertMessage& m) { AlertPresenter::show(m); }) {}
const std::vector<NotificationGroup>& NotificationsModel::groups() const {
static const std::vector<NotificationGroup> kEmpty;
return notifications_ ? notifications_->groups : kEmpty;
}
std::size_t NotificationsModel::entryCount() const {
std::size_t n = 0;
for (const auto& g : groups()) n += g.entries.size();
return n;
}
bool NotificationsModel::hasMore() const {
return notifications_ && notifications_->nextCursor && entryCount() < spec::SocialLimits::kMaxNotificationEntries;
}
void NotificationsModel::refreshUnreadCount(std::function<void()> done) {
AppState::instance().loadUnreadCounts(api_, std::move(done));
}
void NotificationsModel::load(std::function<void()> done) {
if (!AppState::instance().isLoggedIn()) {
notifications_.reset();
if (done) done();
return;
}
++pending_;
api_.notifications([this, alive = alive_, done](Result<Notifications> r) {
if (!*alive) return;
--pending_;
if (r.ok()) {
notifications_ = r.value();
loadMoreFailed_ = false;
} else {
alertSink_(AlertMessage::presentedError(r.error()));
}
if (done) done();
});
}
void NotificationsModel::refresh(std::function<void()> done) {
if (!AppState::instance().isLoggedIn()) {
notifications_.reset();
if (done) done();
return;
}
++pending_;
++refreshing_;
api_.notifications([this, alive = alive_, done](Result<Notifications> r) {
if (!*alive) return;
if (r.ok()) {
notifications_ = r.value();
loadMoreFailed_ = false;
refreshUnreadCount([this, alive, done] {
if (!*alive) return;
--pending_;
--refreshing_;
if (done) done();
});
} else {
--pending_;
--refreshing_;
alertSink_(AlertMessage::presentedError(r.error()));
if (done) done();
}
});
}
void NotificationsModel::loadMore() {
if (isLoadingMore_ || loadMoreFailed_ || !hasMore()) return;
const Date cursor = *notifications_->nextCursor;
isLoadingMore_ = true;
++pending_;
api_.notifications(std::optional<Date>(cursor), [this, alive = alive_](Result<Notifications> r) {
if (!*alive) return;
--pending_;
isLoadingMore_ = false;
if (!r.ok()) {
loadMoreFailed_ = true; // no automatic retry loop behind an error alert; refresh clears it
alertSink_(AlertMessage::presentedError(r.error()));
return;
}
if (notifications_) {
std::vector<std::string> duplicates;
notifications_ = merged(*notifications_, r.value(), &duplicates);
if (!duplicates.empty()) {
std::string ids;
for (const auto& d : duplicates) ids += (ids.empty() ? "" : ", ") + d;
Log::warn("Dropped duplicate notification IDs while loading more (backend pagination bug, report to backend team): " + ids);
}
} else {
notifications_ = r.value();
}
});
}
void NotificationsModel::open(const Notification& notification, std::function<void(std::optional<NotificationRoute>)> done) {
++pending_;
api_.openNotification(notification.data.id, [this, alive = alive_, notification, done](Result<NotificationOpen> r) {
if (!*alive) return;
if (!r.ok()) {
--pending_;
alertSink_(AlertMessage::presentedError(r.error()));
if (done) done(std::nullopt);
return;
}
const auto route = NotificationRouting::route(notification, r.value().redirect);
load([this, alive, route, done] {
if (!*alive) return;
refreshUnreadCount([this, alive, route, done] {
if (!*alive) return;
--pending_;
if (done) done(route);
});
});
});
}
void NotificationsModel::markRead(const std::string& uid) {
if (notifications_) notifications_ = applyingRead(*notifications_, {uid});
api_.markNotificationRead(uid, [this, alive = alive_](Result<Unit> r) {
if (!*alive) return;
if (r.ok()) {
refreshUnreadCount(nullptr);
} else {
alertSink_(AlertMessage::presentedError(r.error()));
load();
}
});
}
void NotificationsModel::markAllRead() {
std::set<std::string> all;
for (const auto& g : groups()) {
for (const auto& e : g.entries) all.insert(e.data.id);
}
if (notifications_) notifications_ = applyingRead(*notifications_, all);
api_.markAllNotificationsRead([this, alive = alive_](Result<Unit> r) {
if (!*alive) return;
if (r.ok()) {
refreshUnreadCount(nullptr);
} else {
alertSink_(AlertMessage::presentedError(r.error()));
load();
}
});
}
Notifications NotificationsModel::merged(const Notifications& existing, const Notifications& older, std::vector<std::string>* duplicates) {
std::vector<NotificationGroup> groups = existing.groups;
for (const NotificationGroup& g : older.groups) {
auto it = std::find_if(groups.begin(), groups.end(), [&](const NotificationGroup& x) { return x.label == g.label; });
if (it != groups.end()) it->entries.insert(it->entries.end(), g.entries.begin(), g.entries.end());
else groups.push_back(g);
}
std::set<std::string> seen;
Notifications out;
out.nextCursor = older.nextCursor;
for (NotificationGroup& g : groups) {
NotificationGroup kept;
kept.label = g.label;
for (Notification& e : g.entries) {
if (!seen.insert(e.data.id).second) {
if (duplicates) duplicates->push_back(e.data.id);
continue;
}
kept.entries.push_back(std::move(e));
}
if (!kept.entries.empty()) out.groups.push_back(std::move(kept));
}
return out;
}
Notifications NotificationsModel::applyingRead(const Notifications& n, const std::set<std::string>& uids) {
Notifications out = n;
for (NotificationGroup& g : out.groups) {
for (Notification& e : g.entries) {
if (uids.count(e.data.id)) e.data.read = true;
}
}
return out;
}
} // namespace dp