smartuniverse/ts/smartuniverse.classes.universechannel.ts

64 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-04-13 13:45:48 +00:00
import * as plugins from './smartuniverse.plugins';
2018-04-29 12:17:26 +00:00
import { Objectmap } from 'lik';
2018-04-13 13:45:48 +00:00
/**
2018-04-29 12:17:26 +00:00
* enables messages to stay within a certain scope.
2018-04-13 13:45:48 +00:00
*/
export class UniverseChannel {
2018-04-29 12:17:26 +00:00
/**
* stores the channels that are available within the universe
*/
2018-05-23 22:14:57 +00:00
public static channelStore = new Objectmap<UniverseChannel>();
/**
* allows messages to be processed in a blacklist mode for further analysis
*/
public static blackListChannel = new UniverseChannel('blacklist', 'nada');
2018-05-23 21:50:45 +00:00
2018-05-07 16:50:07 +00:00
/**
2018-05-19 22:41:59 +00:00
* creates new channels
* @param channelArg the name of the topic
* @param passphraseArg the secret thats used for a certain topic.
2018-05-07 16:50:07 +00:00
*/
2018-05-23 22:14:57 +00:00
public static createChannel (channelNameArg: string, passphraseArg: string) {
2018-05-19 22:41:59 +00:00
const newChannel = new UniverseChannel(channelNameArg, passphraseArg);
return newChannel;
2018-05-23 21:50:45 +00:00
};
2018-05-23 22:14:57 +00:00
public static authorizeForChannel (channelNameArg: string, passphraseArg: string) {
const foundChannel = this.channelStore.find(universeChannel => {
const result = universeChannel.authenticate(channelNameArg, passphraseArg);
return result;
});
if(foundChannel) {
return foundChannel;
} else {
return this.blackListChannel;
}
};
2018-05-19 22:41:59 +00:00
/**
* the name of the channel
*/
public name: string;
2018-04-29 12:17:26 +00:00
/**
2018-05-19 22:41:59 +00:00
* the passphrase for the channel
2018-04-29 12:17:26 +00:00
*/
2018-05-19 22:41:59 +00:00
public passphrase: string;
2018-04-29 12:17:26 +00:00
2018-05-19 22:41:59 +00:00
constructor(channelNameArg: string, passphraseArg: string) {
this.name = channelNameArg;
this.passphrase = passphraseArg;
UniverseChannel.channelStore.add(this);
}
2018-04-13 13:45:48 +00:00
/**
* authenticates a client on the server side
*/
2018-05-23 22:14:57 +00:00
public authenticate(channelNameArg: string, passphraseArg: string): boolean {
return (this.name === channelNameArg && this.passphrase === passphraseArg);
2018-05-19 22:41:59 +00:00
}
2018-04-13 13:45:48 +00:00
}