Style improvements in client.js

This commit is contained in:
Andrew Ferrazzutti 2022-04-08 19:01:32 -04:00
parent a22fe2630f
commit aee66976f6
1 changed files with 29 additions and 18 deletions

View File

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