pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 1 | import Axios from 'axios'; |
| 2 | import React from 'react'; |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame] | 3 | import Message from '../../../../Components/Transcripts/Message'; |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 4 | import _ from 'lodash'; |
| 5 | |
| 6 | const emojiRegex = /(<a?:.+:\D+>)/g; |
| 7 | const userRegex = /(<@!?\d+>)/g; |
| 8 | const channelRegex = /(<#\d+>)/g; |
| 9 | |
| 10 | async 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 Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 30 | const username = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}users/${userID}`)).data; |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 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, ''); |
Skyler Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 39 | const channelName = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}channels/${channelID}`)).data; |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 40 | dict["channels"][channel] = channelName; |
| 41 | } |
| 42 | } |
| 43 | // console.log("parseText", dict) |
| 44 | return dict |
| 45 | } |
| 46 | |
| 47 | async 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 | } |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 94 | |
| 95 | function Index(props) { |
TheCodedProf | da197f2 | 2023-03-01 18:15:51 -0500 | [diff] [blame] | 96 | 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", |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 107 | }}>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> |
TheCodedProf | da197f2 | 2023-03-01 18:15:51 -0500 | [diff] [blame] | 108 | <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> |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 122 | </div> |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | export default Index; |
| 126 | export async function getServerSideProps(ctx) { |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame] | 127 | if(!ctx.params.code) { |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 128 | return { |
| 129 | redirect: { |
| 130 | destination: '/nucleus/transcript/about', |
| 131 | permanent: true |
| 132 | } |
| 133 | } |
| 134 | } |
Skyler Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 135 | /*return { |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 136 | redirect: { |
Samuel Shuert | 5bf5b90 | 2023-03-05 21:18:38 -0500 | [diff] [blame] | 137 | destination: `/nucleus/transcript/${ctx.params.code}/human?key=${ctx.query.key}&iv=${ctx.query.iv}`, |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 138 | permanent: true |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 139 | } |
Skyler Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 140 | }*/ |
| 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 | } |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 154 | } |
Skyler Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 155 | const linkedData = await parse(code.data.messages) |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 156 | |
Skyler Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 157 | const channelName = (await Axios.get(`${process.env.NUCLEUS_CALLBACK}channels/${code.data.channel}`)).data; |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 158 | |
Skyler Grey | 6c3d463 | 2023-03-06 09:51:21 +0000 | [diff] [blame^] | 159 | return { |
| 160 | props: { |
| 161 | data: code.data, |
| 162 | linkedData, |
| 163 | channelName |
| 164 | } |
| 165 | } |
Samuel Shuert | 5bf5b90 | 2023-03-05 21:18:38 -0500 | [diff] [blame] | 166 | } |