From 28145c26a43f8cd99bb00e4ed371b9a39363ea77 Mon Sep 17 00:00:00 2001 From: IanM <16573496+imorland@users.noreply.github.com> Date: Mon, 14 Oct 2024 17:37:18 +0100 Subject: [PATCH] improve country flag display feature (#46) * improve country flag display feature * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot --- extend.php | 9 +++--- js/src/@shims/shims.d.ts | 14 +++++++++ js/src/admin/index.js | 13 +++++++- js/src/forum/extend.ts | 3 +- js/src/forum/extenders/extendCommentPost.tsx | 2 +- resources/locale/en.yml | 4 ++- src/Api/AttachRelation.php | 22 +++++++++++-- src/Api/BasicUserAttributes.php | 33 ++++++++++++++++++++ src/Api/CurrentUserAttributes.php | 27 ++++++++++++++++ 9 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 js/src/@shims/shims.d.ts create mode 100644 src/Api/BasicUserAttributes.php create mode 100644 src/Api/CurrentUserAttributes.php diff --git a/extend.php b/extend.php index 61e60b9..48564dc 100644 --- a/extend.php +++ b/extend.php @@ -13,12 +13,12 @@ use Flarum\Api\Controller; use Flarum\Api\Serializer\BasicUserSerializer; +use Flarum\Api\Serializer\CurrentUserSerializer; use Flarum\Api\Serializer\PostSerializer; use Flarum\Extend; use Flarum\Frontend\Document; use Flarum\Post\Post; use Flarum\Settings\Event\Saving; -use Flarum\User\User; use FoF\GeoIP\Api\GeoIP; return [ @@ -74,9 +74,10 @@ ->registerPreference('showIPCountry', 'boolval', false), (new Extend\ApiSerializer(BasicUserSerializer::class)) - ->attribute('showIPCountry', function (BasicUserSerializer $serializer, User $user) { - return (bool) $user->getPreference('showIPCountry'); - }), + ->attributes(Api\BasicUserAttributes::class), + + (new Extend\ApiSerializer(CurrentUserSerializer::class)) + ->attributes(Api\CurrentUserAttributes::class), (new Extend\Conditional()) ->whenExtensionEnabled('fof-default-user-preferences', fn () => [ diff --git a/js/src/@shims/shims.d.ts b/js/src/@shims/shims.d.ts new file mode 100644 index 0000000..b548593 --- /dev/null +++ b/js/src/@shims/shims.d.ts @@ -0,0 +1,14 @@ +import IPInfo from '../forum/models/IPInfo'; + +declare module 'flarum/common/models/Post' { + export default interface Post { + ip_info: () => IPInfo; + } +} + +declare module 'flarum/common/models/User' { + export default interface User { + showIPCountry: () => boolean; + canSeeCountry: () => boolean; + } +} diff --git a/js/src/admin/index.js b/js/src/admin/index.js index e7c3f17..5279d24 100644 --- a/js/src/admin/index.js +++ b/js/src/admin/index.js @@ -2,5 +2,16 @@ import app from 'flarum/admin/app'; import GeoipSettingsPage from './components/ExtensionSettingsPage'; app.initializers.add('fof/geoip', () => { - app.extensionData.for('fof-geoip').registerPage(GeoipSettingsPage); + app.extensionData + .for('fof-geoip') + .registerPage(GeoipSettingsPage) + .registerPermission( + { + icon: 'fas fa-globe', + permission: 'fof-geoip.canSeeCountry', + label: app.translator.trans('fof-geoip.admin.permissions.see_country'), + }, + 'moderate', + 50 + ); }); diff --git a/js/src/forum/extend.ts b/js/src/forum/extend.ts index 7d72f36..624427e 100644 --- a/js/src/forum/extend.ts +++ b/js/src/forum/extend.ts @@ -11,5 +11,6 @@ export default [ .hasOne('ip_info'), new Extend.Model(User) // - .attribute('showIPCountry'), + .attribute('showIPCountry') + .attribute('canSeeCountry'), ]; diff --git a/js/src/forum/extenders/extendCommentPost.tsx b/js/src/forum/extenders/extendCommentPost.tsx index 173ef15..853009a 100644 --- a/js/src/forum/extenders/extendCommentPost.tsx +++ b/js/src/forum/extenders/extendCommentPost.tsx @@ -10,7 +10,7 @@ export default function extendCommentPost() { if (app.forum.attribute('fof-geoip.showFlag')) { const ipInfo = this.attrs.post.ip_info?.(); const postUser = this.attrs.post.user(); - if (postUser && postUser.showIPCountry() && ipInfo) { + if ((ipInfo && postUser && postUser.showIPCountry()) || app.session.user?.canSeeCountry?.()) { const { image } = getIPData(ipInfo); if (image) { items.add('country', image, 100); diff --git a/resources/locale/en.yml b/resources/locale/en.yml index 4b88191..76fa433 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -1,5 +1,7 @@ fof-geoip: admin: + permissions: + see_country: Always display the country of the IP address settings: title: FriendsOfFlarum GeoIP @@ -24,7 +26,7 @@ fof-geoip: quota_label: Lookup quota show_flag_label: Show country flag for each post - show_flag_help: Without disclosing the IP address, show the country flag of the IP address of the post author. + show_flag_help: Without disclosing the IP address, show the country flag of the IP address of the post author, when the user opts in via their preferences. forum: alerts: diff --git a/src/Api/AttachRelation.php b/src/Api/AttachRelation.php index ba7e271..b8d50e0 100644 --- a/src/Api/AttachRelation.php +++ b/src/Api/AttachRelation.php @@ -13,16 +13,34 @@ use Flarum\Api\Serializer\PostSerializer; use Flarum\Post\Post; +use Flarum\Settings\SettingsRepositoryInterface; use FoF\GeoIP\Api\Serializer\BasicIPInfoSerializer; use FoF\GeoIP\Api\Serializer\IPInfoSerializer; use Tobscure\JsonApi\Relationship; class AttachRelation { - public function __invoke(PostSerializer $serializer, Post $post): Relationship + public function __construct( + protected SettingsRepositoryInterface $settings + ) { + } + + public function __invoke(PostSerializer $serializer, Post $post): ?Relationship { $viewIPs = $serializer->getActor()->can('viewIps', $post); - return $serializer->hasOne($post, $viewIPs ? IPInfoSerializer::class : BasicIPInfoSerializer::class, 'ip_info'); + if ($viewIPs) { + return $serializer->hasOne($post, IPInfoSerializer::class, 'ip_info'); + } + + $viewCountry = $serializer->getActor()->can('fof-geoip.canSeeCountry'); + $showFlagsFeatureEnabled = $this->settings->get('fof-geoip.showFlag'); + $userPreference = $post->user->getPreference('showIPCountry'); + + if ($viewCountry || ($showFlagsFeatureEnabled && $userPreference)) { + return $serializer->hasOne($post, BasicIPInfoSerializer::class, 'ip_info'); + } + + return null; } } diff --git a/src/Api/BasicUserAttributes.php b/src/Api/BasicUserAttributes.php new file mode 100644 index 0000000..3ed36e1 --- /dev/null +++ b/src/Api/BasicUserAttributes.php @@ -0,0 +1,33 @@ +settings->get('fof-geoip.showFlag')) { + $attributes['showIPCountry'] = (bool) $user->getPreference('showIPCountry'); + } + + return $attributes; + } +} diff --git a/src/Api/CurrentUserAttributes.php b/src/Api/CurrentUserAttributes.php new file mode 100644 index 0000000..506fba9 --- /dev/null +++ b/src/Api/CurrentUserAttributes.php @@ -0,0 +1,27 @@ +getActor()->can('fof-geoip.canSeeCountry')) { + $attributes['canSeeCountry'] = true; + } + + return $attributes; + } +}