Skip to content
Open
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
132 changes: 45 additions & 87 deletions nodejs/User.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {io} from "./websocket.js";
import {getOnlineUSer, getUserStatus} from "./login.mjs";
import {AWAY_TIME, DEFAULT_STATE} from "./config.mjs";
import { io } from "./websocket.js";
import { getOnlineUSer } from "./login.mjs";
import { AWAY_TIME, DEFAULT_STATE } from "./config.mjs";

class User {
userId;
Expand All @@ -12,130 +12,88 @@ class User {
offline = false;
away = false;
awayTime = 0;

constructor(userId, socket, status) {
this.sockets.push(socket);
this.status = status;
this.initUserAway();
this.userId = userId;
this.status = status;
this.sockets.push(socket);
this.offline = status === DEFAULT_STATE;
this.awayTime=AWAY_TIME;
this.awayTime = AWAY_TIME;
this.initUserAway(); // async, kein await im Konstruktor möglich
}

addSocket(socket) {
if ((this.sockets.indexOf(socket) === -1)) {
this.sockets.push(socket);
}
if (!this.sockets.includes(socket)) this.sockets.push(socket);
}

removeSocket(socket) {
const index = this.sockets.indexOf(socket);
if (index > -1) { // only splice array when item is found
this.sockets.splice(index, 1); // 2nd parameter means remove one item only
}

}

setStatus(status) {
if (status === 'offline') {
this.offline = true;
} else {
this.offline = false;
}
this.status = status;
this.initUserAway();
}

hasSocket(socket) {
if (socket in this.sockets) {
return true
}
return false;
}

getSockets() {
return this.sockets
if (index > -1) this.sockets.splice(index, 1);
}

getStatus() {
if (this.offline || this.sockets.length === 0) {
return 'offline';
}
if (this.inMeeting.length > 0) {
return 'inMeeting';
}
if (this.away){
return 'away';
}
if (this.offline || this.sockets.length === 0) return 'offline';
if (this.inMeeting.length > 0) return 'inMeeting';
if (this.away) return 'away';
return this.status;
}

setAlive() {
this.initUserAway()
async sendStatus() {
const onlineUsers = await getOnlineUSer();
io.emit('sendOnlineUser', JSON.stringify(onlineUsers));
this.sendToAllSockets('sendUserStatus', this.getStatus());
}

sendStatus() {
io.emit('sendOnlineUser', JSON.stringify(getOnlineUSer()));
this.sendToAllSockets('sendUserStatus',this.status);
}

getUserId() {
return this.userId;
}

setuserId(value) {
this._userId = value;
}

initUserAway() {
async initUserAway() {
clearTimeout(this.awayTimer);
this.awayTimer = null;
var that = this;

if (this.away === true) {
this.away = false;
this.sendStatus();
await this.sendStatus();
}

this.oldStatus = null;
this.awayTimer = setTimeout(function () {
that.away = true;
that.sendStatus();
}, 60000 * this.awayTime)
this.awayTimer = setTimeout(async () => {
this.away = true;
await this.sendStatus();
}, 60000 * this.awayTime);
}

async setStatus(status) {
this.status = status;
this.offline = status === 'offline';
await this.initUserAway();
}

sendToAllSockets(ev, message) {
for (const socket of this.sockets) socket.emit(ev, message);
}

enterMeeting(socket) {
if (!this.inMeeting.includes(socket)) {
this.inMeeting.push(socket);
}
if (!this.inMeeting.includes(socket)) this.inMeeting.push(socket);
}

leaveMeeting(socket) {
for (var i = 0; i < this.inMeeting.length; i++) {
if (this.inMeeting[i] === socket) {
this.inMeeting.splice(i, 1);
}
}
const idx = this.inMeeting.indexOf(socket);
if (idx > -1) this.inMeeting.splice(idx, 1);
}

checkUserLeftTheApp() {
return this.sockets.length === 0;
}
setAwayTime(awayTime){

async setAwayTime(awayTime) {
try {
awayTime = parseInt(awayTime);
if (Number.isInteger(awayTime)){
if (Number.isInteger(awayTime)) {
this.awayTime = awayTime;
this.sendToAllSockets('sendUserTimeAway',this.awayTime);
this.sendToAllSockets('sendUserTimeAway', this.awayTime);
}
}catch (e) {
console.log(e)
}
}

sendToAllSockets(ev,message){
for (var prop in this.sockets) {
var tmpSocket = this.sockets[prop];
tmpSocket.emit(ev, message);
} catch (e) {
console.error(e);
}
}
}

export {User};
export { User };
19 changes: 12 additions & 7 deletions nodejs/config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export var WEBSOCKET_SECRET=process.env.WEBSOCKET_SECRET ||"MY_SECRET"
export var MERCURE_INTERNAL_URL=process.env.MERCURE_INTERNAL_URL || "/.well-known/mercure";
export var PORT =process.env.PORT || 3000;
export var AWAY_TIME =process.env.AWAY_TIME || 5;
export var DEFAULT_STATE =process.env.DEFAULT_STATE || 'offline';
export var KEY_FILE =process.env.KEY_FILE || './tls_certificate/key.pem';
export var CERT_FILE =process.env.CERT_FILE || './tls_certificate/cert.pem';
export var WEBSOCKET_SECRET = process.env.WEBSOCKET_SECRET || "MY_SECRET";
export var MERCURE_INTERNAL_URL = process.env.MERCURE_INTERNAL_URL || "/.well-known/mercure";
export var PORT = process.env.PORT || 3000;
export var AWAY_TIME = process.env.AWAY_TIME || 5;
export var DEFAULT_STATE = process.env.DEFAULT_STATE || "offline";
export var KEY_FILE = process.env.KEY_FILE || "./tls_certificate/key.pem";
export var CERT_FILE = process.env.CERT_FILE || "./tls_certificate/cert.pem";

// Redis für Clusterbetrieb
export var REDIS_ENABLED = process.env.REDIS_ENABLED === "true";
export var REDIS_HOST = process.env.REDIS_HOST || "redis";
export var REDIS_PORT = process.env.REDIS_PORT || 6379;
Loading
Loading