// SPDX-FileCopyrightText: 2023 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later class Forgejo { /** * Represents a Forgejo instance. * @constructor * @param {string} url - The URL of the Forgejo instance * @param {string} username - username of the logged in user */ constructor(url, username) { this.url = new URL(url); this.username = username; } /** * Get all notifications */ async getNotifications() { this.url.path = "/api/v1/notifications"; let res = await fetch(this.url); return await res.json(); } /** * Get number of unread notifications */ async getNumUnreadNotifications() { this.url.path = "/api/v1/notifications/new"; let res = await fetch(this.url); return await res.json()["new"]; } /** * Get Notification Thread * @param {number} id - The ID of a notification thread */ async getNotificationThread(id) { this.url.path = `/api/v1/notifications/threads/${id}`; let res = await fetch(this.url); return await res.json(); } /** * Get the time at which the issue was last read */ lastReadTime(issue) { return 10120123; // TODO: return last read UNIX epoch time from storage. If unread, return 0 } /** * Check if logged in user is mentioned in the issue */ async findMentionsInIssue(id) { let issue = this.getIssue(id); let unreadTime = this.lastReadTime(issue); const mentionUtil = (str) => str.includes(this.username); const items = []; if (issue.created > unreadTime) { items.push(issue.title); items.push(issue.body); } issue.commentsData.forEach((comment) => { if (comment.created_at > unreadTime) { items.push(comment.body); return; } if (comment.updated_at) { if (comment.updated_at > unreadTime) { items.push(comment.body); return; } } }); items.forEach((item) => { if (mentionUtil(item)) { return true; } }); return false; } /** * Mark Notification Read * @param {number} id - The ID of a notification thread */ async markNotificationRead(id) { this.url.path = `/api/v1/notifications/threads/${id}`; await fetch(this.url, { method: "PATCH" }); } /** * Mark Notification Read for a specific repository * @param {string} owner - Name of the owner of the repository * @param {string} repo - Name of the repository */ async markNotificationReadForRepo(owner, repo) { this.url.path = `/api/v1/repos/${owner}/${repo}/notifications`; await fetch(this.url, { method: "PUT" }); } /** * Get Issue and its comments * @param {str} owner - The owner of the repository * @param {str} repo - The name of the repository * @param {number} id - The ID of an issue */ async getIssue(owner, repo, id) {} } export default Forgejo;