@rmdes/indiekit-endpoint-microsub 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/lib/controllers/reader.js +50 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -96,6 +96,7 @@ export default class MicrosubEndpoint {
|
|
|
96
96
|
readerRouter.get("/search", readerController.searchPage);
|
|
97
97
|
readerRouter.post("/search", readerController.searchFeeds);
|
|
98
98
|
readerRouter.post("/subscribe", readerController.subscribe);
|
|
99
|
+
readerRouter.post("/api/mark-read", readerController.markAllRead);
|
|
99
100
|
router.use("/reader", readerRouter);
|
|
100
101
|
|
|
101
102
|
return router;
|
|
@@ -17,7 +17,11 @@ import {
|
|
|
17
17
|
createFeed,
|
|
18
18
|
deleteFeed,
|
|
19
19
|
} from "../storage/feeds.js";
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
getTimelineItems,
|
|
22
|
+
getItemById,
|
|
23
|
+
markItemsRead,
|
|
24
|
+
} from "../storage/items.js";
|
|
21
25
|
import { getUserId } from "../utils/auth.js";
|
|
22
26
|
import {
|
|
23
27
|
validateChannelName,
|
|
@@ -173,6 +177,33 @@ export async function updateSettings(request, response) {
|
|
|
173
177
|
response.redirect(`${request.baseUrl}/channels/${uid}`);
|
|
174
178
|
}
|
|
175
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Mark all items in channel as read
|
|
182
|
+
* @param {object} request - Express request
|
|
183
|
+
* @param {object} response - Express response
|
|
184
|
+
* @returns {Promise<void>}
|
|
185
|
+
*/
|
|
186
|
+
export async function markAllRead(request, response) {
|
|
187
|
+
const { application } = request.app.locals;
|
|
188
|
+
const userId = getUserId(request);
|
|
189
|
+
const { channel: channelUid } = request.body;
|
|
190
|
+
|
|
191
|
+
const channelDocument = await getChannel(application, channelUid, userId);
|
|
192
|
+
if (!channelDocument) {
|
|
193
|
+
return response.status(404).render("404");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Mark all items as read using the special "last-read-entry" value
|
|
197
|
+
await markItemsRead(
|
|
198
|
+
application,
|
|
199
|
+
channelDocument._id,
|
|
200
|
+
["last-read-entry"],
|
|
201
|
+
userId,
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
response.redirect(`${request.baseUrl}/channels/${channelUid}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
176
207
|
/**
|
|
177
208
|
* Delete channel
|
|
178
209
|
* @param {object} request - Express request
|
|
@@ -376,7 +407,7 @@ export async function submitCompose(request, response) {
|
|
|
376
407
|
if (!micropubEndpoint) {
|
|
377
408
|
return response.status(500).render("error", {
|
|
378
409
|
title: "Error",
|
|
379
|
-
|
|
410
|
+
content: "Micropub endpoint not configured",
|
|
380
411
|
});
|
|
381
412
|
}
|
|
382
413
|
|
|
@@ -450,20 +481,34 @@ export async function submitCompose(request, response) {
|
|
|
450
481
|
|
|
451
482
|
// Handle error
|
|
452
483
|
const errorBody = await micropubResponse.text();
|
|
484
|
+
const statusText = micropubResponse.statusText || "Unknown error";
|
|
453
485
|
console.error(
|
|
454
486
|
`[Microsub] Micropub error: ${micropubResponse.status} ${errorBody}`,
|
|
455
487
|
);
|
|
456
488
|
|
|
489
|
+
// Parse error message from response body if JSON
|
|
490
|
+
let errorMessage = `Micropub error: ${statusText}`;
|
|
491
|
+
try {
|
|
492
|
+
const errorJson = JSON.parse(errorBody);
|
|
493
|
+
if (errorJson.error_description) {
|
|
494
|
+
errorMessage = String(errorJson.error_description);
|
|
495
|
+
} else if (errorJson.error) {
|
|
496
|
+
errorMessage = String(errorJson.error);
|
|
497
|
+
}
|
|
498
|
+
} catch {
|
|
499
|
+
// Not JSON, use status text
|
|
500
|
+
}
|
|
501
|
+
|
|
457
502
|
return response.status(micropubResponse.status).render("error", {
|
|
458
503
|
title: "Error",
|
|
459
|
-
|
|
504
|
+
content: errorMessage,
|
|
460
505
|
});
|
|
461
506
|
} catch (error) {
|
|
462
507
|
console.error(`[Microsub] Micropub request failed: ${error.message}`);
|
|
463
508
|
|
|
464
509
|
return response.status(500).render("error", {
|
|
465
510
|
title: "Error",
|
|
466
|
-
|
|
511
|
+
content: `Failed to create post: ${error.message}`,
|
|
467
512
|
});
|
|
468
513
|
}
|
|
469
514
|
}
|
|
@@ -559,6 +604,7 @@ export const readerController = {
|
|
|
559
604
|
channel,
|
|
560
605
|
settings,
|
|
561
606
|
updateSettings,
|
|
607
|
+
markAllRead,
|
|
562
608
|
deleteChannel: deleteChannelAction,
|
|
563
609
|
feeds,
|
|
564
610
|
addFeed,
|
package/package.json
CHANGED