diff --git a/app/components/inappchat/helpers/index.js b/app/components/inappchat/helpers/index.js index 73a297e7..0219f3f2 100644 --- a/app/components/inappchat/helpers/index.js +++ b/app/components/inappchat/helpers/index.js @@ -1,4 +1,3 @@ export { default as messagesSortedByDate } from './sortMessagesByDate'; export { default as emojify } from './emojify'; export { emojis } from './emojisThatAnimate'; -export { useSsl, rcURL } from './url'; diff --git a/app/components/inappchat/helpers/url.js b/app/components/inappchat/helpers/url.js deleted file mode 100644 index fcb05d94..00000000 --- a/app/components/inappchat/helpers/url.js +++ /dev/null @@ -1,7 +0,0 @@ -// uses localhost:3000 for development and the domain provided in .env in prod -let url = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : process.env.NEXT_PUBLIC_ROCKET_CHAT_HOST; - -export const rcURL = new URL(url); - -// Use useSsl: false only if server url starts with http:// -export const useSsl = !/http:\/\//.test(url); diff --git a/app/components/inappchat/inappchat.js b/app/components/inappchat/inappchat.js index 2030b76d..4053d07d 100644 --- a/app/components/inappchat/inappchat.js +++ b/app/components/inappchat/inappchat.js @@ -2,7 +2,7 @@ import { useEffect, useState } from "react"; import { Rocketchat } from "@rocket.chat/sdk"; import { getMessages, sendMessage } from "./lib/api"; import styles from "../../styles/Inappchat.module.css"; -import { emojify, messagesSortedByDate, rcURL, useSsl } from "./helpers"; +import { emojify, messagesSortedByDate } from "./helpers"; import { Message, MessageBody, @@ -22,8 +22,11 @@ import InappchatTextInput from "./inappchattextinput"; const rcClient = new Rocketchat({ logger: console, protocol: "ddp" }); -const InAppChat = ({ closeChat, cookies, rid }) => { +const InAppChat = ({ host, closeChat, cookies, rid }) => { const [messages, setMessages] = useState([]); + const isAuth = cookies.rc_token && cookies.rc_uid; + const rcURL = new URL(host); + const useSsl = !/http:\/\//.test(host); useEffect(() => { const runRealtime = async (token, rid) => { @@ -42,7 +45,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => { }; async function getData() { try { - const data = await getMessages(rid, cookies); + const data = await getMessages(host, rid, cookies); setMessages(data.messages); } catch (err) { console.log(err.message); @@ -56,7 +59,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => { if (message.trim() === "") { return; } - const msg = await sendMessage(rid, message, cookies); + const msg = await sendMessage(host, rid, message, cookies); setMessages([...messages, msg.message]); }; @@ -67,7 +70,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
- {cookies.rc_token && cookies.rc_uid ? ( + {isAuth ? ( messagesSortedByDate(messages)?.map((m) => ( @@ -94,7 +97,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => { ) : (

Please login into{" "} - + RocketChat {" "} to chat! diff --git a/app/components/inappchat/lib/api.js b/app/components/inappchat/lib/api.js index 4cc433b7..42a6f3c3 100644 --- a/app/components/inappchat/lib/api.js +++ b/app/components/inappchat/lib/api.js @@ -1,9 +1,7 @@ -import { rcURL } from "../helpers"; - -export const getMessages = async (rid, cookies) => { +export const getMessages = async (host, rid, cookies) => { try { const messages = await fetch( - `${rcURL.origin}/api/v1/channels.messages?roomId=${rid}`, + `${host}/api/v1/channels.messages?roomId=${rid}`, { headers: { "Content-Type": "application/json", @@ -20,9 +18,9 @@ export const getMessages = async (rid, cookies) => { } }; -export const sendMessage = async (rid, message, cookies) => { +export const sendMessage = async (host, rid, message, cookies) => { try { - const msg = await fetch(`${rcURL.origin}/api/v1/chat.sendMessage`, { + const msg = await fetch(`${host}/api/v1/chat.sendMessage`, { body: `{"message": { "rid": "${rid}", "msg": "${message}" }}`, headers: { "Content-Type": "application/json", diff --git a/app/pages/virtualconf/greenroom/index.js b/app/pages/virtualconf/greenroom/index.js index bc13470e..2ca52c65 100644 --- a/app/pages/virtualconf/greenroom/index.js +++ b/app/pages/virtualconf/greenroom/index.js @@ -23,7 +23,7 @@ const Greenroom = ({ cookies }) => {

{openChat ? ( - + ) : ( )} diff --git a/app/pages/virtualconf/mainstage/[id].js b/app/pages/virtualconf/mainstage/[id].js index 88e0f5c9..ace57767 100644 --- a/app/pages/virtualconf/mainstage/[id].js +++ b/app/pages/virtualconf/mainstage/[id].js @@ -32,7 +32,7 @@ export default function ConfMainStage({ cookies }) { {openChat ? ( - + ) : ( )}