Skip to content

Commit 28145c2

Browse files
imorlandStyleCIBot
andauthored
improve country flag display feature (#46)
* improve country flag display feature * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent aa8c44a commit 28145c2

File tree

9 files changed

+117
-10
lines changed

9 files changed

+117
-10
lines changed

extend.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
use Flarum\Api\Controller;
1515
use Flarum\Api\Serializer\BasicUserSerializer;
16+
use Flarum\Api\Serializer\CurrentUserSerializer;
1617
use Flarum\Api\Serializer\PostSerializer;
1718
use Flarum\Extend;
1819
use Flarum\Frontend\Document;
1920
use Flarum\Post\Post;
2021
use Flarum\Settings\Event\Saving;
21-
use Flarum\User\User;
2222
use FoF\GeoIP\Api\GeoIP;
2323

2424
return [
@@ -74,9 +74,10 @@
7474
->registerPreference('showIPCountry', 'boolval', false),
7575

7676
(new Extend\ApiSerializer(BasicUserSerializer::class))
77-
->attribute('showIPCountry', function (BasicUserSerializer $serializer, User $user) {
78-
return (bool) $user->getPreference('showIPCountry');
79-
}),
77+
->attributes(Api\BasicUserAttributes::class),
78+
79+
(new Extend\ApiSerializer(CurrentUserSerializer::class))
80+
->attributes(Api\CurrentUserAttributes::class),
8081

8182
(new Extend\Conditional())
8283
->whenExtensionEnabled('fof-default-user-preferences', fn () => [

js/src/@shims/shims.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import IPInfo from '../forum/models/IPInfo';
2+
3+
declare module 'flarum/common/models/Post' {
4+
export default interface Post {
5+
ip_info: () => IPInfo;
6+
}
7+
}
8+
9+
declare module 'flarum/common/models/User' {
10+
export default interface User {
11+
showIPCountry: () => boolean;
12+
canSeeCountry: () => boolean;
13+
}
14+
}

js/src/admin/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@ import app from 'flarum/admin/app';
22
import GeoipSettingsPage from './components/ExtensionSettingsPage';
33

44
app.initializers.add('fof/geoip', () => {
5-
app.extensionData.for('fof-geoip').registerPage(GeoipSettingsPage);
5+
app.extensionData
6+
.for('fof-geoip')
7+
.registerPage(GeoipSettingsPage)
8+
.registerPermission(
9+
{
10+
icon: 'fas fa-globe',
11+
permission: 'fof-geoip.canSeeCountry',
12+
label: app.translator.trans('fof-geoip.admin.permissions.see_country'),
13+
},
14+
'moderate',
15+
50
16+
);
617
});

js/src/forum/extend.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export default [
1111
.hasOne('ip_info'),
1212

1313
new Extend.Model(User) //
14-
.attribute('showIPCountry'),
14+
.attribute('showIPCountry')
15+
.attribute('canSeeCountry'),
1516
];

js/src/forum/extenders/extendCommentPost.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function extendCommentPost() {
1010
if (app.forum.attribute<boolean>('fof-geoip.showFlag')) {
1111
const ipInfo = this.attrs.post.ip_info?.();
1212
const postUser = this.attrs.post.user();
13-
if (postUser && postUser.showIPCountry() && ipInfo) {
13+
if ((ipInfo && postUser && postUser.showIPCountry()) || app.session.user?.canSeeCountry?.()) {
1414
const { image } = getIPData(ipInfo);
1515
if (image) {
1616
items.add('country', image, 100);

resources/locale/en.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
fof-geoip:
22
admin:
3+
permissions:
4+
see_country: Always display the country of the IP address
35
settings:
46
title: FriendsOfFlarum GeoIP
57

@@ -24,7 +26,7 @@ fof-geoip:
2426
quota_label: Lookup quota
2527

2628
show_flag_label: Show country flag for each post
27-
show_flag_help: Without disclosing the IP address, show the country flag of the IP address of the post author.
29+
show_flag_help: Without disclosing the IP address, show the country flag of the IP address of the post author, when the user opts in via their preferences.
2830

2931
forum:
3032
alerts:

src/Api/AttachRelation.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,34 @@
1313

1414
use Flarum\Api\Serializer\PostSerializer;
1515
use Flarum\Post\Post;
16+
use Flarum\Settings\SettingsRepositoryInterface;
1617
use FoF\GeoIP\Api\Serializer\BasicIPInfoSerializer;
1718
use FoF\GeoIP\Api\Serializer\IPInfoSerializer;
1819
use Tobscure\JsonApi\Relationship;
1920

2021
class AttachRelation
2122
{
22-
public function __invoke(PostSerializer $serializer, Post $post): Relationship
23+
public function __construct(
24+
protected SettingsRepositoryInterface $settings
25+
) {
26+
}
27+
28+
public function __invoke(PostSerializer $serializer, Post $post): ?Relationship
2329
{
2430
$viewIPs = $serializer->getActor()->can('viewIps', $post);
2531

26-
return $serializer->hasOne($post, $viewIPs ? IPInfoSerializer::class : BasicIPInfoSerializer::class, 'ip_info');
32+
if ($viewIPs) {
33+
return $serializer->hasOne($post, IPInfoSerializer::class, 'ip_info');
34+
}
35+
36+
$viewCountry = $serializer->getActor()->can('fof-geoip.canSeeCountry');
37+
$showFlagsFeatureEnabled = $this->settings->get('fof-geoip.showFlag');
38+
$userPreference = $post->user->getPreference('showIPCountry');
39+
40+
if ($viewCountry || ($showFlagsFeatureEnabled && $userPreference)) {
41+
return $serializer->hasOne($post, BasicIPInfoSerializer::class, 'ip_info');
42+
}
43+
44+
return null;
2745
}
2846
}

src/Api/BasicUserAttributes.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of fof/geoip.
5+
*
6+
* Copyright (c) FriendsOfFlarum.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FoF\GeoIP\Api;
13+
14+
use Flarum\Api\Serializer\BasicUserSerializer;
15+
use Flarum\Settings\SettingsRepositoryInterface;
16+
use Flarum\User\User;
17+
18+
class BasicUserAttributes
19+
{
20+
public function __construct(
21+
protected SettingsRepositoryInterface $settings
22+
) {
23+
}
24+
25+
public function __invoke(BasicUserSerializer $serializer, User $user, array $attributes): array
26+
{
27+
if ($this->settings->get('fof-geoip.showFlag')) {
28+
$attributes['showIPCountry'] = (bool) $user->getPreference('showIPCountry');
29+
}
30+
31+
return $attributes;
32+
}
33+
}

src/Api/CurrentUserAttributes.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of fof/geoip.
5+
*
6+
* Copyright (c) FriendsOfFlarum.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FoF\GeoIP\Api;
13+
14+
use Flarum\Api\Serializer\CurrentUserSerializer;
15+
use Flarum\User\User;
16+
17+
class CurrentUserAttributes
18+
{
19+
public function __invoke(CurrentUserSerializer $serializer, User $user, array $attributes): array
20+
{
21+
if ($serializer->getActor()->can('fof-geoip.canSeeCountry')) {
22+
$attributes['canSeeCountry'] = true;
23+
}
24+
25+
return $attributes;
26+
}
27+
}

0 commit comments

Comments
 (0)