shadok-ai 0.20.21 → 0.20.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -3
- package/context/google-skill/SKILL.md +23 -4
- package/context/google-skill/scripts/google.mjs +34 -5
- package/context/google-skill/test/google.test.mjs +30 -5
- package/dist/connected-accounts.d.ts +7 -5
- package/dist/connected-accounts.js +6 -3
- package/dist/connected-accounts.js.map +1 -1
- package/dist/google-actions.d.ts +22 -3
- package/dist/google-actions.js +76 -4
- package/dist/google-actions.js.map +1 -1
- package/dist/google.d.ts +79 -2
- package/dist/google.js +193 -9
- package/dist/google.js.map +1 -1
- package/dist/profiles.js +10 -1
- package/dist/profiles.js.map +1 -1
- package/dist/social-drafts.d.ts +12 -1
- package/dist/social-drafts.js +4 -1
- package/dist/social-drafts.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +1 -1
- package/public/social-card.js +30 -5
package/dist/google.js
CHANGED
|
@@ -1730,7 +1730,7 @@ export async function readDriveFile(token, o, fetchFn) {
|
|
|
1730
1730
|
}
|
|
1731
1731
|
return none("This is a binary file: files are not downloaded — only its details are shown.");
|
|
1732
1732
|
}
|
|
1733
|
-
// ---- bin, move, rename (never a permanent delete
|
|
1733
|
+
// ---- bin, move, rename (never a permanent delete) ----------------------------
|
|
1734
1734
|
const fileUrl = (id, extra = {}) => `${DRIVE}/${pathId("file", id)}?${new URLSearchParams({ supportsAllDrives: "true", fields: "id", ...extra })}`;
|
|
1735
1735
|
/** To the BIN — `trashed: true`, which the person can undo for about 30 days.
|
|
1736
1736
|
* `files.delete` (permanent) and emptying the bin are not called anywhere. */
|
|
@@ -1981,10 +1981,9 @@ export async function replyToComment(token, fileId, commentId, o, fetchFn) {
|
|
|
1981
1981
|
const j = await sendJson("comment reply", "POST", url, token, body, fetchFn, true);
|
|
1982
1982
|
return { id: j.id ?? "" };
|
|
1983
1983
|
}
|
|
1984
|
-
/**
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
const access = [];
|
|
1984
|
+
/** Every permission of a file (`permissions.list`, 5 pages of 100 at most). */
|
|
1985
|
+
export async function listPermissions(token, id, fetchFn) {
|
|
1986
|
+
const out = [];
|
|
1988
1987
|
let page;
|
|
1989
1988
|
for (let i = 0; i < 5; i++) {
|
|
1990
1989
|
const p = new URLSearchParams({ supportsAllDrives: "true", pageSize: "100", fields: "nextPageToken,permissions(id,type,role,emailAddress,domain,displayName,deleted,pendingOwner,expirationTime)" });
|
|
@@ -1992,14 +1991,199 @@ export async function listAccess(token, id, fetchFn) {
|
|
|
1992
1991
|
p.set("pageToken", page);
|
|
1993
1992
|
const j = await getJson("access listing", `${DRIVE}/${pathId("file", id)}/permissions?${p}`, token, fetchFn);
|
|
1994
1993
|
for (const x of j.permissions ?? []) {
|
|
1995
|
-
|
|
1996
|
-
|
|
1994
|
+
out.push({
|
|
1995
|
+
id: x.id ?? "",
|
|
1996
|
+
type: x.type ?? "",
|
|
1997
|
+
role: x.role ?? "",
|
|
1998
|
+
...(x.emailAddress ? { email: x.emailAddress.toLowerCase() } : {}),
|
|
1999
|
+
...(x.domain ? { domain: x.domain } : {}),
|
|
2000
|
+
...(x.displayName ? { displayName: x.displayName } : {}),
|
|
2001
|
+
...(x.deleted ? { deleted: true } : {}),
|
|
2002
|
+
...(x.pendingOwner ? { pendingOwner: true } : {}),
|
|
2003
|
+
...(x.expirationTime ? { expirationTime: x.expirationTime } : {}),
|
|
2004
|
+
});
|
|
1997
2005
|
}
|
|
1998
2006
|
page = j.nextPageToken;
|
|
1999
2007
|
if (!page)
|
|
2000
|
-
return {
|
|
2008
|
+
return { permissions: out, truncated: false };
|
|
2009
|
+
}
|
|
2010
|
+
return { permissions: out, truncated: true };
|
|
2011
|
+
}
|
|
2012
|
+
/** How a person reads one permission: "anyone with the link", "everyone at
|
|
2013
|
+
* example.org", or the name and address. */
|
|
2014
|
+
export function permissionWho(x) {
|
|
2015
|
+
const name = x.type === "anyone" ? "anyone with the link" : x.type === "domain" ? `everyone at ${x.domain ?? "?"}` : who({ displayName: x.displayName, emailAddress: x.email });
|
|
2016
|
+
return x.deleted ? `${name} (deleted account)` : name;
|
|
2017
|
+
}
|
|
2018
|
+
/** Who can reach a file — a READ (`drive access`). Changing it is
|
|
2019
|
+
* `sharePlan` / `applyShare` / `removeAccess`, each behind its own action. */
|
|
2020
|
+
export async function listAccess(token, id, fetchFn) {
|
|
2021
|
+
const r = await listPermissions(token, id, fetchFn);
|
|
2022
|
+
const access = r.permissions.map((x) => ({ type: x.type, role: x.role, who: permissionWho(x), ...(x.pendingOwner ? { pendingOwner: true } : {}), ...(x.expirationTime ? { expires: x.expirationTime } : {}) }));
|
|
2023
|
+
return { access, truncated: r.truncated };
|
|
2024
|
+
}
|
|
2025
|
+
// ---- sharing ------------------------------------------------------------------
|
|
2026
|
+
//
|
|
2027
|
+
// Only NAMED people and groups, as viewer, commenter or editor. Never "anyone
|
|
2028
|
+
// with the link", never a whole domain, never a change of owner: those are not
|
|
2029
|
+
// in the vocabulary at all, so no setting can reach them. Removing access is
|
|
2030
|
+
// the other direction and lives in `removeAccess`.
|
|
2031
|
+
export const SHARE_ROLES = ["reader", "commenter", "writer"];
|
|
2032
|
+
export const MAX_SHARE_ADDRESSES = 20;
|
|
2033
|
+
export const MAX_SHARE_MESSAGE_CHARS = 1000;
|
|
2034
|
+
const SHARE_EMAIL_RE = /^[^\s@<>(),;:"[\]\\]{1,64}@[A-Za-z0-9-]{1,63}(\.[A-Za-z0-9-]{1,63})+$/;
|
|
2035
|
+
/** Roles above editor: such a permission is never changed by a share. */
|
|
2036
|
+
const HIGH_ROLES = new Set(["owner", "organizer", "fileOrganizer"]);
|
|
2037
|
+
export function shareRoleVerdict(v) {
|
|
2038
|
+
if (v === undefined || v === null || v === "")
|
|
2039
|
+
return "reader";
|
|
2040
|
+
const alias = { reader: "reader", viewer: "reader", view: "reader", commenter: "commenter", comment: "commenter", writer: "writer", editor: "writer", edit: "writer" };
|
|
2041
|
+
const r = typeof v === "string" ? alias[v.toLowerCase()] : undefined;
|
|
2042
|
+
if (!r)
|
|
2043
|
+
throw new GoogleInputError("--role must be viewer, commenter or editor (an owner is never changed by an agent).");
|
|
2044
|
+
return r;
|
|
2045
|
+
}
|
|
2046
|
+
/** The people (`users`) and groups (`groups`) to share with: plain addresses,
|
|
2047
|
+
* lowercased, each once, 20 at most. */
|
|
2048
|
+
export function shareTargetsVerdict(users, groups) {
|
|
2049
|
+
const out = [];
|
|
2050
|
+
const seen = new Set();
|
|
2051
|
+
const take = (raw, type) => {
|
|
2052
|
+
if (raw === undefined || raw === null)
|
|
2053
|
+
return;
|
|
2054
|
+
if (!Array.isArray(raw) || !raw.every((x) => typeof x === "string"))
|
|
2055
|
+
throw new GoogleInputError("The addresses must be a list of email addresses.");
|
|
2056
|
+
for (const x of raw) {
|
|
2057
|
+
const email = x.trim().toLowerCase();
|
|
2058
|
+
if (!SHARE_EMAIL_RE.test(email))
|
|
2059
|
+
throw new GoogleInputError(`'${x.slice(0, 80)}' is not an email address. Only named people and groups can be given access — never "anyone with the link" or a whole domain.`);
|
|
2060
|
+
if (seen.has(email))
|
|
2061
|
+
continue;
|
|
2062
|
+
seen.add(email);
|
|
2063
|
+
out.push({ email, type });
|
|
2064
|
+
}
|
|
2065
|
+
};
|
|
2066
|
+
take(users, "user");
|
|
2067
|
+
take(groups, "group");
|
|
2068
|
+
if (!out.length)
|
|
2069
|
+
throw new GoogleInputError("Give at least one address to share with (--with <email>, or --group <email> for a Google group).");
|
|
2070
|
+
if (out.length > MAX_SHARE_ADDRESSES)
|
|
2071
|
+
throw new GoogleInputError(`At most ${MAX_SHARE_ADDRESSES} addresses per share.`);
|
|
2072
|
+
return out;
|
|
2073
|
+
}
|
|
2074
|
+
/** The note Google puts in its notification email; absent = none. */
|
|
2075
|
+
export function shareMessageVerdict(v) {
|
|
2076
|
+
if (v === undefined || v === null || v === "")
|
|
2077
|
+
return undefined;
|
|
2078
|
+
if (typeof v !== "string")
|
|
2079
|
+
throw new GoogleInputError("The message must be text.");
|
|
2080
|
+
const t = v.replace(/\r\n?/g, "\n").trim();
|
|
2081
|
+
if (!t)
|
|
2082
|
+
return undefined;
|
|
2083
|
+
if (/[\u0000-\u0008\u000b-\u001f\u007f]/.test(t))
|
|
2084
|
+
throw new GoogleInputError("The message contains control characters.");
|
|
2085
|
+
if ([...t].length > MAX_SHARE_MESSAGE_CHARS)
|
|
2086
|
+
throw new GoogleInputError(`The message is over ${MAX_SHARE_MESSAGE_CHARS} characters.`);
|
|
2087
|
+
return t;
|
|
2088
|
+
}
|
|
2089
|
+
/**
|
|
2090
|
+
* What sharing with `targets` as `role` would change on a file whose
|
|
2091
|
+
* permissions are `current`. An address that already holds that role is left
|
|
2092
|
+
* out (`unchanged`); one that is owner / organizer is never touched (`kept`).
|
|
2093
|
+
* Pure: computed for the card, and computed AGAIN at apply time against the
|
|
2094
|
+
* permissions as they are then.
|
|
2095
|
+
*/
|
|
2096
|
+
export function sharePlan(current, targets, role) {
|
|
2097
|
+
const grants = [];
|
|
2098
|
+
const unchanged = [];
|
|
2099
|
+
const kept = [];
|
|
2100
|
+
for (const t of targets) {
|
|
2101
|
+
const have = current.find((p) => (p.type === "user" || p.type === "group") && p.email === t.email);
|
|
2102
|
+
if (have && HIGH_ROLES.has(have.role))
|
|
2103
|
+
kept.push(t.email);
|
|
2104
|
+
else if (have && have.role === role)
|
|
2105
|
+
unchanged.push(t.email);
|
|
2106
|
+
else
|
|
2107
|
+
grants.push({ email: t.email, type: t.type, role, ...(have ? { from: have.role } : {}) });
|
|
2108
|
+
}
|
|
2109
|
+
return { grants, unchanged, kept };
|
|
2110
|
+
}
|
|
2111
|
+
/** Delete a permission. A permission already gone is not an error. */
|
|
2112
|
+
async function deletePermission(token, fileId, permissionId, fetchFn) {
|
|
2113
|
+
const url = `${DRIVE}/${pathId("file", fileId)}/permissions/${pathId("permission", permissionId)}?${new URLSearchParams({ supportsAllDrives: "true" })}`;
|
|
2114
|
+
let res;
|
|
2115
|
+
try {
|
|
2116
|
+
res = await fetchFn(url, { method: "DELETE", headers: bearer(token), signal: deadline() });
|
|
2117
|
+
}
|
|
2118
|
+
catch {
|
|
2119
|
+
throw new GoogleOutcomeUnknownError();
|
|
2120
|
+
}
|
|
2121
|
+
if (res.status === 404)
|
|
2122
|
+
return;
|
|
2123
|
+
if (!res.ok)
|
|
2124
|
+
throw await failure("access removal", res);
|
|
2125
|
+
}
|
|
2126
|
+
/**
|
|
2127
|
+
* Apply a share: the permissions are read AGAIN and the plan recomputed, so a
|
|
2128
|
+
* change made since the card (someone already added, a role raised) is never
|
|
2129
|
+
* undone — a share only ever gives `role` to the addresses the card named.
|
|
2130
|
+
* Google emails each new person unless `notify` is false.
|
|
2131
|
+
*/
|
|
2132
|
+
export async function applyShare(token, fileId, grants, o, fetchFn) {
|
|
2133
|
+
const { permissions } = await listPermissions(token, fileId, fetchFn);
|
|
2134
|
+
const applied = [];
|
|
2135
|
+
for (const g of grants) {
|
|
2136
|
+
const plan = sharePlan(permissions, [{ email: g.email, type: g.type }], g.role);
|
|
2137
|
+
const step = plan.grants[0];
|
|
2138
|
+
if (!step)
|
|
2139
|
+
continue;
|
|
2140
|
+
const have = permissions.find((p) => (p.type === "user" || p.type === "group") && p.email === g.email);
|
|
2141
|
+
if (have) {
|
|
2142
|
+
const url = `${DRIVE}/${pathId("file", fileId)}/permissions/${pathId("permission", have.id)}?${new URLSearchParams({ supportsAllDrives: "true", fields: "id" })}`;
|
|
2143
|
+
await sendJson("access change", "PATCH", url, token, { role: g.role }, fetchFn, true);
|
|
2144
|
+
}
|
|
2145
|
+
else {
|
|
2146
|
+
const q = new URLSearchParams({ supportsAllDrives: "true", fields: "id", sendNotificationEmail: o.notify ? "true" : "false" });
|
|
2147
|
+
if (o.notify && o.message)
|
|
2148
|
+
q.set("emailMessage", o.message);
|
|
2149
|
+
await sendJson("share", "POST", `${DRIVE}/${pathId("file", fileId)}/permissions?${q}`, token, { type: g.type, role: g.role, emailAddress: g.email }, fetchFn, true);
|
|
2150
|
+
}
|
|
2151
|
+
applied.push(step);
|
|
2001
2152
|
}
|
|
2002
|
-
return {
|
|
2153
|
+
return { applied };
|
|
2154
|
+
}
|
|
2155
|
+
/**
|
|
2156
|
+
* Which permissions taking access away from `emails` (and, with `anyone`, the
|
|
2157
|
+
* "anyone with the link" access) removes. Refused, before anything is changed:
|
|
2158
|
+
* an owner, the account itself (it would lock the cockpit out of the file), an
|
|
2159
|
+
* address with no access of its own (it may come through a group or a domain).
|
|
2160
|
+
*/
|
|
2161
|
+
export function unsharePlan(current, emails, anyone, self) {
|
|
2162
|
+
const out = [];
|
|
2163
|
+
const me = self.toLowerCase();
|
|
2164
|
+
for (const e of emails) {
|
|
2165
|
+
if (e === me)
|
|
2166
|
+
throw new GoogleInputError("That is this account's own access: removing it would lock the cockpit out of the file.");
|
|
2167
|
+
const have = current.find((p) => (p.type === "user" || p.type === "group") && p.email === e);
|
|
2168
|
+
if (!have)
|
|
2169
|
+
throw new GoogleInputError(`${e} has no access of its own to that file (it may come through a group or the whole domain) — run \`drive access\` to see who has what.`);
|
|
2170
|
+
if (HIGH_ROLES.has(have.role))
|
|
2171
|
+
throw new GoogleInputError(`${e} is an owner or organizer of that file: that access is for the person to change in Drive.`);
|
|
2172
|
+
out.push(have);
|
|
2173
|
+
}
|
|
2174
|
+
if (anyone) {
|
|
2175
|
+
const links = current.filter((p) => p.type === "anyone");
|
|
2176
|
+
if (!links.length)
|
|
2177
|
+
throw new GoogleInputError("That file is not shared with anyone who has the link.");
|
|
2178
|
+
out.push(...links);
|
|
2179
|
+
}
|
|
2180
|
+
if (!out.length)
|
|
2181
|
+
throw new GoogleInputError("Give the addresses to remove (--with <email>) or --anyone for the link sharing.");
|
|
2182
|
+
return out;
|
|
2183
|
+
}
|
|
2184
|
+
export async function removeAccess(token, fileId, permissions, fetchFn) {
|
|
2185
|
+
for (const p of permissions)
|
|
2186
|
+
await deletePermission(token, fileId, p.id, fetchFn);
|
|
2003
2187
|
}
|
|
2004
2188
|
// ---- download ---------------------------------------------------------------
|
|
2005
2189
|
export const MAX_DOWNLOAD_BYTES = 50 * 1024 * 1024;
|