Skip to content

Commit

Permalink
Merge pull request #71 from Arquisoft/dev-renderfriendslocations
Browse files Browse the repository at this point in the history
Friends locations proper rendering
  • Loading branch information
Manuhcuartas authored Apr 23, 2023
2 parents 070ff04 + e50c466 commit 815e2b6
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 30 deletions.
8 changes: 0 additions & 8 deletions webapp/src/App.test.js

This file was deleted.

22 changes: 20 additions & 2 deletions webapp/src/components/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {readLocations, writeLocations} from "../handlers/podAccess";
import Rating from "react-rating-stars-component";
import EditLocationAltIcon from '@mui/icons-material/EditLocationAlt';
import {Box, InputLabel,Typography, Container,IconButton} from '@mui/material';
import {getFriendsWebIds} from "../handlers/podHandler";

// setting the width and height of the <div> around the google map
const containerStyle = {
Expand Down Expand Up @@ -91,9 +92,26 @@ function Map({ changesInFilters,selectedFilters,isInteractive,session, onMarkerA

// Function to get and set the locations on the map
const retrieveLocations=async () => {
let resource = session.info.webId.replace("/profile/card#me", "/lomap/locations.ttl")
return await readLocations(resource, session); //TODO -> si usamos session handler podríamos tener las localizaciones en session?
let friends = await getFriendsWebIds(session.info.webId);
let resource = session.info.webId.replace("/profile/card#me", "/private/lomap/locations.ttl");
// Code to get the friends locations
let locations = await readLocations(resource, session);
resource = session.info.webId.replace("/profile/card#me", "/public/lomap/locations.ttl");
locations = locations.concat(await readLocations(resource, session));
let friendsLocations = [];
for (let i = 0; i < friends.length; i++) {
try {
//concat it with the previous locations (concat returns a new array instead of modifying any of the existing ones)
friendsLocations = friendsLocations.concat(await readLocations(friends[i].replace("/profile/card", "/") + "public/lomap/locations.ttl",session));
} catch (err) {
//Friend does not have LoMap??
console.log(err);
}
}

return locations.concat(friendsLocations); //TODO -> si usamos session handler podríamos tener las localizaciones en session?
}

async function getAndSetLocations() {
let locationSet = await retrieveLocations()
setMarkers((current) => [...current, ...locationSet]);
Expand Down
Empty file.
18 changes: 3 additions & 15 deletions webapp/src/handlers/podAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,13 @@ import {
getThingAll,
getSolidDataset,
removeThing,
getStringNoLocale, addUrl, addStringNoLocale
getStringNoLocale, getThing, addUrl, addStringNoLocale
} from "@inrupt/solid-client";
import { SCHEMA_INRUPT, RDF} from "@inrupt/vocab-common-rdf";
import {getDefaultSession} from "@inrupt/solid-client-authn-browser";
import {checkForLomap} from "./podHandler";
import {LocationLM} from "../models/location";
import {CoordinatesInvalidFormatException, StringInvalidFormatException} from "../util/Exceptions/exceptions";
import {convertViewObjectsIntoDomainModelObjects} from "../util/Convertor";

/**
* Save user's session changes into de POD.
* @param {User} user
* @returns {Promise<void>}
*
async function writeLocations(user, session) {
//This can be parallel
writeLocIntoPOD(user.resourceURLPublic, user.publicLocat, session);
writeLocIntoPOD(user.resourceURLPrivate, user.privateLocat, session);
}
*/

async function writeLocations(resourceURL, session, list) {
let i = 0;
Expand Down Expand Up @@ -82,6 +69,7 @@ async function writeLocations(resourceURL, session, list) {
i++;
}
window.alert("Saved");

}

async function getDatasetAndRemoveContent(resourceURL,session){
Expand Down Expand Up @@ -110,7 +98,6 @@ async function getDataset(resourceURL,session){
}
return dataset;
}

async function writeReviews(user) {
user.getReviews();
await writeUserReviews(user.resourceURLPublic, user.publicReviews);
Expand Down Expand Up @@ -280,6 +267,7 @@ async function readLocations(resourceURL,session) {
return locationsRetrieved;

}

async function readReviews(resourceURL,session) {
let reviewThings = await getThingsFromDataset(resourceURL, session);
let review, reviewThing;
Expand Down
174 changes: 169 additions & 5 deletions webapp/src/handlers/podHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import {
getPodUrlAll,
createContainerAt,
getSolidDataset, getStringNoLocale, getThing, getUrlAll,
getSolidDataset,
getStringNoLocale,
getThing,
getUrlAll,
getSolidDatasetWithAcl,
hasResourceAcl,
hasAccessibleAcl,
hasFallbackAcl,
createAcl,
createAclFromFallbackAcl,
getResourceAcl,
setAgentResourceAccess,
saveAclFor,
createSolidDataset, saveSolidDatasetAt,


} from "@inrupt/solid-client";
Expand Down Expand Up @@ -46,13 +59,15 @@ export async function checkForLomap(session) {
export async function checkForLomapInPod(pod,session) {
try {

let aux= await getSolidDataset(pod+"lomap",{fetch : session.fetch});
let aux= await getSolidDataset(pod+"public/lomap",{fetch : session.fetch});
let aux2= await getSolidDataset(pod+"private/lomap",{fetch : session.fetch});

} catch (error) {
console.log("Not found lomap folder in pod, we'll try creating one...")
return false;
}
console.log("Found lomap folder in pod.")
await giveFriendsAccess(session, true);
return true;
}

Expand Down Expand Up @@ -90,17 +105,46 @@ export async function requestAccessToLomap( session){
}
);
}

/**
* Mock function that creates a mock locations.ttl file to test the friends stuff
* @param session with the current session
*/
async function mockFiles(session, resource) {
let currentUserLomapLocation = resource;
let mockDataset = createSolidDataset();



try {
await saveSolidDatasetAt(currentUserLomapLocation, mockDataset, {fetch: session.fetch});
} catch (e) {
console.log(e);
}
}


export async function createLomapContainer(pod,session) {
// console.log('linea 94 '+pod)
await createContainerAt(pod + "lomap/",{fetch : session.fetch});
// create the lomap containers
await createContainerAt(pod + "public/lomap/",{fetch : session.fetch});
await createContainerAt(pod + "private/lomap/",{fetch : session.fetch});
// create all the tl files used to store locations and reviews (all empty)
await mockFiles(session, session.info.webId.replace("/profile/card#me", "/private/lomap/locations.ttl"));
await mockFiles(session, session.info.webId.replace("/profile/card#me", "/public/lomap/locations.ttl"));
await mockFiles(session, session.info.webId.replace("/profile/card#me", "/private/lomap/reviews.ttl"));
await mockFiles(session, session.info.webId.replace("/profile/card#me", "/public/lomap/reviews.ttl"));
// create the containers that are going to store the images
await createContainerAt(session.info.webId.replace("/profile/card#me", "/public/lomap/images/"), {fetch: session.fetch});
await createContainerAt(session.info.webId.replace("/profile/card#me", "/private/lomap/images/"), {fetch: session.fetch});
await giveFriendsAccess(session, true);
}

/**
* It returns a list with the friends of the logged in user
* @param webId Containing the webId whose friends we are returning
* @returns {Promise<*[]>} Containing a List of the webIds of the friends
*/
async function getFriendsWebIds(webId) {
export async function getFriendsWebIds(webId) {
let profile = await getProfile(webId);
let ids = getUrlAll(profile, FOAF.knows);

Expand All @@ -111,6 +155,126 @@ async function getFriendsWebIds(webId) {
return list;
}

/**
* Gives all the friends of the user in session access to the lomap info
* @param session containing the current session
*/
async function giveFriendsAccess(session, access) {
let currentUser = session.info.webId
let friends = await getFriendsWebIds(currentUser);
let resourceURL = currentUser.replace("/profile/card#me", "/") + "public/lomap/";

for (let i = 0; i < friends.length; i++) { //Set access to public (friends only) locations
await setAccessToFriend(friends[i].replace("/profile/card","/")+"profile/card#me", resourceURL, access, session);
}
}



/**
* Grant/ Revoke permissions of friends regarding a particular location
* @param friend webID of the friend whose access edit
* @param location location to give permission to
* @param access boolean containing the value regarding permissions
* @param session object containing the current session info
*/
async function setAccessToFriend(friend, location, access, session){
let locationsURL = location + "locations.ttl";
let reviewsURL = location + "reviews.ttl";
await giveAccessToFile(locationsURL, friend, session);
// Fetch the SolidDataset and its associated ACL, if available:
let myDatasetWithAcl;
try {
myDatasetWithAcl = await getSolidDatasetWithAcl(location, {fetch: session.fetch});
// Obtain the SolidDataset's own ACL, if available, or initialise a new one, if possible:
let resourceAcl;
if (!hasResourceAcl(myDatasetWithAcl)) {
if (!hasAccessibleAcl(myDatasetWithAcl)) {
// "The current user does not have permission to change access rights to this Resource."
}
if (!hasFallbackAcl(myDatasetWithAcl)) {
// create new ACL
resourceAcl = createAcl(myDatasetWithAcl);
}
else{
// create ACL from fallback
resourceAcl = createAclFromFallbackAcl(myDatasetWithAcl);
}
} else {
// get the ACL of the dataset
resourceAcl = getResourceAcl(myDatasetWithAcl);
}

let updatedAcl;
if (access) {
// give permissions
updatedAcl = setAgentResourceAccess(
resourceAcl,
friend,
{ read: true, append: true, write: false, control: false }
);
}
else {
// cancel permissions
updatedAcl = setAgentResourceAccess(
resourceAcl,
friend,
{ read: false, append: false, write: false, control: false }
);
}
// save the ACL
await saveAclFor(myDatasetWithAcl, updatedAcl, {fetch: session.fetch});
}
catch (error){
console.log(error)// catch any error
}
}

/**
* Gives access to the locations.ttl file
* @param resource containing the path of such file
* @param friend containing the webID of the friend we are giving access
* @param session with the current session
*/
async function giveAccessToFile(resource, friend, session){
let myDatasetWithAcl;
try {
myDatasetWithAcl = await getSolidDatasetWithAcl(resource, {fetch: session.fetch}); // inventory
// Obtain the SolidDataset's own ACL, if available, or initialise a new one, if possible:
let resourceAcl;
if (!hasResourceAcl(myDatasetWithAcl)) {
if (!hasAccessibleAcl(myDatasetWithAcl)) {
// "The current user does not have permission to change access rights to this Resource."
}
if (!hasFallbackAcl(myDatasetWithAcl)) {
// create new access control list
resourceAcl = createAcl(myDatasetWithAcl);
}
else{
// create access control list from fallback
resourceAcl = createAclFromFallbackAcl(myDatasetWithAcl);
}
} else {
// get the access control list of the dataset
resourceAcl = getResourceAcl(myDatasetWithAcl);
}

let updatedAcl;
// grant permissions
updatedAcl = setAgentResourceAccess(
resourceAcl,
friend,
{ read: true, append: true, write: false, control: false }
);
// save the access control list
await saveAclFor(myDatasetWithAcl, updatedAcl, {fetch: session.fetch});
}
catch (error){ // catch any possible thrown errors
console.log(error)
}
}


/**
* Returns the profile for a given webId
* @param webId containing the webId whose profile we want
Expand Down

0 comments on commit 815e2b6

Please sign in to comment.