SendBird使用文件作为封面创建GroupChannel

Dan*_*y Y 7 javascript sendbird

我正在尝试创建一个带封面照片的群组频道,

this.sendBirdInstance.GroupChannel.createChannelWithUserIds(userIds, true, this.groupName, this.groupPhotoFile, '', function (createdChannel, error) {
...
}
Run Code Online (Sandbox Code Playgroud)

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

coverUrl:封面图像的文件或URL,您可以将其渲染到UI中.

但是在添加文件时,我总是得到: "SendBirdException", code: 800110, message: "Invalid arguments."

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

谢谢,

Dav*_*vid 3

正如您已经经历过的(我几天前看到您在 GitHub 中创建了一个问题),SendBird 的支持有点不可靠。

事实上,他们只提供了 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
});
Run Code Online (Sandbox Code Playgroud)

您正在使用这样的函数:

createChannelWithUserIds(o, n, t, r, a, s); 
Run Code Online (Sandbox Code Playgroud)

所以第四个参数(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
Run Code Online (Sandbox Code Playgroud)

该参数无效。

您的文件不是字符串,不是 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({})
}
Run Code Online (Sandbox Code Playgroud)

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

我无法说出开发人员通过此函数验证文件时的意图是什么,而是一个标准的 FILE 对象:

  • 其原型链中有属性,但未直接分配给实例,因此第一个条件是true
  • 当 stringify 在当今所有主流浏览器中返回一个空对象时(并不总是这样),所以第二个条件也是true

正如我们之前看到的,我们需要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 )
}
Run Code Online (Sandbox Code Playgroud)

但我不会打扰它。它可以在未来的版本中用于不同的目的。

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

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

为此,您可以重写 SendBird 实例中的函数:

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

sb.GroupChannel.createChannelWithUserIds = function(){ ... };
Run Code Online (Sandbox Code Playgroud)

但不能保证它能正常工作,并且可能会在未来的版本中出现故障,因此我只需编辑 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,new p("Invalid arguments.",J.INVALID_PARAMETER),s);
Run Code Online (Sandbox Code Playgroud)

和:

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,new p("Invalid arguments.",J.INVALID_PARAMETER),s);
Run Code Online (Sandbox Code Playgroud)

在当前版本(v3.0.41)中,您会发现上面的代码有两个重合之处:一个是 for ,createChannel另一个是 for createChannelWithUserIds,您可以将两者替换。

当然,编辑 .js 文件很烦人,因为每次升级 SendGrid 时都需要小心地替换代码。你可以在 CI 管道中创建一个自动任务来为你完成这件事,你想。

希望 SendGrid 开发人员能够承认您的问题并在未来的版本中修复它。