-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase_channel.js
More file actions
190 lines (159 loc) · 4.67 KB
/
base_channel.js
File metadata and controls
190 lines (159 loc) · 4.67 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { isEmpty, isPresent } from '@ember/utils';
import { tracked, cached } from '@glimmer/tracking';
import { TrackedArray } from 'tracked-built-ins';
import Message from 'hyperchannel/models/message';
import moment from 'moment';
export default class BaseChannel {
@tracked account = null;
@tracked id = '';
@tracked name = ''; // e.g. kosmos-dev@kosmos.chat or #kosmos-dev
@tracked displayName = ''; // e.g. Kosmos Dev
@tracked isLogged;
@tracked connected = false;
@tracked topic = null;
@tracked userList = new TrackedArray([]);
@tracked messages = new TrackedArray([]);
@tracked unreadMessages = false;
@tracked unreadMentions = false;
@tracked visible = false; // Current/active channel
constructor (props) {
Object.assign(this, props);
if (isEmpty(this.id)) {
switch(this.protocol) {
case 'XMPP':
this.id = this.name;
break;
case 'IRC':
this.id = `${this.name}@${this.account.server.hostname}`;
break;
}
}
}
get protocol () {
return this.account.protocol;
}
get sockethubPersonId () {
return this.account.sockethubPersonId;
}
get sockethubChannelId () {
let id;
switch (this.protocol) {
case 'XMPP':
id = this.name;
break;
case 'IRC':
id = `${this.name}@${this.account.server.hostname}`;
break;
}
return id;
}
get slug () {
// This could be based on server type in the future. E.g. IRC would be
// server URL, while Campfire would be another id.
return this.id.replace(/#/g,'');
}
get shortName () {
switch (this.protocol) {
case 'IRC':
return this.name.replace(/#/g,'');
case 'XMPP':
return this.name.match(/^(.+)@/)[1];
default:
return this.name;
}
}
get domain () {
const match = this.id.match(/@([^/]+)/);
return match[1];
}
get unreadMessagesClass () {
if (this.visible || !this.unreadMessages) {
return null;
}
return this.unreadMentions ? 'unread-mentions' : 'unread-messages';
}
@cached
get sortedMessages () {
return [...this.messages].sort((a, b) => {
return new Date(a.date) - new Date(b.date);
});
}
@cached
get sortedUserList () {
return [...this.userList].sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
}
addDateHeadline (newMessage) {
let headlineDate = moment(newMessage.date).startOf('day').toDate();
let existingDateHeadline = this.messages.find(function (message) {
return message.type === 'date-headline' &&
message.date.toString() === headlineDate.toString();
});
if (existingDateHeadline) { return; }
let dateMessage = new Message({ type: 'date-headline', date: headlineDate });
this.messages.push(dateMessage);
}
addMessage (message) {
if (message.replaceId) {
this.replaceMessage(message);
return;
}
this.addDateHeadline(message);
// Find the last non-date-headline message for grouping check
const chatMessages = this.messages.filter(m => m.type !== 'date-headline');
const prevMsg = chatMessages[chatMessages.length - 1];
if (prevMsg &&
(prevMsg.nickname === message.nickname) &&
moment(message.date).isBefore(moment(prevMsg.date).add(120, 'seconds'))) {
message.grouped = true;
}
this.messages.push(message);
if (!this.visible) {
this.unreadMessages = true;
if (message.content.match(this.account.nickname)) {
this.unreadMentions = true;
}
}
}
replaceMessage (newMessage) {
const messagesFromNick = this.sortedMessages
.filter(msg => msg.nickname === newMessage.nickname);
const lastMessage = messagesFromNick[messagesFromNick.length - 1];
if (lastMessage &&
(lastMessage.id === newMessage.replaceId)) {
lastMessage.content = newMessage.content;
lastMessage.edited = true;
// TODO replace date?
}
}
confirmPendingMessage (content) {
const message = this.messages.filter(msg => msg.pending)
.find(msg => msg.content === content);
if (isPresent(message)) {
message.pending = false;
return true;
} else {
return false;
}
}
addUser (username) {
if (!this.userList.includes(username)) {
this.userList.push(username);
}
}
removeUser (username) {
const index = this.userList.indexOf(username);
if (index > -1) {
this.userList.splice(index, 1);
}
}
serialize () {
return {
accountId: this.account.id,
id: this.id,
name: this.name,
displayName: this.displayName
}
}
}