【问题标题】:SendBird create GroupChannel with file as coverSendBird 以文件为封面创建 GroupChannel
【发布时间】:2017-09-20 03:38:16
【问题描述】:

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

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

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

coverUrl : 封面图片的文件或 URL,您可以获取到 渲染到 UI 中。

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

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

谢谢,

【问题讨论】:

  • 您使用的是哪个版本的 SDK?
  • 我使用的是 3.0.40 版本
  • 您是否更幸运使用Platform API?请注意,您可以在文档中阅读,您必须发送Multipart request。如果您可以通过 REST API 发送文件,那么也许可以覆盖返回 800110 错误的 js 检查。
  • 谢谢大卫,API 是唯一的方法吗? ,因为在这种情况下,我必须检查 CORS 并且从 javascript 发送多部分请求并不简单,但如果您确定无法使用 javascript SDK,我会走这条路。

标签: javascript sendbird


【解决方案1】:

正如你已经体验过的(我看到你几天前创建了一个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 也不是 undefined,所以一切都归结为 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 对象:

  • 在其原型链中有属性,但不直接分配给实例,所以第一个条件是true
  • 现在所有主流浏览器中stringify返回空对象时(it was not always like so),所以第二个条件也是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 )
}

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

所以最好的办法是将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 开发人员能够确认您的问题并在未来的版本中修复它。

【讨论】:

  • 我收到“无效参数”。
  • 哇它好用!我会告诉 sendbird 这个错误 :D 非常感谢
  • 很高兴听到这个丹尼!稍后我会重写它作为正确的答案,以便对更多人有用。
  • 不知何故迟到了,但正如承诺的那样,我已经用完整的细节重写了我的答案。
猜你喜欢
  • 1970-01-01
  • 2014-01-31
  • 2015-04-28
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多