blob: 3fc2ed6c823781cd5ba50ce7bf9360aa49380b85 [file] [log] [blame]
pineafanf5dd1872023-02-28 17:33:16 +00001import Axios from 'axios';
2import React from 'react';
TheCodedProf2a69bd92023-03-02 16:36:32 -05003import Message from '../../../../Components/Transcripts/Message';
TheCodedProff054b8c2023-03-03 15:32:19 -05004import _ from 'lodash';
5
6const emojiRegex = /(<a?:.+:\D+>)/g;
7const userRegex = /(<@!?\d+>)/g;
8const channelRegex = /(<#\d+>)/g;
9
10async function parseText(text) {
11 const dict = {
12 "emojis": {},
13 "users": {},
14 "channels": {}
15 }
16 const emojis = text.match(emojiRegex);
17 if (emojis) {
18 for (let i = 0; i < emojis.length; i++) {
19 const emoji = emojis[i];
20 const emojiID = emoji.replaceAll(/\D/g, '');
21 const emojiURL = `https://cdn.discord.com/emojis/${emojiID}`;
22 dict["emojis"][emoji] = emojiURL;
23 }
24 }
25 const users = text.match(userRegex);
26 if (users) {
27 for (let i = 0; i < users.length; i++) {
28 const user = users[i];
29 const userID = user.replaceAll(/\D/g, '');
Skyler Grey6c3d4632023-03-06 09:51:21 +000030 const username = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}users/${userID}`)).data;
TheCodedProff054b8c2023-03-03 15:32:19 -050031 dict["users"][user.replaceAll(/<@|>/g, '')] = username;
32 }
33 }
34 const channels = text.match(channelRegex);
35 if (channels) {
36 for (let i = 0; i < channels.length; i++) {
37 const channel = channels[i];
38 const channelID = channel.replaceAll(/\D/g, '');
Skyler Grey6c3d4632023-03-06 09:51:21 +000039 const channelName = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}channels/${channelID}`)).data;
TheCodedProff054b8c2023-03-03 15:32:19 -050040 dict["channels"][channel] = channelName;
41 }
42 }
43 // console.log("parseText", dict)
44 return dict
45}
46
47async function parse(json) {
48 let dict = {
49 "emojis": {},
50 "users": {},
51 "channels": {}
52 }
53
54 await _.forEach(json, async (message, index) => {
55 let thisMessage = {}
56 if(message.content) {
57 let parsed = await parseText(message.content)
58 // console.log(parsed)
59 _.assign(thisMessage, parsed)
60 }
61 if(message.embeds) {
62 _.forEach(message.embeds, async (embed, _index) => {
63 // console.log(embed)
64 if(embed.description) {
65 let parsed = await parseText(embed.description)
66 _.assign(thisMessage, parsed)
67 }
68 if(embed.title) {
69 let parsed = await parseText(embed.title)
70 _.assign(thisMessage, parsed)
71 }
72 if(embed.fields) {
73 _.forEach(embed.fields, async (field, _index) => {
74 if(field.name) {
75 let parsed = await parseText(field.name)
76 _.assign(thisMessage, parsed)
77 }
78 if(field.value) {
79 let parsed = await parseText(field.value)
80 _.assign(thisMessage, parsed)
81 }
82 })
83 }
84 })
85 }
86 // console.log(thisMessage)
87 _.assign(dict, thisMessage)
88 // console.log(index, dict)
89 })
90
91 // console.log("dict", dict)
92 return dict;
93}
pineafanf5dd1872023-02-28 17:33:16 +000094
95function Index(props) {
TheCodedProfda197f22023-03-01 18:15:51 -050096 return <div style={{overflowY: "scroll", overflowX: "hidden"}}>
97 <div style={{
98 width: "100vw",
99 height: "40px",
100 backgroundColor: "#F27878",
101 display: "flex",
102 justifyContent: "left",
103 alignItems: "center",
104 paddingLeft: "25px",
105 color: "white",
106 fontSize: "1.5em",
TheCodedProff054b8c2023-03-03 15:32:19 -0500107 }}>Transcript for: {props.data.for.username}#{props.data.for.discriminator} | In #{<a href={`https://discord.com/channels/${props.data.guild}/${props.data.channel}`}>{props.channelName}</a>} | Type: {props.data.type}</div>
TheCodedProfda197f22023-03-01 18:15:51 -0500108 <div style={{
109 width: "100vw",
110 backgroundColor: "var(--theme-ui-colors-background)", //Can we change this to be not black please. it's too contrast heavy
111 margintop: "-50px",
112 padding: "25px",
113 paddingTop: "10px",
114 transition: "all 0.3s ease-in-out"
115 }}>
116 {
117 props.data.messages.map((message, index) => {
118 return <Message key={index.toString()} message={message} />
119 })
120 }
121 </div>
pineafanf5dd1872023-02-28 17:33:16 +0000122 </div>
pineafanf5dd1872023-02-28 17:33:16 +0000123}
124
125export default Index;
126export async function getServerSideProps(ctx) {
TheCodedProf2a69bd92023-03-02 16:36:32 -0500127 if(!ctx.params.code) {
pineafanf5dd1872023-02-28 17:33:16 +0000128 return {
129 redirect: {
130 destination: '/nucleus/transcript/about',
131 permanent: true
132 }
133 }
134 }
Skyler Grey6c3d4632023-03-06 09:51:21 +0000135 /*return {
TheCodedProff054b8c2023-03-03 15:32:19 -0500136 redirect: {
Samuel Shuert5bf5b902023-03-05 21:18:38 -0500137 destination: `/nucleus/transcript/${ctx.params.code}/human?key=${ctx.query.key}&iv=${ctx.query.iv}`,
TheCodedProff054b8c2023-03-03 15:32:19 -0500138 permanent: true
pineafanf5dd1872023-02-28 17:33:16 +0000139 }
Skyler Grey6c3d4632023-03-06 09:51:21 +0000140 }*/
141 let code;
142 console.log("getting props")
143 try {
144 code = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}transcript/${ctx.params.code}?key=${ctx.query.key}&iv=${ctx.query.iv}`));
145 } catch (e) {
146 console.log(e)
147 console.log(`${process.env.NUCLEUS_CALLBACK}transcript/${ctx.params.code}?key=${ctx.query.key}&iv=${ctx.query.iv}`)
148 return {
149 redirect: {
150 destination: '/nucleus/transcript/invalid',
151 permanent: true
152 }
153 }
pineafanf5dd1872023-02-28 17:33:16 +0000154 }
Skyler Grey6c3d4632023-03-06 09:51:21 +0000155 const linkedData = await parse(code.data.messages)
TheCodedProff054b8c2023-03-03 15:32:19 -0500156
Skyler Grey6c3d4632023-03-06 09:51:21 +0000157 const channelName = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}channels/${code.data.channel}`)).data;
TheCodedProff054b8c2023-03-03 15:32:19 -0500158
Skyler Grey6c3d4632023-03-06 09:51:21 +0000159 return {
160 props: {
161 data: code.data,
162 linkedData,
163 channelName
164 }
165 }
Samuel Shuert5bf5b902023-03-05 21:18:38 -0500166}