Fast-fail when trying to create a DM with a non-friend user

This commit is contained in:
Andrew Ferrazzutti 2022-05-16 02:00:30 -04:00
parent 1541732d0b
commit 47b9623446
1 changed files with 18 additions and 1 deletions

View File

@ -1212,8 +1212,9 @@ export default class PeerClient {
*/ */
createDirectChat = async (req) => { createDirectChat = async (req) => {
const userClient = this.#getUser(req.mxid) const userClient = this.#getUser(req.mxid)
const channelList = userClient.talkClient.channelList.normal await this.#requireInFriendsList(userClient.serviceClient, req.user_id)
const channelList = userClient.talkClient.channelList.normal
const createChannel = const createChannel =
!req.user_id.equals(userClient.userId) !req.user_id.equals(userClient.userId)
? channelList.createChannel.bind(channelList, { ? channelList.createChannel.bind(channelList, {
@ -1237,6 +1238,22 @@ export default class PeerClient {
return res return res
} }
/**
* @param {ServiceApiClient} serviceClient
* @param {Long} id
*/
async #requireInFriendsList(serviceClient, id) {
let listRes = await serviceClient.requestFriendList()
if (!listRes.success) {
this.error("Failed to check friends list")
throw listRes
}
const isFriend = -1 != listRes.result.friends.findIndex(friend => id.equals(friend.userId))
if (!isFriend) {
throw new ProtocolError("This user is not in your friends list")
}
}
/** /**
* @param {object} req * @param {object} req
* @param {string} req.mxid * @param {string} req.mxid