Edits history of script submission #23190 for ' Resolve 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)}`,
      }))
    }
    
    /**
     * Resolve Comment
     * Mark a comment on a file as resolved by posting a resolve reply, optionally with a closing message.
     */
    export async function main(
      auth: RT.Gdrive,
      file_id: string,
      comment_id: DynSelect_comment_id,
      content: string | undefined
    ) {
      const url = new URL(
        `https://www.googleapis.com/drive/v3/files/${file_id}/comments/${comment_id}/replies`
      )
      url.searchParams.append("fields", "*")
    
      const body: { [key: string]: any } = { action: "resolve" }
      if (content) body.content = content
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago