1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/lib/utils/userFetcher.ts

42 lines
1.1 KiB
TypeScript

import SyncTargetRegistry from '../SyncTargetRegistry';
import eventManager from '../eventManager';
import Setting from '../models/Setting';
import { reg } from '../registry';
import Logger from '../Logger';
const logger = Logger.create('userFetcher');
interface UserApiResponse {
inbox?: {
jop_id: string;
};
inbox_email?: string;
can_use_share_permissions?: number;
}
const userFetcher = async () => {
if (Setting.value('sync.target') !== SyncTargetRegistry.nameToId('joplinCloud')) {
return;
}
const syncTarget = reg.syncTarget();
const fileApi = await syncTarget.fileApi();
const api = fileApi.driver().api();
const owner: UserApiResponse = await api.exec('GET', `api/users/${api.userId}`);
logger.info('Got user:', owner);
Setting.setValue('sync.10.inboxId', owner.inbox ? owner.inbox.jop_id : '');
Setting.setValue('sync.10.inboxEmail', owner.inbox_email ? owner.inbox_email : '');
Setting.setValue('sync.10.canUseSharePermissions', !!owner.can_use_share_permissions);
};
// Listen to the event only once
export const initializeUserFetcher = () => {
eventManager.once('sessionEstablished', userFetcher);
};
export default userFetcher;