Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions howtos/how-to-lock-a-conversation-to-read-only/README.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This is an example project for TalkJS's tutorial on [How to lock a conversation to be read-only](https://talkjs.com/resources/how-to-lock-a-conversation-to-be-read-only/).

The project uses TalkJS's [custom conversation actions](https://talkjs.com/docs/Features/Customizations/Conversation_Actions/) to add a new "Set Read-Only" option to the chat inbox UI. We add code to listen for this custom action and use the [ConversationBuilder.setParticipant](https://talkjs.com/docs/Reference/JavaScript_Chat_SDK/ConversationBuilder/#ConversationBuilder__setParticipant) method to set each participant's access level to read-only.

## Prerequisites

To run this tutorial, you will need:

- A [TalkJS account](https://talkjs.com/dashboard/login)

## How to run the tutorial

1. Clone or download the project.
2. Go to the **Roles** tab of your TalkJS dashboard. Create a new role called "admin".
3. Click on the settings for the new admin role. In the **Custom conversation actions** section, add a new custom conversation action with a name of "setReadOnly" and a label of "Set Read-Only".
4. Replace `<APP_ID>` in the `index.html` file with the value found in your [TalkJS dashboard](https://talkjs.com/dashboard/login).
5. Open index.html, or run debug the program in your IDE of choice.
88 changes: 88 additions & 0 deletions howtos/how-to-lock-a-conversation-to-read-only/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<title>TalkJS tutorial</title>
</head>

<!-- minified snippet to load TalkJS without delaying your page -->
<script>
(function (t, a, l, k, j, s) {
s = a.createElement("script");
s.async = 1;
s.src = "https://cdn.talkjs.com/talk.js";
a.head.appendChild(s);
k = t.Promise;
t.Talk = {
v: 3,
ready: {
then: function (f) {
if (k)
return new k(function (r, e) {
l.push([f, r, e]);
});
l.push([f]);
},
catch: function () {
return k && new k();
},
c: l,
},
};
})(window, document, []);
</script>

<script>
Talk.ready.then(function () {
var me = new Talk.User({
id: '123456',
name: 'Alice',
email: 'alice@example.com',
photoUrl: 'https://talkjs.com/images/avatar-1.jpg',
welcomeMessage: 'Hey there! How are you? :-)',
role: "admin",
});
window.talkSession = new Talk.Session({
appId: 'YOUR_APP_ID',
me: me,
});
var other = new Talk.User({
id: '654321',
name: 'Sebastian',
email: 'Sebastian@example.com',
photoUrl: 'https://talkjs.com/images/avatar-5.jpg',
welcomeMessage: 'Hey, how can I help?',
});

var conversation = talkSession.getOrCreateConversation(
Talk.oneOnOneId(me, other)
);
conversation.setParticipant(me);
conversation.setParticipant(other);

var inbox = talkSession.createInbox({ selected: conversation });
inbox.mount(document.getElementById('talkjs-container'));

inbox.onCustomConversationAction('setReadOnly', (event) => {
console.log('Locked conversation with id:', event.conversation.id);
inbox.destroy();
conversation = talkSession.getOrCreateConversation(event.conversation.id);
conversation.setParticipant(me, { access: 'Read' });
conversation.setParticipant(other, { access: 'Read' });
inbox = talkSession.createInbox({ selected: conversation });
inbox.mount(document.getElementById('talkjs-container'));
});
});

</script>

<body>
<!-- container element in which TalkJS will display a chat UI -->
<div id="talkjs-container" style="width: 90%; margin: 30px; height: 500px">
<i>Loading chat...</i>
</div>
</body>
</html>