diff --git a/index.js b/index.js index a69503a..068126b 100644 --- a/index.js +++ b/index.js @@ -96,6 +96,7 @@ export default class MicrosubEndpoint { readerRouter.get("/search", readerController.searchPage); readerRouter.post("/search", readerController.searchFeeds); readerRouter.post("/subscribe", readerController.subscribe); + readerRouter.post("/api/mark-read", readerController.markAllRead); router.use("/reader", readerRouter); return router; diff --git a/lib/controllers/reader.js b/lib/controllers/reader.js index 608165f..b9dae27 100644 --- a/lib/controllers/reader.js +++ b/lib/controllers/reader.js @@ -17,7 +17,11 @@ import { createFeed, deleteFeed, } from "../storage/feeds.js"; -import { getTimelineItems, getItemById } from "../storage/items.js"; +import { + getTimelineItems, + getItemById, + markItemsRead, +} from "../storage/items.js"; import { getUserId } from "../utils/auth.js"; import { validateChannelName, @@ -622,6 +626,33 @@ export async function subscribe(request, response) { response.redirect(`${request.baseUrl}/channels/${channelUid}/feeds`); } +/** + * Mark all items in channel as read + * @param {object} request - Express request + * @param {object} response - Express response + * @returns {Promise} + */ +export async function markAllRead(request, response) { + const { application } = request.app.locals; + const userId = getUserId(request); + const { channel: channelUid } = request.body; + + const channelDocument = await getChannel(application, channelUid, userId); + if (!channelDocument) { + return response.status(404).render("404"); + } + + // Mark all items as read using the special "last-read-entry" value + await markItemsRead( + application, + channelDocument._id, + ["last-read-entry"], + userId, + ); + + response.redirect(`${request.baseUrl}/channels/${channelUid}`); +} + export const readerController = { index, channels, @@ -630,6 +661,7 @@ export const readerController = { channel, settings, updateSettings, + markAllRead, deleteChannel: deleteChannelAction, feeds, addFeed, diff --git a/package.json b/package.json index 46aa30c..018d1c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rmdes/indiekit-endpoint-microsub", - "version": "1.0.15", + "version": "1.0.16", "description": "Microsub endpoint for Indiekit. Enables subscribing to feeds and reading content using the Microsub protocol.", "keywords": [ "indiekit",