2018-04-13 13:45:48 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
2019-01-31 01:52:18 +00:00
|
|
|
import { Objectmap } from '@pushrocks/lik';
|
2018-05-26 11:44:32 +00:00
|
|
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
|
|
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
2018-04-29 12:17:26 +00:00
|
|
|
|
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-05-24 14:55:24 +00:00
|
|
|
// ======
|
|
|
|
// STATIC
|
|
|
|
// ======
|
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-26 11:44:32 +00:00
|
|
|
public static createChannel(
|
|
|
|
universeCacheArg: UniverseCache,
|
|
|
|
channelNameArg: string,
|
|
|
|
passphraseArg: string
|
|
|
|
) {
|
|
|
|
const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg);
|
2019-04-11 15:52:01 +00:00
|
|
|
universeCacheArg.channelMap.add(newChannel);
|
2018-05-19 22:41:59 +00:00
|
|
|
return newChannel;
|
2018-05-26 11:44:32 +00:00
|
|
|
}
|
2018-05-23 21:50:45 +00:00
|
|
|
|
2018-05-24 14:55:24 +00:00
|
|
|
/**
|
|
|
|
* returns boolean wether certain channel exists
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
|
2018-05-28 10:07:25 +00:00
|
|
|
const channel = universeCacheArg.channelMap.find(channelArg => {
|
2018-05-24 14:55:24 +00:00
|
|
|
return channelArg.name === channelNameArg;
|
|
|
|
});
|
2018-05-26 11:44:32 +00:00
|
|
|
if (channel) {
|
2018-05-24 14:55:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:44:32 +00:00
|
|
|
public static authorizeAMessageForAChannel(
|
|
|
|
universeCacheArg: UniverseCache,
|
|
|
|
universeMessageArg: UniverseMessage
|
|
|
|
) {
|
2018-05-28 10:07:25 +00:00
|
|
|
const foundChannel = universeCacheArg.channelMap.find(universeChannel => {
|
2018-05-26 11:44:32 +00:00
|
|
|
const result = universeChannel.authenticate(universeMessageArg);
|
|
|
|
return result;
|
2018-05-23 22:14:57 +00:00
|
|
|
});
|
2018-05-26 11:44:32 +00:00
|
|
|
if (foundChannel) {
|
|
|
|
universeMessageArg.authenticated = true;
|
|
|
|
universeMessageArg.universeChannelList.add(foundChannel);
|
2018-05-23 22:14:57 +00:00
|
|
|
return foundChannel;
|
|
|
|
} else {
|
2018-05-26 11:44:32 +00:00
|
|
|
universeMessageArg.authenticated = false;
|
|
|
|
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
|
2018-05-23 22:14:57 +00:00
|
|
|
}
|
2018-05-26 11:44:32 +00:00
|
|
|
}
|
2018-05-23 22:14:57 +00:00
|
|
|
|
2018-05-24 14:55:24 +00:00
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2018-05-19 22:41:59 +00:00
|
|
|
/**
|
|
|
|
* the name of the channel
|
|
|
|
*/
|
|
|
|
public name: string;
|
2018-05-26 11:44:32 +00:00
|
|
|
public universeCacheInstance: UniverseCache;
|
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-26 11:44:32 +00:00
|
|
|
constructor(universeCacheArg: UniverseCache, channelNameArg: string, passphraseArg: string) {
|
2018-05-19 22:41:59 +00:00
|
|
|
this.name = channelNameArg;
|
|
|
|
this.passphrase = passphraseArg;
|
|
|
|
}
|
2018-04-13 13:45:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* authenticates a client on the server side
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public authenticate(universeMessageArg: UniverseMessage): boolean {
|
|
|
|
return (
|
|
|
|
this.name === universeMessageArg.requestedChannelName &&
|
|
|
|
this.passphrase === universeMessageArg.requestedChannelPassphrase
|
|
|
|
);
|
2018-05-19 22:41:59 +00:00
|
|
|
}
|
2018-05-28 10:07:25 +00:00
|
|
|
|
|
|
|
public pushToClients(messageArg: UniverseMessage) {}
|
2018-04-13 13:45:48 +00:00
|
|
|
}
|