From 753ea38654d2eac08acaf9f1f809b7b2a27f7a7d Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 15 Aug 2017 14:01:50 +0100 Subject: [PATCH] Add UserAddress --- src/UserAddress.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/UserAddress.js diff --git a/src/UserAddress.js b/src/UserAddress.js new file mode 100644 index 0000000000..9eee48629d --- /dev/null +++ b/src/UserAddress.js @@ -0,0 +1,54 @@ +/* +Copyright 2017 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +const emailRegex = /^\S+@\S+\.\S+$/; + +const mxidRegex = /^@\S+:\S+$/; + +import PropTypes from 'prop-types'; +export const addressTypes = [ + 'mx', 'email', +]; + +// PropType definition for an object describing +// an address that can be invited to a room (which +// could be a third party identifier or a matrix ID) +// along with some additional information about the +// address / target. +export const UserAddressType = PropTypes.shape({ + addressType: PropTypes.oneOf(addressTypes).isRequired, + address: PropTypes.string.isRequired, + displayName: PropTypes.string, + avatarMxc: PropTypes.string, + // true if the address is known to be a valid address (eg. is a real + // user we've seen) or false otherwise (eg. is just an address the + // user has entered) + isKnown: PropTypes.bool, +}); + +export function getAddressType(inputText) { + const isEmailAddress = emailRegex.test(inputText); + const isMatrixId = mxidRegex.test(inputText); + + // sanity check the input for user IDs + if (isEmailAddress) { + return 'email'; + } else if (isMatrixId) { + return 'mx'; + } else { + return null; + } +}