Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Oct 6, 2023
1 parent 9c2eb9c commit 7d05fc5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 145 deletions.
154 changes: 31 additions & 123 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,175 +8,83 @@
use Coyote\Http\Resources\MicroblogCollection;
use Coyote\Microblog;
use Coyote\Repositories\Contracts\ActivityRepositoryInterface as ActivityRepository;
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface as MicroblogRepository;
use Coyote\Repositories\Contracts\ReputationRepositoryInterface as ReputationRepository;
use Coyote\Repositories\Contracts\TopicRepositoryInterface as TopicRepository;
use Coyote\Repositories\Contracts\WikiRepositoryInterface as WikiRepository;
use Coyote\Repositories\Criteria\Forum\OnlyThoseWithAccess as OnlyThoseForumsWithAccess;
use Coyote\Repositories\Criteria\Forum\SkipHiddenCategories;
use Coyote\Repositories\Criteria\Topic\OnlyThoseWithAccess as OnlyThoseTopicsWithAccess;
use Coyote\Repositories\Criteria\Forum\OnlyThoseWithAccess as OnlyThoseForumsWithAccess;
use Coyote\Services\Flags;
use Coyote\Services\Microblogs\Builder;
use Coyote\Services\Session\Renderer;
use Coyote\Services\Widgets\Patronage;
use Coyote\Services\Widgets\WhatsNew;
use Illuminate\Contracts\Cache;
use Illuminate\View\View;

class HomeController extends Controller
{
/**
* @var MicroblogRepository
*/
protected $microblog;

/**
* @var ReputationRepository
*/
protected $reputation;

/**
* @var ActivityRepository
*/
protected $activity;

/**
* @var TopicRepository
*/
protected $topic;

/**
* @var WikiRepository
*/
protected $wiki;

/**
* @param MicroblogRepository $microblog
* @param ReputationRepository $reputation
* @param ActivityRepository $activity
* @param TopicRepository $topic
* @param WikiRepository $wiki
*/
public function __construct(
MicroblogRepository $microblog,
ReputationRepository $reputation,
ActivityRepository $activity,
TopicRepository $topic,
WikiRepository $wiki
) {
private ReputationRepository $reputation,
private ActivityRepository $activity,
private TopicRepository $topic,
)
{
parent::__construct();

$this->microblog = $microblog;
$this->reputation = $reputation;
$this->activity = $activity;
$this->topic = $topic;
$this->wiki = $wiki;
}

/**
* @return \Illuminate\View\View
*/
public function index()
public function index(): View
{
$result = [];
$reflection = new \ReflectionClass($this);

$cache = $this->getCacheFactory();

$cache = app(Cache\Repository::class);
$this->topic->pushCriteria(new OnlyThoseTopicsWithAccess());
$this->topic->pushCriteria(new SkipHiddenCategories($this->userId));

foreach ($reflection->getMethods(\ReflectionMethod::IS_PRIVATE) as $method) {
$method = $method->name;
$snake = snake_case($method);

if (substr($snake, 0, 3) === 'get') {
$name = substr($snake, 4);

if (in_array($name, ['reputation'])) {
$result[$name] = $cache->remember('homepage:' . $name, 30 * 60, function () use ($method) {
return $this->$method();
});
} else {
$result[$name] = $this->$method();
}
}
}

return $this->view('home', $result)
return $this->view('home', [
'flags' => $this->flags(),
'microblogs' => $this->getMicroblogs(),
'interesting' => $this->topic->interesting(),
'newest' => $this->topic->newest(),
'viewers' => $this->getViewers(),
'activities' => $this->getActivities(),
'reputation' => $cache->remember('homepage:reputation', 30 * 60, fn() => [
'month' => $this->reputation->monthly(),
'year' => $this->reputation->yearly(),
'total' => $this->reputation->total()
])
])
->with('settings', $this->getSettings())
->with('flags', $this->flags())
->with('whats_new', resolve(WhatsNew::class)->render())
->with('patronage', resolve(Patronage::class)->render());
}

/**
* @return array
*/
private function getReputation()
{
return [
'month' => $this->reputation->monthly(),
'year' => $this->reputation->yearly(),
'total' => $this->reputation->total()
];
}

/**
* @return array
*/
private function getMicroblogs()
private function getMicroblogs(): array
{
/** @var Builder $builder */
$builder = app(Builder::class);

$microblogs = $builder->orderByScore()->popular();

MicroblogResource::withoutWrapping();

return (new MicroblogCollection($microblogs))->resolve($this->request);
}

/**
* @return mixed
*/
private function getNewest()
{
return $this->topic->newest();
}

/**
* @return mixed
*/
private function getInteresting()
{
return $this->topic->interesting();
}

/**
* @return array
*/
private function getActivities()
private function getActivities(): array
{
$this->activity->pushCriteria(new OnlyThoseForumsWithAccess($this->auth));
$this->activity->pushCriteria(new SkipHiddenCategories($this->userId));

$result = $this->activity->latest(20);

return ActivityResource::collection($result)->toArray($this->request);
}

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
private function getViewers()
private function getViewers(): View
{
/** @var Renderer $viewers */
$viewers = app(Renderer::class);
return $viewers->render();
}

private function flags()
private function flags(): array
{
$flags = $flags = resolve(Flags::class)->fromModels([Microblog::class])->permission('microblog-delete')->get();
$flags = app(Flags::class)
->fromModels([Microblog::class])
->permission('microblog-delete')
->get();

return FlagResource::collection($flags)->toArray($this->request);
}
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Resources/MicroblogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Coyote\Http\Resources;

use Coyote\Microblog;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractPaginator;

class MicroblogCollection extends ResourceCollection
{
/**
* DO NOT REMOVE! This will preserver keys from being filtered in data
* DO NOT REMOVE! This will preserve keys from being filtered in data
*
* @var bool
*/
Expand Down
12 changes: 6 additions & 6 deletions app/Models/Reputation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Reputation extends Model
const WIKI_RATE = 8;
const GUIDE = 9;

const CHINESE = 1;
const CHINESE = 1;
const USER_MENTION = 10;
const VOTE = 50;
const PROFILE_LINK = 50;
const SIG_LINK = 50;
const CREATE_TAGS = 300;
const SHORT_TITLE = 1000;
const NO_ADS = 10000;
const EDIT_POST = 10000;
const SIG_LINK = 50;
const CREATE_TAGS = 300;
const SHORT_TITLE = 1000;
const NO_ADS = 10000;
const EDIT_POST = 10000;

/**
* The attributes that are mass assignable.
Expand Down
11 changes: 1 addition & 10 deletions app/Repositories/Criteria/Forum/SkipHiddenCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@

class SkipHiddenCategories extends Criteria
{
/**
* @var int|null
*/
protected $userId;

/**
* @param int|null $userId
*/
public function __construct(?int $userId)
public function __construct(private ?int $userId)
{
$this->userId = $userId;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Repositories/Eloquent/ReputationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Coyote\Repositories\Eloquent;

use Carbon\Carbon;
use Coyote\User;
use Coyote\Repositories\Contracts\ReputationRepositoryInterface;
use Coyote\Reputation\Type;
use Coyote\User;

class ReputationRepository extends Repository implements ReputationRepositoryInterface
{
Expand All @@ -14,7 +14,7 @@ class ReputationRepository extends Repository implements ReputationRepositoryInt
*/
public function model()
{
return 'Coyote\Reputation';
return \Coyote\Reputation::class;
}

/**
Expand Down
13 changes: 11 additions & 2 deletions resources/views/home.twig
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,21 @@
</div>

<div class="media-body">
<span class="mb-1 d-block"><a data-user-id="{{ item.id }}" href="{{ route('profile', [item.id]) }}" class="text-body">{{ item.name }}</a></span>
<span class="mb-1 d-block">
<a data-user-id="{{ item.id }}" href="{{ route('profile', [item.id]) }}" class="text-body">
{{ item.name }}
</a>
</span>

<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="{{ item.percentage|round }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ item.percentage|round }}%;"></div>

<span class="rep"><strong>{{ item.reputation|number_format(0, '', ' ') }}</strong> <small>{{ declination(user.reputation, ['punkt', 'punkty', 'punktów'], true) }}</small></span>
<span class="rep">
<strong>{{ item.reputation|number_format(0, '', ' ') }}</strong>
<small>
{{ declination(user.reputation, ['punkt', 'punkty', 'punktów'], true) }}
</small>
</span>
</div>
</div>
</div>
Expand Down

0 comments on commit 7d05fc5

Please sign in to comment.