Initial working extension

This commit is contained in:
Matthew Kilgore 2020-11-23 23:17:55 -05:00
parent 350ce11f17
commit dbc67937ed
25 changed files with 5030 additions and 23 deletions

View File

@ -0,0 +1,20 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
[*.{diff,md}]
trim_trailing_whitespace = false
[*.php]
indent_size = 4

7
extensions/nicknames/.gitattributes vendored Normal file
View File

@ -0,0 +1,7 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
js/src export-ignore
.git* export-ignore
js/dist/*.js -diff

View File

@ -0,0 +1,16 @@
name: Build JavaScript assets
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: flarum/action-build@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

6
extensions/nicknames/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/vendor
composer.phar
.DS_Store
Thumbs.db
node_modules
js/dist/*

View File

@ -0,0 +1,14 @@
preset: recommended
enabled:
- logical_not_operators_with_successor_space
disabled:
- align_double_arrow
- blank_line_after_opening_tag
- multiline_array_trailing_comma
- new_with_braces
- phpdoc_align
- phpdoc_order
- phpdoc_separation
- phpdoc_types

View File

@ -0,0 +1 @@
# Changelog

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2020 Stichting Flarum (Flarum Foundation)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,23 +0,0 @@
# Nickname
![License](https://img.shields.io/badge/license-MIT-blue.svg) [![Latest Stable Version](https://img.shields.io/packagist/v/flarum/nickname.svg)](https://packagist.org/packages/flarum/nickname)
A [Flarum](http://flarum.org) extension.
### Installation
Use [Bazaar](https://discuss.flarum.org/d/5151-flagrow-bazaar-the-extension-marketplace) or install manually with composer:
```sh
composer require flarum/nickname
```
### Updating
```sh
composer update flarum/nickname
```
### Links
- [Packagist](https://packagist.org/packages/flarum/nickname)

View File

@ -0,0 +1,32 @@
{
"name": "flarum/nicknames",
"description": "Use nicknames for users",
"type": "flarum-extension",
"keywords": ["nicknames"],
"license": "MIT",
"support": {
"issues": "https://github.com/flarum/core/issues",
"source": "https://github.com/flarum/nicknames"
},
"require": {
"flarum/core": "^0.1.0-beta.14"
},
"autoload": {
"psr-4": {
"Flarum\\Nicknames\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
},
"flarum-extension": {
"title": "Nicknames",
"icon": {
"name": "fas fa-user-tag",
"backgroundColor": "#8E4529",
"color": "#ffffff"
}
}
}
}

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of flarum/nickname.
*
* Copyright (c) 2020 Flarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Flarum\Nicknames;
use Flarum\Event\ConfigureUserGambits;
use Flarum\Extend;
use Flarum\User\Event\Saving;
use Flarum\User\User;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/resources/less/forum.less'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/resources/less/admin.less'),
(new Extend\User())
->displayNameDriver('nickname', NicknameDriver::class),
(new Extend\ModelUrl(User::class))
->addSlugDriver('idOnly', IdOnlyUserSlugDriver::class),
(new Extend\Event())
->listen(Saving::class, SaveNicknameToDatabase::class)
->listen(ConfigureUserGambits::class, SetUserNicknameGambit::class),
new Extend\Locales(__DIR__ . '/resources/locale'),
];

View File

@ -0,0 +1 @@
export * from './src/admin';

View File

@ -0,0 +1 @@
export * from './src/forum';

4600
extensions/nicknames/js/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
{
"name": "@flarum/nicknames",
"version": "0.0.0",
"private": true,
"dependencies": {
"flarum-webpack-config": "^0.1.0-beta.10",
"webpack": "^4.26.0",
"webpack-cli": "^3.0.7"
},
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
}
}

View File

@ -0,0 +1,3 @@
app.initializers.add('flarum/nickname', () => {
console.log('[flarum/nickname] Hello, admin!');
});

View File

@ -0,0 +1,63 @@
import Modal from 'flarum/components/Modal';
import Button from 'flarum/components/Button';
import Stream from 'flarum/utils/Stream';
export default class NicknameModal extends Modal {
oninit(vnode) {
super.oninit(vnode);
this.nickname = Stream(app.session.user.displayName());
}
className() {
return 'NickameModal Modal--small';
}
title() {
return app.translator.trans('flarum-nicknames.forum.nickname.change');
}
content() {
return (
<div className="Modal-body">
<div className="Form Form--centered">
<div className="Form-group">
<input
type="text"
autocomplete="off"
name="nickname"
className="FormControl"
bidi={this.nickname}
disabled={this.loading} />
</div>
<div className="Form-group">
{Button.component({
className: 'Button Button--primary Button--block',
type: 'submit',
loading: this.loading,
}, app.translator.trans('flarum-nicknames.forum.nickname.submit_button'))}
</div>
</div>
</div>
);
}
onsubmit(e) {
e.preventDefault();
if (this.nickname() === app.session.user.displayName()) {
this.hide();
return;
}
this.loading = true;
app.session.user.save({ nickname: this.nickname() }, {
errorHandler: this.onerror.bind(this),
})
.then(this.hide.bind(this))
.catch(() => {
this.loading = false;
m.redraw();
});
}
}

View File

@ -0,0 +1,40 @@
import { extend } from 'flarum/extend';
import Button from 'flarum/components/Button';
import EditUserModal from 'flarum/components/EditUserModal';
import SettingsPage from 'flarum/components/SettingsPage';
import extractText from 'flarum/utils/extractText';
import Stream from 'flarum/utils/Stream';
import NickNameModal from './components/NicknameModal';
app.initializers.add('flarum/nicknames', () => {
extend(SettingsPage.prototype, 'accountItems', function (items) {
items.add('changeNickname',
<Button className="Button" onclick={() => app.modal.show(NickNameModal)}>
{app.translator.trans('flarum-nicknames.forum.settings.change_nickname_button')}
</Button>
);
});
extend(EditUserModal.prototype, 'oninit', function () {
this.nickname = Stream(this.attrs.user.displayName());
});
extend(EditUserModal.prototype, 'fields', function (items) {
items.add('nickname',
<div className="Form-group">
<label>{app.translator.trans('flarum-nicknames.forum.edit_user.password_heading')}</label>
<input className="FormControl"
placeholder={extractText(app.translator.trans('flarum-nicknames.forum.edit_user.password_text'))}
bidi={this.nickname} />
</div>, 100);
});
extend(EditUserModal.prototype, 'data', function (data) {
const user = this.attrs.user;
if (this.nickname() !== this.attrs.user.username()) {
data.nickname = this.nickname();
}
});
});

View File

@ -0,0 +1 @@
module.exports = require('flarum-webpack-config')();

View File

View File

@ -0,0 +1,19 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function(Builder $schema) {
if (!$schema->hasColumn('users', 'nickname')) {
$schema->table('users', function (Blueprint $table) use ($schema) {
$table->string('nickname', 150)->after('username')->index();
});
}
},
'down' => function(Builder $schema) {
$schema->table('users', function (Blueprint $table) use ($schema) {
$table->dropColumn('nickname');
});
}
];

View File

@ -0,0 +1,21 @@
<?php
namespace Flarum\Nicknames;
use Flarum\Database\AbstractModel;
use Flarum\Http\SlugDriverInterface;
use Flarum\User\User;
class IdOnlyUserSlugDriver implements SlugDriverInterface {
public function toSlug(AbstractModel $instance): string
{
return $instance->id;
}
public function fromSlug(string $slug, User $actor): AbstractModel
{
return User::where('id', $slug)->whereVisibleTo($actor)->firstOrFail();
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Flarum\Nicknames;
use Flarum\User\DisplayName\DriverInterface;
use Flarum\User\User;
class NicknameDriver implements DriverInterface {
public function displayName(User $user): string
{
return $user->nickname ? $user->nickname : $user->username;
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Flarum\Nicknames;
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Flarum\Search\AbstractSearch;
use Flarum\Search\GambitInterface;
use Flarum\User\UserRepository;
class NicknameFullTextGambit implements GambitInterface
{
/**
* @var UserRepository
*/
protected $users;
/**
* @param \Flarum\User\UserRepository $users
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
/**
* @param $searchValue
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getUserSearchSubQuery($searchValue)
{
return $this->users
->query()
->select('id')
->where('username', 'like', "{$searchValue}%")
->orWhere('nickname', 'like',"{$searchValue}%");
}
/**
* {@inheritdoc}
*/
public function apply(AbstractSearch $search, $searchValue)
{
$search->getQuery()
->whereIn(
'id',
$this->getUserSearchSubQuery($searchValue)
);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Flarum\Nicknames;
use Flarum\User\Event\Saving;
use Illuminate\Support\Arr;
class SaveNicknameToDatabase {
public function handle(Saving $event)
{
$user = $event->user;
$data = $event->data;
$actor = $event->actor;
$isSelf = $actor->id === $user->id;
$canEdit = $actor->can('edit', $user);
$attributes = Arr::get($data, 'attributes', []);
if (isset($attributes['nickname'])) {
if (!$isSelf) {
$actor->assertPermission($canEdit);
}
$user->nickname = $attributes['nickname'];
$user->save();
}
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Flarum\Nicknames;
use Flarum\Event\ConfigureUserGambits;
class SetUserNicknameGambit
{
public function handle(ConfigureUserGambits $event) {
$event->gambits->setFulltextGambit(NicknameFullTextGambit::class);
}
}