Skip to content

Commit

Permalink
store onedrive id
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Feb 24, 2025
1 parent 98e9db8 commit 7b3ef30
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions localtypings/pxteditor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,9 @@ declare namespace pxt.editor {
header: string;
text: string;
version: string;

// minecraft specific
driveItemId?: string;
};

interface BaseCloudProxyRequest extends EditorMessageRequest {
Expand Down
51 changes: 50 additions & 1 deletion webapp/src/minecraftAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ interface CloudProxyDeleteResponse {
resp: undefined;
}

interface DriveDBEntry {
id: string;
driveItemId: string;
}

const DRIVE_ID_DB_NAME = "__minecraft_userdriveid"
const DRIVE_ID_TABLE = "userdrive";
const DRIVE_ID_KEYPATH = "id";

export class MinecraftAuthClient extends AuthClient {
protected pendingMessages: pxt.Map<(response: pxt.editor.CloudProxyResponse) => void> = {};
protected preferences: pxt.auth.UserPreferences = {};
Expand Down Expand Up @@ -188,7 +197,6 @@ export class MinecraftAuthClient extends AuthClient {
);
}
else if (path === "/api/user/project") {

if (method === "GET") {
// LIST
let headerIds: string[];
Expand Down Expand Up @@ -339,6 +347,12 @@ export class MinecraftAuthClient extends AuthClient {
headerIds
});

if (resp.resp.success) {
for (const project of resp.resp.resp) {
await this.updateProjectDriveId(project);
}
}

return resp.resp;
}

Expand All @@ -348,10 +362,17 @@ export class MinecraftAuthClient extends AuthClient {
headerId
});

if (resp.resp.success) {
await this.updateProjectDriveId(resp.resp.resp);
}

return resp.resp;
}

protected async setAsync(project: pxt.editor.CloudProject): Promise<pxt.auth.ApiResult<string>> {
const id = await this.lookupProjectDriveId(project.id);
project.driveItemId = id;

const resp = await this.postMessageAsync<pxt.editor.CloudProxySetResponse>({
operation: "set",
project
Expand All @@ -367,5 +388,33 @@ export class MinecraftAuthClient extends AuthClient {

return resp.resp;
}

protected async updateProjectDriveId(project: pxt.editor.CloudProject) {
if (!project.driveItemId) return;

const db = getUserDriveDb();
await db.openAsync();

await db.setAsync(DRIVE_ID_TABLE, {
id: project.id,
driveItemId: project.driveItemId
} as DriveDBEntry);
}

protected async lookupProjectDriveId(headerId: string) {
const db = getUserDriveDb();
await db.openAsync();

const entry = await db.getAsync<DriveDBEntry>(DRIVE_ID_TABLE, headerId);
return entry?.driveItemId;
}
}

function getUserDriveDb() {
const db = new pxt.BrowserUtils.IDBWrapper(DRIVE_ID_DB_NAME, 1, (ev, r) => {
const db = r.result as IDBDatabase;
db.createObjectStore(DRIVE_ID_TABLE, { keyPath: DRIVE_ID_KEYPATH });
});

return db;
}

0 comments on commit 7b3ef30

Please sign in to comment.