Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to pass in an object into sendMessage instead of a string? #37

Open
zuus-keith opened this issue Dec 6, 2018 · 4 comments
Assignees
Labels
bug-in-dependency dependency of this package has a bug

Comments

@zuus-keith
Copy link

So I am trying to attach other fields other than just a string to a message (i.e. other headers that will be used to be displayed with the message) and found in the MessagesCollection schema that the message body can accept either a string or an object.

But attempting to execute this:

let convo = ConversationsCollection.findOne()
convo.sendMessage({a:1, b:2})

Note: A conversation document does exist and is failing on sendMessage.

this error was thrown:

Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' http://*.googleapis.com https://*.googleapis.com http://*.gstatic.com https://*.gstatic.com http://testing-zuus-mobile.s3.amazonaws.com https://testing-zuus-mobile.s3.amazonaws.com http://zuus-profile.s3.amazonaws.com https://zuus-profile.s3.amazonaws.com http://meteor.local http://*.meteor.com https://*.meteor.com http://*.cloudfront.net https://*.cloudfront.net".
modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:43834

    at Function (<anonymous>)
    at modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:43834
    at apply (modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:42819)
    at modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:43871
    at apply (modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:42821)
    at modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:43062
    at template (modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:43833)
    at MessageBox.message (modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:40676)
    at SimpleSchema.messageForError (modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:29982)
    at ValidationContext.keyErrorMessage (modules.js?hash=c55094c9810325902eeae61b1408a9880de63896:30516)

Am I making the wrong assuming that it is possible to pass in an object?
I know a workaround is to wrap the object with JSON.stringify and JSON.parse but I really don't want to do this unless I have to.

@StorytellerCZ
Copy link
Contributor

StorytellerCZ commented Dec 6, 2018

Strange. sendMessage should accept object (though for a different reason than you want to use, still that is fine). The error is also strange as it pertains to your browser-policy. Try disabling it for a test if that changes anything.

@copleykj
Copy link
Owner

copleykj commented Dec 6, 2018

The error in this case is definitely weird, but I do think that issue lies in both in the definition for body, and a bug in SimpleSchema. Looking into this, the schema definition for body is specified as SimpleSchema.oneOf(String, Object) which would lead to the thought that it would accept either, except that objects have keys that would also need to either be defined in the schema so they can be validated, or blackbox needs to be specified in the field definition to allow any key in that field to pass validation.

I initially thought that I could just set add blackbox:true to the definition for the body field and SimpleSchema would use that when it encountered an object instead of a string. This however does not appear to be the case. If I change the definition to just be a type of object and specify blackbox to be true, then everything works fine.

Fortunately this is easily mitigated just by extending the schema with a new definition for the body field in you project..

import { MessagesCollection } from 'meteor/socialize:messaging';

MessagesCollection.attachSchema(new SimpleSchema({
    body: {
        type: Object,
        blackbox: true,
    },
}));

This will replace the body definition and leave the rest of the schema in tact.

I'll look into how simple schema handles oneOf and see about opening an issue and PR to fix it and then once that gets merged I'll issue a patch for this package.

@StorytellerCZ
Copy link
Contributor

@copleykj Any updates on this?

@copleykj
Copy link
Owner

Sorry bud, I've been busy and haven't had a chance to look into this further.

@copleykj copleykj added the bug-in-dependency dependency of this package has a bug label Aug 5, 2020
@copleykj copleykj self-assigned this Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-in-dependency dependency of this package has a bug
Projects
None yet
Development

No branches or pull requests

3 participants