From d5de2b1ef1db8513f264e91e7bff227c6e312d82 Mon Sep 17 00:00:00 2001 From: IanM Date: Mon, 9 Sep 2024 10:33:19 +0100 Subject: [PATCH 1/4] feat: allow users to decide to show or hide flag --- extend.php | 3 ++ js/src/forum/extenders/extendCommentPost.tsx | 3 +- .../forum/extenders/extendUserPreferences.tsx | 29 +++++++++++++++++++ js/src/forum/index.ts | 2 ++ resources/locale/en.yml | 3 ++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 js/src/forum/extenders/extendUserPreferences.tsx diff --git a/extend.php b/extend.php index dac3f7e..f190077 100644 --- a/extend.php +++ b/extend.php @@ -66,4 +66,7 @@ (new Extend\Console()) ->command(Console\LookupUnknownIPsCommand::class), + + (new Extend\User()) + ->registerPreference('showIPCountry', 'boolval', false), ]; diff --git a/js/src/forum/extenders/extendCommentPost.tsx b/js/src/forum/extenders/extendCommentPost.tsx index c6c70db..c7cc9cc 100644 --- a/js/src/forum/extenders/extendCommentPost.tsx +++ b/js/src/forum/extenders/extendCommentPost.tsx @@ -9,7 +9,8 @@ export default function extendCommentPost() { extend(CommentPost.prototype, 'headerItems', function (items: ItemList) { if (app.forum.attribute('fof-geoip.showFlag')) { const ipInfo = this.attrs.post.ip_info?.(); - if (ipInfo) { + const postUser = this.attrs.post.user(); + if (postUser && postUser.preferences().showIPCountry && ipInfo) { const { image } = getIPData(ipInfo); if (image) { items.add('country', image, 100); diff --git a/js/src/forum/extenders/extendUserPreferences.tsx b/js/src/forum/extenders/extendUserPreferences.tsx new file mode 100644 index 0000000..ad2d099 --- /dev/null +++ b/js/src/forum/extenders/extendUserPreferences.tsx @@ -0,0 +1,29 @@ +import app from 'flarum/forum/app'; +import { extend } from 'flarum/common/extend'; +import SettingsPage from 'flarum/forum/components/SettingsPage'; +import Switch from 'flarum/common/components/Switch'; + +export default function extendUserPreferences() { + extend(SettingsPage.prototype, 'privacyItems', function (items) { + if (app.forum.attribute('fof-geoip.showFlag')) { + items.add( + 'ip-country', + Switch.component( + { + state: this.user.preferences().showIPCountry, + onchange: (value) => { + this.showIPCountryLoading = true; + + this.user.savePreferences({ showIPCountry: value }).then(() => { + this.showIPCountryLoading = false; + m.redraw(); + }); + }, + loading: this.showIPCountryLoading, + }, + app.translator.trans('fof-geoip.forum.user.settings.ip_country') + ) + ); + } + }); +} diff --git a/js/src/forum/index.ts b/js/src/forum/index.ts index 4bdadfe..1bf0ee7 100644 --- a/js/src/forum/index.ts +++ b/js/src/forum/index.ts @@ -3,6 +3,7 @@ import extendPostMeta from './extenders/extendPostMeta'; import extendBanIPModal from './extenders/extendBanIPModal'; import extendAccessTokensList from './extenders/extendAccessTokensList'; import extendCommentPost from './extenders/extendCommentPost'; +import extendUserPreferences from './extenders/extendUserPreferences'; export { default as extend } from './extend'; @@ -11,4 +12,5 @@ app.initializers.add('fof/geoip', () => { extendBanIPModal(); extendAccessTokensList(); extendCommentPost(); + extendUserPreferences(); }); diff --git a/resources/locale/en.yml b/resources/locale/en.yml index fcc01f1..6ea32a8 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -43,3 +43,6 @@ fof-geoip: threat_types: "Threat Types" error: "Error" not_enough_data: "Not enough data to draw a map" + user: + settings: + ip_country: Show the flag of the country I post from, based on my IP address From 646b70c38677863aa4bbcb3302c1237a8b1e4cf1 Mon Sep 17 00:00:00 2001 From: IanM Date: Mon, 9 Sep 2024 10:45:13 +0100 Subject: [PATCH 2/4] feat: add support for fof/default-user-preferences --- composer.json | 3 ++- extend.php | 6 ++++++ resources/locale/en.yml | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index eff8e29..ec2480e 100644 --- a/composer.json +++ b/composer.json @@ -64,7 +64,8 @@ }, "require-dev": { "flarum/phpstan": "*", - "fof/drafts": "*" + "fof/drafts": "*", + "fof/default-user-preferences": "*" }, "scripts": { "analyse:phpstan": "phpstan analyse", diff --git a/extend.php b/extend.php index f190077..cd1ced4 100644 --- a/extend.php +++ b/extend.php @@ -69,4 +69,10 @@ (new Extend\User()) ->registerPreference('showIPCountry', 'boolval', false), + + (new Extend\Conditional()) + ->whenExtensionEnabled('fof-default-user-preferences', fn() => [ + (new \FoF\DefaultUserPreferences\Extend\RegisterUserPreferenceDefault()) + ->default('showIPCountry', false, 'bool'), + ]), ]; diff --git a/resources/locale/en.yml b/resources/locale/en.yml index 6ea32a8..52af7c8 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -46,3 +46,9 @@ fof-geoip: user: settings: ip_country: Show the flag of the country I post from, based on my IP address + +fof-default-user-preferences: + admin: + settings: + showIPCountry: Show the flag of the country the user posts from + showIPCountry-help: This is based on their IP address at the time of posting. The country flag (if enabled) will be visible to all users. Admin users and moderators will see the IP address in the tooltip. \ No newline at end of file From b574c9201ced73c315fe66a02ae42199132a6222 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 9 Sep 2024 09:45:29 +0000 Subject: [PATCH 3/4] Apply fixes from StyleCI --- extend.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extend.php b/extend.php index cd1ced4..316fe1f 100644 --- a/extend.php +++ b/extend.php @@ -71,7 +71,7 @@ ->registerPreference('showIPCountry', 'boolval', false), (new Extend\Conditional()) - ->whenExtensionEnabled('fof-default-user-preferences', fn() => [ + ->whenExtensionEnabled('fof-default-user-preferences', fn () => [ (new \FoF\DefaultUserPreferences\Extend\RegisterUserPreferenceDefault()) ->default('showIPCountry', false, 'bool'), ]), From 7a8b8fe92e865c5026f0d2df0a6fca02a5a4bdce Mon Sep 17 00:00:00 2001 From: IanM Date: Mon, 9 Sep 2024 10:46:22 +0100 Subject: [PATCH 4/4] chore: newline --- resources/locale/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/locale/en.yml b/resources/locale/en.yml index 52af7c8..4b88191 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -51,4 +51,4 @@ fof-default-user-preferences: admin: settings: showIPCountry: Show the flag of the country the user posts from - showIPCountry-help: This is based on their IP address at the time of posting. The country flag (if enabled) will be visible to all users. Admin users and moderators will see the IP address in the tooltip. \ No newline at end of file + showIPCountry-help: This is based on their IP address at the time of posting. The country flag (if enabled) will be visible to all users. Admin users and moderators will see the IP address in the tooltip.