Skip to content

Commit

Permalink
feat: allow users to decide to show or hide flag
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 9, 2024
1 parent 38f3a0c commit d5de2b1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@

(new Extend\Console())
->command(Console\LookupUnknownIPsCommand::class),

(new Extend\User())
->registerPreference('showIPCountry', 'boolval', false),
];
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();
});
3 changes: 3 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d5de2b1

Please sign in to comment.