Skip to content

Concat middleware failed to run next middleware twice #2838

@compulim

Description

@compulim

Version

master

Describe the bug

When writing a new (attachment) middleware that render the same downstream middleware twice, it failed.

For example, the following code using concatMiddleware.js will fail to return the correct value (210). It "skipped" the next(value + 1), thus, returned 12 instead.

const combine = () => next => value => {
  return next(value) + next(value + 1);
};

const oddMultiplyByTenAndEvenMultiplyByHundred = () => next => value => {
  if (value % 2) {
    return value * 10;
  }

  return value * 100;
};

const middleware = concatMiddleware(combine, oddMultiplyByTenAndEvenMultiplyByHundred);
const work = middleware()(value => value);

expect(work(1)).toEqual(210);

Steps to reproduce

  1. Write a new attachment middleware that call next() twice. So it will render attachment twice.
  2. Open Web Chat
  3. Type "echo Hello, World!"

Expected behavior

It should render the attachment ("Hello, World!") twice.

Instead, the first time it render the attachment, it succeeded. The second render failed with "no renderer for this attachment" error.

Additional context

IC3 team want to render a photo upload with the thumbnail and file name. So it could looks like the following screenshot.

image

Once the bug is fixed, the code below should produce the screenshot above.

function createUploadMiddleware() {
  return () => next => ({
    activity = {},
    activity: { from: { role } } = {},
    attachment,
    attachment: { contentType, contentUrl, thumbnailUrl } = {}
  }) => {
    if (role === 'user' && /^image\//u.test(contentType) && thumbnailUrl) {
      const patchedAttachment = {
        ...attachment,
        contentType: 'application/octet-stream',
        thumbnailUrl: undefined
      };
      const patchedAttachments = activity.attachments.map(target =>
        target === attachment ? patchedAttachment : target
      );

      return (
        <React.Fragment>
          {next({ activity, attachment })}
          {next({ activity: { ...activity, attachments: patchedAttachments }, attachment: patchedAttachment })}
        </React.Fragment>
      );
    }

    return next({ activity, attachment });
  };
}

[Bug]

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or an unintended behavior.external-omnichannel

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions