Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt code to new Sockethub Client API #270

Merged
merged 7 commits into from
Jan 27, 2022
Merged
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
50 changes: 13 additions & 37 deletions app/components/add-chat-account-xmpp/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,11 @@ export default class AddChatAccountXmppComponent extends Component {
@tracked hostname = 'kosmos.org';
@tracked password;
@tracked connectError = null;
@tracked finishedSetup = false;

get userAddress () {
return `${this.username}@${this.hostname}`;
}

async handleConnectStatus (eventName, message) {
if (this.finishedSetup) {
// TODO remove when double events fixed
console.debug('Account setup already finished, nothing to do')
return;
}

if (message.context !== 'xmpp' ||
!['message', 'completed'].includes(eventName)) { return; }

if (message.type === 'error' &&
message.object.condition === 'not-authorized'
/* && TODO message.actor.id === actor */) {
this.connectError = {
title: 'Account connection failed',
content: message.object.content
}
this.xmpp.sockethub.socket.offAny();
}

if (message.type === 'connect' &&
message.actor.id === `${this.userAddress}/hyperchannel`) {
// Connected successfully
this.xmpp.sockethub.socket.offAny();

const account = await this.addAccount();
this.addDefaultChannels(account);
this.finishedSetup = true;

const firstChannel = this.coms.channels.filterBy('account', account).firstObject;
this.router.transitionTo('channel', firstChannel);
}
}

async addAccount () {
const account = new XmppAccount({
username: this.userAddress,
Expand All @@ -81,8 +46,19 @@ export default class AddChatAccountXmppComponent extends Component {
submitForm (e) {
e.preventDefault();
this.connectError = null;
this.xmpp.sockethub.socket.onAny(this.handleConnectStatus.bind(this));
this.xmpp.connectWithCredentials(this.userAddress, this.password);
this.xmpp.connectWithCredentials(this.userAddress, this.password, async (message) => {
if (message.error) {
this.connectError = {
title: 'Account connection failed',
content: message.error
}
} else {
const account = await this.addAccount();
this.addDefaultChannels(account);
const firstChannel = this.coms.channels.filterBy('account', account).firstObject;
this.router.transitionTo('channel', firstChannel);
}
});
}

}
65 changes: 4 additions & 61 deletions app/services/coms.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class ComsService extends Service {
return {
domain: domain,
channels: this.channels.filterBy('domain', domain).sortBy('name')
}
};
});
}

Expand All @@ -65,9 +65,7 @@ export default class ComsService extends Service {
* @public
*/
setupListeners () {
this.sockethub.socket.on('completed', this.handleSockethubCompleted.bind(this));
this.sockethub.socket.on('message' , this.handleSockethubMessage.bind(this));
this.sockethub.socket.on('failure' , this.handleSockethubFailure.bind(this));
}

/**
Expand Down Expand Up @@ -204,7 +202,8 @@ export default class ComsService extends Service {
removeUserFromChannelUserList (message) {
// TODO handle user quit leaves (multiple channels)
// e.g. target is `{ type: 'service', id: 'irc.freenode.net' }`
const channel = this.getChannel(message.target.id);
const sockethubChannelId = typeof message.target === 'object' ? message.target.id : message.target;
const channel = this.getChannel(sockethubChannelId);
if (channel) {
channel.removeUser(message.actor.name);
}
Expand Down Expand Up @@ -384,22 +383,6 @@ export default class ComsService extends Service {
return this[protocol.toLowerCase()];
}

/*
* @private
*
* Handles completed Sockethub actions:
* - Successfully joined a channel
*/
handleSockethubCompleted (message) {
this.log(`${message.context}_completed`, message);

switch (message.type) {
case 'join':
this[message.context].handleJoinCompleted(message);
break;
}
}

/**
* Handles incoming Sockethub messages:
* - Attendance list for channel
Expand All @@ -418,12 +401,6 @@ export default class ComsService extends Service {
this.updateChannelUserList(message);
}
break;
case 'join':
this.handleChannelJoin(message);
break;
case 'leave':
this.removeUserFromChannelUserList(message);
break;
case 'send':
switch (message.object.type) {
case 'message':
Expand Down Expand Up @@ -451,47 +428,13 @@ export default class ComsService extends Service {
.handlePresenceUpdate(message)
break;
case 'error':
console.warn('Got error update message', message.actor.id, message.object.content);
console.warn('Received error update message', message.actor.id, message.object.content);
break;
}
break;
// case 'error':
// switch(message.context) {
// case 'xmpp':
// this.xmpp.handleErrorMessage(message);
// break;
// }
// break;
}
}

/**
* Handles the various checks assosciated with channel joins
* @private
*/
handleChannelJoin (message) {
if (message.object.type && message.object.type === 'error') {
// failed to join a channel
const channel = this.getChannel(message.target.id, message.actor.id);

if (isPresent(channel)) {
channel.connected = false;
} else {
console.warn('Could not find channel for error message', message);
}
} else {
this.addUserToChannelUserList(message);
}
}

/**
* Handles incoming Sockethub errors/failures
* @private
*/
handleSockethubFailure (message) {
this.log('sh_failure', message);
}

/**
* Utility function for easier logging
* @private
Expand Down
2 changes: 1 addition & 1 deletion app/services/sockethub-irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default class SockethubIrcService extends Service {
});

this.log('irc', 'joining channel', joinMsg);
this.sockethub.socket.emit('message', joinMsg);
this.sockethub.socket.emit('message', joinMsg, this.handleJoinCompleted.bind(this));
break;
case 'person':
channel.connected = true;
Expand Down
37 changes: 23 additions & 14 deletions app/services/sockethub-xmpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class SockethubXmppService extends Service {
@service logger;
@service coms;

connectWithCredentials (userAddress, password) {
connectWithCredentials (userAddress, password, callback) {
const sockethubPersonId = `${userAddress}/hyperchannel`;

this.sockethub.ActivityStreams.Object.create({
Expand Down Expand Up @@ -85,8 +85,10 @@ export default class SockethubXmppService extends Service {
};

this.log('xmpp', 'connecting to XMPP server...');
this.sockethub.socket.emit('credentials', credentialsJob);
this.sockethub.socket.emit('message', connectJob);
this.sockethub.socket.emit('credentials', credentialsJob, (err) => {
if (err) { this.log('failed to store credentials: ', err); }
});
this.sockethub.socket.emit('message', connectJob, callback);
}

/**
Expand Down Expand Up @@ -123,8 +125,13 @@ export default class SockethubXmppService extends Service {
};

this.log('xmpp', 'connecting to XMPP server...');
this.sockethub.socket.emit('credentials', credentialsJob);
this.sockethub.socket.emit('message', connectJob);
this.sockethub.socket.emit('credentials', credentialsJob, (err) => {
if (err) { this.log('failed to store credentials: ', err); }
});
this.sockethub.socket.emit('message', connectJob, (message) => {
if (message.error) { this.log('failed to connect to xmpp server: ', message); }
else { this.coms.handleSockethubMessage(message); }
});
}

handleJoinCompleted (message) {
Expand Down Expand Up @@ -158,14 +165,11 @@ export default class SockethubXmppService extends Service {
id: channel.sockethubPersonId,
name: channel.account.nickname
},
target: {
id: channel.sockethubChannelId,
type: type
}
target: channel.sockethubChannelId
});

this.log('xmpp', 'joining channel', joinMsg);
this.sockethub.socket.emit('message', joinMsg);
this.sockethub.socket.emit('message', joinMsg, this.handleJoinCompleted.bind(this));
}

/**
Expand Down Expand Up @@ -239,12 +243,17 @@ export default class SockethubXmppService extends Service {
if (!channel.isUserChannel) {
const leaveMsg = buildActivityObject(channel.account, {
type: 'leave',
target: channel.sockethubChannelId,
object: {}
target: channel.sockethubChannelId
});

this.log('leave', 'leaving channel', leaveMsg);
this.sockethub.socket.emit('message', leaveMsg);
this.sockethub.socket.emit('message', leaveMsg, (message) => {
if (message.error) {
console.warn(message.error);
} else {
this.coms.removeUserFromChannelUserList.bind(this.coms)
}
});
}
}

Expand Down Expand Up @@ -284,7 +293,7 @@ export default class SockethubXmppService extends Service {
if (message.target.type === 'room') {
channel = this.coms.channels.findBy('sockethubChannelId', targetChannelId);

// TODO Find account for new channel by sockerhubPersonId
// TODO Find account for new channel by sockethubPersonId
if (!channel) {
console.warn('Received message for unknown channel', message);
// channel = this.coms.createChannel(space, targetChannelId);
Expand Down
7 changes: 3 additions & 4 deletions release/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">


<meta name="hyperchannel/config/environment" content="%7B%22modulePrefix%22%3A%22hyperchannel%22%2C%22environment%22%3A%22production%22%2C%22rootURL%22%3A%22%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_APPLICATION_TEMPLATE_WRAPPER%22%3Afalse%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22hyperchannel%22%2C%22version%22%3A%220.12.0%2B3d5c3063%22%7D%2C%22sockethubURL%22%3A%22https%3A%2F%2Fsockethub.kosmos.org%3A10550%22%2C%22publicLogsUrl%22%3A%22https%3A%2F%2Fstorage.5apps.com%2Fkosmos%2Fpublic%2Fchat-messages%22%2C%22spacePresets%22%3A%5B%7B%22id%22%3A%22freenode%22%2C%22name%22%3A%22Freenode%22%2C%22description%22%3A%22The%20main%20network%20for%20free%20and%20open%20source%20software%20communities%22%2C%22website%22%3A%22https%3A%2F%2Ffreenode.net%2F%22%2C%22protocol%22%3A%22IRC%22%2C%22server%22%3A%7B%22hostname%22%3A%22irc.freenode.net%22%2C%22secure%22%3Atrue%2C%22port%22%3A7000%7D%2C%22botkaURL%22%3A%22https%3A%2F%2Ffreenode.botka.kosmos.org%22%7D%2C%7B%22id%22%3A%22moznet%22%2C%22name%22%3A%22Mozilla%22%2C%22description%22%3A%22Mozilla's%20public%20IRC%20network%22%2C%22website%22%3A%22https%3A%2F%2Fwiki.mozilla.org%2FIRC%22%2C%22protocol%22%3A%22IRC%22%2C%22server%22%3A%7B%22hostname%22%3A%22irc.mozilla.org%22%2C%22secure%22%3Atrue%2C%22port%22%3A6697%7D%7D%2C%7B%22id%22%3A%22gimpnet%22%2C%22name%22%3A%22GIMPnet%22%2C%22description%22%3A%22Home%20of%20the%20GNOME%20desktop%20project%22%2C%22website%22%3A%22https%3A%2F%2Fwiki.gnome.org%2FCommunity%2FGettingInTouch%2FIRC%22%2C%22protocol%22%3A%22IRC%22%2C%22server%22%3A%7B%22hostname%22%3A%22irc.gnome.org%22%2C%22secure%22%3Atrue%2C%22port%22%3A6697%7D%7D%2C%7B%22id%22%3A%22irc-localhost%22%2C%22name%22%3A%22IRC%20localhost%22%2C%22description%22%3A%22Local%20IRC%20server%20for%20development%22%2C%22website%22%3A%22%22%2C%22protocol%22%3A%22IRC%22%2C%22server%22%3A%7B%22hostname%22%3A%22localhost%22%2C%22secure%22%3Afalse%2C%22port%22%3A6667%7D%2C%22botkaURL%22%3A%22http%3A%2F%2Flocalhost%3A4242%22%7D%2C%7B%22id%22%3A%22xmpp-localhost%22%2C%22name%22%3A%22XMPP%20localhost%22%2C%22description%22%3A%22Local%20XMPP%20server%20for%20development%22%2C%22website%22%3Anull%2C%22protocol%22%3A%22XMPP%22%2C%22server%22%3A%7B%22hostname%22%3A%22localhost%22%2C%22secure%22%3Afalse%2C%22port%22%3A5222%7D%7D%2C%7B%22id%22%3A%225apps%22%2C%22name%22%3A%22XMPP%205apps%22%2C%22description%22%3A%225apps%20XMPP%20server%22%2C%22website%22%3A%22https%3A%2F%2F5apps.com%22%2C%22protocol%22%3A%22XMPP%22%2C%22server%22%3A%7B%22hostname%22%3A%22xmpp.5apps.com%22%2C%22secure%22%3Atrue%2C%22port%22%3A5222%7D%7D%2C%7B%22id%22%3A%22xmpp-kosmos%22%2C%22name%22%3A%22XMPP%20Kosmos%22%2C%22description%22%3A%22Kosmos%20XMPP%20server%22%2C%22website%22%3A%22https%3A%2F%2Fkosmos.org%22%2C%22protocol%22%3A%22XMPP%22%2C%22server%22%3A%7B%22hostname%22%3A%22xmpp.kosmos.org%22%2C%22secure%22%3Atrue%2C%22port%22%3A5222%7D%7D%5D%2C%22defaultSpaceId%22%3A%22freenode%22%2C%22exportApplicationGlobal%22%3Afalse%7D" />

<link integrity="" rel="stylesheet" href="/assets/vendor.css">
<link integrity="" rel="stylesheet" href="/assets/hyperchannel.css">


</head>
<body>


<script src="https://sockethub.kosmos.org:10550/activity-streams.min.js"></script>

<script src="https://sockethub.kosmos.org:10550/socket.io.js"></script>
<script src="https://sockethub.kosmos.org:10550/sockethub-client.js"></script>

Expand Down
1 change: 0 additions & 1 deletion vendor/sh-assets-local.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<script src="http://localhost:10550/activity-streams.min.js"></script>
<script src="http://localhost:10550/socket.io.js"></script>
<script src="http://localhost:10550/sockethub-client.js"></script>
1 change: 0 additions & 1 deletion vendor/sh-assets-remote.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<script src="https://sockethub.kosmos.org:10550/activity-streams.min.js"></script>
<script src="https://sockethub.kosmos.org:10550/socket.io.js"></script>
<script src="https://sockethub.kosmos.org:10550/sockethub-client.js"></script>