Skip to content

Commit

Permalink
feat: add setting to sync permissions of an entry on every sync
Browse files Browse the repository at this point in the history
Closes #69
  • Loading branch information
eXaminator committed Aug 23, 2021
1 parent 5e0a496 commit 7a6f805
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ level will be flattened to this 3rd level instead.
in foundry. With this setting the image will additionally be displayed in the journal entry next to its meta data and
text.
- **Automatically set permissions**: When importing an entity, set the best default permission based on whether the
entity is marked as private or public.
entity is marked as private or public. This can be set to never sync the permissions, only sync them on the initial
import or sync them for every import (and potentially override manually set default permissions).
- **Quest status icon**: Adds an icon in front of every Kanka quest in the journal sidebar to show whether it is
completed or not.
- **Show private entities**: If you disable this option, the module won't allow you sync entities that were marked as
Expand Down
2 changes: 2 additions & 0 deletions src/KankaFoundry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import KankaApi from './api/KankaApi';
import { logError, logInfo } from './logger';
import migrateV1 from './migrations/migrateV1';
import migrateV2 from './migrations/migrateV2';
import migrateV3 from './migrations/migrateV3';
import moduleConfig from './module.json';
import KankaFoundrySettings from './module/KankaFoundrySettings';
import KankaJournalHelper from './module/KankaJournalHelper';
Expand Down Expand Up @@ -48,6 +49,7 @@ export default class KankaFoundry {
await this.#renderLocalization.setLanguage(this.settings.importLanguage || this.game.i18n.lang);
migrateV1(this);
await migrateV2(this);
await migrateV3(this);

this.#isInitialized = true;
} catch (error) {
Expand Down
20 changes: 12 additions & 8 deletions src/lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,18 @@ KANKA:
hint: >
Soll die Hierarchie aus Kanka mit Ordnern reproduziert werden? Die maximale Tiefe beträgt
3 Verzeichnisse.
automaticPermissions:
label: Automatisch Berechtigungen setzen
hint: Alle Spieler erhalten automatisch Beobachter Rechte für neu importierte Einträge.
questStatusIcon:
label: Quest Status-Icon
hint: |
Zeigt ein Icon entsprechend des Quest-Status vor dem Namen von Quests in der Liste aller Einträge
an.
automaticPermissions:
label: Automatisch Berechtigungen setzen
hint: Alle Spieler erhalten automatisch Beobachter Rechte für neu importierte Einträge.
values:
never: Rechte nie setzen
initial: Rechte bei erstem Import setzen
always: Rechte bei jedem Import setzen
questStatusIcon:
label: Quest Status-Icon
hint: |
Zeigt ein Icon entsprechend des Quest-Status vor dem Namen von Quests in der Liste aller Einträge
an.
error:
ErrorInvalidAccessToken: Das Kanka Zugriffs-Token ist ungültig..
ErrorTokenExpired: Dein Kanka Zugriffs-Token ist abgelaufen. Bitte hinterlege ein neues.
Expand Down
4 changes: 4 additions & 0 deletions src/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ KANKA:
automaticPermissions:
label: Automatically set permissions
hint: All players will automatically get observer permissions for for newly imported entities.
values:
never: Never set permissions
initial: Set permissions on initial sync
always: Set permissions on every update
questStatusIcon:
label: Quest status icon
hint: |
Expand Down
3 changes: 1 addition & 2 deletions src/migrations/migrateV2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type KankaFoundry from '../KankaFoundry';

export default async function migrateV2(module: KankaFoundry): Promise<void> {
const g = game as Game;
const entries = g.journal?.filter(e => !!e.getFlag(module.name, 'id') && !e.getFlag('core', 'sheetClass')) ?? [];
const entries = module.game.journal?.filter(e => !!e.getFlag(module.name, 'id') && !e.getFlag('core', 'sheetClass')) ?? [];

await Promise.all(entries.map(entry => entry.setFlag('core', 'sheetClass', `${module.name}.KankaJournalApplication`)));
}
15 changes: 15 additions & 0 deletions src/migrations/migrateV3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type KankaFoundry from '../KankaFoundry';
import { AutomaticPermissionValue } from '../module/KankaFoundrySettings';
import { KankaSettings } from '../types/KankaSettings';

export default async function migrateV3(module: KankaFoundry): Promise<void> {
const permissionSetting = module.game.settings.get(module.name, KankaSettings.automaticPermissions) as string;

if (permissionSetting === 'false') {
module.game.settings.set(module.name, KankaSettings.automaticPermissions, AutomaticPermissionValue.never);
} else if (permissionSetting === 'true') {
module.game.settings.set(module.name, KankaSettings.automaticPermissions, AutomaticPermissionValue.initial);
} else if (!AutomaticPermissionValue[permissionSetting]) {
module.game.settings.set(module.name, KankaSettings.automaticPermissions, AutomaticPermissionValue.never);
}
}
31 changes: 21 additions & 10 deletions src/module/KankaFoundrySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import {

type KankaBrowserViews = 'list' | 'grid';

export enum AutomaticPermissionValue {
never = 'never',
initial = 'initial',
always = 'always'
}

export default class KankaFoundrySettings {
#module: KankaFoundry;

Expand Down Expand Up @@ -130,10 +136,10 @@ export default class KankaFoundrySettings {
);

this.register(
KankaSettings.automaticPermissions,
KankaSettings.questQuestStatusIcon,
{
name: this.#module.getMessage('settings.automaticPermissions.label'),
hint: this.#module.getMessage('settings.automaticPermissions.hint'),
name: this.#module.getMessage('settings.questStatusIcon.label'),
hint: this.#module.getMessage('settings.questStatusIcon.hint'),
scope: 'world',
config: true,
type: Boolean,
Expand All @@ -142,14 +148,19 @@ export default class KankaFoundrySettings {
);

this.register(
KankaSettings.questQuestStatusIcon,
KankaSettings.automaticPermissions,
{
name: this.#module.getMessage('settings.questStatusIcon.label'),
hint: this.#module.getMessage('settings.questStatusIcon.hint'),
name: this.#module.getMessage('settings.automaticPermissions.label'),
hint: this.#module.getMessage('settings.automaticPermissions.hint'),
scope: 'world',
config: true,
type: Boolean,
default: false,
type: String,
default: AutomaticPermissionValue.never,
choices: {
[AutomaticPermissionValue.never]: this.#module.getMessage('settings.automaticPermissions.values.never'),
[AutomaticPermissionValue.initial]: this.#module.getMessage('settings.automaticPermissions.values.initial'),
[AutomaticPermissionValue.always]: this.#module.getMessage('settings.automaticPermissions.values.always'),
},
},
);

Expand Down Expand Up @@ -235,8 +246,8 @@ export default class KankaFoundrySettings {
return this.getSetting<boolean>(KankaSettings.keepTreeStructure);
}

public get automaticPermissions(): boolean {
return this.getSetting<boolean>(KankaSettings.automaticPermissions);
public get automaticPermissions(): AutomaticPermissionValue {
return this.getSetting<AutomaticPermissionValue>(KankaSettings.automaticPermissions);
}

public get questStatusIcon(): boolean {
Expand Down
23 changes: 16 additions & 7 deletions src/module/KankaJournalHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { KankaApiChildEntity, KankaApiEntity, KankaApiEntityId, KankaApiEntityTy
import { ProgressFn } from '../types/progress';
import Reference from '../types/Reference';
import createTypeLoaders from './createTypeLoaders';
import { AutomaticPermissionValue } from './KankaFoundrySettings';
import ReferenceCollection from './ReferenceCollection';
import AbstractTypeLoader from './TypeLoaders/AbstractTypeLoader';

Expand Down Expand Up @@ -154,6 +155,7 @@ export default class KankaJournalHelper {
await entry.update({
[`flags.${this.module.name}.snapshot`]: null,
[`flags.${this.module.name}.references`]: null,
permission: { default: this.getExpectedPermission(entity, true) },
});
await entry.update(journalData);
} else {
Expand All @@ -162,15 +164,9 @@ export default class KankaJournalHelper {
.filter((ref): ref is Reference => !!ref) ?? [];
const folder = await this.ensureFolderPath(type, path);

let defaultPermissions: foundry.CONST.EntityPermission | undefined;

if (this.module.settings.automaticPermissions && !entity.is_private) {
defaultPermissions = CONST.ENTITY_PERMISSIONS.OBSERVER;
}

entry = await JournalEntry.create({
...journalData,
permission: { default: defaultPermissions },
permission: { default: this.getExpectedPermission(entity, false) },
folder: folder?.id,
}) as JournalEntry;
}
Expand All @@ -180,6 +176,19 @@ export default class KankaJournalHelper {
return entry;
}

protected getExpectedPermission(
entity: KankaApiChildEntity,
isUpdate: boolean,
): foundry.CONST.EntityPermission | undefined {
const setting = this.module.settings.automaticPermissions;

if (setting === AutomaticPermissionValue.never) return undefined;
if (setting === AutomaticPermissionValue.initial && isUpdate) return undefined;

if (entity.is_private) return CONST.ENTITY_PERMISSIONS.NONE;
return CONST.ENTITY_PERMISSIONS.OBSERVER;
}

protected async createFolder(
name: string,
parent: Folder | undefined,
Expand Down

0 comments on commit 7a6f805

Please sign in to comment.