Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow users to decide to show or hide flag #43

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading