From 0a59abc7e3a99eeef2a6e7a9bf96cc0183674aab Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Tue, 19 Oct 2021 16:46:44 +0200 Subject: [PATCH] Make global user self-fetching and restore sync access (#501 #163) This partly reverts commit 5a8465a1f584debba84b434fcf80179d30bd7cd8. --- .../src/explorer/explorer-event-controller.ts | 7 +++- frontend/src/global/ld-store.ts | 2 - frontend/src/global/user.ts | 41 ++++--------------- frontend/src/landing/landing-view.ts | 2 +- frontend/src/main.ts | 2 +- frontend/src/sparql/compile-query-test.ts | 2 +- frontend/src/sparql/compile-query.ts | 24 +++++------ frontend/src/utilities/prefetch-utilities.ts | 12 ++---- 8 files changed, 33 insertions(+), 59 deletions(-) diff --git a/frontend/src/explorer/explorer-event-controller.ts b/frontend/src/explorer/explorer-event-controller.ts index 61d1332a..4d7cd3f8 100644 --- a/frontend/src/explorer/explorer-event-controller.ts +++ b/frontend/src/explorer/explorer-event-controller.ts @@ -34,6 +34,7 @@ import { import { itemsForSourceQuery } from '../sparql/compile-query'; import SemanticQuery from '../semantic-search/model'; import modelToQuery from '../semantic-search/modelToQuery'; +import userChannel from '../common-user/user-radio'; interface ExplorerEventController extends Events { } class ExplorerEventController { @@ -338,8 +339,10 @@ export default ExplorerEventController; */ export function getItems(source: Node): ItemGraph { const sparqlItems = new ItemGraph(); - let queryString = itemsForSourceQuery(asURI(source), {}); - sparqlItems.sparqlQuery(queryString); + userChannel.request('promise').then(() => { + const queryString = itemsForSourceQuery(asURI(source), {}); + sparqlItems.sparqlQuery(queryString); + }); return sparqlItems; } diff --git a/frontend/src/global/ld-store.ts b/frontend/src/global/ld-store.ts index 5a89438f..0b76596c 100644 --- a/frontend/src/global/ld-store.ts +++ b/frontend/src/global/ld-store.ts @@ -9,7 +9,6 @@ import { readit, } from '../common-rdf/ns'; import ldChannel from '../common-rdf/radio'; -import userChannel from '../common-user/user-radio'; import Store from '../common-rdf/store'; import './ontology'; @@ -41,7 +40,6 @@ export const globalGraph = new Store(); export function prefetch() { inhouseGraphs.forEach(ns => globalGraph.import(ns)); - userChannel.trigger('cache'); ldChannel.trigger('cache:ontology'); ldChannel.trigger('cache:nlp-ontology'); ldChannel.trigger('cache:item-list'); diff --git a/frontend/src/global/user.ts b/frontend/src/global/user.ts index 2577c809..a7a0b24c 100644 --- a/frontend/src/global/user.ts +++ b/frontend/src/global/user.ts @@ -1,44 +1,21 @@ +import { constant } from 'lodash'; + import User from '../common-user/user-model'; import userChannel from '../common-user/user-radio'; import { staff } from '../common-rdf/ns'; -import Model from '../core/model'; const user = new User(); -let promise: PromiseLike = null; +const returnUser = constant(user); +const promise = user.fetch().then(returnUser); function getUserURI() { - return fetchUser().then( () => { - const username = user.get('username'); - if (username) return staff(username); - }) -} - -function getPermission(permission: string) { - return fetchUser().then( () => { - return user.hasPermission(permission); - }) -} - -function fetchUser() { - if (!promise) { - promise = user.fetch().then(resolveUser, rejectUser); - } - return promise; -} - -function resolveUser(): User { - promise = Promise.resolve(user); - return user; -} - -function rejectUser(error) { - promise = Promise.reject(error); - return error; + const username = user.get('username'); + if (username) return staff(username); } -userChannel.once('cache', fetchUser); -userChannel.reply('user', fetchUser); +userChannel.reply('user', returnUser); +userChannel.reply('promise', constant(promise)); userChannel.reply('current-user-uri', getUserURI); -userChannel.reply('permission', getPermission); +userChannel.reply('permission', user.hasPermission, user); export default user; diff --git a/frontend/src/landing/landing-view.ts b/frontend/src/landing/landing-view.ts index 8e7fcd1e..43e80990 100644 --- a/frontend/src/landing/landing-view.ts +++ b/frontend/src/landing/landing-view.ts @@ -17,7 +17,7 @@ export default class LandingView extends View { user: string; initialize() { - userChannel.request('user').then( (user) => { + userChannel.request('promise').then( (user) => { this.user = user.get('username'); }) this.awaitNodeLists(); diff --git a/frontend/src/main.ts b/frontend/src/main.ts index 3fbb8a72..46dcfb83 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -8,7 +8,7 @@ import { i18nPromise } from './global/i18n'; import './global/internalLinks'; import './global/hbsHelpers'; import './global/hbsPartials'; -import user from './global/user'; +import './global/user'; import { prefetch } from './global/ld-store'; import './global/item-cache'; import './global/history-notfound-trigger'; diff --git a/frontend/src/sparql/compile-query-test.ts b/frontend/src/sparql/compile-query-test.ts index 0b29b2c5..2d7c1cde 100644 --- a/frontend/src/sparql/compile-query-test.ts +++ b/frontend/src/sparql/compile-query-test.ts @@ -4,7 +4,7 @@ import userChannel from '../common-user/user-radio'; import { itemsForSourceQuery } from './compile-query'; beforeEach(function() { - userChannel.reply('permission', constant(Promise.resolve(true))); + userChannel.reply('permission', constant(true)); }); describe('itemsForSourceQuery', function () { diff --git a/frontend/src/sparql/compile-query.ts b/frontend/src/sparql/compile-query.ts index 003da538..7a5be093 100644 --- a/frontend/src/sparql/compile-query.ts +++ b/frontend/src/sparql/compile-query.ts @@ -24,11 +24,10 @@ export interface SPARQLQueryOptions { export function itemsForSourceQuery(source: string, { ...options }: SPARQLQueryOptions) { let data = { sourceURI: source, from: 'source' }; - return userChannel.request('permission', 'view_all_annotations').then( perm => { - if (!perm) data['userURI'] = userChannel.request('current-user-uri'); - const finalData = { ...data, ...options }; - return itemsTemplate(finalData); - }); + const perm = userChannel.request('permission', 'view_all_annotations'); + if (!perm) data['userURI'] = userChannel.request('current-user-uri'); + const finalData = { ...data, ...options }; + return itemsTemplate(finalData); } export function listNodesQuery(itemQuery: boolean, { ...options }: SPARQLQueryOptions) { @@ -36,14 +35,15 @@ export function listNodesQuery(itemQuery: boolean, { ...options }: SPARQLQueryOp return listNodesTemplate(data); } -export async function nodesByUserQuery(itemQuery: boolean, { ...options }: SPARQLQueryOptions) { - const uri = await userChannel.request('current-user-uri'); - if (!uri) { - return null; +export function nodesByUserQuery( + itemQuery: boolean, options: SPARQLQueryOptions = {} +) { + const userURI = userChannel.request('current-user-uri'); + if (!userURI) { + throw new Error('no authenticated user (hint: await user promise)'); } - const data = {itemQuery: itemQuery, userURI: uri}; - const finalData = { ...data, ...options }; - return nodesByUserTemplate(finalData); + const data = { ...defaultOptions, itemQuery, userURI, ...options }; + return nodesByUserTemplate(data); } export function randomNodesQuery(randomNodes: Model[], lastNode: Model, { ...options }: SPARQLQueryOptions) { diff --git a/frontend/src/utilities/prefetch-utilities.ts b/frontend/src/utilities/prefetch-utilities.ts index 9a418908..56a5ddbb 100644 --- a/frontend/src/utilities/prefetch-utilities.ts +++ b/frontend/src/utilities/prefetch-utilities.ts @@ -14,14 +14,10 @@ export function userNodesFactory() { function getUserNodes(userNodes: ItemGraph, queryingItems: boolean): PromiseLike { if (!promise) { - promise = nodesByUserQuery(queryingItems, {}).then( (query) => { - if (!query) { - return handleError('user not authenticated'); - } - return userNodes.sparqlQuery(query).then( - () => handleSuccess(userNodes), handleError - ); - }); + const query = nodesByUserQuery(queryingItems); + promise = userNodes.sparqlQuery(query).then( + () => handleSuccess(userNodes), handleError + ); } return promise; }