Skip to content

Commit

Permalink
Merge pull request #49 from osoc24/47-get-resources-pod
Browse files Browse the repository at this point in the history
fetching resources
  • Loading branch information
Zelzahn authored Jul 18, 2024
2 parents 828ce85 + e8401cf commit 229bec7
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 199 deletions.
9 changes: 7 additions & 2 deletions controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"types": "dist/index.d.ts",
"keywords": [],
"author": "Open Summer of Code",
"contributors": ["BT-Creator", "Vicba", "Zelzahn"],
"contributors": [
"BT-Creator",
"Vicba",
"Zelzahn"
],
"license": "ISC",
"files": [
"/dist"
Expand All @@ -21,6 +25,7 @@
"packageManager": "[email protected]+sha512.140036830124618d624a2187b50d04289d5a087f326c9edfc0ccd733d76c4f52c3a313d4fc148794a2a9d81553016004e6742e8cf850670268a7387fc220c903",
"dependencies": {
"@inrupt/solid-client": "^2.0.1",
"@inrupt/solid-client-authn-browser": "^2.2.4"
"@inrupt/solid-client-authn-browser": "^2.2.4",
"@inrupt/vocab-common-rdf": "^1.0.5"
}
}
8 changes: 8 additions & 0 deletions controller/pnpm-lock.yaml

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

19 changes: 19 additions & 0 deletions controller/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
import { FOAF } from "@inrupt/vocab-common-rdf";

export * from "./pod";
export * from "./index-file";

export const Schema = {
name: FOAF.name,
mbox: FOAF.mbox,
description: "http://schema.org/description",
img: FOAF.img,
phone: FOAF.phone,

text: "https://schema.org/text",
video: "https://schema.org/video",
image: "https://schema.org/image",

additionalType: "https://schema.org/additionalType",
location: "https://schema.org/location",
provider: "https://schema.org/provider",
scheduledTime: "https://schema.org/scheduledTime",
};
110 changes: 109 additions & 1 deletion controller/src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import {
getPropertyAll,
getSolidDataset,
getThingAll,
getThing,
getStringNoLocale,
getUrl,
getContainedResourceUrlAll,
getDatetime,
getStringWithLocale,
} from "@inrupt/solid-client";
import {
getAgentAccessAll,
getPublicAccess,
} from "@inrupt/solid-client/universal";
import { Session } from "@inrupt/solid-client-authn-browser";
import { Permission, url } from "./types";
// import { FOAF } from "@inrupt/vocab-common-rdf";
import { Permission, url, Post, Appointment } from "./types";
import { Schema } from "./index";

/**
* List the pods that are from the currently authenticated user.
Expand Down Expand Up @@ -108,3 +116,103 @@ function accessModesToPermissions(
}))
);
}

/**
* @param session An active Solid client connection
* @param url URL to the user's pod
* @returns the information about the user
*/
export async function getProfileInfo(
session: Session,
url: url
): Promise<{
name: string;
mbox: string;
description: string;
img: string;
phone: string;
}> {
const dataset = await getSolidDataset(`${url}/profile/card#me`, {
fetch: session.fetch,
});
const profileThing = getThing(dataset, `${url}/profile/card#me`);

if (!profileThing) {
throw new Error("Profile information not found");
}

const name = getStringNoLocale(profileThing, Schema.name) ?? "";
const mbox = getUrl(profileThing, Schema.mbox) ?? "";
const description =
getStringWithLocale(profileThing, Schema.description, "en-us") ?? "";
const img = getUrl(profileThing, Schema.img) ?? "";
const phone = getUrl(profileThing, Schema.phone) ?? "";

return { name, mbox, description, img, phone };
}

/**
* @param session An active Solid client connection
* @param url URL to the user's pod
* @param mapFunction Function to map Thing to required type
* @returns The resources mapped to the required type
*/
async function fetchResources<T>(
session: Session,
url: string,
mapFunction: (thing: any) => T
): Promise<T[]> {
const dataset = await getSolidDataset(url, { fetch: session.fetch });
const resources = getContainedResourceUrlAll(dataset);

const result: T[] = [];

await Promise.all(
resources.map(async (resource) => {
const resourceDataset = await getSolidDataset(resource, {
fetch: session.fetch,
});
const things = getThingAll(resourceDataset);
result.push(...things.map(mapFunction));
})
);

return result;
}

/**
* @param session An active Solid client connection
* @param url URL to the user's pod
* @returns Get the user's posts
*/
export async function getPosts(session: Session, url: string): Promise<Post[]> {
const mapPost = (thing: any): Post => ({
text: getStringNoLocale(thing, Schema.text) || "",
video: getUrl(thing, Schema.video) || "",
image: getUrl(thing, Schema.image) || "",
});
return fetchResources(session, `${url}/profile/posts/`, mapPost);
}

/**
* @param session An active Solid client connection
* @param url URL to the user's pod
* @returns Get the user's appointments
*/
export async function getAppointments(
session: Session,
url: string
): Promise<Appointment[]> {
const mapAppointment = (thing: any): Appointment => {
const scheduledTime = getDatetime(thing, Schema.scheduledTime);
return {
type: getStringNoLocale(thing, Schema.additionalType) || "",
location: getStringNoLocale(thing, Schema.location) || "",
provider: getStringNoLocale(thing, Schema.provider) || "",
date: scheduledTime ? new Date(scheduledTime).toLocaleDateString() : "",
time: scheduledTime ? new Date(scheduledTime).toLocaleTimeString() : "",
};
};

return fetchResources(session, `${url}/appointments/`, mapAppointment);
}
14 changes: 14 additions & 0 deletions controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ export enum Type {
}

export type url = string;

export interface Post {
text: string;
video?: string;
image?: string;
}

export interface Appointment {
type?: string;
location?: string;
provider?: string;
date?: string;
time?: string;
}
Loading

0 comments on commit 229bec7

Please sign in to comment.