SendBird 以文件为封面创建 GroupChannel

SendBird create GroupChannel with file as cover

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

this.sendBirdInstance.GroupChannel.createChannelWithUserIds(userIds, true, this.groupName, this.groupPhotoFile, '', function (createdChannel, error) {
...
}

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

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

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

有没有办法用文件而不是 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 object/array 是否等于空的 object/array.

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

  • 在其原型链中有属性,但没有直接赋值给实例,所以第一个条件是true
  • when stringify returns a empty object 现在所有主流浏览器(it was not always like so),所以第二个条件也是true.

正如我们之前看到的,我们需要h()到return false:如果returns true.[=36=,则验证失败]

要解决此问题,您可以将 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,new p("Invalid arguments.",J.INVALID_PARAMETER),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,new p("Invalid arguments.",J.INVALID_PARAMETER),s);

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

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

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