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, ''); |
| 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 | |
| 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 | } |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 135 | return { |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 136 | redirect: { |
TheCodedProf | 4f35711 | 2023-03-03 17:19:34 -0500 | [diff] [blame] | 137 | destination: `/nucleus/transcript/${ctx.params.code}/human?key=${ctx.query.key}`, |
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 | } |
| 140 | } |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 141 | // let code; |
| 142 | // try { |
TheCodedProf | 4f35711 | 2023-03-03 17:19:34 -0500 | [diff] [blame] | 143 | // code = (await Axios.get(`http://${process.env.NUCLEUS_CALLBACK}/transcript/${ctx.params.code}?key=${ctx.query.key`)) |
TheCodedProf | f054b8c | 2023-03-03 15:32:19 -0500 | [diff] [blame] | 144 | // } 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 | // } |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 163 | } |