#include "screens/notifications/NotificationRoute.hpp"
#include <algorithm>
#include <cctype>
#include <cstring>
#include <vector>
#include "screens/notifications/UrlSyntax.hpp"
#include "spec/SocialSpec.hpp"
namespace dp {
namespace {
std::vector<std::string> segments(const std::string& path) {
std::vector<std::string> out;
std::string cur;
for (char c : path) {
if (c == '/') {
if (!cur.empty()) out.push_back(std::move(cur));
cur.clear();
} else {
cur.push_back(c);
}
}
if (!cur.empty()) out.push_back(cur);
if (out.size() > spec::SocialLimits::kMaxRouteSegments) out.resize(spec::SocialLimits::kMaxRouteSegments);
return out;
}
std::optional<std::string> commentUidFromToken(const std::string& token) {
for (const char* prefix : {"comment-", "comment_", "comment:", "comment"}) {
const std::size_t n = std::strlen(prefix);
if (token.compare(0, n, prefix) == 0) {
std::string uid = token.substr(n);
if (uid.empty()) return std::nullopt;
return uid;
}
}
return std::nullopt;
}
std::optional<std::string> queryComment(const std::string& query) {
static const char* const kNames[] = {"comment", "comment_id", "commentId", "comment_uid"};
std::size_t pos = 0;
while (pos <= query.size() && !query.empty()) {
std::size_t amp = query.find('&', pos);
if (amp == std::string::npos) amp = query.size();
const std::string item = query.substr(pos, amp - pos);
const auto eq = item.find('=');
const std::string name = UrlSyntax::percentDecode(item.substr(0, eq));
const bool known = std::any_of(std::begin(kNames), std::end(kNames), [&](const char* k) { return name == k; });
if (known) {
// first matching item decides, like first(where:); an empty value gives no result
if (eq == std::string::npos) return std::nullopt;
const std::string value = UrlSyntax::percentDecode(item.substr(eq + 1));
if (value.empty()) return std::nullopt;
return value;
}
pos = amp + 1;
}
return std::nullopt;
}
} // namespace
std::optional<std::string> NotificationRouting::postSlug(const std::string& url) {
const auto parts = UrlSyntax::parse(url);
if (!parts) return std::nullopt;
const auto segs = segments(parts->path);
const auto it = std::find(segs.begin(), segs.end(), "posts");
if (it == segs.end() || it + 1 == segs.end()) return std::nullopt;
return *(it + 1);
}
std::optional<std::string> NotificationRouting::commentUid(const std::string& url) {
const auto parts = UrlSyntax::parse(url);
if (!parts) return std::nullopt;
if (parts->fragment) {
if (auto uid = commentUidFromToken(*parts->fragment)) return uid;
}
if (auto uid = queryComment(parts->query)) return uid;
const auto segs = segments(parts->path);
const auto it = std::find(segs.begin(), segs.end(), "comments");
if (it != segs.end() && it + 1 != segs.end()) return *(it + 1);
return std::nullopt;
}
std::optional<PostDestination> NotificationRouting::postDestination(const std::string& redirect) {
const auto slug = postSlug(redirect);
if (!slug) return std::nullopt;
PostDestination d;
d.slug = *slug;
if (auto uid = commentUid(redirect)) d.scrollToCommentId = "comment:" + *uid;
return d;
}
std::optional<NotificationRoute> NotificationRouting::route(const Notification& n, const std::string& redirect) {
NotificationRoute r;
if (n.data.type && n.data.type->kind() == NotificationType::Message && n.actor) {
r.kind = NotificationRoute::Kind::Conversation;
r.user = *n.actor;
return r;
}
if (n.data.type && n.data.type->kind() == NotificationType::Badge) {
r.kind = NotificationRoute::Kind::OwnProfile;
return r;
}
if (n.data.type && n.data.type->kind() == NotificationType::Issue) {
r.kind = NotificationRoute::Kind::Web;
r.url = std::string(spec::SocialStrings::kIssueUrlPrefix) + n.data.relatedId;
if (!UrlSyntax::parse(r.url)) return std::nullopt; // URL(string:) failed
return r;
}
if (auto d = postDestination(redirect)) {
r.kind = NotificationRoute::Kind::Post;
r.post = *d;
return r;
}
return std::nullopt;
}
} // namespace dp