8

How to Hide Some Chat Messages for Certain Users in a TalkJS Chat

 2 years ago
source link: https://dev.to/talkjs/how-to-hide-some-chat-messages-for-certain-users-in-a-talkjs-chat-23g4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this tutorial, we’ll learn how to hide specific chat messages for users using the TalkJS Chat API message filters. We will also learn how to combine TalkJS custom message attributes with message filters to implement a wide variety of use cases for filtering chat messages.

What Are Message Filters?

TalkJs message filters are used to control which messages show up in the Chatbox UI. You can filter messages based on type, origin, sender, and custom attributes.

Note: Messages are only filtered in the message list (Chatbox UI). The inbox UI's conversation feed will always show the last message sent to the conversation, regardless of the message filter set.

Applying Message Filters in a Chat UI

To apply filters to a Chatbox, use the setMessageFilter method and pass a MessagePredicate to apply filters based on Custom attributes, sender, origin, and type.

Let’s see different examples of filters that can be applied in a Chat UI using a simple Vanilla Js implementation of TalkJS Chatbox. Here, we create a TalkJs user and bind that user object to a new Chatbox instance using the TalkJs session.

Note that, apart from plain JavaScript, TalkJs SDK supports Vue, Angular, and React. And the API will be the same across these frameworks. You can check out our docs for more information.

<script>
    Talk.ready.then(function() {
        var me = new Talk.User({
            id: "333333",
            name: "Chris Pratt",
            email: "[email protected]",
            photoUrl: "https://i.picsum.photos/id/1025/4951/3301.jpg?hmac=_aGh5AtoOChip_iaMo8ZvvytfEojcgqbCH7dzaz-H8Y",
            welcomeMessage: "Hey there! How are you? :-)",
            role: "Accountant"
        });

  window.talkSession = new Talk.Session({
            appId: "<appID>",
            me: me
        });

        var chatbox = talkSession.createChatbox();
        chatbox.mount(document.getElementById("talkjs-container"));
    });
</script>
Enter fullscreen modeExit fullscreen mode

Fig: Demo VanillaJS application

Only Showing Text Messages

A chat can contain text messages, attachments, and locations. To show only text messages in a Chat, you can first add a custom attribute to every message sent using the on("sendMessage") method.

Specifically, we’re adding a custom attribute named “textMessage” to every message sent except for attachments. The value of the custom attribute does not matter here as we’ll not be using it. We just need to have the custom attribute present for all text messages.

chatbox.on("sendMessage", function(ev) {
    if (!ev.message.attachment) {
        ev.override({
            custom: {
                textMessage: "true"
            }
        });
    }
});
Enter fullscreen modeExit fullscreen mode

Now, to show only text messages in a Chatbox, simply filter out messages with the “textMessage'' custom attribute set using the “exists'' check. As an example, I’m adding the filter on the click of a button here.

document.getElementById('filterTextMessages').onclick = function filter() {
    chatbox.setMessageFilter({
        custom: {
            textMessage: "exists"
        }
    })
}
Enter fullscreen modeExit fullscreen mode

Only Showing Messages with Attachments

We can use the same custom “textMessage” attribute, but with “!exists” check and filter out just the attachments. This filter will omit all messages with the “textMessage” attribute present, leaving us with only those messages with attachments.

document.getElementById('filterAttachments').onclick = function filter() {
    chatbox.setMessageFilter({
        custom: {
            textMessage: "!exists"
        }
    })
}
Enter fullscreen modeExit fullscreen mode

Only Showing Admin Messages (Filter Based on Sender's Role)

Every TalkJS user has a role assigned to them during creation, and you can filter the messages based on this role using the “sender” FieldPredicate.

Let’s take an example and show only those messages sent by users with the role “Admin.”

document.getElementById('showAdminMessages').onclick = function filter() {
    chatbox.setMessageFilter({sender: {role: ["==", "Admin"]}})
}
Enter fullscreen modeExit fullscreen mode

Filtering out Messages from Banned Users

In your chat application, you will sometimes need to block or filter messages from banned users for various reasons. You can change the user’s role to “Banned” and then filter out messages in a chatbox based on the Sender role.

var me = new Talk.User({
    id: "154721",
    name: "Matt Wong ",
    email: "[email protected]",
    photoUrl: "https://picsum.photos/id/237/200/300",
    welcomeMessage: "Hey there! How are you? :-)",
    role: "Banned",
});

document.getElementById('hideBannerUserMessages').onclick = function banned() {
    chatbox.setMessageFilter({
        sender: {
            role: ["!=", "Banned"]
        }
    })
}
Enter fullscreen modeExit fullscreen mode

Reset Filters

Finally, to reset or remove all filters, just call the setMessageFilter method again and pass NULL; this should clear all current filters.

document.getElementById('resetFilters').onclick = function reset() {
    chatbox.setMessageFilter(null)
}
Enter fullscreen modeExit fullscreen mode

If you run into any issues, please get in touch via the popup on our website or send an email to [email protected].


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK