Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 1 addition & 6 deletions app/controllers/base_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ export default class BaseChannelController extends Controller {

let message = this.createMessage(newMessage, 'message-chat-me');

this.coms.transferMeMessage(
this.model.account,
this.model.sockethubChannelId,
message.content,
message.id
);
this.coms.transferMeMessage(this.model, message.content);

this.model.addMessage(message);
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/base_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class BaseChannel {
id = this.name;
break;
case 'IRC':
id = `${this.account.server.hostname}/${this.name}`;
id = `${this.name}@${this.account.server.hostname}`;
break;
}
return id;
Expand Down
43 changes: 31 additions & 12 deletions app/services/coms.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export default class ComsService extends Service {
*/
setupListeners () {
this.sockethub.client.socket.on('message', this.handleSockethubMessage.bind(this));
this.sockethub.client.socket.on('completed', this.handleJobCompleted.bind(this));
this.sockethub.client.socket.on('failed', this.handleJobFailed.bind(this));
this.sockethub.client.socket.on('client_error', (err) => {
console.error('Sockethub client error:', err);
});
}

/**
Expand Down Expand Up @@ -166,8 +171,13 @@ export default class ComsService extends Service {
* Invokes the send-action-message function on the appropriate transport service
* @public
*/
transferMeMessage (account, target, content) {
switch (account.protocol) {
transferMeMessage (channel, content) {
const target = {
id: channel.sockethubChannelId,
type: channel.isUserChannel ? 'person' : 'room',
name: channel.name
};
switch (channel.protocol) {
case 'XMPP':
// TODO implement
break;
Expand Down Expand Up @@ -411,10 +421,11 @@ export default class ComsService extends Service {
* @private
*/
handleSockethubMessage (message) {
this.log(`${message.context}_message`, 'SH message', message);
const platform = this.sockethub.platformForMessage(message);
this.log(`${platform}_message`, 'SH message', message);

if (message.actor.type === 'service') {
this.log(`${message.context}_message`, 'skipping service message');
this.log(`${platform}_message`, 'skipping service message');
return;
}

Expand All @@ -424,17 +435,11 @@ export default class ComsService extends Service {
this.updateChannelUserList(message);
}
break;
// TODO remove deprecated term in favor of query
case 'observe':
if (message.object['type'] === 'attendance') {
this.updateChannelUserList(message);
}
break;
case 'send':
switch (message.object.type) {
case 'message':
case 'me':
this.getServiceForSockethubPlatform(message.context)
this.getServiceForSockethubPlatform(platform)
.addMessageToChannel(message);
break;
}
Expand All @@ -448,7 +453,7 @@ export default class ComsService extends Service {
this.updateUsername(message);
break;
case 'presence':
this.getServiceForSockethubPlatform(message.context)
this.getServiceForSockethubPlatform(platform)
.handlePresenceUpdate(message)
break;
case 'error':
Expand All @@ -459,6 +464,20 @@ export default class ComsService extends Service {
}
}

handleJobCompleted (message) {
this.log('completed', 'Job completed', message);
if (message.type === 'join') {
const platform = this.sockethub.platformForMessage(message);
this.getServiceForSockethubPlatform(platform)
.handleJoinCompleted(message);
}
}

handleJobFailed (message) {
this.log('failed', 'Job failed', message);
console.warn('Sockethub job failed:', message);
}

/**
* Utility function for easier logging
* @private
Expand Down
117 changes: 44 additions & 73 deletions app/services/sockethub-irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,6 @@ import { isEmpty } from '@ember/utils';
import UserChannel from 'hyperchannel/models/user_channel';
import channelMessageFromSockethubObject from 'hyperchannel/utils/channel-message-from-sockethub-object';

/**
* Build an activity object for sending to Sockethub
*
* @param {Account} account - account model the activity belongs to
* @param {Object} details - the activity details
* @returns {Object} the activity object
* @private
*/
function buildActivityObject (account, details) {
let baseObject = {
context: 'irc',
actor: account.sockethubPersonId
};

return { ...baseObject, ...details };
}

/**
* Build a message object
*
* @param account {Account} account instance
* @param target {String} where to send the message to (channelId)
* @param content {String} the message itself
* @param type {String} can be either 'message' or 'me'
* @returns {Object} the activity object
*/
function buildMessageObject (account, target, content, type='message') {
return buildActivityObject(account, {
type: 'send',
target: target,
object: {
type: type,
content: content
}
});
}

/**
* This service provides helpers for SocketHub IRC communications
* @class hyperchannel/services/sockethub-irc
Expand All @@ -55,20 +18,39 @@ export default class SockethubIrcService extends Service {
}

/**
* - Creates an ActivityStreams person object for
* future use
* - Emits credentials for future IRC server messages,
* like e.g. `join`
* @public
* Build an activity object for sending to Sockethub
* @private
*/
connect (account, callback) {
this.sockethubClient.ActivityStreams.Object.create({
id: account.sockethubPersonId,
type: 'person',
name: account.nickname
buildActivityObject (account, details) {
let baseObject = {
'@context': this.sockethub.contextFor('irc'),
actor: { id: account.sockethubPersonId, type: 'person' }
};

return { ...baseObject, ...details };
}

/**
* Build a message object
* @private
*/
buildMessageObject (account, target, content, type='message') {
return this.buildActivityObject(account, {
type: 'send',
target: target,
object: {
type: type,
content: content
}
});
}

const credentialsJob = buildActivityObject(account, {
/**
* Emits credentials and connects to an IRC server
* @public
*/
connect (account, callback) {
const credentialsJob = this.buildActivityObject(account, {
type: 'credentials',
object: {
type: 'credentials',
Expand All @@ -77,12 +59,11 @@ export default class SockethubIrcService extends Service {
secure: account.server.secure,
sasl: account.server.sasl,
nick: account.nickname,
// username: account.username,
...(account.password && { password: account.password }),
}
});

const connectJob = buildActivityObject(account, {
const connectJob = this.buildActivityObject(account, {
type: 'connect'
});

Expand Down Expand Up @@ -110,23 +91,19 @@ export default class SockethubIrcService extends Service {
}

handlePresenceUpdate (message) {
const hostname = message.target.id.match(/(.+)\//)[1];
const hostname = message.target.id.match(/@(.+)$/)[1];
const account = this.coms.accounts.find(acc => acc?.server?.hostname === hostname);
if (isEmpty(account)) { console.warn('No account for presence update message found.', message); return; }

let channel = this.coms.channels.find(ch => ch.sockethubChannelId === message.target.id);

// TODO document why there might be no channel instance, or remove
if (isEmpty(channel)) {
console.debug('No channel for presence update message found. Creating it.', message);
channel = this.coms.createChannel(account, message.target.name, message.target.id);
}

// Hotfix for adding one's own user to the channel and marking it as
// connected.
// ATM, Sockethub doesn't send any events or information that we
// successfully joined a channel. So for now we just assume, if we receive
// presence updates from other users, we should be in the channel, too.
// If we receive presence updates from other users, we should be in the
// channel too.
channel.addUser(account.nickname);
channel.connected = true;

Expand All @@ -140,15 +117,9 @@ export default class SockethubIrcService extends Service {
join (channel, type) {
switch(type) {
case 'room':
this.sockethubClient.ActivityStreams.Object.create({
type: type,
id: channel.sockethubChannelId,
name: channel.name
});

var joinMsg = buildActivityObject(channel.account, {
var joinMsg = this.buildActivityObject(channel.account, {
type: 'join',
target: channel.sockethubChannelId
target: { id: channel.sockethubChannelId, type: 'room', name: channel.name }
});

this.log('irc', 'joining channel', joinMsg);
Expand All @@ -169,7 +140,7 @@ export default class SockethubIrcService extends Service {
*/
transferMessage (target, message) {
const channel = this.coms.getChannel(target.id);
const messageJob = buildMessageObject(channel.account, target, message.content);
const messageJob = this.buildMessageObject(channel.account, target, message.content);

this.log('send', 'sending message job', messageJob);
this.sockethubClient.socket.emit('message', messageJob);
Expand All @@ -181,7 +152,7 @@ export default class SockethubIrcService extends Service {
*/
transferMeMessage (target, content) {
const channel = this.coms.getChannel(target.id);
const message = buildMessageObject(channel.account, target, content, 'me');
const message = this.buildMessageObject(channel.account, target, content, 'me');

this.log('send', 'sending message job', message);
this.sockethubClient.socket.emit('message', message);
Expand Down Expand Up @@ -213,9 +184,9 @@ export default class SockethubIrcService extends Service {
*/
leave (channel) {
if (!channel.isUserChannel) {
let leaveMsg = buildActivityObject(channel.account, {
let leaveMsg = this.buildActivityObject(channel.account, {
type: 'leave',
target: channel.sockethubChannelId,
target: { id: channel.sockethubChannelId, type: 'room' },
object: {}
});

Expand All @@ -229,9 +200,9 @@ export default class SockethubIrcService extends Service {
* @public
*/
changeTopic (channel, topic) {
let topicMsg = buildActivityObject(channel.account, {
let topicMsg = this.buildActivityObject(channel.account, {
type: 'update',
target: channel.sockethubChannelId,
target: { id: channel.sockethubChannelId, type: 'room' },
object: {
type: 'topic',
topic: topic
Expand All @@ -246,9 +217,9 @@ export default class SockethubIrcService extends Service {
* @public
*/
queryAttendance (channel) {
let msg = buildActivityObject(channel.account, {
let msg = this.buildActivityObject(channel.account, {
type: 'query',
target: channel.sockethubChannelId,
target: { id: channel.sockethubChannelId, type: 'room' },
object: {
type: 'attendance'
}
Expand Down
Loading
Loading