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 dac3f7e..316fe1f 100644 --- a/extend.php +++ b/extend.php @@ -66,4 +66,13 @@ (new Extend\Console()) ->command(Console\LookupUnknownIPsCommand::class), + + (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/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..4b88191 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -43,3 +43,12 @@ 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 + +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.