0

Delete Comment

by
Published today

Delete a comment from a file.

Script gdrive Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
export type DynSelect_comment_id = string
4

5
// Dropdown of the file's comments (first 100, newest first).
6
export async function comment_id(auth: RT.Gdrive, file_id: string) {
7
  const response = await fetch(
8
    `https://www.googleapis.com/drive/v3/files/${file_id}/comments?pageSize=100&fields=comments(id,content,resolved,author/displayName)`,
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { comments } = (await response.json()) as {
20
    comments: {
21
      id: string
22
      content: string
23
      resolved?: boolean
24
      author?: { displayName?: string }
25
    }[]
26
  }
27
  return comments.map((c) => ({
28
    value: c.id,
29
    label: `${c.resolved ? "[resolved] " : ""}${c.author?.displayName ?? "?"}: ${c.content.slice(0, 80)}`,
30
  }))
31
}
32

33
/**
34
 * Delete Comment
35
 * Delete a comment (and its replies) from a file. Only the comment's author or the file's owner can delete it.
36
 */
37
export async function main(
38
  auth: RT.Gdrive,
39
  file_id: string,
40
  comment_id: DynSelect_comment_id
41
) {
42
  const response = await fetch(
43
    `https://www.googleapis.com/drive/v3/files/${file_id}/comments/${comment_id}`,
44
    {
45
      method: "DELETE",
46
      headers: {
47
        Authorization: `Bearer ${auth.token}`,
48
        Accept: "application/json",
49
      },
50
    }
51
  )
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  return { success: true }
58
}
59