0

Resolve Comment

by
Published today

Mark a comment on a file as resolved, optionally with a closing reply.

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
 * Resolve Comment
35
 * Mark a comment on a file as resolved by posting a resolve reply, optionally with a closing message.
36
 */
37
export async function main(
38
  auth: RT.Gdrive,
39
  file_id: string,
40
  comment_id: DynSelect_comment_id,
41
  content: string | undefined
42
) {
43
  const url = new URL(
44
    `https://www.googleapis.com/drive/v3/files/${file_id}/comments/${comment_id}/replies`
45
  )
46
  url.searchParams.append("fields", "*")
47

48
  const body: { [key: string]: any } = { action: "resolve" }
49
  if (content) body.content = content
50

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

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

65
  return await response.json()
66
}
67