trypost/app/Models/User.php
Paulo Castellano adfba2755e feat: Add Pinterest integration and user language preferences
Pinterest Integration:
- Add Pinterest OAuth controller and routes
- Add PinterestPublisher service with support for pins, video pins, and carousels
- Add PinterestPreview component with board selector and content type options
- Add Pinterest content types enum (Pin, VideoPin, Carousel)
- Add Pinterest to Platform enum with proper configuration
- Support sandbox mode via PINTEREST_SANDBOX env variable
- Pass platform-specific data (boards) through PlatformPreview

Language Feature:
- Add languages table with migration
- Add Language model and seeder (en-US, pt-BR)
- Add LanguageCombobox component for profile settings
- Set default language (en-US) on user registration
- Add language_id foreign key to users table

UI Improvements:
- Refactor PlatformPreview to support contentTypeOptions, meta, and platformData props
- Move content type and board selectors into platform-specific preview components

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-18 13:10:01 -03:00

209 lines
5.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\User\Persona;
use App\Enums\User\Setup;
use App\Models\Traits\HasMedia;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Laravel\Fortify\TwoFactorAuthenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use Billable, HasFactory, HasMedia, HasUuids, Notifiable, TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
'setup',
'persona',
'current_workspace_id',
'language_id',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'two_factor_secret',
'two_factor_recovery_codes',
'remember_token',
];
protected $appends = ['avatar'];
/**
* @return array{url: string, media_id: string|null}
*/
public function getAvatarAttribute(): array
{
$media = $this->getFirstMedia('avatar');
return [
'url' => $media?->url ?? $this->getFallbackAvatarUrl($this->name),
'media_id' => $media?->id,
];
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'two_factor_confirmed_at' => 'datetime',
'setup' => Setup::class,
'persona' => Persona::class,
];
}
/**
* Get workspaces owned by this user.
*/
public function workspaces(): HasMany
{
return $this->hasMany(Workspace::class);
}
/**
* Get workspaces where the user is a member (not owner).
*/
public function memberWorkspaces(): BelongsToMany
{
return $this->belongsToMany(Workspace::class)
->withPivot('role')
->withTimestamps();
}
/**
* Get the user's current workspace.
*/
public function currentWorkspace(): BelongsTo
{
return $this->belongsTo(Workspace::class, 'current_workspace_id');
}
/**
* Get the user's language.
*/
public function language(): BelongsTo
{
return $this->belongsTo(Language::class);
}
/**
* Switch to a different workspace.
*/
public function switchWorkspace(Workspace $workspace): void
{
$this->update(['current_workspace_id' => $workspace->id]);
}
/**
* Check if user belongs to a workspace (owner or member).
*/
public function belongsToWorkspace(Workspace $workspace): bool
{
return $this->workspaces()->where('id', $workspace->id)->exists()
|| $this->memberWorkspaces()->where('workspaces.id', $workspace->id)->exists();
}
/**
* Get the count of workspaces the user owns.
*/
public function ownedWorkspacesCount(): int
{
return $this->workspaces()->count();
}
/**
* Check if user has an active subscription.
*/
public function hasActiveSubscription(): bool
{
return $this->subscribed('default');
}
/**
* Check if user has ever had a subscription (for trial eligibility).
*/
public function hasEverSubscribed(): bool
{
return $this->subscriptions()->exists();
}
/**
* Check if user can create more workspaces based on subscription.
*/
public function canCreateWorkspace(): bool
{
// If no subscription, allow first workspace free (or require subscription)
if (! $this->hasActiveSubscription()) {
return $this->ownedWorkspacesCount() === 0;
}
$subscription = $this->subscription('default');
return $subscription && $this->ownedWorkspacesCount() < $subscription->quantity;
}
/**
* Increment workspace quantity on subscription.
*/
public function incrementWorkspaceQuantity(): void
{
if ($this->hasActiveSubscription()) {
$this->subscription('default')->incrementQuantity();
}
}
/**
* Decrement workspace quantity on subscription.
*/
public function decrementWorkspaceQuantity(): void
{
if ($this->hasActiveSubscription()) {
$subscription = $this->subscription('default');
if ($subscription->quantity > 1) {
$subscription->decrementQuantity();
}
}
}
/**
* Sync subscription quantity with actual workspace count.
*/
public function syncWorkspaceQuantity(): void
{
if ($this->hasActiveSubscription()) {
$count = $this->ownedWorkspacesCount();
if ($count > 0) {
$this->subscription('default')->updateQuantity($count);
}
}
}
}