From 4395935cbeda9e9bc70aa9109367030cadff75cc Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 12 Mar 2015 10:38:18 +1030 Subject: [PATCH] Implement user "bio" field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Perhaps this should be an extension, but it is pretty essential and I can’t think of many instances where it wouldn’t be wanted. Would be very easy to extract later on if need be. --- .../2015_02_24_000000_create_users_table.php | 2 + .../src/Api/Actions/Users/UpdateAction.php | 2 +- .../src/Api/Serializers/UserSerializer.php | 2 + .../src/Core/Commands/EditUserCommand.php | 2 + .../core/src/Core/CoreServiceProvider.php | 1 + .../src/Core/Events/UserBioWasChanged.php | 13 ++++ .../Commands/EditUserCommandHandler.php | 5 ++ framework/core/src/Core/Models/User.php | 71 +++++++++++++++++++ 8 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 framework/core/src/Core/Events/UserBioWasChanged.php diff --git a/framework/core/migrations/2015_02_24_000000_create_users_table.php b/framework/core/migrations/2015_02_24_000000_create_users_table.php index 3b5e6cf6d..7c36024b1 100644 --- a/framework/core/migrations/2015_02_24_000000_create_users_table.php +++ b/framework/core/migrations/2015_02_24_000000_create_users_table.php @@ -21,6 +21,8 @@ class CreateUsersTable extends Migration { $table->string('confirmation_token')->nullable(); $table->boolean('is_activated')->default(0); $table->string('password'); + $table->text('bio')->nullable(); + $table->text('bio_html')->nullable(); $table->dateTime('join_time')->nullable(); $table->dateTime('last_seen_time')->nullable(); $table->dateTime('read_time')->nullable(); diff --git a/framework/core/src/Api/Actions/Users/UpdateAction.php b/framework/core/src/Api/Actions/Users/UpdateAction.php index e058b4ab3..25e3cbf82 100644 --- a/framework/core/src/Api/Actions/Users/UpdateAction.php +++ b/framework/core/src/Api/Actions/Users/UpdateAction.php @@ -22,7 +22,7 @@ class UpdateAction extends BaseAction // second one failed, the first one would still have succeeded.) $command = new EditUserCommand($userId, $this->actor->getUser()); $this->hydrate($command, $params->get('users')); - $this->dispatch($command); + $user = $this->dispatch($command, $params); // Presumably, the user was updated successfully. (The command handler // would have thrown an exception if not.) We set this user as our diff --git a/framework/core/src/Api/Serializers/UserSerializer.php b/framework/core/src/Api/Serializers/UserSerializer.php index 87e4a2b4c..82f862d2e 100644 --- a/framework/core/src/Api/Serializers/UserSerializer.php +++ b/framework/core/src/Api/Serializers/UserSerializer.php @@ -30,6 +30,7 @@ class UserSerializer extends UserBasicSerializer $canEdit = $user->can($actorUser, 'edit'); $attributes += [ + 'bioHtml' => $user->bioHtml, 'joinTime' => $user->join_time ? $user->join_time->toRFC3339String() : null, 'lastSeenTime' => $user->last_seen_time ? $user->last_seen_time->toRFC3339String() : null, 'discussionsCount' => (int) $user->discussions_count, @@ -40,6 +41,7 @@ class UserSerializer extends UserBasicSerializer if ($canEdit) { $attributes += [ + 'bio' => $user->bio, 'isActivated' => $user->is_activated, 'email' => $user->email, 'isConfirmed' => $user->is_confirmed diff --git a/framework/core/src/Core/Commands/EditUserCommand.php b/framework/core/src/Core/Commands/EditUserCommand.php index b4e625359..fb22897e6 100644 --- a/framework/core/src/Core/Commands/EditUserCommand.php +++ b/framework/core/src/Core/Commands/EditUserCommand.php @@ -12,6 +12,8 @@ class EditUserCommand public $password; + public $bio; + public $readTime; public function __construct($userId, $user) diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index d4e4937b2..8d507b67f 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -118,6 +118,7 @@ class CoreServiceProvider extends ServiceProvider Model::setValidator($this->app['validator']); User::setHasher($this->app['hash']); + User::setFormatter($this->app['flarum.formatter']); } public function registerPermissions() diff --git a/framework/core/src/Core/Events/UserBioWasChanged.php b/framework/core/src/Core/Events/UserBioWasChanged.php new file mode 100644 index 000000000..6b88e3bc5 --- /dev/null +++ b/framework/core/src/Core/Events/UserBioWasChanged.php @@ -0,0 +1,13 @@ +user = $user; + } +} diff --git a/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php b/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php index 709d0a6ec..86210ff5f 100644 --- a/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php @@ -1,6 +1,8 @@ password)) { $userToEdit->changePassword($command->password); } + if (isset($command->bio)) { + $userToEdit->changeBio($command->bio); + } if (! empty($command->readTime)) { $userToEdit->markAllAsRead(); } diff --git a/framework/core/src/Core/Models/User.php b/framework/core/src/Core/Models/User.php index 5822db69f..a61cebea9 100755 --- a/framework/core/src/Core/Models/User.php +++ b/framework/core/src/Core/Models/User.php @@ -2,12 +2,14 @@ use Illuminate\Contracts\Hashing\Hasher; use Tobscure\Permissible\Permissible; +use Flarum\Core\Formatter\FormatterManager; use Flarum\Core\Exceptions\InvalidConfirmationTokenException; use Flarum\Core\Events\UserWasDeleted; use Flarum\Core\Events\UserWasRegistered; use Flarum\Core\Events\UserWasRenamed; use Flarum\Core\Events\UserEmailWasChanged; use Flarum\Core\Events\UserPasswordWasChanged; +use Flarum\Core\Events\UserBioWasChanged; use Flarum\Core\Events\UserWasActivated; use Flarum\Core\Events\UserEmailWasConfirmed; @@ -15,6 +17,13 @@ class User extends Model { use Permissible; + /** + * The text formatter instance. + * + * @var \Flarum\Core\Formatter\Formatter + */ + protected static $formatter; + /** * The validation rules for this model. * @@ -146,6 +155,37 @@ class User extends Model $this->attributes['password'] = $value ? static::$hasher->make($value) : null; } + /** + * Change the user's bio. + * + * @param string $bio + * @return $this + */ + public function changeBio($bio) + { + $this->bio = $bio; + + $this->raise(new UserBioWasChanged($this)); + + return $this; + } + + /** + * Get the content formatter as HTML. + * + * @param string $value + * @return string + */ + public function getBioHtmlAttribute($value) + { + if (! $value) { + $this->bio_html = $value = static::formatBio($this->bio); + $this->save(); + } + + return $value; + } + /** * Mark all discussions as read by setting the user's read_time. * @@ -332,4 +372,35 @@ class User extends Model { static::$hasher = $hasher; } + + /** + * Get text formatter instance. + * + * @return \Flarum\Core\Formatter\FormatterManager + */ + public static function getFormatter() + { + return static::$formatter; + } + + /** + * Set text formatter instance. + * + * @param \Flarum\Core\Formatter\FormatterManager $formatter + */ + public static function setFormatter(FormatterManager $formatter) + { + static::$formatter = $formatter; + } + + /** + * Format a string of post content using the set formatter. + * + * @param string $content + * @return string + */ + protected static function formatBio($content) + { + return static::$formatter->format($content); + } }