Skip to content

Commit

Permalink
Make global user self-fetching and restore sync access (#501 #163)
Browse files Browse the repository at this point in the history
This partly reverts commit 5a8465a.
  • Loading branch information
jgonggrijp committed Nov 10, 2021
1 parent 87b420f commit 0a59abc
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 59 deletions.
7 changes: 5 additions & 2 deletions frontend/src/explorer/explorer-event-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions frontend/src/global/ld-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
Expand Down
41 changes: 9 additions & 32 deletions frontend/src/global/user.ts
Original file line number Diff line number Diff line change
@@ -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<Model> = 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;
2 changes: 1 addition & 1 deletion frontend/src/landing/landing-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/sparql/compile-query-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
24 changes: 12 additions & 12 deletions frontend/src/sparql/compile-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ 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) {
let data = { itemQuery: itemQuery, ...options }
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) {
Expand Down
12 changes: 4 additions & 8 deletions frontend/src/utilities/prefetch-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ export function userNodesFactory() {

function getUserNodes(userNodes: ItemGraph, queryingItems: boolean): PromiseLike<ItemGraph> {
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;
}
Expand Down

0 comments on commit 0a59abc

Please sign in to comment.