Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Inappchat improvements and fixes #112

Merged
merged 3 commits into from
Apr 1, 2022
Merged
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
1 change: 0 additions & 1 deletion app/components/inappchat/helpers/index.js
Original file line number Diff line number Diff line change
@@ -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';
7 changes: 0 additions & 7 deletions app/components/inappchat/helpers/url.js

This file was deleted.

25 changes: 17 additions & 8 deletions app/components/inappchat/inappchat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useEffect, useState } from "react";
import { Rocketchat } from "@rocket.chat/sdk";
import Cookie from 'js-cookie';
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,
Expand All @@ -22,8 +23,12 @@ import InappchatTextInput from "./inappchattextinput";

const rcClient = new Rocketchat({ logger: console, protocol: "ddp" });

const InAppChat = ({ closeChat, cookies, rid }) => {
const InAppChat = ({ host, closeChat, rid }) => {
const [messages, setMessages] = useState([]);
const cookies = { rc_token: Cookie.get('rc_token'), rc_uid: Cookie.get('rc_uid') };
const isAuth = cookies.rc_token && cookies.rc_uid;
const rcURL = new URL(host);
const useSsl = !/http:\/\//.test(host);

useEffect(() => {
const runRealtime = async (token, rid) => {
Expand All @@ -41,8 +46,12 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
}
};
async function getData() {
const data = await getMessages(rid, cookies);
setMessages(data.messages);
try {
const data = await getMessages(host, rid, cookies);
setMessages(data.messages);
} catch (err) {
console.log(err.message);
}
}
getData();
runRealtime(cookies.rc_token, rid);
Expand All @@ -52,7 +61,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]);
};

Expand All @@ -63,7 +72,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
</div>
<div className={styles.chatbox}>
<Box>
{cookies.rc_token && cookies.rc_uid ? (
{isAuth ? (
messagesSortedByDate(messages)?.map((m) => (
<Message className="customclass" clickable key={m._id}>
<MessageContainer>
Expand All @@ -90,15 +99,15 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
) : (
<p>
Please login into{" "}
<a href="https://open.rocket.chat" target="_blank">
<a href={host} rel="noopener noreferrer" target="_blank">
RocketChat
</a>{" "}
to chat!
</p>
)}
</Box>
</div>
{cookies.rc_token && cookies.rc_uid && <InappchatTextInput sendMsg={sendMsg} />}
{isAuth && <InappchatTextInput sendMsg={sendMsg} />}
</div>
);
};
Expand Down
10 changes: 4 additions & 6 deletions app/components/inappchat/lib/api.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dompurify": "^2.3.5",
"emoji-toolkit": "^6.6.0",
"firebase": "^9.6.3",
"js-cookie": "^3.0.1",
"marked": "^4.0.12",
"next": "12.0.7",
"next-auth": "^4.2.1",
Expand Down
13 changes: 3 additions & 10 deletions app/pages/virtualconf/greenroom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import InAppChat from "../../../components/inappchat/inappchat";
import { Button } from "react-bootstrap";

const greenroom_rid = process.env.NEXT_PUBLIC_ROCKET_CHAT_GREENROOM_RID;
const host = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://community.liaison.rocketchat.digital";

const Greenroom = ({ cookies }) => {
const Greenroom = () => {
const [openChat, setOpenChat] = useState(false);

const handleOpenChat = () => {
Expand All @@ -23,7 +24,7 @@ const Greenroom = ({ cookies }) => {
<div className={styles.container}>
</div>
{openChat ? (
<InAppChat closeChat={handleOpenChat} cookies={cookies} rid={greenroom_rid} />
<InAppChat host={host} closeChat={handleOpenChat} rid={greenroom_rid} />
) : (
<Button style={{ float: 'right' }} onClick={handleOpenChat}>Open Chat</Button>
)}
Expand All @@ -41,11 +42,3 @@ const Greenroom = ({ cookies }) => {
}

export default Greenroom;

Greenroom.getInitialProps = ({ req }) => {
const cookies = req.cookies;

return {
cookies,
};
};
13 changes: 3 additions & 10 deletions app/pages/virtualconf/mainstage/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Videostreamer from "../../../components/clientsideonly/videostreamer";
import InAppChat from '../../../components/inappchat/inappchat';

const rid = process.env.NEXT_PUBLIC_ROCKET_CHAT_CONF_RID;
const host = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://community.liaison.rocketchat.digital";

export default function ConfMainStage({ cookies }) {
export default function ConfMainStage() {
const [openChat, setOpenChat] = useState(false);

const handleOpenChat = () => {
Expand All @@ -32,19 +33,11 @@ export default function ConfMainStage({ cookies }) {

</Container>
{openChat ? (
<InAppChat closeChat={handleOpenChat} cookies={cookies} rid={rid} />
<InAppChat host={host} closeChat={handleOpenChat} rid={rid} />
) : (
<Button onClick={handleOpenChat}>Open Chat</Button>
)}
</div>
</>
);
}

ConfMainStage.getInitialProps = ({ req }) => {
const cookies = req.cookies;

return {
cookies,
};
};