AAGaming00 / rich-quotes

MIT License
9 stars 3 forks source link

Attempt to find a function Discord has to normalize embed keys #54

Open github-actions[bot] opened 4 years ago

github-actions[bot] commented 4 years ago

Attempt to find a function Discord has to normalize embed key's

https://github.com/AAGaming00/rich-quotes/blob/986309ac736cf23f625e2cdfacca194e452e0e69/components/Quote.jsx#L74

const { Tooltip, Icon, Spinner } = require('powercord/components');

const RequestError = require('./RequestError');
const RenderError = require('./RenderError');

const getMsg = require('../utils/getMessage.js');
const embedHandler = require('../utils/embedHandler.js');

let lastFetch = 0;

module.exports = class RichQuote extends React.Component {
  constructor (props) {
    super(props); this.state = { searchStatus: false, errorParams: false };
  }

  static getDerivedStateFromProps (props, state) {
    return { ...Object.assign({}, props), ...state };
  }

  async linkRes() {
    const MessageC = await getModule(m => m.prototype && m.prototype.getReaction && m.prototype.isSystemDM);
    const parser = await getModule(["parse", "parseTopic"]);

    if (this.state.link[0] !== '000000000000000000') {
      const getWithQueue = (() => {
          let pending = Promise.resolve()

          const run = async ([guildId, channelId, messageId]) => {
            try { await pending } finally {
              return getMsg(guildId, channelId, messageId, lastFetch);
            }
          }
          return (link) => (pending = run(link));
        })(),
        originalMessage = await getWithQueue(this.state.link);

      lastFetch = Date.now();

      if (originalMessage.error) {
        this.state.errorParams = originalMessage;
        this.state.errorParams.link = this.state.link;
      }
      else {
        const { getChannel } = await getModule(['getChannel']);
        const { renderSimpleAccessories } = await getModule(m => m?.default?.displayName == 'renderAccessories');

        let messageData = { ...originalMessage };
        let hasEmbedSpoilers = false;

        if (this.state.settings.displayEmbeds) embedHandler(messageData, this.state.settings, hasEmbedSpoilers);
        else { 
          messageData.embeds = [];
          messageData.attachments = [];
        }

        if (!this.state.settings.displayReactions) messageData.reactions = [];

        this.state.content = await parser.parse(
          messageData.content.trim(), true, 
          { channelId: this.state.thisChannel }
        );

        this.state.author = messageData.author;

        this.state.message = await new MessageC({ ...messageData });
        this.state.channel = await getChannel(messageData.channel_id);

        if (this.state.settings.displayEmbeds && (this.state.message.embeds?.length !== 0 || this.state.message.attachments?.length !== 0)) {
          if (this.state.message.embeds?.length !== 0) {
            // @todo Attempt to find a function Discord has to normalize embed key's
            const fixers = [['description','rawDescription'],['title','rawTitle']];

            this.state.message.embeds.forEach((e, i) => fixers.forEach((f) => {
              if (e[f[0]]) {
                this.state.message.embeds[i][f[1]] = e[f[0]];
                delete this.state.message.embeds[i][f[0]];
              }
            }))
          }

          this.state.accessories = renderSimpleAccessories({ message: this.state.message, channel: this.state.channel}, hasEmbedSpoilers);
        } else this.state.accessories = false;
      }
    } else {
      // funni preview handler
      const getCurrentUser = await getModule([ 'getCurrentUser' ]);

      this.state.content = await parser.parse(
       'Check out this preview', true, 
        { channelId: '000000000000000000' }
      );

      this.state.author = await getCurrentUser.getCurrentUser();

      this.state.message = await new MessageC({ ...'' });
      this.state.channel = { id: 'owo', name: 'test-channel'};
      this.state.link = ['000000000000000000','000000000000000000','000000000000000000'];
    }

    this.setState(this.state);
  }

  async searchRes() {
    const { transitionTo } = await getModule([ 'transitionTo' ]);

    const setStatus = (s, link) => this.setState({ searchStatus: s, link });

    // contains code by Bowser65 (Powercord's server, https://discord.com/channels/538759280057122817/539443165455974410/662376605418782730)
    function searchAPI (content, author_id, max_id, id, dm, asc) {

924b5b1a6c8194010ff13448937c9a01101dc7cf