Pony.fm/app/Models/User.php

560 lines
18 KiB
PHP
Raw Permalink Normal View History

2015-08-30 13:22:00 +02:00
<?php
/**
* Pony.fm - A community for pony fan music.
2018-04-21 06:25:36 +02:00
* Copyright (C) 2015 Feld0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-02-14 03:34:58 +01:00
namespace App\Models;
2015-08-31 14:35:47 +02:00
use Carbon\Carbon;
use DB;
2015-08-31 14:35:47 +02:00
use Gravatar;
2015-08-30 15:01:12 +02:00
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
2015-08-31 13:19:29 +02:00
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
2015-08-30 13:22:00 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Auth;
2015-08-30 14:29:12 +02:00
use Illuminate\Support\Str;
use League\OAuth2\Client\Token\AccessToken;
2021-02-14 03:34:58 +01:00
use App\Contracts\Commentable;
use App\Contracts\Searchable;
use App\Traits\IndexedInElasticsearchTrait;
use Validator;
use Venturecraft\Revisionable\RevisionableTrait;
2015-08-30 13:22:00 +02:00
2016-01-01 01:36:08 +01:00
/**
2021-02-14 03:34:58 +01:00
* App\Models\User
2016-01-01 01:36:08 +01:00
*
* @property integer $id
* @property string $display_name
* @property string $username
* @property boolean $sync_names
* @property string $email
* @property string $gravatar
* @property string $slug
* @property boolean $uses_gravatar
* @property boolean $can_see_explicit_content
* @property string $bio
* @property integer $track_count
* @property integer $comment_count
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $avatar_id
* @property string $remember_token
* @property boolean $is_archived
* @property \Carbon\Carbon $disabled_at
2021-02-14 03:34:58 +01:00
* @property-read \App\Models\Image $avatar
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ResourceUser[] $users
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Role[] $roles
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Track[] $tracks
2016-01-01 01:36:08 +01:00
* @property-read mixed $url
* @property-read mixed $message_url
* @property-read \Illuminate\Database\Eloquent\Collection|\Venturecraft\Revisionable\Revision[] $revisionHistory
2021-02-14 03:34:58 +01:00
* @method static \Illuminate\Database\Query\Builder|\App\Models\User userDetails()
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Notification[] $notifications
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $followers
* @property-read mixed $user
2021-02-14 03:34:58 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Activity[] $activities
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Activity[] $notificationActivities
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Email[] $emails
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\EmailSubscription[] $emailSubscriptions
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDisplayName($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUsername($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereSyncNames($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereGravatar($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereSlug($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUsesGravatar($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCanSeeExplicitContent($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereBio($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereTrackCount($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCommentCount($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereAvatarId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereIsArchived($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDisabledAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User withEmailSubscriptionFor($activityType)
2016-12-10 17:47:49 +01:00
* @mixin \Eloquent
2021-02-14 03:34:58 +01:00
* @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePoniverseId($poniverseId)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereLinkedToPoniverse()
* @property int $redirect_to
2021-02-14 03:34:58 +01:00
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereRedirectTo($value)
2016-01-01 01:36:08 +01:00
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, \Illuminate\Contracts\Auth\Access\Authorizable, Searchable, Commentable
2015-08-30 13:22:00 +02:00
{
use Authenticatable, CanResetPassword, Authorizable, RevisionableTrait, IndexedInElasticsearchTrait;
2016-01-07 19:16:37 +01:00
protected $elasticsearchType = 'user';
2015-08-30 15:01:12 +02:00
2015-08-30 14:29:12 +02:00
protected $table = 'users';
2015-11-09 20:35:30 +01:00
protected $casts = [
'id' => 'integer',
'sync_names' => 'boolean',
'uses_gravatar' => 'boolean',
'can_see_explicit_content' => 'boolean',
'track_count' => 'integer',
'comment_count' => 'integer',
'avatar_id' => 'integer',
'is_archived' => 'boolean',
2017-09-20 23:02:39 +02:00
'redirect_to' => 'integer'
2015-11-09 20:35:30 +01:00
];
protected $dates = ['created_at', 'updated_at', 'disabled_at'];
protected $hidden = ['disabled_at', 'remember_token'];
2015-08-30 14:29:12 +02:00
public function scopeUserDetails($query)
{
if (Auth::check()) {
$query->with([
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
'users' => function ($query) {
2015-08-30 14:29:12 +02:00
$query->whereUserId(Auth::user()->id);
}
]);
}
2017-11-28 17:44:28 +01:00
return $query;
2015-08-30 14:29:12 +02:00
}
/**
* Returns users with an email subscription to the given activity type.
*
* @param $query
* @param int $activityType one of the TYPE_* constants in the Activity class
* @return mixed
*/
public function scopeWithEmailSubscriptionFor($query, int $activityType) {
return $query->whereHas('emailSubscriptions', function ($query) use ($activityType) {
$query->where('activity_type', $activityType);
});
}
/**
* Finds a user by their Poniverse account ID.
*
* @param $query
* @param int $poniverseId
* @return mixed
*/
public function scopeWherePoniverseId($query, int $poniverseId) {
return $query
->whereLinkedToPoniverse($query)
->where('oauth2_tokens.external_user_id', '=', $poniverseId);
}
/**
* Filters the list of users to those who have a linked Poniverse account.
*
* @param $query
* @return mixed
*/
public function scopeWhereLinkedToPoniverse($query) {
return $query
->join('oauth2_tokens', 'users.id', '=', 'oauth2_tokens.user_id')
->select('users.*', 'oauth2_tokens.external_user_id');
}
/**
* Gets this user's OAuth access token record.
*
* @return AccessToken|null
*/
public function getAccessToken() {
$accessTokenRecord = DB::table('oauth2_tokens')->where('user_id', '=', $this->id)->first();
if ($accessTokenRecord === null) {
return null;
} else {
return new AccessToken([
'access_token' => $accessTokenRecord->access_token,
'refresh_token' => $accessTokenRecord->refresh_token,
'expires' => Carbon::parse($accessTokenRecord->expires)->timestamp,
'resource_owner_id' => $accessTokenRecord->external_user_id,
]);
}
}
/**
* Updates this user's access token record.
*
* @param AccessToken $accessToken
*/
public function setAccessToken(AccessToken $accessToken) {
DB::table('oauth2_tokens')
->where('user_id', '=', $this->id)
->update([
'access_token' => $accessToken->getToken(),
'refresh_token' => $accessToken->getRefreshToken(),
'expires' => Carbon::createFromTimestampUTC($accessToken->getExpires()),
// NOTE: external_user_id does not get updated!
]);
}
/**
* Takes the given string, slugifies it, and increments a counter if needed
* to generate a unique slug version of it.
*
* @param string $name
* @return string a unique slug
*/
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
private static function getUniqueSlugForName(string $name):string
{
$baseSlug = Str::slug($name);
// Ensure that the slug we generate is long enough.
for ($i = Str::length($baseSlug); $i < config('ponyfm.user_slug_minimum_length'); $i++) {
$baseSlug = $baseSlug.'-';
}
$slugBeingTried = $baseSlug;
$counter = 2;
while (true) {
$existingEntity = static::where('slug', $slugBeingTried)->first();
$validator = Validator::make(['slug' => $slugBeingTried], ['isNotReservedSlug']);
if ($existingEntity || $validator->fails()) {
$slugBeingTried = "{$baseSlug}-{$counter}";
$counter++;
continue;
} else {
break;
}
}
return $slugBeingTried;
}
2015-11-09 20:35:30 +01:00
/**
* @param string $username used to perform the search
2015-11-09 20:35:30 +01:00
* @param string $displayName
* @param string|null $email set to null if creating an archived user
* @param bool $createArchivedUser if true, includes archived users in the search and creates an archived user
2015-11-09 20:35:30 +01:00
* @return User
*/
public static function findOrCreate(
string $username,
string $displayName,
string $email = null,
bool $createArchivedUser = false
) {
$user = static::where(DB::raw('LOWER(username)'), Str::lower($username));
if (false === $createArchivedUser) {
$user = $user->where('is_archived', false);
}
$user = $user->first();
2015-11-09 20:35:30 +01:00
if (null !== $user) {
return $user;
} else {
$user = new User;
$user->username = $username;
$user->display_name = $displayName;
2016-06-06 08:15:56 +02:00
$user->slug = self::getUniqueSlugForName($displayName);
2015-11-09 20:35:30 +01:00
$user->email = $email;
$user->uses_gravatar = true;
$user->is_archived = $createArchivedUser;
2015-11-09 20:35:30 +01:00
$user->save();
$user = $user->fresh();
$user->wasRecentlyCreated = true;
2015-11-09 20:35:30 +01:00
return $user;
}
}
2015-08-30 14:29:12 +02:00
public function avatar()
{
2016-06-06 08:15:56 +02:00
return $this->belongsTo(Image::class);
2015-08-30 14:29:12 +02:00
}
public function users()
{
2016-06-06 08:15:56 +02:00
return $this->hasMany(ResourceUser::class, 'artist_id');
2015-08-30 14:29:12 +02:00
}
public function followers()
{
return $this->belongsToMany(User::class, 'followers', 'artist_id', 'user_id');
}
2015-08-30 14:29:12 +02:00
public function roles()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function comments():HasMany
2015-08-30 14:29:12 +02:00
{
2016-06-06 08:15:56 +02:00
return $this->hasMany(Comment::class, 'profile_id')->orderBy('created_at', 'desc');
2015-08-30 14:29:12 +02:00
}
public function tracks()
{
2016-06-06 08:15:56 +02:00
return $this->hasMany(Track::class, 'user_id');
}
public function notifications()
{
return $this->hasMany(Notification::class, 'user_id');
}
public function notificationActivities()
{
return $this->hasManyThrough(Activity::class, Notification::class, 'user_id', 'notification_id', 'id');
}
public function emails()
{
return $this->hasManyThrough(Email::class, Notification::class, 'user_id', 'notification_id', 'id');
}
public function emailSubscriptions()
{
return $this->hasMany(EmailSubscription::class, 'user_id', 'id');
}
2015-08-30 14:29:12 +02:00
public function getIsArchivedAttribute()
{
return (bool) $this->attributes['is_archived'];
2015-08-30 14:29:12 +02:00
}
public function getUrlAttribute()
{
return action('ArtistsController@getProfile', $this->slug);
2015-08-30 14:29:12 +02:00
}
public function getMessageUrlAttribute()
{
return 'http://mlpforums.com/index.php?app=members&module=messaging&section=send&do=form&fromMemberID='.$this->id;
2015-08-30 14:29:12 +02:00
}
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->password_hash;
}
public function getReminderEmail()
{
return $this->email;
}
public function setDisplayNameAttribute($value)
{
$this->attributes['display_name'] = $value;
}
public function getAvatarUrl($type = Image::NORMAL)
{
if (!$this->uses_gravatar && $this->avatar !== null) {
2015-08-30 14:29:12 +02:00
return $this->avatar->getUrl($type);
}
if ($this->email == "redacted@example.net") {
return Gravatar::getUrl($this->id."", Image::$ImageTypes[$type]['width'], "identicon");
2015-08-30 14:29:12 +02:00
}
$email = $this->gravatar;
if (!strlen($email)) {
$email = $this->email;
}
return Gravatar::getUrl($email, Image::$ImageTypes[$type]['width']);
}
public function getAvatarUrlLocal($type = Image::NORMAL)
{
if (!$this->uses_gravatar && $this->avatar !== null) {
return $this->avatar->getFile($type);
}
if ($this->email == "redacted@example.net") {
return Gravatar::getUrl($this->id."", Image::$ImageTypes[$type]['width'], "identicon");
}
$email = $this->gravatar;
if (!strlen($email)) {
$email = $this->email;
}
return Gravatar::getUrl($email, Image::$ImageTypes[$type]['width']);
}
public function getSettingsUrl()
{
return route('account:settings', ['slug' => $this->slug]);
}
2015-08-30 13:22:00 +02:00
/**
2015-08-30 14:29:12 +02:00
* Get the token value for the "remember me" session.
2015-08-30 13:22:00 +02:00
*
2015-08-30 14:29:12 +02:00
* @return string
2015-08-30 13:22:00 +02:00
*/
2015-08-30 14:29:12 +02:00
public function getRememberToken()
{
return $this->remember_token;
}
2015-08-30 13:22:00 +02:00
/**
2015-08-30 14:29:12 +02:00
* Set the token value for the "remember me" session.
2015-08-30 13:22:00 +02:00
*
2015-08-30 14:29:12 +02:00
* @param string $value
* @return void
2015-08-30 13:22:00 +02:00
*/
2015-08-30 14:29:12 +02:00
public function setRememberToken($value)
{
$this->remember_token = $value;
}
2015-08-30 13:22:00 +02:00
/**
2015-08-30 14:29:12 +02:00
* Get the column name for the "remember me" token.
2015-08-30 13:22:00 +02:00
*
2015-08-30 14:29:12 +02:00
* @return string
2015-08-30 13:22:00 +02:00
*/
2015-08-30 14:29:12 +02:00
public function getRememberTokenName()
{
return "remember_token";
}
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
public function getUserAttribute():User
{
return $this;
}
/**
* @inheritdoc
*/
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
public function getResourceType():string
{
return 'profile';
}
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
public function activities():MorphMany
{
return $this->morphMany(Activity::class, 'resource');
}
/**
* Returns true if this user has the given role.
*
* @param string $roleName
* @return bool
*/
public function hasRole($roleName):bool
{
foreach ($this->roles as $role) {
if ($role->name === $roleName) {
return true;
}
}
return false;
}
2016-01-07 19:16:37 +01:00
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
public static function mapPublicUserSummary(User $user)
{
return [
'id' => $user->id,
'name' => $user->display_name,
'slug' => $user->slug,
'url' => $user->url,
'is_archived' => $user->is_archived,
'avatars' => [
'small' => $user->getAvatarUrl(Image::SMALL),
'normal' => $user->getAvatarUrl(Image::NORMAL)
],
'created_at' => $user->created_at
];
}
/**
* Helper method that returns a row for every type of notifiable activity.
* It's meant to be used for the notification settings screen.
*
* @return array
*/
private function emailSubscriptionsJoined() {
return DB::select('
SELECT "subscriptions".*, "activity_types".* FROM
(SELECT * FROM "email_subscriptions"
WHERE "email_subscriptions"."deleted_at" IS NULL
AND "email_subscriptions"."user_id" = ?) as "subscriptions"
RIGHT JOIN "activity_types"
ON "subscriptions"."activity_type" = "activity_types"."activity_type"
', [$this->id]);
}
/**
* Generates an array of the user's notification settings. It's meant to be
* used for the notification settings screen.
*
* @return array
*/
public function getNotificationSettings() {
$settings = [];
$emailSubscriptions = $this->emailSubscriptionsJoined();
foreach($emailSubscriptions as $subscription) {
// TODO: remove this check when news and album notifications are implemented
if (!in_array($subscription->activity_type, [Activity::TYPE_NEWS, Activity::TYPE_PUBLISHED_ALBUM])) {
$settings[] = [
'description' => $subscription->description,
'activity_type' => $subscription->activity_type,
'receive_emails' => $subscription->id !== NULL
];
}
}
return $settings;
}
2016-01-07 19:16:37 +01:00
/**
* Returns this model in Elasticsearch-friendly form. The array returned by
* this method should match the current mapping for this model's ES type.
*
* @return array
*/
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
public function toElasticsearch():array
{
return [
'username' => $this->username,
'display_name' => $this->display_name,
'tracks' => $this->tracks->pluck('title'),
];
2016-01-07 19:16:37 +01:00
}
/**
* @inheritdoc
*/
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
public function shouldBeIndexed():bool
{
return $this->disabled_at === null;
}
}