diff --git a/composer.json b/composer.json index f9642a3..e5d8f6e 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ } ], "require": { - "flarum/core": "^1.2.0" + "flarum/core": "^1.8.0" }, "authors": [ { @@ -44,11 +44,15 @@ "color": "#fff" }, "optional-dependencies": [ - "fof/byobu" + "fof/byobu", + "blomstra/gdpr" ] }, "flagrow": { "discuss": "https://discuss.flarum.org/d/20957" } + }, + "require-dev": { + "blomstra/gdpr": "@beta" } } diff --git a/extend.php b/extend.php index 6ac7434..e408339 100644 --- a/extend.php +++ b/extend.php @@ -11,6 +11,7 @@ namespace FoF\Drafts; +use Blomstra\Gdpr\Extend\UserData; use Flarum\Api\Serializer\CurrentUserSerializer; use Flarum\Api\Serializer\ForumSerializer; use Flarum\Extend; @@ -68,4 +69,10 @@ (new Extend\User()) ->registerPreference('draftAutosaveEnable', 'boolVal', false) ->registerPreference('draftAutosaveInterval', 'intVal', 6), + + (new Extend\Conditional()) + ->whenExtensionEnabled('blomstra-gdpr', [ + (new UserData()) + ->addType(Data\Drafts::class), + ]), ]; diff --git a/src/Data/Drafts.php b/src/Data/Drafts.php new file mode 100644 index 0000000..a3ec2dd --- /dev/null +++ b/src/Data/Drafts.php @@ -0,0 +1,55 @@ +where('user_id', $this->user->id) + ->each(function (Draft $draft) use ($zip) { + $zip->addFromString( + "draft-{$draft->id}.json", + json_encode( + $this->sanitize($draft), + JSON_PRETTY_PRINT + ) + ); + }); + } + + protected function sanitize(Draft $draft): array + { + return Arr::except($draft->toArray(), [ + 'user_id', 'relationships', 'scheduled_validation_error', 'extra', + ]); + } + + public function anonymize(): void + { + // In the case of drafts, it makes no sense to keep them after a user is anonymized. + $this->delete(); + } + + public function delete(): void + { + Draft::query() + ->where('user_id', $this->user->id) + ->delete(); + } +}