blob: 9a0c5424a1a51916769e09ee57bceba0eed218e6 [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, '');
30 const username = (await Axios.get(`http://${process.env.NUCLEUS_CALLBACK}/users/${userID}`)).data;
31 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, '');
39 const channelName = (await Axios.get(`http://${process.env.NUCLEUS_CALLBACK}/channels/${channelID}`)).data;
40 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 }
pineafanf5dd1872023-02-28 17:33:16 +0000135 return {
TheCodedProff054b8c2023-03-03 15:32:19 -0500136 redirect: {
TheCodedProf4f357112023-03-03 17:19:34 -0500137 destination: `/nucleus/transcript/${ctx.params.code}/human?key=${ctx.query.key}`,
TheCodedProff054b8c2023-03-03 15:32:19 -0500138 permanent: true
pineafanf5dd1872023-02-28 17:33:16 +0000139 }
140 }
TheCodedProff054b8c2023-03-03 15:32:19 -0500141 // let code;
142 // try {
TheCodedProf4f357112023-03-03 17:19:34 -0500143 // code = (await Axios.get(`http://${process.env.NUCLEUS_CALLBACK}/transcript/${ctx.params.code}?key=${ctx.query.key`))
TheCodedProff054b8c2023-03-03 15:32:19 -0500144 // } catch (e) {
145 // return {
146 // redirect: {
147 // destination: '/nucleus/transcript/invalid',
148 // permanent: true
149 // }
150 // }
151 // }
152 // const linkedData = await parse(code.data.messages)
153
154 // const channelName = (await Axios.get(`http://${process.env.NUCLEUS_CALLBACK}/channels/${code.data.channel}`)).data;
155
156 // return {
157 // props: {
158 // data: code.data,
159 // linkedData,
160 // channelName
161 // }
162 // }
pineafanf5dd1872023-02-28 17:33:16 +0000163}