Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added link targeting for ios and android #18

Merged
merged 8 commits into from
Dec 2, 2024
Merged
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
40 changes: 40 additions & 0 deletions .env.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
APP_ENV=testing
APP_URL=https://lua.sh
APP_KEY=base64:Zfo+2dkSaHvem5LCiZS/baYHv2Pv1vrSc0F2NaF29Ec=

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=1440

MAIL_MAILER=log

# Stripe
STRIPE_KEY=
STRIPE_SECRET=
STRIPE_WEBHOOK_SECRET=

# Sentry
SENTRY_LARAVEL_DSN=
SENTRY_TRACES_SAMPLE_RATE=1.0
SENTRY_AUTH_TOKEN=
SENTRY_ENVIRONMENT=

TELESCOPE_ENABLED=false

# Vite
VITE_STRIPE_KEY="${STRIPE_KEY}"
VITE_SENTRY_AUTH_TOKEN="${SENTRY_AUTH_TOKEN}"
VITE_SENTRY_DSN_PUBLIC="${SENTRY_LARAVEL_DSN}"
VITE_SENTRY_ENVIRONMENT="${SENTRY_ENVIRONMENT}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
4 changes: 2 additions & 2 deletions .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ jobs:
run: php artisan config:clear

- name: Run Migration
run: php artisan migrate -v
run: php artisan migrate:fresh --seed -v
env:
DB_PORT: ${{ job.services.mysql.ports['3306'] }}
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}

- name: Run PHP Unit Tests
run: vendor/bin/phpunit
run: vendor/bin/pest
env:
DB_PORT: ${{ job.services.mysql.ports['3306'] }}
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}
14 changes: 0 additions & 14 deletions .github/workflows/commitlint.yml

This file was deleted.

15 changes: 15 additions & 0 deletions app/Enums/Link/Os.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Enums\Link;

enum Os: string
{
case ANDROID = 'Android';
case IOS = 'iOS';
case WINDOWS = 'Windows';
case MACOS = 'MacOS';
case LINUX = 'Linux';
case UNKNOWN = 'Unknown OS';
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function store(CreateRequest $request)
'key' => $request->key,
'url' => $request->url,
'link' => "https://{$request->domain}/{$request->key}",
'ios' => $request->ios,
'android' => $request->android,
'utm_source' => $request->utm_source,
'utm_medium' => $request->utm_medium,
'utm_campaign' => $request->utm_campaign,
Expand All @@ -75,6 +77,8 @@ public function update($id, UpdateRequest $request)
'key' => $request->key,
'url' => $request->url,
'link' => "https://{$request->domain}/{$request->key}",
'ios' => $request->ios,
'android' => $request->android,
'utm_source' => $request->utm_source,
'utm_medium' => $request->utm_medium,
'utm_campaign' => $request->utm_campaign,
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function store(CreateRequest $request)
'domain' => $request->domain,
'key' => $key,
'url' => $request->url,
'ios' => $request->ios,
'android' => $request->android,
'link' => "https://{$request->domain}/{$key}",
'utm_source' => $request->utm_source,
'utm_medium' => $request->utm_medium,
Expand Down Expand Up @@ -108,6 +110,8 @@ public function update($id, UpdateRequest $request)
'key' => $key,
'url' => $request->url,
'link' => "https://{$request->domain}/{$key}",
'ios' => $request->ios,
'android' => $request->android,
'utm_source' => $request->utm_source,
'utm_medium' => $request->utm_medium,
'utm_campaign' => $request->utm_campaign,
Expand Down
23 changes: 22 additions & 1 deletion app/Http/Controllers/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace App\Http\Controllers;

use App\Services\UserAgentService;

use Illuminate\Support\Facades\Gate;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

use App\Enums\Link\Os;

use App\Jobs\ProcessLinkStat;

use App\Models\Link;
Expand All @@ -20,7 +24,6 @@ public function __invoke($key, Request $request): RedirectResponse
->with('workspace')
->firstOrFail();


$reachEventLimit = Gate::inspect('reached-event-limit', $link->workspace);

ProcessLinkStat::dispatchIf(
Expand All @@ -32,6 +35,24 @@ public function __invoke($key, Request $request): RedirectResponse
$request->input('qr') ? true : false,
$request->header('Referer')
);

/**
* If the link has an iOS or Android redirect URL, we need to check the user's OS
* and redirect to the appropriate URL.
*/
if ($link->ios || $link->android) {
$service = new UserAgentService();
$os = $service->getOS($request->userAgent());

if ($os === Os::IOS->value && $link->ios) {
return redirect($link->ios, 302);
}

if ($os === Os::ANDROID->value && $link->android) {
return redirect($link->android, 302);
}
}

return redirect($link->url, 302);
}
}
3 changes: 2 additions & 1 deletion app/Http/Requests/Link/CreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function rules(): array
Rule::unique('links')->where('key', $this->key)->ignore($this->route('id')),
],
'url' => ['required', 'url', 'max:255', 'min:2'],

'ios' => ['nullable', 'url', 'max:255', 'min:2'],
'android' => ['nullable', 'url', 'max:255', 'min:2'],
'utm_source' => Rule::when(
fn() => $this->utm_source,
['required', 'string', 'max:255', 'min:2']
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Link/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function rules(): array
Rule::unique('links')->where('key', $this->key)->ignore($this->route('id')),
],
'url' => ['required', 'url', 'max:255', 'min:2'],

'ios' => ['nullable', 'url', 'max:255', 'min:2'],
'android' => ['nullable', 'url', 'max:255', 'min:2'],
'utm_source' => Rule::when(
fn() => $this->utm_source,
['required', 'string', 'max:255', 'min:2']
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Resources/Api/LinkResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function toArray(Request $request): array
'domain' => $this->domain,
'key' => $this->key,
'url' => $this->url,
'ios' => $this->ios,
'android' => $this->android,
'link' => $this->link,
'utm_source' => $this->utm_source,
'utm_medium' => $this->utm_medium,
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ProcessLinkStat.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(): void
// user geo
$geo = geoip($this->ip);

$linkStat = LinkStat::create([
LinkStat::create([
'workspace_id' => $this->link->workspace_id,
'link_id' => $this->link->id,
'event' => $this->qr ? Event::QR_SCAN : Event::CLICK,
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

use App\Enums\Link\Os;

class Link extends Model
{
use HasFactory;
Expand All @@ -27,6 +29,8 @@ class Link extends Model
'key',
'url',
'link',
'ios',
'android',
'utm_source',
'utm_medium',
'utm_campaign',
Expand Down Expand Up @@ -54,6 +58,7 @@ protected function casts(): array
{
return [
'last_click' => 'datetime',
'os' => Os::class,
];
}

Expand Down
14 changes: 8 additions & 6 deletions app/Services/UserAgentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Services;

use App\Enums\Link\Os;

class UserAgentService
{
/**
Expand Down Expand Up @@ -41,11 +43,11 @@ public function getBrowser(string $userAgent): string
public function getOS(string $userAgent): string
{
$osArray = [
'Windows' => 'Windows',
'MacOS' => '(Mac_PowerPC)|(Macintosh)',
'Linux' => 'Linux',
'iOS' => 'iPhone|iPad',
'Android' => 'Android',
Os::ANDROID->value => 'Android',
Os::IOS->value => 'iPhone|iPad',
Os::WINDOWS->value => 'Windows',
Os::MACOS->value => '(Mac_PowerPC)|(Macintosh)',
Os::LINUX->value => 'Linux',
];

foreach ($osArray as $key => $pattern) {
Expand All @@ -54,7 +56,7 @@ public function getOS(string $userAgent): string
}
}

return 'Unknown OS';
return Os::UNKNOWN->value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
|
*/

'default' => env('DB_CONNECTION', 'sqlite'),
'default' => env('DB_CONNECTION', 'mysql'),

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions database/factories/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function definition(): array
'key' => $slug,
'url' => $this->faker->url,
'link' => "https://{$domain}/{$slug}",
'ios' => $this->faker->url,
'android' => $this->faker->url,
'utm_source' => $this->faker->word,
'utm_medium' => $this->faker->word,
'utm_campaign' => $this->faker->word,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('links', function (Blueprint $table) {
$table->string('ios')->after('link')->nullable();
$table->string('android')->after('ios')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('links', function (Blueprint $table) {
$table->dropColumn('ios');
$table->dropColumn('android');
});
}
};
Loading
Loading