Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ChatView.vue 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template lang="pug">
  2. main.view--chat
  3. header.mb6(v-if='profile && grouping')
  4. h3 chatting with:
  5. p {{ target.profile_id }} | {{ grouping.profile.user_name }} | {{ grouping.profile.user_email }}
  6. h3 logged in as:
  7. p {{ profile.id }} | {{ profile._profile.user_name }} | {{ profile._profile.user_email }}
  8. //- p subscriptions: {{ profile.chatter.subscriptions }}
  9. hr
  10. .w-flex.column(v-if='!grouping._loading')
  11. .w-flex.row
  12. button(@click='reveal(7)') reveal my name
  13. button(@click='reveal(8)') reveal my email
  14. // TODO: Remove later, only for testing
  15. button(@click='checkData()') check data
  16. p you revealed:
  17. span(v-if="grouping.revealed[profile.id.value]")
  18. ul(v-for="reveal in grouping.revealed[profile.id.value]")
  19. li {{ reveal.description }} : {{ profile._profile[reveal.description] }}
  20. p they revealed:
  21. span(v-if="grouping.revealed[grouping.profile.profile_id]")
  22. ul(v-for="reveal in grouping.revealed[grouping.profile.profile_id]")
  23. li {{ reveal.description }} : {{ grouping.profile[reveal.description] }}
  24. article
  25. template(v-if='isLoading')
  26. w-spinner(bounce)
  27. template(v-if='!isLoading && target')
  28. ul#messages.w-flex.column
  29. template(v-for='chatmessage in messages')
  30. li(
  31. :class='[{ me: chatmessage.publisher == profile.id.value }, chatmessage.publisher]'
  32. v-if='chatmessage.message && chatmessage.message.description'
  33. )
  34. ChatBubble(
  35. :message='chatmessage.message.description'
  36. :publisher-id='chatmessage.publisher'
  37. :time='chatmessage.timetoken'
  38. )
  39. template(v-else-if='!isLoading && !target')
  40. p No match found between profile {{ $route.params.pid }} and {{ profile.id }}...
  41. footer.w-flex.row
  42. w-input(@keyup.enter='sendMessage' outline v-model='toSend')
  43. w-button(@click='sendMessage')
  44. w-icon.icon-paper-plane
  45. MainNav
  46. </template>
  47. <script lang="js">
  48. import ChatBubble from '../components/ChatBubble.vue'
  49. import { currentProfile } from '../services'
  50. import { mixins } from '../utils'
  51. export default {
  52. name: 'ProfileView',
  53. components: { ChatBubble },
  54. mixins: [mixins.profileMixin],
  55. data: () => ({
  56. target: null,
  57. toSend: '',
  58. messages: [],
  59. grouping: null,
  60. }),
  61. watch: {
  62. async profile() {
  63. this.loadTargetProfile()
  64. currentProfile._loading.value = true
  65. this.messages = await currentProfile.chatter.getHistory(this.grouping.grouping_name)
  66. currentProfile.chatter.setOnMessage(this._onMessage)
  67. currentProfile._loading.value = false
  68. },
  69. },
  70. async created() {
  71. this.loadTargetProfile()
  72. currentProfile._loading.value = true
  73. this.grouping = this.getGrouping()
  74. this.messages = await currentProfile.chatter.getHistory(this.grouping.grouping_name)
  75. console.log('this.messages :>> ', this.messages)
  76. currentProfile.chatter.setOnMessage(this._onMessage)
  77. currentProfile._loading.value = false
  78. },
  79. methods: {
  80. async reveal(tagId) {
  81. const grouping = this.getGrouping()
  82. await grouping.reveal(currentProfile.id.value, tagId)
  83. },
  84. // TODO: remove, only for testing reveal()
  85. async checkData() {
  86. console.log(currentProfile)
  87. },
  88. /**
  89. * Pubnub message callback fires when message event
  90. * is detected. We define is HERE because we need the
  91. * component state and `this` context
  92. */
  93. _onMessage(e) {
  94. if (!e.message) return
  95. this.messages.push(e)
  96. },
  97. // TODO: test this
  98. getGrouping() {
  99. return currentProfile.groupings.find(
  100. g => g.profile.profile_id == this.$route.params.pid,
  101. )
  102. },
  103. sendMessage() {
  104. const grouping = this.getGrouping()
  105. currentProfile.chatter.publish(grouping.grouping_name, this.toSend)
  106. this.toSend = ''
  107. },
  108. // TODO: test this
  109. loadTargetProfile() {
  110. try {
  111. const grouping = this.getGrouping()
  112. if (!grouping) {
  113. throw `no match found for ${currentProfile.id.value}`
  114. }
  115. this.target = grouping.profile
  116. } catch (err) {
  117. console.error(err)
  118. }
  119. },
  120. },
  121. }
  122. </script>
  123. <style lang="sass">
  124. main.view--chat
  125. article ul
  126. list-style: none
  127. > li
  128. font-family: sans-serif
  129. background-color: powderblue
  130. margin: 0 0.5em 1em 2em
  131. &.me
  132. margin: 0 2em 1em 0.5em
  133. background-color: lightgreen
  134. p:first-of-type
  135. font-size: 10px
  136. </style>