1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/services/share/reducer.ts
Laurent 0765cf5955
All: Add support for sharing notebooks with Joplin Server (#4772)
- Joplin Server: Adds support for sharing a notebook
- Desktop: Adds support for sharing a notebook with Joplin Server
- Mobile: Adds support for reading and writing to a shared notebook (not possible to share a notebook)
- Cli: Adds support for reading and writing to a shared notebook (not possible to share a notebook)
2021-05-13 17:57:37 +01:00

93 lines
1.8 KiB
TypeScript

import { State as RootState } from '../../reducer';
import { Draft } from 'immer';
import { FolderEntity } from '../database/types';
interface StateShareUserUser {
id: string;
email: string;
full_name?: string;
}
export enum ShareUserStatus {
Waiting = 0,
Accepted = 1,
Rejected = 2,
}
export interface StateShareUser {
id: string;
status: ShareUserStatus;
user: StateShareUserUser;
}
export interface StateShare {
id: string;
type: number;
folder_id: string;
note_id: string;
user?: StateShareUserUser;
}
export interface ShareInvitation {
id: string;
share: StateShare;
status: ShareUserStatus;
}
export interface State {
shares: StateShare[];
shareUsers: Record<string, StateShareUser>;
shareInvitations: ShareInvitation[];
}
export const stateRootKey = 'shareService';
export const defaultState: State = {
shares: [],
shareUsers: {},
shareInvitations: [],
};
export function isSharedFolderOwner(state: RootState, folderId: string): boolean {
const userId = state.settings['sync.userId'];
const share = state[stateRootKey].shares.find(s => s.folder_id === folderId);
if (!share) return false;
return share.user.id === userId;
}
export function isRootSharedFolder(folder: FolderEntity): boolean {
return !!folder.share_id && !folder.parent_id;
}
const reducer = (draftRoot: Draft<RootState>, action: any) => {
if (action.type.indexOf('SHARE_') !== 0) return;
const draft = draftRoot.shareService;
try {
switch (action.type) {
case 'SHARE_SET':
draft.shares = action.shares;
break;
case 'SHARE_USER_SET':
draft.shareUsers[action.shareId] = action.shareUsers;
break;
case 'SHARE_INVITATION_SET':
draft.shareInvitations = action.shareInvitations;
break;
}
} catch (error) {
error.message = `In share reducer: ${error.message} Action: ${JSON.stringify(action)}`;
throw error;
}
};
export default reducer;