Skip to content

Commit

Permalink
Refactor to remove some code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
galfert committed Jan 23, 2017
1 parent 7d01f57 commit 7ba0f71
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 69 deletions.
49 changes: 49 additions & 0 deletions app/models/base_channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Ember from 'ember';

export default Ember.Object.extend({

space: null,
name: '',
userList: null,
messages: null,
connected: false,
sockethubChannelId: null,
topic: null,
unreadMessages: false,
unreadMentions: false,
visible: false, // Current/active channel

slug: Ember.computed('name', function() {
// 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.get('name').replace(/#/g,'');
}),

unreadMessagesClass: Ember.computed('visible', 'unreadMessages', 'unreadMentions', function() {
if (this.get('visible') || !this.get('unreadMessages')) {
return null;
}
return this.get('unreadMentions') ? 'unread-mentions' : 'unread-messages';
}),

addMessage(message) {
this.get('messages').pushObject(message);

if (!this.get('visible')) {
this.set('unreadMessages', true);
if (message.get('content').match(this.get('space.userNickname'))) {
this.set('unreadMentions', true);
}
}
},

addUser(username) {
this.get('userList').pushObject(username);
},

removeUser(username) {
this.get('userList').removeObject(username);
}

});

30 changes: 4 additions & 26 deletions app/models/channel.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import Ember from 'ember';
import BaseChannel from 'hyperchannel/models/base_channel';

export default Ember.Object.extend({
export default BaseChannel.extend({

name: '',
userList: null,
messages: null,
connected: false,
sockethubChannelId: null,
topic: null,
unreadMessages: false,
unreadMentions: false,
visible: false, // Current/active channel

slug: function() {
// 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.get('name').replace(/#/g,'');
}.property('name'),

formattedTopic: function() {
formattedTopic: Ember.computed('topic', function() {
if (this.get('topic') !== null) {
let topic = linkifyStr(this.get('topic'), {
defaultProtocol: 'https',
Expand All @@ -28,13 +13,6 @@ export default Ember.Object.extend({
} else {
return '';
}
}.property('topic'),

unreadMessagesClass: function() {
if (this.get('visible') || !this.get('unreadMessages')) {
return null;
}
return this.get('unreadMentions') ? 'unread-mentions' : 'unread-messages';
}.property('visible', 'unreadMessages', 'unreadMentions')
})

});
8 changes: 7 additions & 1 deletion app/models/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ export default Ember.Object.extend({
return this.get('name').toLowerCase();
}.property('name'),

userNickname: Ember.computed.alias('ircServer.nickname'),

sockethubPersonId: function() {
return `irc://${this.get('ircServer.nickname')}@${this.get('ircServer.hostname')}`;
}.property('ircServer.hostname', 'ircServer.nickname'),

channelSorting: ['name'],
sortedChannels: Ember.computed.sort('channels', 'channelSorting')
sortedChannels: Ember.computed.sort('channels', 'channelSorting'),

addChannel(channel) {
this.get('channels').pushObject(channel);
}

});
17 changes: 3 additions & 14 deletions app/models/user_channel.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import Ember from 'ember';
import BaseChannel from 'hyperchannel/models/base_channel';

export default Ember.Object.extend({
export default BaseChannel.extend({

name: '',
userList: null,
messages: null,
connected: false,
sockethubChannelId: null,
topic: null,
isUserChannel: true,

slug: function() {
// 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.get('name').replace(/#/g,'');
}.property('name'),
isUserChannel: true

});
40 changes: 12 additions & 28 deletions app/services/smt.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ export default Ember.Service.extend({
addUserToChannelUserList: function(message) {
const channel = this.getChannelByMessage(message);
if (channel) {
var userList = channel.get('userList');
userList.pushObject(message.actor.displayName);
channel.addUser(message.actor.displaName);
}
},

removeUserFromChannelUserList: function(message) {
const channel = this.getChannelByMessage(message);
if (channel) {
var userList = channel.get('userList');
userList.removeObject(message.actor.displayName);
channel.removeUser(message.actor.displayName);
}
},

Expand Down Expand Up @@ -232,7 +230,7 @@ export default Ember.Service.extend({
addMessageToChannel: function(message) {
var space = this.get('spaces').findBy('ircServer.hostname',
message.actor['@id'].match(/irc:\/\/.+\@(.+)/)[1]);
var nickname = space.get('ircServer.nickname');
var nickname = space.get('userNickname');

var targetChannelName;
if (nickname === message.target.displayName) {
Expand Down Expand Up @@ -262,14 +260,7 @@ export default Ember.Service.extend({

// TODO should check for message and update sent status if exists
if (message.actor.displayName !== nickname) {
channel.get('messages').pushObject(channelMessage);

if (!channel.get('visible')) {
channel.set('unreadMessages', true);
if (message.object.content.match(nickname)) {
channel.set('unreadMentions', true);
}
}
channel.addMessage(channelMessage);
}
},

Expand Down Expand Up @@ -300,21 +291,21 @@ export default Ember.Service.extend({

createChannel: function(space, channelName) {
var channel = Channel.create({
space: space,
name: channelName,
sockethubChannelId: `irc://${space.get('ircServer.hostname')}/${channelName}`,
messages: []
messages: [],
userList: []
});
this.joinChannel(space, channel, "room");
channel.set('userList', []);
space.get('channels').pushObject(channel);
space.addChannel(channel);

this.loadArchiveMessages(space, channel);

return channel;
},

loadArchiveMessages(space, channel) {
let nickname = space.get('ircServer.nickname');
let today = moment.utc();
let logsUrl = `${config.publicLogsUrl}/${space.get('name').toLowerCase()}/channels/${channel.get('slug')}/`;
logsUrl += today.format('YYYY/MM/DD');
Expand All @@ -332,14 +323,7 @@ export default Ember.Service.extend({
content: message.text
});

channel.get('messages').pushObject(channelMessage);

if (!channel.get('visible')) {
channel.set('unreadMessages', true);
if (message.object.content.match(nickname)) {
channel.set('unreadMentions', true);
}
}
channel.addMessage(channelMessage);
});
}, error => {
console.log(error);
Expand All @@ -350,11 +334,11 @@ export default Ember.Service.extend({
var channel = UserChannel.create({
name: userName,
sockethubChannelId: `irc://${space.get('ircServer.hostname')}/${userName}`,
messages: []
messages: [],
userList: []
});
this.joinChannel(space, channel, "person");
channel.set('userList', []);
space.get('channels').pushObject(channel);
space.addChannel(channel);
return channel;
},

Expand Down

0 comments on commit 7ba0f71

Please sign in to comment.