2017-06-28 03:59:53 +00:00
|
|
|
// @flow
|
2021-05-11 15:07:41 +00:00
|
|
|
import path from "path";
|
2020-08-09 05:53:59 +00:00
|
|
|
import invariant from "invariant";
|
2020-09-22 02:18:09 +00:00
|
|
|
import { find, orderBy, filter, compact, omitBy } from "lodash";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { observable, action, computed, runInAction } from "mobx";
|
2020-10-28 02:15:35 +00:00
|
|
|
import { MAX_TITLE_LENGTH } from "shared/constants";
|
2020-11-09 06:33:52 +00:00
|
|
|
import { subtractDate } from "shared/utils/date";
|
2020-06-20 20:59:15 +00:00
|
|
|
import naturalSort from "shared/utils/naturalSort";
|
|
|
|
import BaseStore from "stores/BaseStore";
|
|
|
|
import RootStore from "stores/RootStore";
|
|
|
|
import Document from "models/Document";
|
2021-05-06 01:48:37 +00:00
|
|
|
import env from "env";
|
2021-05-23 02:34:05 +00:00
|
|
|
import type {
|
|
|
|
NavigationNode,
|
|
|
|
FetchOptions,
|
|
|
|
PaginationParams,
|
|
|
|
SearchResult,
|
|
|
|
} from "types";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { client } from "utils/ApiClient";
|
2017-07-16 18:47:48 +00:00
|
|
|
|
2020-09-21 05:27:11 +00:00
|
|
|
type ImportOptions = {
|
|
|
|
publish?: boolean,
|
|
|
|
};
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export default class DocumentsStore extends BaseStore<Document> {
|
2019-01-10 05:57:17 +00:00
|
|
|
@observable searchCache: Map<string, SearchResult[]> = new Map();
|
2019-04-18 02:11:23 +00:00
|
|
|
@observable starredIds: Map<string, boolean> = new Map();
|
2019-07-08 02:25:45 +00:00
|
|
|
@observable backlinks: Map<string, string[]> = new Map();
|
2020-12-16 03:07:29 +00:00
|
|
|
@observable movingDocumentId: ?string;
|
2017-07-18 05:24:19 +00:00
|
|
|
|
2020-09-21 05:27:11 +00:00
|
|
|
importFileTypes: string[] = [
|
2021-02-08 00:14:03 +00:00
|
|
|
".md",
|
2021-05-11 15:07:41 +00:00
|
|
|
".doc",
|
|
|
|
".docx",
|
2020-09-21 05:27:11 +00:00
|
|
|
"text/markdown",
|
|
|
|
"text/plain",
|
|
|
|
"text/html",
|
2020-10-21 15:53:59 +00:00
|
|
|
"application/msword",
|
2020-09-21 05:27:11 +00:00
|
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
];
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
constructor(rootStore: RootStore) {
|
|
|
|
super(rootStore, Document);
|
|
|
|
}
|
2017-06-28 03:59:53 +00:00
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
2019-04-06 23:20:27 +00:00
|
|
|
get all(): Document[] {
|
2020-08-08 22:18:37 +00:00
|
|
|
return filter(
|
|
|
|
this.orderedData,
|
2020-08-09 01:53:11 +00:00
|
|
|
(d) => !d.archivedAt && !d.deletedAt && !d.template
|
2020-08-08 22:18:37 +00:00
|
|
|
);
|
2019-04-06 23:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed
|
|
|
|
get recentlyViewed(): Document[] {
|
2018-11-21 04:18:24 +00:00
|
|
|
return orderBy(
|
2020-09-21 06:37:09 +00:00
|
|
|
filter(this.all, (d) => d.lastViewedAt),
|
|
|
|
"lastViewedAt",
|
2020-06-20 20:59:15 +00:00
|
|
|
"desc"
|
2018-11-21 04:18:24 +00:00
|
|
|
);
|
2017-07-09 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
2019-04-06 23:20:27 +00:00
|
|
|
get recentlyUpdated(): Document[] {
|
2020-06-20 20:59:15 +00:00
|
|
|
return orderBy(this.all, "updatedAt", "desc");
|
2017-07-09 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
get templates(): Document[] {
|
|
|
|
return orderBy(
|
|
|
|
filter(
|
|
|
|
this.orderedData,
|
2020-08-09 01:53:11 +00:00
|
|
|
(d) => !d.archivedAt && !d.deletedAt && d.template
|
2020-08-08 22:18:37 +00:00
|
|
|
),
|
|
|
|
"updatedAt",
|
|
|
|
"desc"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
createdByUser(userId: string): Document[] {
|
2018-08-11 07:46:10 +00:00
|
|
|
return orderBy(
|
2020-08-09 01:53:11 +00:00
|
|
|
filter(this.all, (d) => d.createdBy.id === userId),
|
2020-06-20 20:59:15 +00:00
|
|
|
"updatedAt",
|
|
|
|
"desc"
|
2018-08-10 06:14:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-27 05:10:24 +00:00
|
|
|
inCollection(collectionId: string): Document[] {
|
2020-08-09 01:53:11 +00:00
|
|
|
return filter(
|
|
|
|
this.all,
|
|
|
|
(document) => document.collectionId === collectionId
|
|
|
|
);
|
2019-06-27 05:10:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
templatesInCollection(collectionId: string): Document[] {
|
|
|
|
return orderBy(
|
|
|
|
filter(
|
|
|
|
this.orderedData,
|
2020-08-09 01:53:11 +00:00
|
|
|
(d) =>
|
2020-08-08 22:18:37 +00:00
|
|
|
!d.archivedAt &&
|
|
|
|
!d.deletedAt &&
|
|
|
|
d.template === true &&
|
|
|
|
d.collectionId === collectionId
|
|
|
|
),
|
|
|
|
"updatedAt",
|
|
|
|
"desc"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
pinnedInCollection(collectionId: string): Document[] {
|
2018-08-11 07:46:10 +00:00
|
|
|
return filter(
|
2018-11-21 04:18:24 +00:00
|
|
|
this.recentlyUpdatedInCollection(collectionId),
|
2020-08-09 01:53:11 +00:00
|
|
|
(document) => document.pinned
|
2018-03-01 07:28:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-08 07:42:55 +00:00
|
|
|
publishedInCollection(collectionId: string): Document[] {
|
|
|
|
return filter(
|
2019-04-06 23:20:27 +00:00
|
|
|
this.all,
|
2020-08-09 01:53:11 +00:00
|
|
|
(document) =>
|
2019-01-08 07:42:55 +00:00
|
|
|
document.collectionId === collectionId && !!document.publishedAt
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:37:27 +00:00
|
|
|
rootInCollection(collectionId: string): Document[] {
|
|
|
|
const collection = this.rootStore.collections.get(collectionId);
|
|
|
|
if (!collection) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return compact(collection.documents.map((node) => this.get(node.id)));
|
|
|
|
}
|
|
|
|
|
2019-01-08 07:42:55 +00:00
|
|
|
leastRecentlyUpdatedInCollection(collectionId: string): Document[] {
|
2020-06-20 20:59:15 +00:00
|
|
|
return orderBy(this.inCollection(collectionId), "updatedAt", "asc");
|
2019-01-08 07:42:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 04:18:24 +00:00
|
|
|
recentlyUpdatedInCollection(collectionId: string): Document[] {
|
2020-06-20 20:59:15 +00:00
|
|
|
return orderBy(this.inCollection(collectionId), "updatedAt", "desc");
|
2017-11-20 05:32:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 07:14:43 +00:00
|
|
|
recentlyPublishedInCollection(collectionId: string): Document[] {
|
|
|
|
return orderBy(
|
2019-01-08 07:42:55 +00:00
|
|
|
this.publishedInCollection(collectionId),
|
2020-06-20 20:59:15 +00:00
|
|
|
"publishedAt",
|
|
|
|
"desc"
|
2019-01-08 07:14:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-08 07:42:55 +00:00
|
|
|
alphabeticalInCollection(collectionId: string): Document[] {
|
2020-06-20 20:59:15 +00:00
|
|
|
return naturalSort(this.inCollection(collectionId), "title");
|
2019-01-08 07:42:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:57:17 +00:00
|
|
|
searchResults(query: string): SearchResult[] {
|
|
|
|
return this.searchCache.get(query) || [];
|
|
|
|
}
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
get starred(): Document[] {
|
2020-08-09 01:53:11 +00:00
|
|
|
return orderBy(
|
|
|
|
filter(this.all, (d) => d.isStarred),
|
|
|
|
"updatedAt",
|
|
|
|
"desc"
|
|
|
|
);
|
2019-04-06 23:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed
|
|
|
|
get archived(): Document[] {
|
|
|
|
return filter(
|
2020-06-20 20:59:15 +00:00
|
|
|
orderBy(this.orderedData, "archivedAt", "desc"),
|
2020-08-09 01:53:11 +00:00
|
|
|
(d) => d.archivedAt && !d.deletedAt
|
2019-04-06 23:20:27 +00:00
|
|
|
);
|
2017-07-09 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 02:51:32 +00:00
|
|
|
@computed
|
|
|
|
get deleted(): Document[] {
|
|
|
|
return filter(
|
2020-06-20 20:59:15 +00:00
|
|
|
orderBy(this.orderedData, "deletedAt", "desc"),
|
2020-08-09 01:53:11 +00:00
|
|
|
(d) => d.deletedAt
|
2019-11-19 02:51:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-20 07:01:49 +00:00
|
|
|
@computed
|
|
|
|
get starredAlphabetical(): Document[] {
|
2020-06-20 20:59:15 +00:00
|
|
|
return naturalSort(this.starred, "title");
|
2018-11-20 07:01:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@computed
|
|
|
|
get templatesAlphabetical(): Document[] {
|
|
|
|
return naturalSort(this.templates, "title");
|
|
|
|
}
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
@computed
|
2020-11-09 06:33:52 +00:00
|
|
|
get totalDrafts(): number {
|
|
|
|
return this.drafts().length;
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:36:09 +00:00
|
|
|
drafts = (
|
|
|
|
options: {
|
|
|
|
...PaginationParams,
|
|
|
|
dateFilter?: "day" | "week" | "month" | "year",
|
|
|
|
collectionId?: string,
|
|
|
|
} = {}
|
|
|
|
): Document[] => {
|
2020-11-09 06:33:52 +00:00
|
|
|
let drafts = filter(
|
2020-06-20 20:59:15 +00:00
|
|
|
orderBy(this.all, "updatedAt", "desc"),
|
2020-08-09 01:53:11 +00:00
|
|
|
(doc) => !doc.publishedAt
|
2018-02-28 06:41:12 +00:00
|
|
|
);
|
2020-11-09 06:33:52 +00:00
|
|
|
|
|
|
|
if (options.dateFilter) {
|
|
|
|
drafts = filter(
|
|
|
|
drafts,
|
|
|
|
(draft) =>
|
|
|
|
new Date(draft.updatedAt) >=
|
2021-01-30 05:36:09 +00:00
|
|
|
subtractDate(new Date(), options.dateFilter || "year")
|
2020-11-09 06:33:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.collectionId) {
|
|
|
|
drafts = filter(drafts, { collectionId: options.collectionId });
|
|
|
|
}
|
|
|
|
|
|
|
|
return drafts;
|
|
|
|
};
|
2018-02-28 06:41:12 +00:00
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get active(): ?Document {
|
2018-12-05 06:24:30 +00:00
|
|
|
return this.rootStore.ui.activeDocumentId
|
|
|
|
? this.data.get(this.rootStore.ui.activeDocumentId)
|
2017-08-29 15:37:17 +00:00
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2019-07-08 02:25:45 +00:00
|
|
|
fetchBacklinks = async (documentId: string): Promise<?(Document[])> => {
|
|
|
|
const res = await client.post(`/documents.list`, {
|
|
|
|
backlinkDocumentId: documentId,
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Document list not available");
|
2019-07-08 02:25:45 +00:00
|
|
|
const { data } = res;
|
2020-06-20 20:59:15 +00:00
|
|
|
runInAction("DocumentsStore#fetchBacklinks", () => {
|
2019-07-08 02:25:45 +00:00
|
|
|
data.forEach(this.add);
|
2020-12-01 06:38:11 +00:00
|
|
|
this.addPolicies(res.policies);
|
2020-08-09 01:53:11 +00:00
|
|
|
this.backlinks.set(
|
|
|
|
documentId,
|
|
|
|
data.map((doc) => doc.id)
|
|
|
|
);
|
2019-07-08 02:25:45 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
getBacklinedDocuments(documentId: string): Document[] {
|
|
|
|
const documentIds = this.backlinks.get(documentId) || [];
|
|
|
|
return orderBy(
|
2020-08-09 01:53:11 +00:00
|
|
|
compact(documentIds.map((id) => this.data.get(id))),
|
2020-06-20 20:59:15 +00:00
|
|
|
"updatedAt",
|
|
|
|
"desc"
|
2019-07-08 02:25:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-23 01:06:39 +00:00
|
|
|
@action
|
|
|
|
fetchChildDocuments = async (documentId: string): Promise<?(Document[])> => {
|
|
|
|
const res = await client.post(`/documents.list`, {
|
|
|
|
parentDocumentId: documentId,
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Document list not available");
|
2019-12-23 01:06:39 +00:00
|
|
|
const { data } = res;
|
2020-06-20 20:59:15 +00:00
|
|
|
runInAction("DocumentsStore#fetchChildDocuments", () => {
|
2019-12-23 01:06:39 +00:00
|
|
|
data.forEach(this.add);
|
2020-12-01 06:38:11 +00:00
|
|
|
this.addPolicies(res.policies);
|
2019-12-23 01:06:39 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-08 02:25:45 +00:00
|
|
|
@action
|
2018-12-05 06:24:30 +00:00
|
|
|
fetchNamedPage = async (
|
2020-06-20 20:59:15 +00:00
|
|
|
request: string = "list",
|
2021-01-30 05:36:09 +00:00
|
|
|
options: ?Object
|
2018-08-11 21:02:37 +00:00
|
|
|
): Promise<?(Document[])> => {
|
2017-07-18 04:46:32 +00:00
|
|
|
this.isFetching = true;
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
try {
|
2017-09-03 21:12:37 +00:00
|
|
|
const res = await client.post(`/documents.${request}`, options);
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Document list not available");
|
|
|
|
runInAction("DocumentsStore#fetchNamedPage", () => {
|
2019-10-06 01:42:03 +00:00
|
|
|
res.data.forEach(this.add);
|
|
|
|
this.addPolicies(res.policies);
|
2017-06-28 03:59:53 +00:00
|
|
|
this.isLoaded = true;
|
|
|
|
});
|
2019-10-06 01:42:03 +00:00
|
|
|
return res.data;
|
2017-07-18 04:46:32 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-06 23:20:27 +00:00
|
|
|
@action
|
|
|
|
fetchArchived = async (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("archived", options);
|
2019-04-06 23:20:27 +00:00
|
|
|
};
|
|
|
|
|
2019-11-19 02:51:32 +00:00
|
|
|
@action
|
|
|
|
fetchDeleted = async (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("deleted", options);
|
2019-11-19 02:51:32 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-11-21 04:18:24 +00:00
|
|
|
fetchRecentlyUpdated = async (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("list", options);
|
2017-09-03 21:12:37 +00:00
|
|
|
};
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@action
|
|
|
|
fetchTemplates = async (options: ?PaginationParams): Promise<*> => {
|
|
|
|
return this.fetchNamedPage("list", { ...options, template: true });
|
|
|
|
};
|
|
|
|
|
2019-01-08 07:42:55 +00:00
|
|
|
@action
|
|
|
|
fetchAlphabetical = async (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("list", {
|
|
|
|
sort: "title",
|
|
|
|
direction: "ASC",
|
2019-01-08 07:42:55 +00:00
|
|
|
...options,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
fetchLeastRecentlyUpdated = async (
|
|
|
|
options: ?PaginationParams
|
|
|
|
): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("list", {
|
|
|
|
sort: "updatedAt",
|
|
|
|
direction: "ASC",
|
2019-01-08 07:42:55 +00:00
|
|
|
...options,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-08 07:14:43 +00:00
|
|
|
@action
|
|
|
|
fetchRecentlyPublished = async (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("list", {
|
|
|
|
sort: "publishedAt",
|
|
|
|
direction: "DESC",
|
2019-01-08 07:14:43 +00:00
|
|
|
...options,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2017-12-04 00:50:50 +00:00
|
|
|
fetchRecentlyViewed = async (options: ?PaginationParams): Promise<*> => {
|
2020-09-21 06:37:09 +00:00
|
|
|
return this.fetchNamedPage("viewed", options);
|
2017-06-28 04:20:09 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-08-11 06:03:47 +00:00
|
|
|
fetchStarred = (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("starred", options);
|
2018-02-28 06:41:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
2018-08-11 06:03:47 +00:00
|
|
|
fetchDrafts = (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("drafts", options);
|
2017-06-28 05:15:29 +00:00
|
|
|
};
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
@action
|
2018-08-11 06:03:47 +00:00
|
|
|
fetchPinned = (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("pinned", options);
|
2018-03-01 07:28:36 +00:00
|
|
|
};
|
|
|
|
|
2018-08-10 06:14:51 +00:00
|
|
|
@action
|
2018-08-11 06:03:47 +00:00
|
|
|
fetchOwned = (options: ?PaginationParams): Promise<*> => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return this.fetchNamedPage("list", options);
|
2018-08-10 06:14:51 +00:00
|
|
|
};
|
|
|
|
|
2020-09-26 18:07:49 +00:00
|
|
|
@action
|
2021-01-30 05:36:09 +00:00
|
|
|
searchTitles = async (query: string) => {
|
2020-09-26 18:07:49 +00:00
|
|
|
const res = await client.get("/documents.search_titles", {
|
|
|
|
query,
|
|
|
|
});
|
|
|
|
invariant(res && res.data, "Search response should be available");
|
|
|
|
|
|
|
|
// add the documents and associated policies to the store
|
|
|
|
res.data.forEach(this.add);
|
|
|
|
this.addPolicies(res.policies);
|
|
|
|
return res.data;
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2017-12-04 00:50:50 +00:00
|
|
|
search = async (
|
|
|
|
query: string,
|
2021-01-30 05:36:09 +00:00
|
|
|
options: {
|
|
|
|
offset?: number,
|
|
|
|
limit?: number,
|
|
|
|
dateFilter?: "day" | "week" | "month" | "year",
|
|
|
|
includeArchived?: boolean,
|
|
|
|
includeDrafts?: boolean,
|
|
|
|
collectionId?: string,
|
|
|
|
userId?: string,
|
|
|
|
}
|
2018-08-05 01:32:56 +00:00
|
|
|
): Promise<SearchResult[]> => {
|
2020-08-09 01:53:11 +00:00
|
|
|
const compactedOptions = omitBy(options, (o) => !o);
|
2020-06-20 20:59:15 +00:00
|
|
|
const res = await client.get("/documents.search", {
|
2019-04-23 14:31:20 +00:00
|
|
|
...compactedOptions,
|
2017-12-04 00:50:50 +00:00
|
|
|
query,
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Search response should be available");
|
2019-01-10 05:57:17 +00:00
|
|
|
|
2019-12-24 01:59:15 +00:00
|
|
|
// add the documents and associated policies to the store
|
2020-08-09 01:53:11 +00:00
|
|
|
res.data.forEach((result) => this.add(result.document));
|
2019-12-24 01:59:15 +00:00
|
|
|
this.addPolicies(res.policies);
|
2019-01-10 05:57:17 +00:00
|
|
|
|
|
|
|
// store a reference to the document model in the search cache instead
|
|
|
|
// of the original result from the API.
|
|
|
|
const results: SearchResult[] = compact(
|
2020-08-09 01:53:11 +00:00
|
|
|
res.data.map((result) => {
|
2019-01-10 05:57:17 +00:00
|
|
|
const document = this.data.get(result.document.id);
|
|
|
|
if (!document) return null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
ranking: result.ranking,
|
|
|
|
context: result.context,
|
|
|
|
document,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
let existing = this.searchCache.get(query) || [];
|
|
|
|
|
|
|
|
// splice modifies any existing results, taking into account pagination
|
|
|
|
existing.splice(options.offset || 0, options.limit || 0, ...results);
|
|
|
|
|
|
|
|
this.searchCache.set(query, existing);
|
2019-12-24 02:08:40 +00:00
|
|
|
return res.data;
|
2017-09-13 06:30:18 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-12-05 06:24:30 +00:00
|
|
|
prefetchDocument = (id: string) => {
|
2020-07-17 04:26:23 +00:00
|
|
|
if (!this.data.get(id) && !this.getByUrl(id)) {
|
2018-12-05 06:24:30 +00:00
|
|
|
return this.fetch(id, { prefetch: true });
|
2018-05-13 20:26:06 +00:00
|
|
|
}
|
2017-10-20 07:27:01 +00:00
|
|
|
};
|
|
|
|
|
2020-08-08 22:18:37 +00:00
|
|
|
@action
|
|
|
|
templatize = async (id: string): Promise<?Document> => {
|
|
|
|
const doc: ?Document = this.data.get(id);
|
|
|
|
invariant(doc, "Document should exist");
|
|
|
|
|
|
|
|
if (doc.template) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await client.post("/documents.templatize", { id });
|
|
|
|
invariant(res && res.data, "Document not available");
|
|
|
|
|
|
|
|
this.addPolicies(res.policies);
|
|
|
|
this.add(res.data);
|
|
|
|
|
|
|
|
return this.data.get(res.data.id);
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-12-05 06:24:30 +00:00
|
|
|
fetch = async (
|
|
|
|
id: string,
|
2020-09-21 05:32:28 +00:00
|
|
|
options: FetchOptions = {}
|
2021-05-23 02:34:05 +00:00
|
|
|
): Promise<{ document: ?Document, sharedTree?: NavigationNode }> => {
|
2018-05-13 20:26:06 +00:00
|
|
|
if (!options.prefetch) this.isFetching = true;
|
2017-07-18 04:46:32 +00:00
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
try {
|
2018-12-05 06:24:30 +00:00
|
|
|
const doc: ?Document = this.data.get(id) || this.getByUrl(id);
|
2019-10-06 01:42:03 +00:00
|
|
|
const policy = doc ? this.rootStore.policies.get(doc.id) : undefined;
|
|
|
|
if (doc && policy && !options.force) {
|
2021-05-23 02:34:05 +00:00
|
|
|
return { document: doc };
|
2019-10-06 01:42:03 +00:00
|
|
|
}
|
2018-05-17 06:52:26 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const res = await client.post("/documents.info", {
|
2018-05-13 20:26:06 +00:00
|
|
|
id,
|
|
|
|
shareId: options.shareId,
|
2021-05-23 02:34:05 +00:00
|
|
|
apiVersion: 2,
|
2018-05-13 20:26:06 +00:00
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Document not available");
|
2019-08-22 04:41:37 +00:00
|
|
|
|
|
|
|
this.addPolicies(res.policies);
|
2021-05-23 02:34:05 +00:00
|
|
|
this.add(res.data.document);
|
2017-07-09 05:12:14 +00:00
|
|
|
|
2021-05-23 02:34:05 +00:00
|
|
|
return {
|
|
|
|
document: this.data.get(res.data.document.id),
|
|
|
|
sharedTree: res.data.sharedTree,
|
|
|
|
};
|
2017-07-18 04:46:32 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
@action
|
2019-04-09 04:25:13 +00:00
|
|
|
move = async (
|
2020-12-16 03:07:29 +00:00
|
|
|
documentId: string,
|
2019-04-09 04:25:13 +00:00
|
|
|
collectionId: string,
|
2020-12-31 20:51:12 +00:00
|
|
|
parentDocumentId: ?string,
|
|
|
|
index: ?number
|
2019-04-09 04:25:13 +00:00
|
|
|
) => {
|
2020-12-16 03:07:29 +00:00
|
|
|
this.movingDocumentId = documentId;
|
2018-12-05 06:24:30 +00:00
|
|
|
|
2020-12-16 03:07:29 +00:00
|
|
|
try {
|
|
|
|
const res = await client.post("/documents.move", {
|
|
|
|
id: documentId,
|
|
|
|
collectionId,
|
|
|
|
parentDocumentId,
|
2020-12-31 20:51:12 +00:00
|
|
|
index: index,
|
2020-12-16 03:07:29 +00:00
|
|
|
});
|
|
|
|
invariant(res && res.data, "Data not available");
|
|
|
|
|
|
|
|
res.data.documents.forEach(this.add);
|
|
|
|
res.data.collections.forEach(this.rootStore.collections.add);
|
|
|
|
this.addPolicies(res.policies);
|
|
|
|
} finally {
|
|
|
|
this.movingDocumentId = undefined;
|
|
|
|
}
|
2018-12-05 06:24:30 +00:00
|
|
|
};
|
|
|
|
|
2018-06-05 13:57:26 +00:00
|
|
|
@action
|
|
|
|
duplicate = async (document: Document): * => {
|
2020-10-28 02:15:35 +00:00
|
|
|
const append = " (duplicate)";
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
const res = await client.post("/documents.create", {
|
2019-10-06 01:42:03 +00:00
|
|
|
publish: !!document.publishedAt,
|
2019-04-18 02:11:23 +00:00
|
|
|
parentDocumentId: document.parentDocumentId,
|
2019-07-27 17:33:38 +00:00
|
|
|
collectionId: document.collectionId,
|
2020-08-08 22:18:37 +00:00
|
|
|
template: document.template,
|
2020-10-28 02:15:35 +00:00
|
|
|
title: `${document.title.slice(
|
|
|
|
0,
|
|
|
|
MAX_TITLE_LENGTH - append.length
|
|
|
|
)}${append}`,
|
2018-06-05 13:57:26 +00:00
|
|
|
text: document.text,
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
invariant(res && res.data, "Data should be available");
|
2018-06-05 13:57:26 +00:00
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
const collection = this.getCollectionForDocument(document);
|
|
|
|
if (collection) collection.refresh();
|
2018-06-05 13:57:26 +00:00
|
|
|
|
2019-08-22 04:41:37 +00:00
|
|
|
this.addPolicies(res.policies);
|
2018-12-05 06:24:30 +00:00
|
|
|
return this.add(res.data);
|
2017-06-28 03:59:53 +00:00
|
|
|
};
|
|
|
|
|
2020-09-21 05:27:11 +00:00
|
|
|
@action
|
|
|
|
import = async (
|
|
|
|
file: File,
|
|
|
|
parentDocumentId: string,
|
|
|
|
collectionId: string,
|
|
|
|
options: ImportOptions
|
|
|
|
) => {
|
2021-05-06 01:48:37 +00:00
|
|
|
// file.type can be an empty string sometimes
|
2021-05-11 15:07:41 +00:00
|
|
|
if (
|
|
|
|
file.type &&
|
|
|
|
!this.importFileTypes.includes(file.type) &&
|
|
|
|
!this.importFileTypes.includes(path.extname(file.name))
|
|
|
|
) {
|
2021-05-06 01:48:37 +00:00
|
|
|
throw new Error(`The selected file type is not supported (${file.type})`);
|
|
|
|
}
|
2021-05-11 15:07:41 +00:00
|
|
|
|
2021-05-06 01:48:37 +00:00
|
|
|
if (file.size > env.MAXIMUM_IMPORT_SIZE) {
|
|
|
|
throw new Error("The selected file was too large to import");
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:27:11 +00:00
|
|
|
const title = file.name.replace(/\.[^/.]+$/, "");
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
[
|
|
|
|
{ key: "parentDocumentId", value: parentDocumentId },
|
|
|
|
{ key: "collectionId", value: collectionId },
|
|
|
|
{ key: "title", value: title },
|
|
|
|
{ key: "publish", value: options.publish },
|
|
|
|
{ key: "file", value: file },
|
2020-09-26 18:07:49 +00:00
|
|
|
].forEach((info) => {
|
2020-09-21 05:27:11 +00:00
|
|
|
if (typeof info.value === "string" && info.value) {
|
|
|
|
formData.append(info.key, info.value);
|
|
|
|
}
|
|
|
|
if (typeof info.value === "boolean") {
|
|
|
|
formData.append(info.key, info.value.toString());
|
|
|
|
}
|
|
|
|
if (info.value instanceof File) {
|
|
|
|
formData.append(info.key, info.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await client.post("/documents.import", formData);
|
|
|
|
invariant(res && res.data, "Data should be available");
|
|
|
|
|
|
|
|
this.addPolicies(res.policies);
|
|
|
|
return this.add(res.data);
|
|
|
|
};
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
_add = this.add;
|
|
|
|
|
|
|
|
@action
|
|
|
|
add = (item: Object) => {
|
|
|
|
const document = this._add(item);
|
|
|
|
|
|
|
|
if (item.starred !== undefined) {
|
|
|
|
this.starredIds.set(document.id, item.starred);
|
|
|
|
}
|
|
|
|
|
|
|
|
return document;
|
|
|
|
};
|
|
|
|
|
2019-06-27 05:10:24 +00:00
|
|
|
@action
|
|
|
|
removeCollectionDocuments(collectionId: string) {
|
|
|
|
const documents = this.inCollection(collectionId);
|
2020-08-09 01:53:11 +00:00
|
|
|
const documentIds = documents.map((doc) => doc.id);
|
|
|
|
documentIds.forEach((id) => this.remove(id));
|
2019-06-27 05:10:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
@action
|
2019-08-09 06:09:09 +00:00
|
|
|
async update(params: {
|
|
|
|
id: string,
|
|
|
|
title: string,
|
|
|
|
text: string,
|
|
|
|
lastRevision: number,
|
|
|
|
}) {
|
2018-12-05 06:24:30 +00:00
|
|
|
const document = await super.update(params);
|
|
|
|
|
|
|
|
// Because the collection object contains the url and title
|
|
|
|
// we need to ensure they are updated there as well.
|
|
|
|
const collection = this.getCollectionForDocument(document);
|
|
|
|
if (collection) collection.updateDocument(document);
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
@action
|
2021-06-25 23:14:40 +00:00
|
|
|
async delete(document: Document, options?: {| permanent: boolean |}) {
|
|
|
|
await super.delete(document, options);
|
2018-12-05 06:24:30 +00:00
|
|
|
|
2020-09-01 03:28:41 +00:00
|
|
|
// check to see if we have any shares related to this document already
|
|
|
|
// loaded in local state. If so we can go ahead and remove those too.
|
|
|
|
const share = this.rootStore.shares.getByDocumentId(document.id);
|
|
|
|
if (share) {
|
|
|
|
this.rootStore.shares.remove(share.id);
|
|
|
|
}
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
const collection = this.getCollectionForDocument(document);
|
|
|
|
if (collection) collection.refresh();
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2019-04-06 23:20:27 +00:00
|
|
|
archive = async (document: Document) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
const res = await client.post("/documents.archive", {
|
2019-04-06 23:20:27 +00:00
|
|
|
id: document.id,
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
runInAction("Document#archive", () => {
|
|
|
|
invariant(res && res.data, "Data should be available");
|
2019-04-06 23:20:27 +00:00
|
|
|
document.updateFromJson(res.data);
|
2019-10-06 01:42:03 +00:00
|
|
|
this.addPolicies(res.policies);
|
2019-04-06 23:20:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const collection = this.getCollectionForDocument(document);
|
|
|
|
if (collection) collection.refresh();
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
2021-01-30 05:36:09 +00:00
|
|
|
restore = async (
|
|
|
|
document: Document,
|
|
|
|
options: { revisionId?: string, collectionId?: string } = {}
|
|
|
|
) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
const res = await client.post("/documents.restore", {
|
2018-12-05 06:24:30 +00:00
|
|
|
id: document.id,
|
2021-01-30 05:36:09 +00:00
|
|
|
revisionId: options.revisionId,
|
|
|
|
collectionId: options.collectionId,
|
2018-12-05 06:24:30 +00:00
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
runInAction("Document#restore", () => {
|
|
|
|
invariant(res && res.data, "Data should be available");
|
2018-12-05 06:24:30 +00:00
|
|
|
document.updateFromJson(res.data);
|
2019-10-06 01:42:03 +00:00
|
|
|
this.addPolicies(res.policies);
|
2018-12-05 06:24:30 +00:00
|
|
|
});
|
2019-04-06 23:20:27 +00:00
|
|
|
|
|
|
|
const collection = this.getCollectionForDocument(document);
|
2020-09-01 03:03:05 +00:00
|
|
|
if (collection) collection.refresh();
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
unpublish = async (document: Document) => {
|
|
|
|
const res = await client.post("/documents.unpublish", {
|
|
|
|
id: document.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
runInAction("Document#unpublish", () => {
|
|
|
|
invariant(res && res.data, "Data should be available");
|
|
|
|
document.updateFromJson(res.data);
|
|
|
|
this.addPolicies(res.policies);
|
|
|
|
});
|
|
|
|
|
|
|
|
const collection = this.getCollectionForDocument(document);
|
2019-04-06 23:20:27 +00:00
|
|
|
if (collection) collection.refresh();
|
2017-06-28 03:59:53 +00:00
|
|
|
};
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
pin = (document: Document) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return client.post("/documents.pin", { id: document.id });
|
2017-06-28 03:59:53 +00:00
|
|
|
};
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
unpin = (document: Document) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
return client.post("/documents.unpin", { id: document.id });
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
star = async (document: Document) => {
|
|
|
|
this.starredIds.set(document.id, true);
|
|
|
|
|
|
|
|
try {
|
2020-06-20 20:59:15 +00:00
|
|
|
return client.post("/documents.star", { id: document.id });
|
2019-04-18 02:11:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.starredIds.set(document.id, false);
|
|
|
|
}
|
2018-12-05 06:24:30 +00:00
|
|
|
};
|
2017-08-03 13:48:07 +00:00
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
unstar = (document: Document) => {
|
2019-04-18 02:11:23 +00:00
|
|
|
this.starredIds.set(document.id, false);
|
|
|
|
|
|
|
|
try {
|
2020-06-20 20:59:15 +00:00
|
|
|
return client.post("/documents.unstar", { id: document.id });
|
2019-04-18 02:11:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.starredIds.set(document.id, false);
|
|
|
|
}
|
2018-12-05 06:24:30 +00:00
|
|
|
};
|
2017-07-16 18:47:48 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
getByUrl = (url: string = ""): ?Document => {
|
2020-08-09 01:53:11 +00:00
|
|
|
return find(this.orderedData, (doc) => url.endsWith(doc.urlId));
|
2018-12-05 06:24:30 +00:00
|
|
|
};
|
2017-08-03 13:48:07 +00:00
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
getCollectionForDocument(document: Document) {
|
|
|
|
return this.rootStore.collections.data.get(document.collectionId);
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
}
|