-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversation-template.js
57 lines (57 loc) · 2.29 KB
/
conversation-template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Vue.component('conversation-list', {
props: {
client: Object,
amount: { // defines the amount of conversations searched by the API, defaulted to 10
default : 10,
type: Number
},
size: { // defines the amount of conversations displayed in the list, defaulted to 5
default: 5,
type: Number
}
},
template: `<div>
<select class="select-wide" :size="size">
<option v-for="conversation in conversations" @click="conversationSelected(conversation)">{{ conversation.topic || conversation.topicPlaceholder }}</option>
</select>
</div>`,
data: function () {
return {
conversations: [],
usersHashtable: {}
}
},
created: async function () {
this.getConversations();
},
methods: {
conversationSelected: function (conversation) {
this.$emit('on-change', conversation);
},
getConversations: async function () {
const convs = await this.client.getConversations({ numberOfConversations: this.amount });
const userIdsNeeded = []; // userIds to be added, from direct conversations
convs.forEach(conversation => {
// if this is a direct conversation pushes userId, else sets the title
if (conversation.type === Circuit.Enums.ConversationType.DIRECT) {
const peerUser = conversation.participants.filter(id => id !== this.client.loggedOnUser.userId)[0];
conversation.peerUser = peerUser;
userIdsNeeded.push(peerUser);
}
this.conversations.push(conversation);
});
this.conversations = this.conversations.reverse();
// gets all the user information of direct conversations early
this.client.getUsersById(userIdsNeeded)
.then(users => {
users.forEach(user => {
this.usersHashtable[user.userId] = user;
});
// goes through the conversations setting the title for direct conversations
this.conversations.forEach(conv => conv.topic = conv.topic || conv.topicPlaceholder || this.usersHashtable[conv.peerUser].displayName);
this.$emit('on-loaded', this.conversations);
})
.catch(console.error);
}
}
});