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

ChatView.vue 6.4KB

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