Edits history of script submission #23189 for ' Reply to Comment (gdrive)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_comment_id = string
    
    // Dropdown of the file's comments (first 100, newest first).
    export async function comment_id(auth: RT.Gdrive, file_id: string) {
      const response = await fetch(
        `https://www.googleapis.com/drive/v3/files/${file_id}/comments?pageSize=100&fields=comments(id,content,resolved,author/displayName)`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { comments } = (await response.json()) as {
        comments: {
          id: string
          content: string
          resolved?: boolean
          author?: { displayName?: string }
        }[]
      }
      return comments.map((c) => ({
        value: c.id,
        label: `${c.resolved ? "[resolved] " : ""}${c.author?.displayName ?? "?"}: ${c.content.slice(0, 80)}`,
      }))
    }
    
    /**
     * Reply to Comment
     * Reply to a comment on a file. Set `reopen` to also reopen a resolved comment.
     */
    export async function main(
      auth: RT.Gdrive,
      file_id: string,
      comment_id: DynSelect_comment_id,
      content: string,
      reopen: boolean = false
    ) {
      const url = new URL(
        `https://www.googleapis.com/drive/v3/files/${file_id}/comments/${comment_id}/replies`
      )
      url.searchParams.append("fields", "*")
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(reopen ? { content, action: "reopen" } : { content }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago