# Contact visibility

> Use existing Laravel app roles to control who can discover contacts, start direct chats, and invite people to groups.

[Web documentation](https://threads.teamkrope.com/docs/contact-visibility/) · Inner Threads v1.3.0

The role filter is available in Inner Threads v1.3.0 and later. It defaults to off and uses the roles already stored by your Laravel app. Setup requires no Inner Threads role migrations.

## Connect your existing roles

Implement `TeamKrope\InnerThreads\Contracts\ContactRoleResolver` in your host app. It receives the current request context and a batch of user IDs. Return a list of role keys for each known ID, including the viewer when requested. For an app with one existing `role` column, save this example as `app/Chat/ChatRoles.php`.

```php
<?php

namespace App\Chat;

use App\Models\User;
use TeamKrope\InnerThreads\Contracts\ContactRoleResolver;
use TeamKrope\InnerThreads\Support\Context;

final class ChatRoles implements ContactRoleResolver
{
    public function roles(Context $context, array $userIds): array
    {
        return User::query()->whereKey($userIds)->whereNotNull('role')
            ->pluck('role', 'id')
            ->map(fn (string $role): array => [$role])
            ->all();
    }
}
```

For workspace-scoped roles, query your app's role assignments for `$context->workspaceKey`. Use stable role keys or IDs from the host. If customers can rename their own roles, the resolver can derive a stable key such as `client-admin` from the account's existing permissions. Use the host's [route middleware](https://threads.teamkrope.com/docs/configuration/#routing-and-identity) to control who can use messaging at all.

## Enable contact rules

If the host has no published configuration, run `php artisan vendor:publish --tag=inner-threads-config`. Add this block to the existing return array in `config/inner-threads.php` and replace the role keys with the ones your resolver returns.

```php
'visibility' => [
    'enabled' => true,
    'role_resolver' => \App\Chat\ChatRoles::class,
    'rules' => [
        'super-admin' => ['*'],
        'support' => ['*'],
        'client-admin' => ['super-admin', 'client-admin', 'solicitor', 'case-worker'],
        'solicitor' => ['solicitor'],
        'case-worker' => ['solicitor', 'client-admin'],
    ],
],
```

| Rule | Effect |
| --- | --- |
| `'case-worker' => ['solicitor', 'client-admin']` | A case worker can contact either role. The other roles have their own rules. |
| `'super-admin' => ['*']` | The role can contact everyone the host directory allows in the current workspace. |
| An account has multiple roles | The allowed targets from all matching rules are combined. |
| The viewer has no matching rule | The contact list is empty. The private self-chat remains available. |
| `'viewer' => []` | The viewer can contact only the shared accounts assigned to that workspace, if any. |

## Share a support account

Add an eligible support user ID to `visibility.shared_contacts` for each workspace that should be able to contact it. Use the exact key returned by your [workspace directory](https://threads.teamkrope.com/docs/users-workspaces/#isolate-users-by-company). The default single-workspace key is `default`.

```php
'shared_contacts' => [
    'company:42' => ['17'],
    'company:81' => ['29'],
],
```

The viewer must have a matching role rule, and the host directory must already allow the support account in that workspace. Configure the host directory to return the company's own users and any support accounts explicitly assigned to it. The support account's ability to find the company's users follows its own role rules and workspace access.

## What the rules control

The role filter applies to contact rows in the inbox, name search, the new-conversation picker, direct chat lookup and creation, and new group invitations. Group info only offers **Message** for contactable members. The [HTTP API](https://threads.teamkrope.com/docs/api/#contact-permissions) enforces the same checks on user IDs.

Existing conversations follow workspace access and membership. A permitted sender can contact someone who could not find the sender in the directory, and the recipient can reply after the first message arrives. An empty direct chat with a restricted peer stays out of the inbox. A role change affects discovery and invitations. To revoke access to previous messages, change the relevant conversation membership or the user's access to the workspace.

## Verify the setup

1. Rebuild Laravel's cached configuration if the deployment uses it. Reload widget tabs that already loaded the directory.
2. Use two accounts with different roles to check the inbox, search, picker and group invitations in both directions. Use a second workspace to confirm that the boundary still holds.
3. Sign in as a current group member and verify that their existing history remains available after a role change. Confirm the self-chat still opens.

See [Contact is missing or cannot be added](https://threads.teamkrope.com/docs/troubleshooting/#contact-is-missing-or-cannot-be-added) for resolver, workspace and configuration checks.
