Skip to content

Commit

Permalink
feat: add support for blomstra/gdpr (#88)
Browse files Browse the repository at this point in the history
* feat: add support for blomstra/gdpr

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Oct 10, 2023
1 parent 75c7cf2 commit 847fac2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
],
"require": {
"flarum/core": "^1.2.0"
"flarum/core": "^1.8.0"
},
"authors": [
{
Expand All @@ -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"
}
}
7 changes: 7 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
]),
];
55 changes: 55 additions & 0 deletions src/Data/Drafts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of fof/drafts.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Drafts\Data;

use Blomstra\Gdpr\Data\Type;
use FoF\Drafts\Draft;
use Illuminate\Support\Arr;
use PhpZip\ZipFile;

class Drafts extends Type
{
public function export(ZipFile $zip): void
{
Draft::query()
->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();
}
}

0 comments on commit 847fac2

Please sign in to comment.