Skip to content

Commit

Permalink
feat: org thumbnail (#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuniper authored Mar 5, 2024
1 parent 0c8c56e commit 2d61161
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 30 deletions.
48 changes: 18 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/common/src/ArcGISContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { HubServiceStatus } from "./core/types/ISystemStatus";
import { checkPermission } from "./permissions/checkPermission";
import { HubEntity } from "./core/types/HubEntity";
import { getOrgThumbnailUrl } from "./resources/get-org-thumbnail-url";

/**
* Hash of Hub API end points so updates
Expand Down Expand Up @@ -238,6 +239,11 @@ export interface IArcGISContext {
*/
history: IHubHistory;

/**
* Return the portal thumbnail url
*/
orgThumbnailUrl: string;

/**
* Return the token for a given app, if defined
* @param app
Expand Down Expand Up @@ -805,6 +811,10 @@ export class ArcGISContext implements IArcGISContext {
return this._userHubSettings;
}

public get orgThumbnailUrl(): string {
return getOrgThumbnailUrl(this.portal, this.session?.token);
}

/**
* Return a token for a specific app
* @param app
Expand Down
23 changes: 23 additions & 0 deletions packages/common/src/resources/get-org-thumbnail-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IPortal } from "@esri/arcgis-rest-portal";
import { getPortalUrl } from "../urls/get-portal-url";

/**
* Construct a the full url to a portal thumbnail
*
* - If the portal has a thumbnail, construct the full url
* - If the portal is not public, append on the token
* @param portal
* @param token
* @returns
*/
export function getOrgThumbnailUrl(portal: IPortal, token?: string): string {
let thumbnailUrl = null;
if (portal?.thumbnail) {
const portalUrl = getPortalUrl(portal);
thumbnailUrl = `${portalUrl}/sharing/rest/portals/${portal.id}/resources/${portal.thumbnail}`;
if (token && portal.access !== "public") {
thumbnailUrl = `${thumbnailUrl}?token=${token}`;
}
}
return thumbnailUrl;
}
1 change: 1 addition & 0 deletions packages/common/src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./fetch-and-upload-thumbnail";
export * from "./fetch-image-as-blob";
export * from "./get-item-assets";
export * from "./get-item-thumbnail-url";
export * from "./get-org-thumbnail-url";
export * from "./string-to-blob";
export * from "./upload-resources-from-url";
export * from "./add-solution-resource-url-to-assets";
Expand Down
5 changes: 5 additions & 0 deletions packages/common/test/ArcGISContextManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const onlinePortalSelfResponse = {
},
},
user: cloneObject(onlineUserResponse) as portalModule.IUser,
thumbnail: "fake-thumbnail.jpg",
};

const onlinePortalSelfWithLimitsResponse = {
Expand Down Expand Up @@ -302,6 +303,7 @@ describe("ArcGISContext:", () => {
expect(mgr.context.userHubSettings).toEqual({ schemaVersion: 1 });
expect(mgr.context.isAlphaOrg).toEqual(false);
expect(mgr.context.isBetaOrg).toEqual(false);
expect(mgr.context.orgThumbnailUrl).toBeNull();
});
it("verify alpha and beta orgs", async () => {
const mgr = await ArcGISContextManager.create({
Expand Down Expand Up @@ -689,6 +691,9 @@ describe("ArcGISContext:", () => {
expect(mgr.context.properties.alphaOrgs).toEqual(["FAKEID", "FOTHERID"]);
expect(mgr.context.isAlphaOrg).toBeTruthy();
expect(mgr.context.isBetaOrg).toBeTruthy();
expect(mgr.context.orgThumbnailUrl).toBe(
`${MOCK_AUTH.portal}/portals/FAKEID/resources/fake-thumbnail.jpg?token=${MOCK_AUTH.token}`
);
});
it("verify props update setting session after", async () => {
spyOn(portalModule, "getSelf").and.callFake(() => {
Expand Down
34 changes: 34 additions & 0 deletions packages/common/test/resources/get-portal-thumbnail-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getOrgThumbnailUrl } from "../../src";
import { IPortal } from "@esri/arcgis-rest-portal";

describe("getPortalThumbnailUrl:", () => {
const token = "FAKE_TOKEN";
it("returns null if no thumbnail present", () => {
const portal = {} as IPortal;
expect(getOrgThumbnailUrl(portal, token)).toBeNull();
});
it("constructs url without token for public portals", () => {
const portal = {
id: "abc123",
access: "public",
thumbnail: "photo.jpg",
isPortal: true,
name: "jsmith",
} as IPortal;
expect(getOrgThumbnailUrl(portal, token)).toEqual(
"https://www.arcgis.com/sharing/rest/portals/abc123/resources/photo.jpg"
);
});
it("constructs url with token for non-public portals", () => {
const portal = {
id: "abc123",
access: "org",
thumbnail: "photo.jpg",
isPortal: true,
name: "jsmith",
} as IPortal;
expect(getOrgThumbnailUrl(portal, token)).toEqual(
"https://www.arcgis.com/sharing/rest/portals/abc123/resources/photo.jpg?token=FAKE_TOKEN"
);
});
});

0 comments on commit 2d61161

Please sign in to comment.