smartuniverse/ts/smartuniverse.classes.universemessage.ts

102 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-07-25 09:33:13 +00:00
import * as plugins from './smartuniverse.plugins.js';
import * as interfaces from './interfaces/index.js';
import { Universe } from './smartuniverse.classes.universe.js';
import { UniverseChannel } from './smartuniverse.classes.universechannel.js';
import { UniverseCache } from './smartuniverse.classes.universecache.js';
import { SocketConnection } from '@push.rocks/smartsocket';
import { logger } from './smartuniverse.logging.js';
2018-03-13 05:15:40 +00:00
2018-04-13 13:45:48 +00:00
/**
* represents a message within a universe
* acts as a container to save message states like authentication status
2018-04-13 13:45:48 +00:00
*/
2019-09-10 21:55:20 +00:00
export class UniverseMessage<T> implements interfaces.IUniverseMessage {
2019-09-01 15:04:25 +00:00
public static createMessageFromPayload(
socketConnectionArg: SocketConnection,
dataArg: interfaces.IUniverseMessage
) {
2019-08-13 13:48:20 +00:00
const universeMessageInstance = new UniverseMessage(dataArg);
universeMessageInstance.socketConnection = socketConnectionArg;
return universeMessageInstance;
2019-08-12 12:59:37 +00:00
}
2019-04-22 07:58:36 +00:00
public id: string;
public timestamp: number;
2020-09-30 00:50:43 +00:00
public smartTimestamp: plugins.smarttime.TimeStamp;
2019-04-22 07:58:36 +00:00
public messageText: string;
public passphrase: string;
2019-09-10 21:55:20 +00:00
public payload: T;
2019-04-22 07:58:36 +00:00
public targetChannelName: string;
2019-08-13 13:48:20 +00:00
public socketConnection: SocketConnection;
2018-05-07 16:50:07 +00:00
/**
* the UniverseCache the message is attached to
2018-05-07 16:50:07 +00:00
*/
public universeCache: UniverseCache;
2018-05-19 22:41:59 +00:00
/**
* enables unprotected grouping of messages for efficiency purposes.
*/
2020-09-24 18:13:48 +00:00
public universeChannelList = new plugins.lik.ObjectMap<UniverseChannel>();
/**
* wether the message is authenticated
*/
2019-08-12 13:12:31 +00:00
public authenticated: boolean = false;
2019-04-22 22:28:57 +00:00
2018-05-19 22:41:59 +00:00
/**
2019-04-22 07:58:36 +00:00
* a destruction timer for this message
2018-05-19 22:41:59 +00:00
*/
2020-09-30 00:50:43 +00:00
public destructionTimer: plugins.smarttime.Timer; // a timer to take care of message destruction
2018-03-13 05:15:40 +00:00
2018-04-29 12:17:26 +00:00
/**
* the constructor to create a universe message
2018-05-07 16:50:07 +00:00
* @param messageArg
* @param attachedPayloadArg
2018-04-29 12:17:26 +00:00
*/
2019-08-13 13:48:20 +00:00
constructor(messageDescriptor: interfaces.IUniverseMessage) {
2020-09-30 00:50:43 +00:00
this.smartTimestamp = new plugins.smarttime.TimeStamp(this.timestamp);
2019-04-22 07:58:36 +00:00
this.messageText = messageDescriptor.messageText;
this.targetChannelName = messageDescriptor.targetChannelName;
this.passphrase = messageDescriptor.passphrase;
this.payload = messageDescriptor.payload;
// prevent memory issues
2019-09-17 13:40:54 +00:00
this.setDestructionTimer();
2018-05-19 22:41:59 +00:00
}
public setUniverseCache(universeCacheArg: UniverseCache) {
this.universeCache = universeCacheArg;
2018-05-19 22:41:59 +00:00
}
2019-09-01 15:04:25 +00:00
public setTargetChannel() {}
2019-08-13 13:48:20 +00:00
2019-09-17 13:40:54 +00:00
public setDestructionTimer(selfdestructAfterArg?: number) {
2018-05-19 22:41:59 +00:00
if (selfdestructAfterArg) {
2020-09-30 00:50:43 +00:00
this.destructionTimer = new plugins.smarttime.Timer(selfdestructAfterArg);
2018-05-19 22:41:59 +00:00
this.destructionTimer.start();
// set up self destruction by removing this from the parent messageCache
2019-11-09 12:00:30 +00:00
this.destructionTimer.completed
.then(async () => {
this.universeCache.messageMap.remove(this);
})
2020-09-24 18:17:52 +00:00
.catch((err) => {
2019-11-09 12:00:30 +00:00
console.log(err);
console.log(this);
});
2018-05-19 22:41:59 +00:00
} else {
2019-09-17 13:40:54 +00:00
plugins.smartdelay.delayFor(1000).then(() => {
if (!this.destructionTimer) {
this.setDestructionTimer(6000);
}
});
2018-05-19 22:41:59 +00:00
}
2018-05-23 21:50:45 +00:00
}
2018-05-19 22:41:59 +00:00
/**
* handles bad messages for further analysis
*/
2019-04-22 07:58:36 +00:00
public handleAsBadMessage() {
2020-09-24 18:13:48 +00:00
logger.log('warn', 'received a bad message');
}
2018-03-13 05:15:40 +00:00
}