Individual document sharing with permissions (#5814)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Tom Moor <tom@getoutline.com>
This commit is contained in:
@@ -16,7 +16,7 @@ export default class MembershipsStore extends Store<Membership> {
|
||||
|
||||
@action
|
||||
fetchPage = async (
|
||||
params: PaginationParams | undefined
|
||||
params: (PaginationParams & { id?: string }) | undefined
|
||||
): Promise<Membership[]> => {
|
||||
this.isFetching = true;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import SharesStore from "./SharesStore";
|
||||
import StarsStore from "./StarsStore";
|
||||
import SubscriptionsStore from "./SubscriptionsStore";
|
||||
import UiStore from "./UiStore";
|
||||
import UserMembershipsStore from "./UserMembershipsStore";
|
||||
import UsersStore from "./UsersStore";
|
||||
import ViewsStore from "./ViewsStore";
|
||||
import WebhookSubscriptionsStore from "./WebhookSubscriptionStore";
|
||||
@@ -58,6 +59,7 @@ export default class RootStore {
|
||||
views: ViewsStore;
|
||||
fileOperations: FileOperationsStore;
|
||||
webhookSubscriptions: WebhookSubscriptionsStore;
|
||||
userMemberships: UserMembershipsStore;
|
||||
|
||||
constructor() {
|
||||
// Models
|
||||
@@ -84,6 +86,7 @@ export default class RootStore {
|
||||
this.registerStore(ViewsStore);
|
||||
this.registerStore(FileOperationsStore);
|
||||
this.registerStore(WebhookSubscriptionsStore);
|
||||
this.registerStore(UserMembershipsStore);
|
||||
|
||||
// Non-models
|
||||
this.registerStore(DocumentPresenceStore, "presence");
|
||||
|
||||
@@ -21,14 +21,13 @@ export default class StarsStore extends Store<Star> {
|
||||
const res = await client.post(`/stars.list`, params);
|
||||
invariant(res?.data, "Data not available");
|
||||
|
||||
let models: Star[] = [];
|
||||
runInAction(`StarsStore#fetchPage`, () => {
|
||||
return runInAction(`StarsStore#fetchPage`, () => {
|
||||
res.data.documents.forEach(this.rootStore.documents.add);
|
||||
models = res.data.stars.map(this.add);
|
||||
const models = res.data.stars.map(this.add);
|
||||
this.addPolicies(res.policies);
|
||||
this.isLoaded = true;
|
||||
return models;
|
||||
});
|
||||
return models;
|
||||
} finally {
|
||||
this.isFetching = false;
|
||||
}
|
||||
|
||||
104
app/stores/UserMembershipsStore.ts
Normal file
104
app/stores/UserMembershipsStore.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import invariant from "invariant";
|
||||
import { action, runInAction, computed } from "mobx";
|
||||
import UserMembership from "~/models/UserMembership";
|
||||
import { PaginationParams } from "~/types";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import RootStore from "./RootStore";
|
||||
import Store, { PAGINATION_SYMBOL, RPCAction } from "./base/Store";
|
||||
|
||||
export default class UserMembershipsStore extends Store<UserMembership> {
|
||||
actions = [
|
||||
RPCAction.List,
|
||||
RPCAction.Create,
|
||||
RPCAction.Delete,
|
||||
RPCAction.Update,
|
||||
];
|
||||
|
||||
constructor(rootStore: RootStore) {
|
||||
super(rootStore, UserMembership);
|
||||
}
|
||||
|
||||
@action
|
||||
fetchPage = async (
|
||||
params?: PaginationParams | undefined
|
||||
): Promise<UserMembership[]> => {
|
||||
this.isFetching = true;
|
||||
|
||||
try {
|
||||
const res = await client.post(`/userMemberships.list`, params);
|
||||
invariant(res?.data, "Data not available");
|
||||
|
||||
return runInAction(`UserMembershipsStore#fetchPage`, () => {
|
||||
res.data.documents.forEach(this.rootStore.documents.add);
|
||||
this.addPolicies(res.policies);
|
||||
this.isLoaded = true;
|
||||
return res.data.memberships.map(this.add);
|
||||
});
|
||||
} finally {
|
||||
this.isFetching = false;
|
||||
}
|
||||
};
|
||||
|
||||
@action
|
||||
fetchDocumentMemberships = async (
|
||||
params: (PaginationParams & { id: string }) | undefined
|
||||
): Promise<UserMembership[]> => {
|
||||
this.isFetching = true;
|
||||
|
||||
try {
|
||||
const res = await client.post(`/documents.memberships`, params);
|
||||
invariant(res?.data, "Data not available");
|
||||
|
||||
return runInAction(`MembershipsStore#fetchDocmentMemberships`, () => {
|
||||
res.data.users.forEach(this.rootStore.users.add);
|
||||
|
||||
const response = res.data.memberships.map(this.add);
|
||||
this.isLoaded = true;
|
||||
|
||||
response[PAGINATION_SYMBOL] = res.pagination;
|
||||
return response;
|
||||
});
|
||||
} finally {
|
||||
this.isFetching = false;
|
||||
}
|
||||
};
|
||||
|
||||
@action
|
||||
async create({ documentId, userId, permission }: Partial<UserMembership>) {
|
||||
const res = await client.post("/documents.add_user", {
|
||||
id: documentId,
|
||||
userId,
|
||||
permission,
|
||||
});
|
||||
|
||||
return runInAction(`UserMembershipsStore#create`, () => {
|
||||
invariant(res?.data, "Membership data should be available");
|
||||
res.data.users.forEach(this.rootStore.users.add);
|
||||
|
||||
const memberships = res.data.memberships.map(this.add);
|
||||
return memberships[0];
|
||||
});
|
||||
}
|
||||
|
||||
@action
|
||||
async delete({ documentId, userId }: UserMembership) {
|
||||
await client.post("/documents.remove_user", {
|
||||
id: documentId,
|
||||
userId,
|
||||
});
|
||||
this.removeAll({ userId, documentId });
|
||||
}
|
||||
|
||||
@computed
|
||||
get orderedData(): UserMembership[] {
|
||||
const memberships = Array.from(this.data.values());
|
||||
|
||||
return memberships.sort((a, b) => {
|
||||
if (a.index === b.index) {
|
||||
return a.updatedAt > b.updatedAt ? -1 : 1;
|
||||
}
|
||||
|
||||
return a.index < b.index ? -1 : 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import invariant from "invariant";
|
||||
import differenceWith from "lodash/differenceWith";
|
||||
import filter from "lodash/filter";
|
||||
import orderBy from "lodash/orderBy";
|
||||
import { observable, computed, action, runInAction } from "mobx";
|
||||
@@ -249,6 +250,18 @@ export default class UsersStore extends Store<User> {
|
||||
}
|
||||
};
|
||||
|
||||
notInDocument = (documentId: string, query = "") => {
|
||||
const document = this.rootStore.documents.get(documentId);
|
||||
const teamMembers = this.activeOrInvited;
|
||||
const documentMembers = document?.members ?? [];
|
||||
const users = differenceWith(
|
||||
teamMembers,
|
||||
documentMembers,
|
||||
(teamMember, documentMember) => teamMember.id === documentMember.id
|
||||
);
|
||||
return queriedUsers(users, query);
|
||||
};
|
||||
|
||||
notInCollection = (collectionId: string, query = "") => {
|
||||
const memberships = filter(
|
||||
this.rootStore.memberships.orderedData,
|
||||
|
||||
Reference in New Issue
Block a user