logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

chat.js (776B)


  1. const chat = {
  2. state: {
  3. messages: [],
  4. channel: { state: '' }
  5. },
  6. mutations: {
  7. setChannel (state, channel) {
  8. state.channel = channel
  9. },
  10. addMessage (state, message) {
  11. state.messages.push(message)
  12. state.messages = state.messages.slice(-19, 20)
  13. },
  14. setMessages (state, messages) {
  15. state.messages = messages.slice(-19, 20)
  16. }
  17. },
  18. actions: {
  19. initializeChat (store, socket) {
  20. const channel = socket.channel('chat:public')
  21. channel.on('new_msg', (msg) => {
  22. store.commit('addMessage', msg)
  23. })
  24. channel.on('messages', ({ messages }) => {
  25. store.commit('setMessages', messages)
  26. })
  27. channel.join()
  28. store.commit('setChannel', channel)
  29. }
  30. }
  31. }
  32. export default chat