Chatwoot/app/javascript/widget/store/modules/contacts.js
Sojan Jose 791d90c6b7
chore: Migrate PubSub Token to contact inbox (#3434)
At present, the websocket pubsub tokens are present at the contact objects in chatwoot. A better approach would be to have these tokens at the contact_inbox object instead. This helps chatwoot to deliver the websocket events targetted to the specific widget connection, stop contact events from leaking into other chat sessions from the same contact.

Fixes #1682
Fixes #1664

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2021-11-22 23:32:17 +05:30

73 lines
1.7 KiB
JavaScript

import ContactsAPI from '../../api/contacts';
const state = {
currentUser: {},
};
const SET_CURRENT_USER = 'SET_CURRENT_USER';
export const getters = {
getCurrentUser(_state) {
return _state.currentUser;
},
};
export const actions = {
get: async ({ commit }) => {
try {
const { data } = await ContactsAPI.get();
commit(SET_CURRENT_USER, data);
} catch (error) {
// Ignore error
}
},
update: async ({ dispatch }, { identifier, user: userObject }) => {
try {
const user = {
email: userObject.email,
name: userObject.name,
avatar_url: userObject.avatar_url,
identifier_hash: userObject.identifier_hash,
phone_number: userObject.phone_number,
};
await ContactsAPI.update(identifier, user);
dispatch('get');
if (userObject.identifier_hash) {
dispatch('conversation/clearConversations', {}, { root: true });
dispatch('conversation/fetchOldConversations', {}, { root: true });
}
} catch (error) {
// Ignore error
}
},
setCustomAttributes: async (_, customAttributes = {}) => {
try {
await ContactsAPI.setCustomAttributes(customAttributes);
} catch (error) {
// Ignore error
}
},
deleteCustomAttribute: async (_, customAttribute) => {
try {
await ContactsAPI.deleteCustomAttribute(customAttribute);
} catch (error) {
// Ignore error
}
},
};
export const mutations = {
[SET_CURRENT_USER]($state, user) {
const { currentUser } = $state;
$state.currentUser = { ...currentUser, ...user };
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};