-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optionally display country flags for all users in post header (#42
) * feat: optional country flag display * Apply fixes from StyleCI * only apply flag when data is available * use Intl.displaynames for country display * only serialize country code on basic serializer * has IP address when used as an ID in serializer --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
ab64d25
commit 27a3615
Showing
9 changed files
with
129 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import app from 'flarum/forum/app'; | ||
import { extend } from 'flarum/common/extend'; | ||
import ItemList from 'flarum/common/utils/ItemList'; | ||
import CommentPost from 'flarum/forum/components/CommentPost'; | ||
import type Mithril from 'mithril'; | ||
import { getIPData } from '../helpers/IPDataHelper'; | ||
|
||
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 { image } = getIPData(ipInfo); | ||
if (image) { | ||
items.add('country', image, 100); | ||
} | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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\PostSerializer; | ||
use Flarum\Post\Post; | ||
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 | ||
{ | ||
$viewIPs = $serializer->getActor()->can('viewIps', $post); | ||
|
||
return $serializer->hasOne($post, $viewIPs ? IPInfoSerializer::class : BasicIPInfoSerializer::class, 'ip_info'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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\Serializer; | ||
|
||
use Flarum\Api\Serializer\AbstractSerializer; | ||
use FoF\GeoIP\Model\IPInfo; | ||
use InvalidArgumentException; | ||
|
||
class BasicIPInfoSerializer extends AbstractSerializer | ||
{ | ||
protected $type = 'ip_info'; | ||
|
||
/** | ||
* @param IPInfo $ip | ||
* | ||
* @return array | ||
*/ | ||
protected function getDefaultAttributes($ip): array | ||
{ | ||
if (!($ip instanceof IPInfo)) { | ||
throw new InvalidArgumentException( | ||
get_class($this).' can only serialize instances of '.IPInfo::class | ||
); | ||
} | ||
|
||
return [ | ||
'countryCode' => $ip->country_code, | ||
]; | ||
} | ||
|
||
/** | ||
* @param IPInfo $model | ||
* | ||
* @return string | ||
*/ | ||
public function getId($model) | ||
{ | ||
return hash('sha256', $model->address); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters