diff --git a/packages/chat/src/index.js b/packages/chat/src/index.js index 5dd1326..9a94082 100644 --- a/packages/chat/src/index.js +++ b/packages/chat/src/index.js @@ -19,6 +19,8 @@ import defaultState, {channelsToHydrate} from './state'; import joinChannel from './join-channel'; import leaveChannel from './leave-channel'; +export {ensureCanonical}; + export * from './state'; export default { diff --git a/packages/user/src/client/effects.js b/packages/user/src/client/effects.js index 9267468..79436a4 100644 --- a/packages/user/src/client/effects.js +++ b/packages/user/src/client/effects.js @@ -25,13 +25,13 @@ export default ({config: {'%socket': socket}}) => ({ [submitAddFriend]: async ({dispatch, getState}, {payload: name}) => { const state = getState(); const userId = idSelector(state); - const id = await socket.send(['AddFriend', {name}]); + const [id, canonicalName] = await socket.send(['AddFriend', {name}]); dispatch(addFriendship({ addeeId: id, adderId: userId, status: 'pending', })); - dispatch(setUsernames({[id]: name})); + dispatch(setUsernames({[id]: canonicalName})); }, [submitBlock]: async ({dispatch}, {payload: id}) => { dispatch(block(id)); diff --git a/packages/user/src/packets/add-friend.server.js b/packages/user/src/packets/add-friend.server.js index a6d3f04..1431ca6 100644 --- a/packages/user/src/packets/add-friend.server.js +++ b/packages/user/src/packets/add-friend.server.js @@ -1,4 +1,6 @@ import {ModelMap} from '@latus/db'; +import {parseChannel} from '@reddichat/core'; +import {ensureCanonical} from '@reddichat/chat'; import AddFriend from './add-friend'; @@ -10,11 +12,13 @@ export default (latus) => class AddFriendServer extends AddFriend(latus) { Friendship, User, } = ModelMap(latus); + const chat = await ensureCanonical(latus, {type: 'u', name}); + const canonical = parseChannel(chat.name); const adderId = req.user.id; const adderName = req.user.redditUsername; const user = ( - await User.findOne({where: {redditUsername: name}}) - || await User.create({redditUsername: name}) + await User.findOne({where: {redditUsername: canonical.name}}) + || await User.create({redditUsername: canonical.name}) ); const addeeId = user.id; const addeeName = user.redditUsername; @@ -34,7 +38,7 @@ export default (latus) => class AddFriendServer extends AddFriend(latus) { : ['AddFriend', {addeeId, adderId, name: id === adderId ? addeeName : adderName}]; socket.to(`/u/${id}`).send(packet); }); - return addeeId; + return [addeeId, canonical.name]; } };