Skip to content

Commit

Permalink
Clean Microblog
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Dec 27, 2023
1 parent 7611bc5 commit c9ec290
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 116 deletions.
10 changes: 5 additions & 5 deletions app/Http/Controllers/Microblog/VoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use Coyote\Microblog;
use Coyote\Notifications\Microblog\VotedNotification;
use Coyote\Reputation;
use Coyote\Services\Stream\Activities\Vote as Stream_Vote;
use Coyote\Services\Stream\Objects\Comment as Stream_Comment;
use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog;
use Coyote\Services\UrlBuilder;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
use Coyote\Services\Stream\Activities\Vote as Stream_Vote;
use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog;
use Coyote\Services\Stream\Objects\Comment as Stream_Comment;

/**
* Ocena glosow na dany wpis na mikro (lub wyswietlanie loginow ktorzy oddali ow glos)
Expand Down Expand Up @@ -73,7 +73,7 @@ public function post(Microblog $microblog, Request $request)
$microblog->votes++;
}

$microblog->score = $microblog->getScore();
$microblog->resetScore();
$target = null;

// reputacje przypisujemy tylko za ocene wpisu a nie komentarza!!
Expand Down Expand Up @@ -108,7 +108,7 @@ public function post(Microblog $microblog, Request $request)

public function voters(Microblog $microblog)
{
$microblog->load(['voters', 'voters.user' => fn ($query) => $query->select('id', 'name')->withTrashed()]);
$microblog->load(['voters', 'voters.user' => fn($query) => $query->select('id', 'name')->withTrashed()]);

return ['id' => $microblog->id, 'parent_id' => $microblog->parent_id, 'users' => $microblog->voters->pluck('user.name')];
}
Expand Down
175 changes: 64 additions & 111 deletions app/Models/Microblog.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Coyote;

use Carbon\Carbon;
use Coyote\Microblog\Vote;
use Coyote\Models\Asset;
use Coyote\Models\Scopes\ForUser;
Expand All @@ -10,8 +10,16 @@
use Coyote\Services\Media\SerializeClass;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support;

/**
* @property int $id
Expand All @@ -20,8 +28,8 @@
* @property int $votes
* @property int $score
* @property int $is_sponsored
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property Carbon $created_at
* @property Carbon $updated_at
* @property string $deleted_at
* @property string $text
* @property string $html
Expand All @@ -30,184 +38,129 @@
* @property Tag[] $tags
* @property User $user
* @property Microblog[] $children
* @property Microblog\Vote[]|\Illuminate\Support\Collection $voters
* @property Asset[]|\Illuminate\Support\Collection $assets
* @property Microblog\Vote[]|Support\Collection $voters
* @property Asset[]|Support\Collection $assets
*/
class Microblog extends Model
{
use SoftDeletes, Taggable, ForUser, SerializeClass;
use Searchable{
use Searchable {
getIndexBody as parentGetIndexBody;
}

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['parent_id', 'user_id', 'text'];

/**
* @var string
*/
protected $dateFormat = 'Y-m-d H:i:se';

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at', 'deleted_at'];

/**
* @var string[]
*/
protected $casts = ['is_sponsored' => 'bool'];
protected $attributes = [
'votes' => 0,
'views' => 1,
];

/**
* Domyslne wartosci dla nowego modelu
*
* @var array
*/
protected $attributes = ['votes' => 0, 'views' => 1];

/**
* Html version of the entry.
*
* @var null|string
*/
private $html = null;
private ?string $html = null;

public static function boot()
{
parent::boot();

static::creating(function (Microblog $model) {
// nadajemy domyslna wartosc sortowania przy dodawaniu elementu
$model->score = $model->getScore();
});

static::creating(fn(Microblog $model) => $model->resetScore());
static::addGlobalScope(resolve(UserRelationsScope::class));
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function assets()
public function assets(): MorphMany
{
return $this->morphMany(Asset::class, 'content');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'resource', 'tag_resources');
}

/**
* Prosty "algorytm" do generowania rankingu danego wpisu na podstawie ocen i czasu dodania
*
* @return int
*/
public function getScore()
public function resetScore(): void
{
$timestamp = $this->created_at ? $this->created_at->timestamp : time();

return (int) (($this->votes * 5) + (($timestamp - 1380153600) / 3600));
$this->score = $this->getScore();
}

public function setHtmlAttribute($value)
private function getScore(): int
{
$this->html = $value;
$timestamp = $this->created_at ? $this->created_at->timestamp : time();
$hours = (int)($timestamp - 1380153600) / 3600; // since 26 september, 2023.
return $this->votes * 5 + $hours;
}

/**
* @return null|string
*/
public function getHtmlAttribute()
public function comments(): HasMany
{
if ($this->html !== null) {
return $this->html;
}
return $this
->hasMany(self::class, 'parent_id', 'id')
->orderBy('microblogs.id', 'ASC');
}

return $this->html = app('parser.post')->parse($this->text);
public function subscribers(): MorphMany
{
return $this->morphMany(Subscription::class, 'resource');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
public function voters(): HasMany
{
return $this->hasMany(self::class, 'parent_id', 'id')->orderBy('microblogs.id', 'ASC');
return $this->hasMany(Vote::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function subscribers()
public function page(): MorphOne
{
return $this->morphMany(Subscription::class, 'resource');
return $this->morphOne(Page::class, 'content');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function voters()
public function parent(): HasOne
{
return $this->hasMany(Vote::class);
return $this
->hasOne(Microblog::class, 'id', 'parent_id')
->withoutGlobalScope(UserRelationsScope::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function page()
public function user(): BelongsTo
{
return $this->morphOne('Coyote\Page', 'content');
return $this->belongsTo(User::class)
->select(['id', 'name', 'deleted_at', 'is_blocked', 'photo', 'is_online', 'reputation'])
->withTrashed();
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function parent()
public function setHtmlAttribute(string $value): void
{
return $this->hasOne('Coyote\Microblog', 'id', 'parent_id')->withoutGlobalScope(UserRelationsScope::class);
$this->html = $value;
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
public function getHtmlAttribute(): string
{
return $this->belongsTo(User::class)->select(['id', 'name', 'deleted_at', 'is_blocked', 'photo', 'is_online', 'reputation'])->withTrashed();
if ($this->html === null) {
$this->html = app('parser.post')->parse($this->text);
}
return $this->html;
}

public function scopeIncludeIsSubscribed(Builder $builder, int $userId): Builder
{
$this->addSelectIfNull($builder);

return $builder
->addSelect(new Expression('CASE WHEN mw.user_id IS NULL THEN false ELSE true END AS is_subscribed'))
->leftJoin('subscriptions AS mw', function ($join) use ($userId) {
$join->on('mw.resource_id', '=', 'microblogs.id')->where('mw.resource_type', '=', static::class)->where('mw.user_id', '=', $userId);
});
->leftJoin('subscriptions AS mw', fn(JoinClause $join) => $join
->on('mw.resource_id', '=', 'microblogs.id')
->where('mw.resource_type', '=', static::class)
->where('mw.user_id', '=', $userId));
}

public function scopeIncludeIsVoted(Builder $builder, int $userId): Builder
{
$this->addSelectIfNull($builder);

return $builder
->addSelect(new Expression('CASE WHEN mv.id IS NULL THEN false ELSE true END AS is_voted'))
->leftJoin('microblog_votes AS mv', function ($join) use ($userId) {
$join->on('mv.microblog_id', '=', 'microblogs.id')->where('mv.user_id', '=', $userId);
});
->leftJoin('microblog_votes AS mv', fn(JoinClause $join) => $join
->on('mv.microblog_id', '=', 'microblogs.id')
->where('mv.user_id', '=', $userId));
}

private function addSelectIfNull(Builder $builder)
private function addSelectIfNull(Builder $builder): void
{
if (is_null($builder->getQuery()->columns)) {
if ($builder->getQuery()->columns === null) {
$builder->select([$builder->getQuery()->from . '.*']);
}
}
Expand Down

0 comments on commit c9ec290

Please sign in to comment.