If you want to have the guarantee that only one channel exists between N users you can instantiate the Channel without ID and the list of its members.(如果要保证N个用户之间仅存在一个频道,则可以实例化没有ID的频道及其成员列表。)
When you do that, the API will ensure that only one channel with the list of members exists (order of the members does not matter).(当您执行此操作时,API将确保仅存在一个包含成员列表的频道(成员的顺序无关紧要)。)
const distinctChannel = conversation.channel("messaging","", {
members: [user1.id, user2.id],
});
await distinctChannel.create();
Since the channel data is going to be the same for both members;(由于两个成员的渠道数据都将相同;) I suggest to not store the image
field the way you do in your code example.(我建议不要image
在代码示例中那样存储image
字段。) It is easier to use the "other" member profile image as the channel image when you render the conversation.(呈现对话时,使用“其他”成员个人资料图像作为频道图像会更容易。)
For example:(例如:)
let channelImage = "https://path/to/fallback/image";
otherMembers = channel.members.filter(member => member.user.id != currentUser.id);
if otherMembers.length > 0 {
channelImage = otherMembers[0].image;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…