All files index.ts

86.66% Statements 65/75
73.33% Branches 11/15
88.88% Functions 16/18
86.48% Lines 64/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244        1x                                   2x 2x               2x             22x 21x   1x               20x             1x 1x         1x             3x 3x           3x           1x 1x           1x 1x               1x 1x         1x             5x             5x 5x 5x   5x 12x     5x   5x 5x 5x     5x 2x 2x 2x                   5x 5x 12x 5x 5x     5x                                                         7x 7x         7x   7x 7x     7x 7x 7x       7x           7x                 6x 6x         6x   6x 4x 4x     4x 4x 4x     4x     6x   6x       2x  
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
 
import Auth from "./auth";
import User from "./spec/user";
import Notification from "./spec/notification";
import Issue from "./spec/issue";
import Repository from "./spec/repository";
import Comment from "./spec/comments";
 
class Forgejo {
  url: URL;
  username: string;
  token?: Auth;
  /**
   * 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: string, username: string) {
    this.url = new URL(url);
    this.username = username;
  }
 
  /**
   * Authenticate with token
   * @param {Auth} token - access token
   */
  setTokenAuth(token: string) {
    this.token = new Auth(token);
  }
 
  /**
   * Get access token
   */
  getTokenAuth(): Auth {
    if (this.token) {
      return this.token;
    } else {
      throw new Error("set authentication token before use");
    }
  }
 
  /**
   * Get access token in header format for the fetch API
   */
  getTokenAuthHeader() {
    return { Authorization: `token ${this.getTokenAuth().getToken()}` };
  }
 
  /**
   * Get logged in user
   */
  async getUser(): Promise<User> {
    this.url.pathname = "/api/v1/user";
    let res = await fetch(this.url, {
      method: "GET",
      credentials: "include",
      headers: this.getTokenAuthHeader(),
    });
    return await res.json();
  }
 
  /**
   * Get all notifications
   */
  async getNotifications(): Promise<Array<Notification>> {
    this.url.pathname = "/api/v1/notifications";
    let res = await fetch(this.url, {
      method: "GET",
      credentials: "include",
      headers: this.getTokenAuthHeader(),
    });
 
    return await res.json();
  }
  /**
   * Get number of unread notifications
   */
  async getNumUnreadNotifications(): Promise<number> {
    this.url.pathname = "/api/v1/notifications/new";
    let res = await fetch(this.url, {
      method: "GET",
      credentials: "include",
      headers: this.getTokenAuthHeader(),
    });
 
    let data = await res.json();
    return data["new"];
  }
 
  /**
   * Get Notification Thread
   * @param {number} id - The ID of a notification thread
   */
  async getNotificationThread(id: number): Promise<Notification> {
    this.url.pathname = `/api/v1/notifications/threads/${id}`;
    let res = await fetch(this.url, {
      method: "GET",
      credentials: "include",
      headers: this.getTokenAuthHeader(),
    });
    return await res.json();
  }
 
  /**
   * Get the time at which the issue was last read
   */
  lastReadTime(issue: Issue): Date {
    return new Date("01/01/01"); // TODO: return last read time from storage. If unread, return 0
  }
 
  /**
   * Check if logged in user is mentioned in the issue
   */
  async findMentionsInIssue(owner: string, repo: string, id: number) {
    let issue = await this.getIssue(owner, repo, id);
    issue = await this.getCommentsForIssue(issue);
    let unreadTime = this.lastReadTime(issue);
 
    const mentionUtil = (str: string): boolean => {
      return str.includes(this.username);
    };
 
    const items = [];
 
    if (new Date(issue.created_at) > unreadTime) {
      items.push(issue.title);
      items.push(issue.body);
    }
 
    issue.lazy_comments_data.forEach((comment) => {
      if (comment.created_at > unreadTime) {
        items.push(comment.body);
        return;
      }
      Iif (comment.updated_at) {
        Iif (comment.updated_at > unreadTime) {
          items.push(comment.body);
          return;
        }
      }
    });
 
    let mention = false;
    items.forEach((item) => {
      if (mentionUtil(item)) {
        mention = true;
        return;
      }
    });
    return mention;
  }
 
  /**
   * Mark Notification Read
   * @param {number} id - The ID of a notification thread
   */
  async markNotificationRead(id: number) {
    this.url.pathname = `/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: string, repo: string) {
    this.url.pathname = `/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: string, repo: string, id: number): Promise<Issue> {
    this.url.pathname = `/api/v1/repos/${owner}/${repo}/issues/${id}`;
    let res = await fetch(this.url, {
      method: "GET",
      credentials: "include",
      headers: this.getTokenAuthHeader(),
    });
    let issue = await res.json();
 
    if (typeof issue.created_at === "string") {
      issue.created_at = new Date(issue.created_at);
    }
 
    if (issue.updated_at) {
      if (typeof issue.updated_at === "string") {
        issue.updated_at = new Date(issue.updated_at);
      }
    }
 
    Iif (issue.closed_at) {
      Iif (typeof issue.closed_at === "string") {
        issue.closed_at = new Date(issue.closed_at);
      }
    }
 
    return issue;
  }
 
  /**
   * Fetch and save comments for the issue objects
   */
  async getCommentsForIssue(issue: Issue): Promise<Issue> {
    // TODO: check if issue.number != issue.id causes problems. I'm assuming
    // Issue.number is the local repository issue ID and issue.id is DB ID
    this.url.pathname = `/api/v1/repos/${issue.repository.owner}/${issue.repository.name}/issues/${issue.number}/comments`;
    let res = await fetch(this.url, {
      method: "GET",
      credentials: "include",
      headers: this.getTokenAuthHeader(),
    });
    let c: Array<Comment> = await res.json();
 
    c = c.map((comment) => {
      if (typeof comment.created_at === "string") {
        comment.created_at = new Date(comment.created_at);
      }
 
      if (comment.updated_at) {
        if (typeof comment.updated_at === "string") {
          comment.updated_at = new Date(comment.updated_at);
        }
      }
      return comment;
    });
 
    issue.lazy_comments_data = c;
 
    return issue;
  }
}
 
export default Forgejo;