javascript – SendBird创建GroupChannel文件作为封面

我正在尝试创建一个带封面照片的群组频道,
this.sendBirdInstance.GroupChannel.createChannelWithUserIds(userIds,true,this.groupName,this.groupPhotoFile,'',function (createdChannel,error) {
...
}

根据文档,我可以添加网址或文件

coverUrl : the file or URL of the cover image,which you can fetch to
render into the UI.

但是在添加文件时,我总是得到:“SendBirdException”,代码:800110,消息:“无效的参数.”

有没有办法用文件而不是url创建一个组(因为我希望用户上传文件)?

谢谢,

解决方法

正如您已经经历过的(我看到您几天前创建了一个 issue in GitHub)SendBird的支持有点不可靠.

事实上,他们提供了just a minified version的JavaScript SDK(我个人觉得非常差),这两者都有帮助.

无论如何,我可以隔离createChannelWithUserIds函数

! function (e,n) {
    // ... 
}(this,function () {
    // ... 
    var h = function (e) {
        for (var n in e) if (e.hasOwnProperty(n)) return !1;
        return Array.isArray(e) ? JSON.stringify(e) === JSON.stringify([]) : JSON.stringify(e) === JSON.stringify({})
    },// ... 
    A = function () { // it returns SendBird function
        // ... 
        var w = function (e) { // w is this.GroupChannel
            // ... 
            w.createChannelWithUserIds = function () {
                // ...
                // here comes the param validation (I've added spaces for a better lecture):
                if (!Array.isArray(e) || "boolean" != typeof n || "string" != typeof t && null !== t && void 0 !== t || "string" != typeof r && h(r) && null !== r && void 0 !== r || "string" != typeof a && null !== a && void 0 !== a || "string" != typeof i && null !== i && void 0 !== i) return void U(null,new p("Invalid arguments.",J.INVALID_ParaMETER),s);
                // It will return "Invalid arguments." if any of the conditions evaluates to true       
                // ... 
            }
        }
    }
    return function () {
        // ... 
    }().SendBird
});

您正在使用这样的功能

createChannelWithUserIds(o,n,t,r,a,s);

所以第四个参数(r)是coverURL:带有封面图片文件(this.groupPhotoFile);

它的验证基本上是这样说的:

"string" != typeof r    // if `r` is not a string (a URL)
&& h(r)                 // and the returned value of function h(r) is true
&& null !== r           // and it is not null
&& void 0 !== r         // and it is not undefined

该参数无效.

您的文件不是字符串,不是null而且未定义,因此所有内容都归结为h()函数

var h = function (e) {
    for (var n in e) if (e.hasOwnProperty(n)) return !1;
    return Array.isArray(e) ? JSON.stringify(e) === JSON.stringify([]) : JSON.stringify(e) === JSON.stringify({})
}

如果对象具有任何属性是对象本身的成员(即不属于原型链的属性),则上面的函数首先检查.
然后,如果它没有任何属性,则检查stringify对象/数组是否等于空对象/数组.

我不能说开发人员在通过此函数验证文件时的意图是什么,但是标准的FILE对象:

>在其原型链中具有属性,但未直接分配给实例,因此第一个条件为真.
>当stringify现在在所有主流浏览器中返回一个空对象(it was not always like so)时,第二个条件也是如此.

正如我们之前看到的,我们需要h()返回false:如果返回true,则验证失败.

解决此问题,您可以将h()函数更改为:

var h = function( e ){
    return !(e instanceof File);
    // or return e.constructor != File;
    // or return Object.getPrototypeOf( e ) != File.prototype );
    // or return e.__proto__ != File.prototype )
    // or return e.constructor.prototype != File.prototype )
}

但我不会惹它.它可以在具有不同目的的未来版本中使用.

因此,最好的办法是将createChannelWithUserIds()函数更改为:

>从验证中删除对h()函数调用.
>将其替换为对您自己的文件验证的调用

为此,您可以覆盖SendBird实例中的函数

var sb = new SendBird( { appId: ... } );

sb.GroupChannel.createChannelWithUserIds = function(){ ... };

但它不能保证工作,并且可能在将来的版本中中断,所以我只想编辑SendBird.min.js文件.
换句话说,替换:

if(!Array.isArray(e)||"boolean"!=typeof n||"string"!=typeof t&&null!==t&&void 0!==t||"string"!=typeof r&&h(r)&&null!==r&&void 0!==r||"string"!=typeof a&&null!==a&&void 0!==a||"string"!=typeof i&&null!==i&&void 0!==i)return void U(null,s);

有:

if(!Array.isArray(e)||"boolean"!=typeof n||"string"!=typeof t&&null!==t&&void 0!==t||"string"!=typeof a&&null!==a&&void 0!==a||"string"!=typeof i&&null!==i&&void 0!==i)return void U(null,s);

在当前版本(v3.0.41)中,您将找到上述代码的两个巧合:一个用于createChannel,另一个用于createChannelWithUserIds,您可以替换它们.

当然,编辑.js文件很烦人,因为每次升级SendGrid时都需要注意更换代码.
您可以在CI管道中创建一个自动任务,以便为您完成.

希望SendGrid开发人员能够确认您的问题并在将来的版本中修复它.

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面