RTippin / messenger

Laravel messenger. A full messenger suite for your new / existing laravel app! Private and group threads between multiple models, with real-time messaging, reactions, attachments, calling, chat bots, and more!
https://tippindev.com
MIT License
416 stars 84 forks source link

Messenger provider not set or compatible #5

Closed tlartaud2 closed 3 years ago

tlartaud2 commented 3 years ago

Hi there,

I've tried your messenger-demo app and it looks amazing! I am now trying to integrate it into my own Laravel app and I'm getting stuck with the following 403 error: Messenger provider not set or compatible. I got this message after I ran php artisan migrate.

So I then edited the config file to have the following config, but it didn't work either. Should I have run php artisan migrate after editing this?

'providers' => [
        'user' => [
            'model' => Common\Auth\BaseUser::class,
            'searchable' => true,
            'friendable' => true,
            'devices' => false,
            'default_avatar' => public_path('vendor/messenger/images/users.png'),
            'provider_interactions' => [
                'can_message' => true,
                'can_search' => true,
                'can_friend' => true,
            ],
        ],
    ],

My user model looks like this

<?php namespace Common\Auth;

use App\User;
use Carbon\Carbon;
use Common\Auth\Permissions\Permission;
use Common\Auth\Permissions\Traits\HasPermissionsRelation;
use Common\Auth\Roles\Role;
use Common\Auth\Traits\HasAvatarAttribute;
use Common\Auth\Traits\HasDisplayNameAttribute;
use Common\Billing\Billable;
use Common\Billing\BillingPlan;
use Common\Files\FileEntry;
use Common\Files\FileEntryPivot;
use Common\Files\Traits\SetsAvailableSpaceAttribute;
use Common\Notifications\NotificationSubscription;
use Common\Settings\Settings;
use Eloquent;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
use Illuminate\Notifications\Notifiable;
use Arr;
use Storage;
use RTippin\Messenger\Contracts\MessengerProvider;
use RTippin\Messenger\Contracts\Searchable;
use RTippin\Messenger\Traits\Messageable;
use RTippin\Messenger\Traits\Search;

/**
 * @property int $id
 * @property string|null $username
 * @property string|null $first_name
 * @property string|null $last_name
 * @property string|null $gender
 * @property-read Collection|Permission[] $permissions
 * @property string $email
 * @property string $password
 * @property integer|null $available_space
 * @property string|null $remember_token
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property int $stripe_active
 * @property string|null $stripe_id
 * @property string|null $stripe_subscription
 * @property string|null $stripe_plan
 * @property string|null $last_four
 * @property string|null $trial_ends_at
 * @property string|null $subscription_ends_at
 * @property string $avatar
 * @property-read string $display_name
 * @property-read mixed $followers_count
 * @property-read bool $has_password
 * @property-read Collection|Role[] $roles
 * @property-read DatabaseNotificationCollection|DatabaseNotification[] $notifications
 * @property-read NotificationSubscription[]|Collection $notificationSubscriptions
 * @method BaseUser compact()
 * @method Builder whereNeedsNotificationFor(string $eventId)
 * @mixin Eloquent
 */
abstract class BaseUser extends Authenticatable implements MessengerProvider, Searchable
{
    use Notifiable, Billable, SetsAvailableSpaceAttribute, HasPermissionsRelation, HasAvatarAttribute, HasDisplayNameAttribute;
    use Messageable, Search; //comes with messenger package

    // prevent avatar from being set along with other user details
    protected $guarded = ['id', 'avatar'];
    protected $hidden = ['password', 'remember_token', 'pivot', 'legacy_permissions', 'api_token'];
    protected $casts = [
        'id' => 'integer',
        'available_space' => 'integer',
        'email_verified_at' => 'datetime',
    ];
    protected $appends = ['display_name', 'has_password'];
    protected $billingEnabled = true;
    protected $gravatarSize;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->billingEnabled = app(Settings::class)->get('billing.enable');
    }

    // ...

Any chance that you can guide me to make this compatible?

Regards.

RTippin commented 3 years ago

I assume you get the provider error when you try to run the site and not during migration?

The only thing that should be set before migrating my package is the 'provider_uuids' => false|true, as during migration that determines the owner_id/type morphs columns to use Char 36 (UUID) or unsignedBigInteger.

As for your setup currently, the only thing I see that may be an issue is you setting an abstract class as the model. I have never tested using it in that way, but if you have one main abstract class, I assume you have a User / other models that extend that base class. Instead of setting the abstract class in my config, can you try setting it to your parent User/Model that extends the abstract.

One other thing to note, my default SetMessengerProvider middleware takes the current auth user from request to set as the provider.

    protected function setProvider(Request $request): void
    {
        if ($request->user()) {
            $this->messenger->setProvider(
                $request->user()
            );
        }
    }

If you need more control of how to grab the model you need other than just $request->user(), you can make your own middleware, as long as Messenger::setProvider($model) is called before the request hits any of my controllers.

I am online on my demo website as well, so feel free to hop on and message me there should you want a quicker response.