Skip to content

Commit

Permalink
feat: allow users to decide to show or hide flag (#43)
Browse files Browse the repository at this point in the history
* feat: allow users to decide to show or hide flag

* feat: add support for fof/default-user-preferences

* Apply fixes from StyleCI

* chore: newline

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Sep 9, 2024
1 parent 38f3a0c commit 11c6b4f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
},
"require-dev": {
"flarum/phpstan": "*",
"fof/drafts": "*"
"fof/drafts": "*",
"fof/default-user-preferences": "*"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
Expand Down
9 changes: 9 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]),
];
3 changes: 2 additions & 1 deletion js/src/forum/extenders/extendCommentPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default function extendCommentPost() {
extend(CommentPost.prototype, 'headerItems', function (items: ItemList<Mithril.Children>) {
if (app.forum.attribute<boolean>('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);
Expand Down
29 changes: 29 additions & 0 deletions js/src/forum/extenders/extendUserPreferences.tsx
Original file line number Diff line number Diff line change
@@ -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<boolean>('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')
)
);
}
});
}
2 changes: 2 additions & 0 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -11,4 +12,5 @@ app.initializers.add('fof/geoip', () => {
extendBanIPModal();
extendAccessTokensList();
extendCommentPost();
extendUserPreferences();
});
9 changes: 9 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 11c6b4f

Please sign in to comment.