Edits history of script submission #23188 for ' Remove Permission (gdrive)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_permission_id = string
    
    // Dropdown of the file's current permissions.
    export async function permission_id(auth: RT.Gdrive, file_id: string) {
      const response = await fetch(
        `https://www.googleapis.com/drive/v3/files/${file_id}/permissions?supportsAllDrives=true&pageSize=100&fields=permissions(id,type,role,emailAddress,domain)`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { permissions } = (await response.json()) as {
        permissions: {
          id: string
          type: string
          role: string
          emailAddress?: string
          domain?: string
        }[]
      }
      return permissions.map((p) => ({
        value: p.id,
        label: `${p.emailAddress ?? p.domain ?? p.type} (${p.role})`,
      }))
    }
    
    /**
     * Remove Permission
     * Unshare a file or folder by deleting one of its permissions (pick it from the dropdown, or take the id from List Permissions). The owner's permission can't be removed; transfer ownership first.
     */
    export async function main(
      auth: RT.Gdrive,
      file_id: string,
      permission_id: DynSelect_permission_id
    ) {
      const url = new URL(
        `https://www.googleapis.com/drive/v3/files/${file_id}/permissions/${permission_id}`
      )
      url.searchParams.append("supportsAllDrives", "true")
    
      const response = await fetch(url, {
        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