From aee66976f6839d69feea23416c0235e4dfe8636f Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Fri, 8 Apr 2022 19:01:32 -0400 Subject: [PATCH] Style improvements in client.js --- node/src/client.js | 47 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/node/src/client.js b/node/src/client.js index bc28579..92ff962 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -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 } }