Edits history of script submission #23183 for ' Delete 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)}`,
      }))
    }
    
    /**
     * Delete Comment
     * Delete a comment (and its replies) from a file. Only the comment's author or the file's owner can delete it.
     */
    export async function main(
      auth: RT.Gdrive,
      file_id: string,
      comment_id: DynSelect_comment_id
    ) {
      const response = await fetch(
        `https://www.googleapis.com/drive/v3/files/${file_id}/comments/${comment_id}`,
        {
          method: "DELETE",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return { success: true }
    }
    

    Submitted by hugo989 2 hours ago