launcher-host / mercurius

Real-time Messenger for Laravel
http://mercurius.launcher.host/
MIT License
326 stars 51 forks source link

Feature Request: Limiting users who can be messaged/found. #10

Closed Braunson closed 5 years ago

Braunson commented 5 years ago

Description

In my case I'm using Laravel Spark, I only want a user to be able to message users within their team/teams. Regardless of Spark it would be nice to be able to "filter" the people they search/can have a conversation with.

akazorg commented 5 years ago

Hello @Braunson, thanks for your message, we should work on this feature for sure.

We can add some options to the config\mercurius.php, whether we enable that "filter", and specify the model used in that logic, something like this:

...
    'models' => [
        'user'          => App\User::class,
        'contacts'      => Launcher\Mercurius\Models\Contacts::class,    <-- new
        'conversations' => Launcher\Mercurius\Models\Conversation::class,
        'messages'      => Launcher\Mercurius\Models\Message::class,
    ],
...
    'filter_contacts' => true,    <-- new

Thoughts? Thanks!

akazorg commented 5 years ago

Hey @Braunson, for a quick solution to have Mercurius working with Teams in a Laravel Spark app, you can try this steps. Was not tested but will give you an idea.

  1. Add Route on routes\web.php

    Route::post('/receivers', 'App\Controllers\ReceiversController@search');
  2. Create ReceiversController

    php artisan make:controller ReceiversController
  3. Apply filter logic to ReceiversController

Open App\Http\Controllers\ReceiversController and code something like this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ReceiversController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Search for receivers.
     *
     * @param Request        $request
     * @param UserRepository $userRepository
     *
     * @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
     */
    public function search(Request $request, UserRepository $userRepository): Response
    {
        if (($query = $request->input('q')) === null) {
            return response([
                'hits'  => [],
                'total' => 0,
            ]);
        }

        // Apply filter to the Users in Teams/Users
        // you will find this code on the UserRepository
        // ------------------------------------------------
        $limit = 6;
        $userFqcn = config('mercurius.models.user');
        $paginator = $userFqcn::where('name', 'LIKE', '%'.$query.'%')
            ->paginate($limit, [
                'id',
                'name',
                'avatar',
                'is_online',
            ]);
        // ------------------------------------------------

        $result = [
            'total' => $paginator->total(),
            'hits'  => $paginator->items(),
        ];

        return response($result);
    }
}

In a future release of Mercurius we will be able to load custom Controllers, using the config, and this configuration wont be required anymore.

Let us know the result. TY

OwenMelbz commented 5 years ago

Hey - random thought that might be easier....

As Laravel you can apply scope classes via service providers. Just make a scope class that adds a new condition to only show users who should be visible?

That way it’s as flexible as humanly possible as the scoping is 100% in the hands of the developer?

akazorg commented 5 years ago

Great idea @OwenMelbz, thanks for the tip.