Skip to content

Commit

Permalink
improve country flag display feature (#46)
Browse files Browse the repository at this point in the history
* improve country flag display feature

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Oct 14, 2024
1 parent aa8c44a commit 28145c2
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 10 deletions.
9 changes: 5 additions & 4 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use Flarum\Api\Controller;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Extend;
use Flarum\Frontend\Document;
use Flarum\Post\Post;
use Flarum\Settings\Event\Saving;
use Flarum\User\User;
use FoF\GeoIP\Api\GeoIP;

return [
Expand Down Expand Up @@ -74,9 +74,10 @@
->registerPreference('showIPCountry', 'boolval', false),

(new Extend\ApiSerializer(BasicUserSerializer::class))
->attribute('showIPCountry', function (BasicUserSerializer $serializer, User $user) {
return (bool) $user->getPreference('showIPCountry');
}),
->attributes(Api\BasicUserAttributes::class),

(new Extend\ApiSerializer(CurrentUserSerializer::class))
->attributes(Api\CurrentUserAttributes::class),

(new Extend\Conditional())
->whenExtensionEnabled('fof-default-user-preferences', fn () => [
Expand Down
14 changes: 14 additions & 0 deletions js/src/@shims/shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import IPInfo from '../forum/models/IPInfo';

declare module 'flarum/common/models/Post' {
export default interface Post {
ip_info: () => IPInfo;
}
}

declare module 'flarum/common/models/User' {
export default interface User {
showIPCountry: () => boolean;
canSeeCountry: () => boolean;
}
}
13 changes: 12 additions & 1 deletion js/src/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@ import app from 'flarum/admin/app';
import GeoipSettingsPage from './components/ExtensionSettingsPage';

app.initializers.add('fof/geoip', () => {
app.extensionData.for('fof-geoip').registerPage(GeoipSettingsPage);
app.extensionData
.for('fof-geoip')
.registerPage(GeoipSettingsPage)
.registerPermission(
{
icon: 'fas fa-globe',
permission: 'fof-geoip.canSeeCountry',
label: app.translator.trans('fof-geoip.admin.permissions.see_country'),
},
'moderate',
50
);
});
3 changes: 2 additions & 1 deletion js/src/forum/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export default [
.hasOne('ip_info'),

new Extend.Model(User) //
.attribute('showIPCountry'),
.attribute('showIPCountry')
.attribute('canSeeCountry'),
];
2 changes: 1 addition & 1 deletion js/src/forum/extenders/extendCommentPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function extendCommentPost() {
if (app.forum.attribute<boolean>('fof-geoip.showFlag')) {
const ipInfo = this.attrs.post.ip_info?.();
const postUser = this.attrs.post.user();
if (postUser && postUser.showIPCountry() && ipInfo) {
if ((ipInfo && postUser && postUser.showIPCountry()) || app.session.user?.canSeeCountry?.()) {
const { image } = getIPData(ipInfo);
if (image) {
items.add('country', image, 100);
Expand Down
4 changes: 3 additions & 1 deletion resources/locale/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fof-geoip:
admin:
permissions:
see_country: Always display the country of the IP address
settings:
title: FriendsOfFlarum GeoIP

Expand All @@ -24,7 +26,7 @@ fof-geoip:
quota_label: Lookup quota

show_flag_label: Show country flag for each post
show_flag_help: Without disclosing the IP address, show the country flag of the IP address of the post author.
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.

forum:
alerts:
Expand Down
22 changes: 20 additions & 2 deletions src/Api/AttachRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,34 @@

use Flarum\Api\Serializer\PostSerializer;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\GeoIP\Api\Serializer\BasicIPInfoSerializer;
use FoF\GeoIP\Api\Serializer\IPInfoSerializer;
use Tobscure\JsonApi\Relationship;

class AttachRelation
{
public function __invoke(PostSerializer $serializer, Post $post): Relationship
public function __construct(
protected SettingsRepositoryInterface $settings
) {
}

public function __invoke(PostSerializer $serializer, Post $post): ?Relationship
{
$viewIPs = $serializer->getActor()->can('viewIps', $post);

return $serializer->hasOne($post, $viewIPs ? IPInfoSerializer::class : BasicIPInfoSerializer::class, 'ip_info');
if ($viewIPs) {
return $serializer->hasOne($post, IPInfoSerializer::class, 'ip_info');
}

$viewCountry = $serializer->getActor()->can('fof-geoip.canSeeCountry');
$showFlagsFeatureEnabled = $this->settings->get('fof-geoip.showFlag');
$userPreference = $post->user->getPreference('showIPCountry');

if ($viewCountry || ($showFlagsFeatureEnabled && $userPreference)) {
return $serializer->hasOne($post, BasicIPInfoSerializer::class, 'ip_info');
}

return null;
}
}
33 changes: 33 additions & 0 deletions src/Api/BasicUserAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\GeoIP\Api;

use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;

class BasicUserAttributes
{
public function __construct(
protected SettingsRepositoryInterface $settings
) {
}

public function __invoke(BasicUserSerializer $serializer, User $user, array $attributes): array
{
if ($this->settings->get('fof-geoip.showFlag')) {
$attributes['showIPCountry'] = (bool) $user->getPreference('showIPCountry');
}

return $attributes;
}
}
27 changes: 27 additions & 0 deletions src/Api/CurrentUserAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\GeoIP\Api;

use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\User\User;

class CurrentUserAttributes
{
public function __invoke(CurrentUserSerializer $serializer, User $user, array $attributes): array
{
if ($serializer->getActor()->can('fof-geoip.canSeeCountry')) {
$attributes['canSeeCountry'] = true;
}

return $attributes;
}
}

0 comments on commit 28145c2

Please sign in to comment.