0

Reply to Comment

by
Published today

Reply to a comment on 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
 * Reply to Comment
35
 * Reply to a comment on a file. Set `reopen` to also reopen a resolved comment.
36
 */
37
export async function main(
38
  auth: RT.Gdrive,
39
  file_id: string,
40
  comment_id: DynSelect_comment_id,
41
  content: string,
42
  reopen: boolean = false
43
) {
44
  const url = new URL(
45
    `https://www.googleapis.com/drive/v3/files/${file_id}/comments/${comment_id}/replies`
46
  )
47
  url.searchParams.append("fields", "*")
48

49
  const response = await fetch(url, {
50
    method: "POST",
51
    headers: {
52
      Authorization: `Bearer ${auth.token}`,
53
      "Content-Type": "application/json",
54
      Accept: "application/json",
55
    },
56
    body: JSON.stringify(reopen ? { content, action: "reopen" } : { content }),
57
  })
58

59
  if (!response.ok) {
60
    throw new Error(`${response.status} ${await response.text()}`)
61
  }
62

63
  return await response.json()
64
}
65