UNPKG

@wulperstudio/chat

Version:
299 lines (298 loc) 13.2 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import io from "socket.io-client"; import { ObjectId } from "bson"; import { Buffer } from "buffer"; import { ImageWorks } from "../helpers/ImageWorks"; class ModuleChatControl { constructor(externals, service, id, nickname, email, locale, avatar) { this.chanelNow = null; this.roomMembers = []; this.socket = null; this.credentials = null; // private inbox: InboxModel = []; // private global?: String = undefined; this.isgroup = false; const wselect = new URLSearchParams(window.location.search).get("wselect"); const wmessage = new URLSearchParams(window.location.search).get("wmessage"); const wautosend = new URLSearchParams(window.location.search).get("wautosend"); const wdirect = new URLSearchParams(window.location.search).get("wdirect"); const winvite_nickname = new URLSearchParams(window.location.search).get("winvite.nickname"); const winvite_email = new URLSearchParams(window.location.search).get("winvite.email"); const winvite_locale = new URLSearchParams(window.location.search).get("winvite.locale"); const winvite_avatar = new URLSearchParams(window.location.search).get("winvite.avatar"); localStorage.removeItem("wchat-maintenance"); this.socket = io(process.env.API_SOCKET || "", { secure: true, reconnection: true, rejectUnauthorized: true, }); this.socket.on("connect", function () { console.info("connect"); }); this.socket.on("connect_error", (err) => { console.error("[connect_error]: ", err); }); this.socket.on("error", function (data) { console.error("[error]: ", data || "error"); }); this.socket.on("connect_failed", function (data) { console.error("[connect_failed]: ", data); }); this.start_connection(service, id, nickname, email, locale, avatar); this.socket.on("@register-user-out", (user_data) => { localStorage.setItem("wchat-maintenance", JSON.stringify(user_data)); this.credentials = user_data; localStorage.setItem("x-usid", user_data.usid); localStorage.setItem("x-nickname", user_data.nickname); this.get_out_group(); /** * confirma la connection */ externals.connection && externals.connection(user_data); /** * crea una conexion si viene de otra cista con udid */ if (wselect) { this.register_thread(wselect, (wdirect === 'true'), winvite_nickname, winvite_email, service, winvite_locale, winvite_avatar); } /** * activa la escucha de los eventos callback */ this.receive_call_event(externals); /** * lista por primer vez los contactos */ this.get_list_contacts_groups(); /** * mensaje por defecto y envio automatico */ if (wmessage) { externals.set_text_msg && externals.set_text_msg(wmessage, wautosend === 'yes'); } /** * trae contactos del mismo servicio */ this.get_members_bighall(); }); } start_connection(service, in_usid, in_nickname, email, in_locale, avatar) { const usid = in_usid || new ObjectId().toHexString(); const nickname = in_nickname || new ObjectId().toHexString().substr(19); const locale = in_locale || "en"; const object_user = JSON.stringify({ service, usid, nickname, email, locale, avatar }); setTimeout(() => { var _a; const base = Buffer.from(object_user).toString("base64"); (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit("@register-user-in", base); }, 1000); } set_id_chanel(id) { this.chanelNow = id; } set_isgroup(isgroup) { this.isgroup = isgroup; } register_thread(selected, direct, nickname, email, service, locale, avatar) { var _a, _b, _c; if ((_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid) { const base = Buffer.from(JSON.stringify({ usid: (_b = this.credentials) === null || _b === void 0 ? void 0 : _b.usid, selected, direct, invite: direct ? { nickname, email, service, locale, avatar, } : undefined, })).toString("base64"); (_c = this.socket) === null || _c === void 0 ? void 0 : _c.emit("@register-thread-in", base); } else { console.error("[ERROR]: credentials not exist"); } } get_list_contacts_groups() { var _a, _b, _c; if ((_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid) { (_b = this.socket) === null || _b === void 0 ? void 0 : _b.emit("@get-list-contacts-groups-in", (_c = this.credentials) === null || _c === void 0 ? void 0 : _c.usid); } else { console.error("[ERROR]: credentials not exist"); } } get_inbox(space, isgroup) { var _a, _b; if ((_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid) { const base = Buffer.from(JSON.stringify({ space, isgroup })).toString("base64"); (_b = this.socket) === null || _b === void 0 ? void 0 : _b.emit("@get-inbox-in", base); } else { console.error("[ERROR]: credentials not exist"); } } send_thread_message(msg, files) { var _a, _b, _c; if ((_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid) { const base = Buffer.from(JSON.stringify({ usid: (_b = this.credentials) === null || _b === void 0 ? void 0 : _b.usid, space: this.chanelNow, isgroup: this.isgroup, msg, files: files || [], })).toString("base64"); (_c = this.socket) === null || _c === void 0 ? void 0 : _c.emit("@send-thread-message-in", base); } else { console.error("[ERROR]: credentials not exist"); } } set_mark_as_read_space() { var _a, _b; const base = Buffer.from(JSON.stringify({ usid: (_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid, space: this.chanelNow, })).toString("base64"); (_b = this.socket) === null || _b === void 0 ? void 0 : _b.emit("@set-mark-as-read-space-in", base); } get_members_room_in() { var _a; if (this.isgroup) { const base = Buffer.from(JSON.stringify({ space: this.chanelNow, })).toString("base64"); (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit("@get-members-room-in", base); } } create_room(roomName, raw_members) { var _a, _b; const members = raw_members.map((item) => item.usid); const base = Buffer.from(JSON.stringify({ usid: (_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid, name: roomName, members, })).toString("base64"); (_b = this.socket) === null || _b === void 0 ? void 0 : _b.emit("@create-room-in", base); } get_members_bighall(search) { var _a, _b; const base = Buffer.from(JSON.stringify({ usid: (_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid, search: search || '', })).toString("base64"); (_b = this.socket) === null || _b === void 0 ? void 0 : _b.emit("@get-members-big-hall", base); } upload_file(files) { return __awaiter(this, void 0, void 0, function* () { if (this.credentials) { return yield ImageWorks(files, this.credentials.usid); } return undefined; }); } get_out_group() { var _a, _b; const base = Buffer.from(JSON.stringify({ usid: (_a = this.credentials) === null || _a === void 0 ? void 0 : _a.usid, rmid: this.chanelNow, })).toString("base64"); (_b = this.socket) === null || _b === void 0 ? void 0 : _b.emit("@get-out-group", base); } receive_call_event(externals) { if (this.credentials && this.socket) { this.socket.on("disconnect", () => { console.log('SALIOOOOO'); // this.contection(); // externals.disconnect && externals.disconnect(); }); this.socket.on("@get-list-contacts-groups-out", (rooms) => { // console.log('@get-list-contacts-groups-out', rooms); externals.get_list_contacts_groups_out && externals.get_list_contacts_groups_out(rooms); }); this.socket.on("@register-thread-out", (thread) => { console.log(thread); if (!thread) return; const isgroup = thread.user === undefined; externals.register_thread_out && externals.register_thread_out(thread, isgroup); /** * registra el id del spacio */ this.set_id_chanel(thread.space); /** * solicita los mensajes de la conversacion */ this.get_inbox(thread.space, isgroup); /** * registra si es un grupo */ this.set_isgroup(isgroup); if (isgroup) { this.get_members_room_in(); } }); this.socket.on("@get-inbox-out", (inbox) => { // console.log('@get-inbox-out', inbox); externals.get_inbox_out && externals.get_inbox_out(inbox, this.chanelNow || ""); this.set_mark_as_read_space(); }); this.socket.on("@send-thread-message-out", (status) => { // console.log('@send-thread-message-out', status); if (status && !!this.chanelNow) { /** * solicita los mensajes de la conversacion */ this.get_inbox(this.chanelNow, this.isgroup); } }); this.socket.on("@alert-new-message-out", (alert) => { // console.log('@alert-new-message-out', alert); externals.alert_new_message_out && externals.alert_new_message_out(alert); /** * solicita los contactos y mensajes no leidos */ this.get_list_contacts_groups(); if (this.chanelNow === alert.space) { /** * solicita los contactos y mensajes no leidos */ this.get_inbox(alert.space, alert.group); } }); this.socket.on("@set-mark-as-read-space-out", (status) => { // console.log('@sset-mark-as-read-space-out', status); if (status) { /** * solicita los contactos y mensajes no leidos */ this.get_list_contacts_groups(); } }); this.socket.on("@get-members-room-out", (members) => { // console.log('@get-members-room-out', members); externals.get_members_room && externals.get_members_room(members); }); this.socket.on("@get-members-big-hall-out", (members) => { // console.log('@get-members-room-out', members); externals.search_users_bighall && externals.search_users_bighall(members); }); this.socket.on("@create-room-out", (_group) => { // console.log('@get-members-room-out', members, space, title); this.get_list_contacts_groups(); }); } } } export default ModuleChatControl;