Skip to content

Commit

Permalink
Merge branch 'master' into feature/search-update-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Apr 12, 2018
2 parents 9804142 + a12839e commit e1fd1af
Show file tree
Hide file tree
Showing 190 changed files with 1,612 additions and 769 deletions.
76 changes: 24 additions & 52 deletions app/Console/Commands/LocaleCheck.php
Expand Up @@ -20,10 +20,13 @@

namespace App\Console\Commands;

use File;
use Illuminate\Console\Command;

class LocaleCheck extends Command
{
const FILE_EXTENSION = 'php';

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -58,9 +61,8 @@ public function handle()
}
}

$fallbackFiles = $this->files($fallbackLocale);
$fallbackHeads = $this->heads($fallbackLocale, $fallbackFiles);
$fallbackAll = $this->everything($fallbackLocale, $fallbackHeads);
$fallbackNamespaces = $this->namespaces($fallbackLocale);
$fallbackAll = $this->everything($fallbackLocale, $fallbackNamespaces);

foreach ($locales as $locale) {
if ($locale === $fallbackLocale) {
Expand All @@ -70,11 +72,10 @@ public function handle()
$this->info("Checking locale {$locale}");
$this->info('');

$files = $this->files($locale);
$heads = $this->heads($locale, $files);
$all = $this->everything($locale, $heads);
$namespaces = $this->namespaces($locale);
$all = $this->everything($locale, $namespaces);

$this->checkExtraHeads($locale, $fallbackHeads, $heads);
$this->checkExtraFiles($locale, $fallbackNamespaces, $namespaces);
$this->info('');

$this->checkKeys($locale, $fallbackAll, $all);
Expand All @@ -90,14 +91,15 @@ public function basePath($locale)
return resource_path("lang/{$locale}");
}

public function checkExtraHeads($locale, $baseHeads, $targetHeads)
public function checkExtraFiles($locale, $baseNamespaces, $targetNamespaces)
{
$extras = array_diff($targetHeads, $baseHeads);
$extras = array_diff($targetNamespaces, $baseNamespaces);

if (count($extras) > 0) {
$this->warn('Extraneous files:');
foreach ($extras as $extra) {
$this->warn("- {$locale}/{$extra}.php");
$message = sprintf('- %s/%s.%s', $locale, $extra, static::FILE_EXTENSION);
$this->warn($message);
}
} else {
$this->info("There's no extraneous files in {$locale}.");
Expand Down Expand Up @@ -132,15 +134,15 @@ public function checkKeys($locale, $baseAll, $targetAll)
}
}

public function everything($locale, $heads)
public function everything($locale, $namespaces)
{
$entries = [];

foreach ($heads as $head) {
$baseEntries = trans($head, [], $locale);
foreach ($namespaces as $namespace) {
$baseEntries = trans($namespace, [], $locale);
// trans returns plain string if the file is empty.
if (is_array($baseEntries)) {
$entries = array_merge($entries, array_dot([$head => $baseEntries]));
$entries = array_merge($entries, array_dot([$namespace => $baseEntries]));
}
}

Expand All @@ -149,50 +151,20 @@ public function everything($locale, $heads)
return $entries;
}

public function files($locale, $path = null)
public function namespaces($locale)
{
if ($path === null) {
$path = $this->basePath($locale);
}

$entries = scandir($path);
$files = [];
$namespaces = [];
$trimEnd = -(1 + strlen(static::FILE_EXTENSION));

foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
foreach (File::allFiles($this->basePath($locale)) as $file) {
if ($file->getExtension() !== static::FILE_EXTENSION) {
continue;
}

$entryPath = $path.'/'.$entry;

if (is_link($entryPath)) {
continue;
}

if (is_dir($entryPath)) {
$files = array_merge($files, $this->files($locale, $entryPath));
}

if (is_file($entryPath) && ends_with($entryPath, '.php')) {
$files[] = $entryPath;
}
}

return $files;
}

public function heads($locale, $files)
{
$basePath = $this->basePath($locale);
$trimStart = strlen($basePath) + 1; // +1 is trailing slash
$trimEnd = -strlen('.php');

$heads = [];

foreach ($files as $file) {
$heads[] = substr($file, $trimStart, $trimEnd);
$relativePath = $file->getRelativePathname();
$namespaces[] = substr($relativePath, 0, $trimEnd);
}

return $heads;
return $namespaces;
}
}
15 changes: 12 additions & 3 deletions app/Http/Controllers/ArtistsController.php
Expand Up @@ -67,21 +67,30 @@ public function show($id)
// should probably move services to a separate model if the number increases further (HA HA HA)
$links = [];

if ($artist->user_id) {
$links[] = [
'title' => trans('artist.links.osu'),
'url' => route('users.show', $artist->user_id),
'icon' => 'fas fa-user',
'class' => 'osu',
];
}

if ($artist->website) {
$links[] = [
'title' => trans('artist.links.site'),
'url' => $artist->website,
'icon' => 'globe',
'icon' => 'fas fa-link',
'class' => 'website',
];
}

foreach (['twitter', 'facebook', 'bandcamp', 'patreon', 'soundcloud', 'youtube'] as $service) {
foreach (['twitter', 'facebook', 'spotify', 'bandcamp', 'patreon', 'soundcloud', 'youtube'] as $service) {
if ($artist->$service) {
$links[] = [
'title' => $service === 'youtube' ? 'YouTube' : ucwords($service),
'url' => $artist->$service,
'icon' => $service === 'patreon' ? "extra-social-$service" : $service,
'icon' => "fab fa-{$service}",
'class' => $service,
];
}
Expand Down
91 changes: 57 additions & 34 deletions app/Http/Controllers/BeatmapDiscussionPostsController.php
Expand Up @@ -95,28 +95,7 @@ public function restore($id)

public function store()
{
$discussion = BeatmapDiscussion::findOrNew(Request::input('beatmap_discussion_id'));
$beatmapset = null;

if ($discussion->exists) {
$discussionFilters = ['resolved:bool'];
} else {
$beatmapset = Beatmapset
::where('discussion_enabled', true)
->findOrFail(Request::input('beatmapset_id'));

$discussion->beatmapset_id = $beatmapset->getKey();
$discussion->user_id = Auth::user()->user_id;
$discussion->resolved = false;
$discussionFilters = [
'beatmap_id:int',
'message_type',
'timestamp:int',
];
}

$discussionParams = get_params(Request::all(), 'beatmap_discussion', $discussionFilters);
$discussion->fill($discussionParams);
$discussion = $this->prepareDiscussion(request());

priv_check('BeatmapDiscussionPostStore', $discussion)->ensureCan();

Expand All @@ -125,14 +104,24 @@ public function store()
$posts = [new BeatmapDiscussionPost($postParams)];
$events = [];

$resetNominations = !$discussion->exists &&
$beatmapset->isPending() &&
$beatmapset->hasNominations() &&
$discussion->message_type === 'problem' &&
priv_check('BeatmapsetResetNominations', $beatmapset)->can();
$resetNominations = false;
$disqualify = false;

if (!$discussion->exists && $discussion->message_type === 'problem') {
$resetNominations = $discussion->beatmapset->isPending() &&
$discussion->beatmapset->hasNominations() &&
priv_check('BeatmapsetResetNominations', $discussion->beatmapset)->can();

if ($resetNominations) {
$events[] = BeatmapsetEvent::NOMINATION_RESET;
if ($resetNominations) {
$events[] = BeatmapsetEvent::NOMINATION_RESET;
} else {
$disqualify = $discussion->beatmapset->isQualified() &&
priv_check('BeatmapsetDisqualify', $discussion->beatmapset)->can();

if ($disqualify) {
$events[] = BeatmapsetEvent::DISQUALIFY;
}
}
}

if ($discussion->exists && $discussion->isDirty('resolved')) {
Expand All @@ -142,7 +131,7 @@ public function store()
}

try {
DB::transaction(function () use ($posts, $discussion, $events, $resetNominations) {
DB::transaction(function () use ($posts, $discussion, $events, $resetNominations, $disqualify) {
$discussion->saveOrExplode();

foreach ($posts as $post) {
Expand All @@ -155,16 +144,19 @@ public function store()
BeatmapsetEvent::log($event, Auth::user(), $posts[0])->saveOrExplode();
}

if ($disqualify) {
$discussion->beatmapset->setApproved('pending', Auth::user());
}

// feels like a controller shouldn't be calling refreshCache on a model?
if ($resetNominations) {
if ($resetNominations || $disqualify) {
$discussion->beatmapset->refreshCache();
}
});
} catch (ModelNotSavedException $_e) {
return error_popup(trans('beatmaps.discussion-posts.store.error'));
}

$postIds = array_pluck($posts, 'id');
$beatmapset = $discussion->beatmapset;

BeatmapsetWatch::markRead($beatmapset, Auth::user());
Expand All @@ -174,8 +166,8 @@ public function store()
]))->delayedDispatch();

return [
'beatmapset' => $posts[0]->beatmapset->defaultDiscussionJson(),
'beatmap_discussion_post_ids' => $postIds,
'beatmapset' => $beatmapset->defaultDiscussionJson(),
'beatmap_discussion_post_ids' => array_pluck($posts, 'id'),
'beatmap_discussion_id' => $discussion->id,
];
}
Expand All @@ -199,4 +191,35 @@ public function update($id)
return error_popup(presence($message, trans('beatmaps.discussion-posts.store.error')));
}
}

private function prepareDiscussion($request)
{
$discussionId = get_int($request['beatmap_discussion_id']);

if ($discussionId === null) {
$beatmapset = Beatmapset
::where('discussion_enabled', true)
->findOrFail($request['beatmapset_id']);

$discussion = new BeatmapDiscussion([
'beatmapset_id' => $beatmapset->getKey(),
'user_id' => Auth::user()->getKey(),
'resolved' => false,
]);

$discussionFilters = [
'beatmap_id:int',
'message_type',
'timestamp:int',
];
} else {
$discussion = BeatmapDiscussion::findOrFail($discussionId);
$discussionFilters = ['resolved:bool'];
}

$discussionParams = get_params($request, 'beatmap_discussion', $discussionFilters);
$discussion->fill($discussionParams);

return $discussion;
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/BeatmapsetWatchesController.php
Expand Up @@ -40,7 +40,7 @@ public function index()
{
return view('beatmapset_watches.index')
->with([
'watches' => Auth::user()->beatmapsetWatches()->has('beatmapset')->paginate(50),
'watches' => Auth::user()->beatmapsetWatches()->visible()->paginate(50),
]);
}

Expand Down
19 changes: 0 additions & 19 deletions app/Http/Controllers/BeatmapsetsController.php
Expand Up @@ -236,25 +236,6 @@ public function nominate($id)
return $beatmapset->defaultDiscussionJson();
}

public function disqualify($id)
{
$beatmapset = Beatmapset::findOrFail($id);

priv_check('BeatmapsetDisqualify', $beatmapset)->ensureCan();

if (!$beatmapset->disqualify(Auth::user(), Request::input('comment'))) {
return error_popup(trans('beatmaps.nominations.incorrect_state'));
}

BeatmapsetWatch::markRead($beatmapset, Auth::user());
(new NotifyBeatmapsetUpdate([
'user' => Auth::user(),
'beatmapset' => $beatmapset,
]))->delayedDispatch();

return $beatmapset->defaultDiscussionJson();
}

public function update($id)
{
$beatmapset = Beatmapset::findOrFail($id);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Forum/TopicsController.php
Expand Up @@ -247,7 +247,7 @@ public function show($id)
->with('topic')
->with('user.rank')
->with('user.country')
->with('user.supports')
->with('user.supporterTags')
->get()
->sortBy('post_id');

Expand Down

0 comments on commit e1fd1af

Please sign in to comment.