Slon/Api/V1/Statuses: Allow retrieval of individual status from public timeline

This commit is contained in:
Alec Murphy 2025-03-02 18:07:09 -05:00
parent 715d119882
commit 5258ab19a8
4 changed files with 72 additions and 53 deletions

View file

@ -94,7 +94,7 @@ JsonArray* @slon_api_v1_statuses_lookup_descendants_by_id(U8* id, JsonArray* sta
JsonObject* @slon_api_v1_statuses_find_by_id(U8* id, U8* account_id)
{
if (!id || !account_id) {
if (!id) {
return NULL;
}
JsonObject* status = NULL;
@ -103,6 +103,9 @@ JsonObject* @slon_api_v1_statuses_find_by_id(U8* id, U8* account_id)
if (status) {
return status;
}
if (!account_id) {
return NULL;
}
// Then, lookup in home timeline
status = @slon_api_v1_statuses_lookup_by_id(id, db->o("timelines")->o("home")->a(account_id));
if (status) {
@ -274,23 +277,24 @@ U0 @slon_api_v1_statuses_delete(SlonHttpSession* session)
U0 @slon_api_v1_statuses_get(SlonHttpSession* session)
{
if (session->path_count() < 4) {
session->status(400);
return;
}
if (session->path_count() > 4 && !StrICmp("history", session->path(4))) {
// NOTE: We probably won't support this any time soon
session->send(SLON_EMPTY_JSON_ARRAY);
return;
}
U8* id = session->path(3);
JsonObject* status = NULL;
if (@slon_api_authorized(session)) {
SLON_AUTH_ACCOUNT_ID
if (session->path_count() < 4) {
session->status(400);
return;
}
if (session->path_count() > 4 && !StrICmp("history", session->path(4))) {
// NOTE: We probably won't support this any time soon
session->send(SLON_EMPTY_JSON_ARRAY);
return;
}
U8* id = session->path(3);
JsonObject* status = NULL;
if (session->path_count() > 4 && !StrICmp("context", session->path(4))) {
JsonObject* context = Json.CreateObject();
context->set("ancestors", Json.CreateArray(), JSON_ARRAY);
@ -321,7 +325,12 @@ U0 @slon_api_v1_statuses_get(SlonHttpSession* session)
}
session->status(404);
} else {
session->status(401);
status = @slon_api_v1_statuses_find_by_id(id, NULL);
if (status) {
session->send(status);
return;
}
session->status(404);
}
}

View file

@ -7,36 +7,37 @@ U0 @slon_web_user_get(SlonHttpSession* session)
U8** path_segments = String.Split(StrFind("@", session->path()) + 1, '/', &path_segments_count);
U8* user = path_segments[0];
if (path_segments_count == 1) {
JsonObject* actor = db->o("actors")->@(user);
if (!actor) {
session->status(404);
goto slon_web_user_get_return;
}
// gib profil pl0x
I64 html_file_size;
U8* html_file_data = FileRead("M:/Slon/Static/html/user.html", &html_file_size);
U8* user_file_data = Json.Stringify(actor);
U8* html_data = @slon_calloc(session, (html_file_size * 2) + (StrLen(user_file_data) * 2));
String.Append(html_data, html_file_data);
String.Append(html_data, "<script>getStatuses(");
String.Append(html_data, user_file_data);
String.Append(html_data, ");</script>");
session->content_type("text/html");
session->send(html_data, StrLen(html_data));
Free(html_file_data);
Free(user_file_data);
@slon_free(session, html_data);
goto slon_web_user_get_return;
} else {
// do something here (statuses, followers, media, etc.)
JsonObject* actor = db->o("actors")->@(user);
if (!actor) {
session->status(404);
goto slon_web_user_get_return;
}
I64 html_file_size;
U8* html_file_data = FileRead("M:/Slon/Static/html/user.html", &html_file_size);
U8* user_file_data = Json.Stringify(actor);
U8* html_data = @slon_calloc(session, (html_file_size * 2) + (StrLen(user_file_data) * 2));
String.Append(html_data, html_file_data);
switch (path_segments_count) {
case 1:
String.Append(html_data, "<script>getStatuses(");
break;
default:
String.Append(html_data, "<script>getStatusById(\"%s\",", path_segments[1]);
break;
}
String.Append(html_data, user_file_data);
String.Append(html_data, ");</script>");
session->content_type("text/html");
session->send(html_data, StrLen(html_data));
Free(html_file_data);
Free(user_file_data);
@slon_free(session, html_data);
slon_web_user_get_return:
Free(path_segments);
}

View file

@ -26,4 +26,4 @@
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/relativeTime.js"></script>
<script>dayjs.extend(window.dayjs_plugin_relativeTime)</script>
<script src=https://error.checksum.fail/js/header.js></script>
<script src=https://error.checksum.fail/js/29137902173921730271.js></script>
<script src=https://error.checksum.fail/js/91274902173.js></script>

View file

@ -23,7 +23,7 @@ function updateStatusContainers() {
let desc = "Term";
for (var y = 0; y < height; y++) {
let ch = y < 4 ? desc[y] : "\u2551";
let ch = y < 4 ? desc[y] : "\u2551";
post_html += "<b>" + ch + "<div style=\"display: inline-block; height: 16px; width: " + ((horizontal_fill_count + 1) * 16).toString() + "px\"></div>\u2551</b><br>";
}
@ -38,7 +38,7 @@ function updateStatusContainers() {
}
}
function smolDate(a){return a.split(" ago")[0].replace("a ","1").replace("an ","1").replace("days","d").replace("day","d").replace("hours","h").replace("hour","h").replace("minutes","m").replace("minute","m").replace("seconds","s").replace("second","s").replace("few","").replace(" ","")};
function smolDate(a) { return a.split(" ago")[0].replace("a ", "1").replace("an ", "1").replace("days", "d").replace("day", "d").replace("hours", "h").replace("hour", "h").replace("minutes", "m").replace("minute", "m").replace("seconds", "s").replace("second", "s").replace("few", "").replace(" ", "") };
function updateStatuses(user, statuses) {
let pageContent = document.getElementsByClassName("page-content")[0];
@ -54,7 +54,7 @@ function updateStatuses(user, statuses) {
content_html += "<div class=status-avatar style=margin:16px;background:url(" + user["icon"]["url"] + ");width:72px;height:72px;background-size:contain;background-repeat:no-repeat></div>";
content_html += "<div class=status-header>" + user["preferredUsername"] + "<br><a href=" + user["url"] + ">@" + user["preferredUsername"] + "@" + location.host + "</a></div>"
content_html += "<div class=status-text><font style=color:#0>" + user["summary"] + "</font></div>";
content_html += "<div class=status-text>Joined " + new Date(Date.parse(user["published"])).toString().substr(0,15) + "</div>";
content_html += "<div class=status-text>Joined " + new Date(Date.parse(user["published"])).toString().substr(0, 15) + "</div>";
content.innerHTML = content_html;
let url = document.createElement('url');
url.textContent = window.location;
@ -66,11 +66,11 @@ function updateStatuses(user, statuses) {
elements.appendChild(container);
let spacer = document.createElement('div');
spacer.style.height = "16px";
elements.appendChild(spacer);
elements.appendChild(spacer);
}
elements.className = "statuses";
statuses.sort((a,b) => b.id - a.id);
statuses.sort((a, b) => b.id - a.id);
for (var i = 0; i < statuses.length; i++) {
let status = statuses[i];
let container = document.createElement('div');
@ -131,8 +131,17 @@ function updateStatuses(user, statuses) {
function getStatuses(user) {
fetch("https://error.checksum.fail/api/v1/accounts/" + user["accountId"] + "/statuses", {
method: 'GET',
headers: {'Accept': 'application/json' }
headers: { 'Accept': 'application/json' }
})
.then(response => response.json())
.then(data => updateStatuses(user, data));
.then(response => response.json())
.then(data => updateStatuses(user, data));
}
function getStatusById(id, user) {
fetch("https://error.checksum.fail/api/v1/statuses/" + id, {
method: 'GET',
headers: { 'Accept': 'application/json' }
})
.then(response => response.json())
.then(data => updateStatuses(user, [data]));
}