Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Console/Commands/IngestJobFeedsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace App\Console\Commands;

use App\Models\Job;
use App\Feed\FeedItem;
use App\Jobs\ScrapeJob;
use Illuminate\Console\Command;
use App\Actions\DiscoverFeedItems;
use App\Feed\FeedItem;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;

Expand Down Expand Up @@ -70,7 +70,7 @@ public function handle() : void
);
}

$this->info("Queued " . $toQueue->count() . " new item(s) from '$name'.");
$this->info('Queued ' . $toQueue->count() . " new item(s) from '$name'.");
});
}

Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/LinkResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public static function table(Table $table) : Table
Action::make('decline')
->schema([
Textarea::make('reason')
->nullable(),
->nullable(),
])
->action(function (Link $record, array $data) {
$record->decline($data['reason']);
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/Jobs/ShowJobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
use App\Models\Job;
use Illuminate\View\View;
use App\Http\Controllers\Controller;
use App\Support\Schema\JobPostingSchema;

class ShowJobController extends Controller
{
public function __invoke(Job $job) : View
{
return view('jobs.show', compact('job'));
return view('jobs.show', [
'job' => $job,
'jobPostingSchema' => JobPostingSchema::fromJob($job),
]);
}
}
289 changes: 289 additions & 0 deletions app/Support/Schema/JobPostingSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
<?php

namespace App\Support\Schema;

use App\Models\Job;

use function collect;

use Illuminate\Support\Str;

class JobPostingSchema
{
public static function fromJob(Job $job) : array
{
$locations = collect($job->locations ?? [])
->filter()
->values()
->all();

$isRemote = self::isRemote($job, $locations);

$jobLocations = self::buildJobLocations($locations, $isRemote);
$applicantLocationRequirements = self::buildApplicantLocationRequirements($locations, $isRemote);

$validThrough = optional($job->created_at)
?->copy()
->addDays(30)
->toIso8601String();

$schema = [
'@context' => 'https://schema.org/',
'@type' => 'JobPosting',
'title' => $job->title,
'description' => $job->description,
'identifier' => [
'@type' => 'PropertyValue',
'name' => $job->company->name,
'value' => (string) $job->id,
],
'datePosted' => optional($job->created_at)?->toIso8601String(),
'validThrough' => $validThrough,
'employmentType' => 'FULL_TIME',
'hiringOrganization' => [
'@type' => 'Organization',
'name' => $job->company->name,
'sameAs' => $job->company->url,
'logo' => $job->company->logo,
],
'jobLocationType' => $isRemote ? 'TELECOMMUTE' : null,
'jobLocation' => $jobLocations,
'applicantLocationRequirements' => $applicantLocationRequirements,
'baseSalary' => [
'@type' => 'MonetaryAmount',
'currency' => $job->currency ?? 'USD',
'value' => [
'@type' => 'QuantitativeValue',
'minValue' => $job->min_salary,
'maxValue' => $job->max_salary,
'unitText' => 'YEAR',
],
],
'directApply' => false,
];

return array_filter(
$schema,
fn ($value) => null !== $value && ([] !== $value || is_bool($value))
);
}

/**
* @param array<int, string> $locations
* @return array<int, array<string, mixed>>|array<string, mixed>
*/
private static function buildJobLocations(array $locations, bool $isRemote) : array
{
if ([] === $locations) {
if ($isRemote) {
return self::remotePlace();
}

return [];
}

$places = collect($locations)
->filter()
->map(fn (string $location) => self::buildPlaceFromLocation($location, $isRemote))
->filter()
->values();

if ($places->isEmpty() && $isRemote) {
return self::remotePlace();
}

return 1 === $places->count()
? $places->first()
: $places->all();
}

/**
* @param array<int, string> $locations
* @return array<int, array<string, string>>|array<string, string>
*/
private static function buildApplicantLocationRequirements(array $locations, bool $isRemote) : array
{
$countries = collect($locations)
->map(fn (string $location) => self::extractCountry($location))
->filter()
->unique()
->values();

if ($countries->isEmpty()) {
if ($isRemote) {
return self::worldwideApplicantRequirement();
}

return [];
}

if (1 === $countries->count()) {
return self::countryApplicantRequirement($countries->first());
}

return $countries
->map(fn (string $country) => self::countryApplicantRequirement($country))
->all();
}

private static function buildPlaceFromLocation(string $location, bool $isRemote) : ?array
{
[$country, $locality, $region] = self::extractLocationParts($location);

if (null === $country && null === $locality && null === $region) {
if ($isRemote || self::containsRemoteKeyword($location)) {
return self::remotePlace($location);
}

return null;
}

$address = [
'@type' => 'PostalAddress',
];

if (null !== $locality) {
$address['addressLocality'] = $locality;
}

if (null !== $region) {
$address['addressRegion'] = $region;
}

$address['addressCountry'] = $country ?? 'Worldwide';

return [
'@type' => 'Place',
'name' => '' !== trim($location) ? trim($location) : ($locality ?? $country ?? 'Remote'),
'address' => $address,
];
}

/**
* @return array{0: string|null, 1: string|null, 2: string|null}
*/
private static function extractLocationParts(string $location) : array
{
$segments = array_values(
array_filter(
array_map(
fn (string $segment) => self::sanitizeSegment($segment),
explode(',', $location)
),
fn (string $segment) => '' !== $segment
)
);

if ([] === $segments) {
return [null, null, null];
}

$country = array_pop($segments);
$locality = [] !== $segments ? array_shift($segments) : null;
$region = [] !== $segments ? implode(', ', $segments) : null;

return [$country, $locality, $region];
}

private static function extractCountry(string $location) : ?string
{
[$country] = self::extractLocationParts($location);

return $country;
}

private static function sanitizeSegment(string $segment) : string
{
$sanitized = trim($segment);

$sanitized = preg_replace_callback(
'/\(([^)]*)\)/',
fn (array $matches) => self::containsRemoteKeyword($matches[1]) ? '' : $matches[0],
$sanitized
);

foreach (self::remoteKeywords() as $keyword) {
$sanitized = preg_replace('/\b' . preg_quote($keyword, '/') . '\b/i', '', $sanitized);
}

$sanitized = preg_replace('/[-–—]/', ' ', $sanitized);

$sanitized = preg_replace('/\s{2,}/', ' ', (string) $sanitized);

return trim((string) $sanitized, " \t\n\r\0\x0B,.-");
}

private static function remotePlace(?string $label = null) : array
{
return [
'@type' => 'Place',
'name' => self::resolveRemoteLabel($label),
'address' => [
'@type' => 'PostalAddress',
'addressCountry' => 'Worldwide',
],
];
}

private static function resolveRemoteLabel(?string $label) : string
{
$resolved = trim((string) $label);

return '' !== $resolved ? $resolved : 'Remote';
}

private static function isRemote(Job $job, array $locations) : bool
{
$setting = trim((string) $job->setting);

if ('' !== $setting) {
return self::containsRemoteKeyword($setting);
}

return collect($locations)->contains(fn (string $location) => self::containsRemoteKeyword($location));
}

private static function containsRemoteKeyword(string $value) : bool
{
$haystack = Str::of($value)->lower();

foreach (self::remoteKeywords() as $keyword) {
if ($haystack->contains($keyword)) {
return true;
}
}

return false;
}

/**
* @return array<int, string>
*/
private static function remoteKeywords() : array
{
return [
'remote',
'telecommute',
'distributed',
'anywhere',
'worldwide',
'global',
];
}

private static function worldwideApplicantRequirement() : array
{
return [
'@type' => 'Country',
'name' => 'Worldwide',
];
}

private static function countryApplicantRequirement(string $country) : array
{
return [
'@type' => 'Country',
'name' => $country,
];
}
}
36 changes: 1 addition & 35 deletions resources/views/jobs/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,6 @@
</article>

<script type="application/ld+json">
{
"@@context": "https://schema.org/",
"@@type": "JobPosting",
"title": @json($job->title),
"description": @json($job->description),
"identifier": {
"@@type": "PropertyValue",
"name": @json($job->company->name),
"value": @json((string) $job->id)
},
"datePosted": @json(optional($job->created_at)->toIso8601String()),
"employmentType": "FULL_TIME",
"hiringOrganization": {
"@@type": "Organization",
"name": @json($job->company->name),
"sameAs": @json($job->company->url),
"logo": @json($job->company->logo)
},
"jobLocationType": @json($job->setting === 'fully-remote' ? 'TELECOMMUTE' : null),
"jobLocation": {
"@@type": "Place",
"name": @json(collect($job->locations)->first())
},
"baseSalary": {
"@@type": "MonetaryAmount",
"currency": @json($job->currency ?? 'USD'),
"value": {
"@@type": "QuantitativeValue",
"minValue": @json($job->min_salary),
"maxValue": @json($job->max_salary),
"unitText": "YEAR"
}
},
"directApply": false
}
{!! json_encode($jobPostingSchema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) !!}
</script>
</x-app>
Loading