Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetching resources #49

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
};
116 changes: 47 additions & 69 deletions controller/src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
getPublicAccess,
} from "@inrupt/solid-client/universal";
import { Session } from "@inrupt/solid-client-authn-browser";
import { FOAF } from "@inrupt/vocab-common-rdf";
// 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 @@ -140,101 +141,78 @@ export async function getProfileInfo(
throw new Error("Profile information not found");
}

const name = getStringNoLocale(profileThing, FOAF.name) ?? "";
const mbox = getUrl(profileThing, FOAF.mbox) ?? "";
const name = getStringNoLocale(profileThing, Schema.name) ?? "";
const mbox = getUrl(profileThing, Schema.mbox) ?? "";
const description =
getStringWithLocale(
profileThing,
"http://schema.org/description",
"en-us"
) ?? "";
const img = getUrl(profileThing, FOAF.img) ?? "";
const phone = getUrl(profileThing, FOAF.phone) ?? "";
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
* @returns get the users posts
* @param mapFunction Function to map Thing to required type
* @returns The resources mapped to the required type
*/
export async function getPosts(session: Session, url: url): Promise<Post[]> {
const postsDataset = await getSolidDataset(`${url}/profile/posts/`, {
fetch: session.fetch,
});
const resources = getContainedResourceUrlAll(postsDataset);
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 posts: Post[] = [];
const result: T[] = [];

await Promise.all(
resources.map(async (resource) => {
const resourceDataset = await getSolidDataset(resource, {
fetch: session.fetch,
});
const things = getThingAll(resourceDataset);
posts.push(
...things.map((thing) => {
const text = getStringNoLocale(thing, "https://schema.org/text");
const video = getUrl(thing, "https://schema.org/video");
const image = getUrl(thing, "https://schema.org/image");
return { text, video, image };
})
);
result.push(...things.map(mapFunction));
})
);

return posts;
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 the users appointments
* @returns Get the user's appointments
*/
export async function getAppointments(
Vicba marked this conversation as resolved.
Show resolved Hide resolved
session: Session,
url: url
url: string
): Promise<Appointment[]> {
const appointmentsDataset = await getSolidDataset(`${url}/appointments/`, {
fetch: session.fetch,
});
const resources = getContainedResourceUrlAll(appointmentsDataset);

const appointments: Appointment[] = [];

await Promise.all(
resources.map(async (resource) => {
const resourceDataset = await getSolidDataset(resource, {
fetch: session.fetch,
});
const things = getThingAll(resourceDataset);
appointments.push(
...things.map((thing) => {
const type =
getStringNoLocale(thing, "https://schema.org/additionalType") ??
null;
const location =
getStringNoLocale(thing, "https://schema.org/location") ?? null;
const provider =
getStringNoLocale(thing, "https://schema.org/provider") ?? null;
const scheduledTime = getDatetime(
thing,
"https://schema.org/scheduledTime"
);

const date = scheduledTime
? new Date(scheduledTime).toLocaleDateString()
: null;
const time = scheduledTime
? new Date(scheduledTime).toLocaleTimeString()
: null;

return { type, location, provider, date, time };
})
);
})
);

return appointments;
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);
}
16 changes: 8 additions & 8 deletions controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export enum Type {
export type url = string;

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

export interface Appointment {
type?: string | null;
location?: string | null;
provider?: string | null;
date?: string | null;
time?: string | null;
type?: string;
location?: string;
provider?: string;
date?: string;
time?: string;
}
23 changes: 23 additions & 0 deletions loama/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,27 @@
import HeaderTab from "../components/header/HeaderTab.vue";
import HeaderBase from '../components/header/HeaderBase.vue'
import PodList from '../components/PodList.vue'
import { getPosts, getAppointments, getProfileInfo } from "loama-controller";
import { onMounted } from "vue";
import type { Session } from "@inrupt/solid-client-authn-browser";
import { store } from "@/store";

const podUrl = 'https://css12.onto-deside.ilabt.imec.be/osoc1';

const session = store.session;

onMounted(async () => {
try {
const fetchedUserProfile = await getProfileInfo(session as Session, podUrl);
console.log("fetchedUserProfile", fetchedUserProfile);

const appointments = await getAppointments(session as Session, podUrl);
console.log("fetchappointments", appointments);

const posts = await getPosts(session as Session, podUrl);
console.log("fetchposts", posts);
} catch (error) {
console.error("Error fetching data:", error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log 👀

}
});
</script>