smartuniverse/ts/smartuniverse.classes.universechannel.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

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';
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 {
// ======
// 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
*/
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-23 21:50:45 +00:00
/**
* returns boolean wether certain channel exists
*/
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
const channel = universeCacheArg.channelMap.find(channelArg => {
return channelArg.name === channelNameArg;
});
if (channel) {
return true;
} else {
return false;
}
}
public static authorizeAMessageForAChannel(
universeCacheArg: UniverseCache,
universeMessageArg: UniverseMessage
) {
const foundChannel = universeCacheArg.channelMap.find(universeChannel => {
const result = universeChannel.authenticate(universeMessageArg);
return result;
2018-05-23 22:14:57 +00:00
});
if (foundChannel) {
universeMessageArg.authenticated = true;
universeMessageArg.universeChannelList.add(foundChannel);
2018-05-23 22:14:57 +00:00
return foundChannel;
} else {
universeMessageArg.authenticated = false;
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
2018-05-23 22:14:57 +00:00
}
}
2018-05-23 22:14:57 +00:00
// ========
// INSTANCE
// ========
2018-05-19 22:41:59 +00:00
/**
* the name of the channel
*/
public name: string;
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
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
*/
public authenticate(universeMessageArg: UniverseMessage): boolean {
return (
this.name === universeMessageArg.requestedChannelName &&
this.passphrase === universeMessageArg.requestedChannelPassphrase
);
2018-05-19 22:41:59 +00:00
}
public pushToClients(messageArg: UniverseMessage) {}
2018-04-13 13:45:48 +00:00
}