Style improvements in client.js

This commit is contained in:
Andrew Ferrazzutti 2022-04-08 19:01:32 -04:00
parent a22fe2630f
commit aee66976f6

View File

@ -34,6 +34,12 @@ const { KnownChatType } = chat
import { emitLines, promisify } from "./util.js"
/**
* @typedef {Object} ChannelProps
* @property {Long} id
* @property {ChannelType} type
*/
ServiceApiClient.prototype.requestFriendList = async function() {
const res = await this._client.requestData(
@ -132,24 +138,22 @@ class UserClient {
}
/**
* @param {Object} channel_props
* @param {Long} channel_props.id
* @param {ChannelType} channel_props.type
* @param {ChannelProps} channelProps
*/
async getChannel(channel_props) {
let channel = this.#talkClient.channelList.get(channel_props.id)
async getChannel(channelProps) {
let channel = this.#talkClient.channelList.get(channelProps.id)
if (channel) {
return channel
} else {
const channelList = getChannelListForType(
this.#talkClient.channelList,
channel_props.type
channelProps.type
)
const res = await channelList.addChannel({
channelId: channel_props.id,
channelId: channelProps.id,
})
if (!res.success) {
throw new Error(`Unable to add ${channel_props.type} channel ${channel_props.id}`)
throw new Error(`Unable to add ${channelProps.type} channel ${channelProps.id}`)
}
return res.result
}
@ -347,10 +351,10 @@ export default class PeerClient {
/**
* @param {string} mxid
* @param {Object} channel_props
* @param {ChannelProps} channelProps
*/
async #getUserChannel(mxid, channel_props) {
return await this.#getUser(mxid).getChannel(channel_props)
async #getUserChannel(mxid, channelProps) {
return await this.#getUser(mxid).getChannel(channelProps)
}
/**
@ -424,7 +428,7 @@ export default class PeerClient {
/**
* @param {Object} req
* @param {string} req.mxid
* @param {Object} req.channel_props
* @param {ChannelProps} req.channel_props
*/
getPortalChannelInfo = async (req) => {
const talkChannel = await this.#getUserChannel(req.mxid, req.channel_props)
@ -442,7 +446,7 @@ export default class PeerClient {
/**
* @param {Object} req
* @param {string} req.mxid
* @param {Object} req.channel_props
* @param {ChannelProps} req.channel_props
*/
getParticipants = async (req) => {
const talkChannel = await this.#getUserChannel(req.mxid, req.channel_props)
@ -452,7 +456,7 @@ export default class PeerClient {
/**
* @param {Object} req
* @param {string} req.mxid
* @param {Object} req.channel_props
* @param {ChannelProps} req.channel_props
* @param {?Long} req.sync_from
* @param {?Number} req.limit
*/
@ -506,7 +510,7 @@ export default class PeerClient {
/**
* @param {Object} req
* @param {string} req.mxid
* @param {Object} req.channel_props
* @param {ChannelProps} req.channel_props
* @param {string} req.text
* @param {?ReplyAttachment} req.reply_to
* @param {?MentionStruct[]} req.mentions
@ -524,7 +528,7 @@ export default class PeerClient {
/**
* @param {Object} req
* @param {string} req.mxid
* @param {Object} req.channel_props
* @param {ChannelProps} req.channel_props
* @param {int} req.type
* @param {number[]} req.data
* @param {string} req.name
@ -676,11 +680,18 @@ export default class PeerClient {
* @param {ChannelType} channelType
*/
function getChannelListForType(channelList, channelType) {
return isChannelTypeOpen(channelType) ? channelList.open : channelList.normal
}
/**
* @param {ChannelType} channelType
*/
function isChannelTypeOpen(channelType) {
switch (channelType) {
case "OM":
case "OD":
return channelList.open
return true
default:
return channelList.normal
return false
}
}