Skip to content

Commit

Permalink
feat: support gdpr export, erasure (#69)
Browse files Browse the repository at this point in the history
* feat: support gdpr export, erasure

* Apply fixes from StyleCI

* chore: set php versions

* newline

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Oct 31, 2023
1 parent 12db387 commit a4b36e0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ jobs:
with:
enable_backend_testing: false
enable_phpstan: true

php_versions: '["8.0", "8.1", "8.2"]'
backend_directory: .
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"optional-dependencies": [
"flarum/subscriptions",
"flarum/approval",
"flarum/mentions"
"flarum/mentions",
"blomstra/gdpr"
]
},
"flagrow": {
Expand All @@ -68,7 +69,8 @@
"flarum/approval": "*",
"flarum/subscriptions": "*",
"flarum/phpstan": "*",
"flarum/mentions": "*"
"flarum/mentions": "*",
"blomstra/gdpr": "@beta"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
Expand Down
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\FollowTags;

use Blomstra\Gdpr\Extend\UserData;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Event as Discussion;
use Flarum\Discussion\Filter\DiscussionFilterer;
Expand Down Expand Up @@ -66,4 +67,10 @@
->type(Notifications\NewPostBlueprint::class, DiscussionSerializer::class, ['alert', 'email'])
->type(Notifications\NewDiscussionTagBlueprint::class, DiscussionSerializer::class, ['alert', 'email'])
->beforeSending(Listeners\PreventMentionNotificationsFromIgnoredTags::class),

(new Extend\Conditional())
->whenExtensionEnabled('blomstra-gdpr', fn () => [
(new UserData())
->addType(Data\TagSubscription::class),
]),
];
7 changes: 7 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ fof-follow-tags:
filtering_options:
none: Followed Discussions
tags: Followed Tags

blomstra-gdpr:
lib:
data:
tagsubscription:
export_description: Exports details of the subscription level for a tag, if one exists.
anonymize_description: Removes the subscription of the user to the tag.
66 changes: 66 additions & 0 deletions src/Data/TagSubscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

namespace FoF\FollowTags\Data;

use Blomstra\Gdpr\Data\Type;
use Flarum\Tags\Tag;
use Flarum\Tags\TagState;
use Illuminate\Support\Arr;

class TagSubscription extends Type
{
public function export(): ?array
{
$exportData = [];

Tag::query()
->each(function (Tag $tag) use (&$exportData) {
$state = $tag->stateFor($this->user);
$sanitized = $this->sanitize($state);

// if the sanitized data has more than simply the `tag_id` key, we'll export it
if (count($sanitized) > 1) {
$exportData[] = ["FollowTagSubscription/tag-{$tag->id}.json" => $this->encodeForExport($sanitized)];
}
});

return $exportData;
}

public function sanitize(TagState $state): array
{
return Arr::except($state->toArray(), ['user_id', 'marked_as_read_at', 'is_hidden']);
}

public function anonymize(): void
{
Tag::query()
->each(function (Tag $tag) {
$state = $tag->stateFor($this->user);

if ($state->exists) {
$state->subscription = null;
$state->save();
}
});
}

public static function deleteDescription(): string
{
return self::anonymizeDescription();
}

public function delete(): void
{
$this->anonymize();
}
}

0 comments on commit a4b36e0

Please sign in to comment.