Skip to content

Commit

Permalink
pleroma notif read endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
velzie committed Jan 2, 2025
1 parent 5b0bc49 commit 895200b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
53 changes: 53 additions & 0 deletions include/querybuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class QueryBuilder {
optional<int> m_offset;

vector<QueryConstraint> m_constraints;
std::map<string, SqlType> m_updateSets;



Expand All @@ -60,6 +61,7 @@ class QueryBuilder {
q.reset();
return q;
}

QueryBuilder select(const vector<string> _columns) {
QueryBuilder q = *this;
q.columns = _columns;
Expand All @@ -68,6 +70,19 @@ class QueryBuilder {
return q;
}

QueryBuilder update() {
QueryBuilder q = *this;
q.queryType = "UPDATE";
q.reset();
return q;
}

QueryBuilder set(const string column, SqlType value) {
QueryBuilder q = *this;
q.m_updateSets[column] = value;
return q;
}

QueryBuilder from(const string table) {
QueryBuilder q = *this;
q.m_table = table;
Expand Down Expand Up @@ -218,6 +233,44 @@ class QueryBuilder {
}

return q;
} else if (queryType == "UPDATE") {
query = "UPDATE " + m_table.value() + " SET ";
for (auto [k, v] : m_updateSets) {
query += k + " = ?,";
}
query.pop_back();
query += " WHERE ";

if (!m_constraints.empty()) {
for (auto c : m_constraints) {
expandUnion(c, query);
query += " AND ";
}
}

query.erase(query.size() - 5);

auto q = STATEMENT(query);
int ibind = 1;

for (auto [k, v] : m_updateSets) {
if (std::holds_alternative<time_t>(v)) {
q.bind(ibind, std::get<time_t>(v));
} else if (std::holds_alternative<bool>(v)) {
q.bind(ibind, std::get<bool>(v));
} else if (std::holds_alternative<string>(v)) {
q.bind(ibind, std::get<string>(v));
}
ibind++;
}

for (auto un : m_constraints) {
expandUnionBind(un, q, ibind);
}

return q;
} else {
ASSERT(false);
}
ASSERT(false);
}
Expand Down
52 changes: 52 additions & 0 deletions src/routes/mastodon/apps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ GET(notifications, "/api/v1/notifications") {
query = query
.select({"*"})
.from("notification")
.where(EQ("notifieeId", authuser.id))
.orderBy("createdAt", "DESC");


Expand Down Expand Up @@ -349,3 +350,54 @@ GET(notification_policy, "/api//v1/notifications/policy") {
GET(announcements, "/api/v1/announcements") {
OK(json::array(), MIMEJSON);
}

POST(pleroma_notifications_read, "/api/v1/pleroma/notifications/read") {
MSAUTH


if (!mp.isValid()) ERROR(400, "invalid multipart");
std::pair<std::string_view, std::string_view> headers[20];

string maxId;
while (true) {
std::optional<std::string_view> optionalPart = mp.getNextPart(headers);
if (!optionalPart.has_value()) {
break;
}

for (int i = 0; headers[i].first.length(); i++) {
if (headers[i].first == "content-disposition") {
uWS::ParameterParser pp(headers[i].second);
while (true) {
auto [key, value] = pp.getKeyValue();
if (key.empty()) break;
if (key == "name") {
if (value == "max_id") {
maxId = optionalPart.value();
}
}
}
}
}
}
if (maxId.empty()) ERROR(400, "max_id is required");

QueryBuilder qb;
qb.update()
.from("notification")
.set("isread", true)
.where(LTE("id", maxId))
.where(EQ("notifieeId", authuser.id))
.build()
.exec();


qb = qb
.select()
.from("notification")
.where(EQ("notifieeId", authuser.id))
.orderBy("createdAt", "DESC");


PAGINATE(qb, Notification, createdAt);
}
8 changes: 4 additions & 4 deletions src/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ namespace Server {

route.second(res, req, j, body->str(), mp, reqUrl);

if (!res->hasResponded()) {
res->writeStatus("204");
res->endWithoutBody();
}
// if (!res->hasResponded()) {
// res->writeStatus("204");
// res->endWithoutBody();
// }
} CPPTRACE_CATCH(const std::exception& e) {
res->writeStatus("500");
res->writeHeader("Content-Type", "text/plain");
Expand Down

0 comments on commit 895200b

Please sign in to comment.