Skip to content

Commit

Permalink
Merge pull request #43 from solid/groupWebid
Browse files Browse the repository at this point in the history
Group webid
  • Loading branch information
bourgeoa authored May 19, 2021
2 parents 779df67 + f79b3d3 commit e5d6416
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
9 changes: 9 additions & 0 deletions contactLogic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Logic for solid contacts

import * as UI from 'solid-ui'
import { getPersonas } from './webidControl'

const ns = UI.ns
const $rdf = UI.rdf
Expand Down Expand Up @@ -146,8 +147,16 @@ export async function addPersonToGroup (thing, group) {
$rdf.st(group, ns.vcard('hasMember'), thing, group.doc()),
$rdf.st(thing, ns.vcard('fn'), pname, group.doc())
]
// find person webIDs
const webIDs = getPersonas(kb, thing).map(webid => webid.value)
webIDs.forEach(webid => {
ins.push($rdf.st(thing, ns.owl('sameAs'), kb.sym(webid), group.doc()))
})
try {
await updater.update([], ins)
// to allow refresh of card groupList
kb.fetcher.unload(group.doc())
kb.fetcher.load(group.doc())
} catch (e) {
throw new Error(`Error adding ${pname} to group ${gname}:` + e)
}
Expand Down
15 changes: 10 additions & 5 deletions contactsPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,13 @@ export default {
) {
console.log('Deleting a contact ' + pname)
await loadAllGroups() // need to wait for all groups to be loaded in case they have a link to this person
await deleteThingAndDoc(person)
// load people.ttl
const nameEmailIndex = kb.any(book, ns.vcard('nameEmailIndex'))
await kb.fetcher.load(nameEmailIndex)

// - delete the references to it in group files and save them back
// - delete the reference in people.ttl and save it back
await deleteThingAndDoc(person)
await deleteRecursive(kb, container)
refreshNames() // "Doesn't work" -- maybe does now with waiting for async
cardMain.innerHTML = 'Contact Data Deleted.'
Expand Down Expand Up @@ -409,12 +413,13 @@ export default {
}

let cards = []
for (const u in selectedGroups) {
if (selectedGroups[u]) {
const a = kb.each(kb.sym(u), ns.vcard('hasMember'))
const groups = Object.keys(selectedGroups).map(groupURI => kb.sym(groupURI))
groups.forEach(group => {
if (selectedGroups[group.value]) {
const a = kb.each(group, ns.vcard('hasMember'), null, group.doc())
cards = cards.concat(a)
}
}
})
cards.sort(compareForSort) // @@ sort by name not UID later
for (let k = 0; k < cards.length - 1;) {
if (cards[k].uri === cards[k + 1].uri) {
Expand Down
38 changes: 28 additions & 10 deletions groupMembershipControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Render a control to record the group memberships we have for this agent
import * as UI from 'solid-ui'

const $rdf = UI.rdf
// const $rdf = UI.rdf
const ns = UI.ns
// const buttons = UI.buttonsn no
// const widgets = UI.widgets
Expand All @@ -11,10 +11,9 @@ const kb = UI.store
// const style = UI.style

// Groups the person is a member of

export async function renderGroupMemberships (person, context) {
// Remove a person from a group
function removeFromGroup (thing, group) {
async function removeFromGroup (thing, group) {
const pname = kb.any(thing, ns.vcard('fn'))
const gname = kb.any(group, ns.vcard('fn'))
const groups = kb.each(null, ns.vcard('hasMember'), thing)
Expand All @@ -26,21 +25,23 @@ export async function renderGroupMemberships (person, context) {
}
const message = 'Remove ' + pname + ' from group ' + gname + '?'
if (confirm(message)) {
const del = [
$rdf.st(group, ns.vcard('hasMember'), thing, group.doc()),
$rdf.st(thing, ns.vcard('fn'), pname, group.doc())
]
const del = kb
.statementsMatching(person, undefined, undefined, group.doc())
.concat(kb.statementsMatching(undefined, undefined, person, group.doc()))
kb.updater.update(del, [], function (uri, ok, err) {
if (!ok) {
const message = 'Error removing member from group ' + group + ': ' + err
groupList.parentNode.appendChild(UI.widgets.errorMessageBlock(dom, message, 'pink'))
return
}
console.log('Removed ' + pname + ' from group ' + gname)
syncGroupList()
})
console.log('Removed ' + pname + ' from group ' + gname)
// to allow refresh of card groupList
kb.fetcher.unload(group.doc())
kb.fetcher.load(group.doc())
syncGroupList()
}
}

function newRowForGroup (group) {
const options = {
deleteFunction: function () {
Expand All @@ -54,11 +55,28 @@ export async function renderGroupMemberships (person, context) {

function syncGroupList () {
const groups = kb.each(null, ns.vcard('hasMember'), person)

utils.syncTableToArray(groupList, groups, newRowForGroup)
}

async function loadGroupsFromBook (book = undefined) {
if (!book) {
book = kb.any(undefined, ns.vcard('includesGroup'))
if (!book) {
throw new Error('findBookFromGroups: Cant find address book which this group is part of')
}
}
const groupIndex = kb.any(book, ns.vcard('groupIndex'))
const gs = book ? kb.each(book, ns.vcard('includesGroup'), null, groupIndex) : []
await kb.fetcher.load(gs)
}

const { dom } = context
const groupList = dom.createElement('table')

// find book any group and load all groups
await loadGroupsFromBook()

groupList.refresh = syncGroupList
syncGroupList()
return groupList
Expand Down
31 changes: 29 additions & 2 deletions webidControl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Render a control to record the webids we have for this agent
/* eslint-disable multiline-ternary */
import * as UI from 'solid-ui'
import { updateMany } from './contactLogic'

const $rdf = UI.rdf
const ns = UI.ns
Expand Down Expand Up @@ -30,18 +31,35 @@ export async function addWebIDToContacts (person, webid, urlType, kb) {
}
}
*/

// check this is a url
try {
const url = new URL(webid)
} catch (error) {
throw new Error(`${WEBID_NOUN}: ${webid} is not a valid url.`)
}

// create a person's webID
console.log(`Adding to ${person} a ${WEBID_NOUN}: ${webid}.`)
const vcardURLThing = kb.bnode()
const insertables = [
$rdf.st(person, ns.vcard('url'), vcardURLThing, person.doc()),
$rdf.st(vcardURLThing, ns.rdf('type'), urlType, person.doc()),
$rdf.st(vcardURLThing, ns.vcard('value'), webid, person.doc())
]
await kb.updater.update([], insertables)
const groups = kb.each(null, ns.vcard('hasMember'), person)
groups.forEach(group => {
insertables.push($rdf.st(person, ns.owl('sameAs'), kb.sym(webid), group.doc()))
})
try {
await updateMany([], insertables)
} catch (err) { throw new Error(`Could not create webId ${WEBID_NOUN}: ${webid}.`) }
}

export async function removeWebIDFromContacts (person, webid, urlType, kb) {
console.log(`Removing from ${person} their ${WEBID_NOUN}: ${webid}.`)

// remove webID from card
const existing = kb.each(person, ns.vcard('url'), null, person.doc())
.filter(urlObject => kb.holds(urlObject, ns.rdf('type'), urlType, person.doc()))
.filter(urlObject => kb.holds(urlObject, ns.vcard('value'), webid, person.doc()))
Expand All @@ -55,6 +73,14 @@ export async function removeWebIDFromContacts (person, webid, urlType, kb) {
$rdf.st(vcardURLThing, ns.vcard('value'), webid, person.doc())
]
await kb.updater.update(deletables, [])

// remove webIDs from groups
const groups = kb.each(null, ns.vcard('hasMember'), person)
const removeFromGroups = []
groups.forEach(group => {
removeFromGroups.push($rdf.st(person, ns.owl('sameAs'), kb.sym(webid), group.doc()))
})
await updateMany(removeFromGroups)
}

// Trace things the same as this - other IDs for same thing
Expand Down Expand Up @@ -86,6 +112,7 @@ export function getSameAs (kb, thing, doc) { // Should this recurse?
return Array.from(found).map(uri => kb.sym(uri)) // return as array of nodes
}

// find person webIDs
export function getPersonas (kb, person) {
const lits = vcardWebIDs(kb, person).concat(getSameAs(kb, person, person.doc()))
const strings = new Set(lits.map(lit => lit.value)) // remove dups
Expand Down Expand Up @@ -206,7 +233,7 @@ export async function renderIdControl (person, dataBrowserContext, options) {
mainCell.setAttribute('colspan', 3)
let main

var profileIsVisible = true
let profileIsVisible = true

const rhs = nav.children[2]
const openButton = rhs.appendChild(widgets.button(dom, DOWN_ARROW, 'View', profileOpenHandler))
Expand Down

0 comments on commit e5d6416

Please sign in to comment.