Files
outline/app/stores/CollectionsStore.js
Tom Moor 07a941a65d Websocket Support (#937)
* Atom / RSS meta link

* Spike

* Feeling good about this spike now

* Remove document.collection

* Remove koa.ctx from all presenters to make them portable outside requests

* Remove full serialized model from events
Move events.add to controllers for now, will eventually be in commands

* collections.create event
parentDocument -> parentDocumentId

* Fix up deprecated tests

* Fixed: Doc creation

* documents.move

* Handle collection deleted

* 💚

* Authorize room join requests

* Move starred data structure
Account for documents with no context on sockets

* Add socket.io-redis

* Add WEBSOCKETS_ENABLED env variable to disable websockets entirely for self hosted
New installations will default to true, existing installations to false

* 💚 No need for promise response here

* Reload notice
2019-04-17 19:11:23 -07:00

113 lines
2.8 KiB
JavaScript

// @flow
import { computed, runInAction } from 'mobx';
import { concat, filter, last } from 'lodash';
import { client } from 'utils/ApiClient';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
import Collection from 'models/Collection';
import naturalSort from 'shared/utils/naturalSort';
export type DocumentPathItem = {
id: string,
collectionId: string,
title: string,
url: string,
type: 'collection' | 'document',
};
export type DocumentPath = DocumentPathItem & {
path: DocumentPathItem[],
};
export default class CollectionsStore extends BaseStore<Collection> {
constructor(rootStore: RootStore) {
super(rootStore, Collection);
}
@computed
get active(): ?Collection {
return this.rootStore.ui.activeCollectionId
? this.data.get(this.rootStore.ui.activeCollectionId)
: undefined;
}
@computed
get orderedData(): Collection[] {
return filter(
naturalSort(Array.from(this.data.values()), 'name'),
d => !d.deletedAt
);
}
@computed
get public(): Collection[] {
return this.orderedData.filter(collection => !collection.private);
}
@computed
get private(): Collection[] {
return this.orderedData.filter(collection => collection.private);
}
/**
* List of paths to each of the documents, where paths are composed of id and title/name pairs
*/
@computed
get pathsToDocuments(): DocumentPath[] {
let results = [];
const travelDocuments = (documentList, collectionId, path) =>
documentList.forEach(document => {
const { id, title, url } = document;
const node = { id, collectionId, title, url, type: 'document' };
results.push(concat(path, node));
travelDocuments(document.children, collectionId, concat(path, [node]));
});
if (this.isLoaded) {
this.data.forEach(collection => {
const { id, name, url } = collection;
const node = {
id,
collectionId: id,
title: name,
url,
type: 'collection',
};
results.push([node]);
travelDocuments(collection.documents, id, [node]);
});
}
return results.map(result => {
const tail = last(result);
return {
...tail,
path: result,
};
});
}
getPathForDocument(documentId: string): ?DocumentPath {
return this.pathsToDocuments.find(path => path.id === documentId);
}
titleForDocument(documentUrl: string): ?string {
const path = this.pathsToDocuments.find(path => path.url === documentUrl);
if (path) return path.title;
}
delete(collection: Collection) {
super.delete(collection);
runInAction(() => {
this.rootStore.documents.fetchRecentlyUpdated();
this.rootStore.documents.fetchRecentlyViewed();
});
}
export = () => {
return client.post('/collections.exportAll');
};
}