Browse Source

:sparkles: grabbing chat history | SIIMEE-T-82

tags/0.0.3^2
j 3 years ago
parent
commit
c2c85f5235

+ 0
- 10
frontend/src/components/ProfileCardList.vue View File

@@ -12,7 +12,6 @@ section.profile-card-list.xs12.w-flex.column
12 12
 
13 13
 <script setup>
14 14
 import { ref } from 'vue'
15
-import { currentProfile } from '../services'
16 15
 import { cardAspects } from '../entities'
17 16
 import ProfileCard from './ProfileCard.vue'
18 17
 
@@ -33,15 +32,6 @@ const props = defineProps({
33 32
         ],
34 33
     },
35 34
 })
36
-
37
-/**
38
- * publish a new message to the chatter with the channel and the message & title is optional
39
- */
40
-const subscribeToChannel = async channelName => {
41
-    // You MUST send chatter channels as an array in an object
42
-    const channels = [channelName]
43
-    currentProfile.chatter.subscribe({ channels })
44
-}
45 35
 </script>
46 36
 
47 37
 <style lang="sass">

+ 30
- 3
frontend/src/services/chat.service.js View File

@@ -101,6 +101,31 @@ class Chatter {
101 101
         this.subscribe({ channels: this._subscriptions })
102 102
         return this.subscriptions
103 103
     }
104
+    async getHistory(channel, count = 10) {
105
+        console.warn('[chatter] grabbing history for channel:', channel)
106
+        let pastMessages = null
107
+        try {
108
+            pastMessages = await this.provider.fetchMessages({
109
+                channels: [channel],
110
+                count: count,
111
+                includeMessageType: true,
112
+                includeUUID: true,
113
+                includeMeta: true,
114
+                includeMessageActions: false,
115
+            })
116
+        } catch (error) {
117
+            console.error('[chatter]', error)
118
+        }
119
+        const channelHistory = pastMessages.channels[channel]
120
+        console.log('channelHistory :>> ', channelHistory)
121
+        return channelHistory
122
+            ? channelHistory.map(msg => ({
123
+                  publisher: msg.uuid,
124
+                  message: new ChatMessage(msg.message.description),
125
+                  timetoken: msg.timetoken,
126
+              }))
127
+            : []
128
+    }
104 129
     /**
105 130
      * Send a message to a channel
106 131
      * example = new ChatMessage({ title: 'example', description: 'ni' })
@@ -118,7 +143,7 @@ class Chatter {
118 143
     /**
119 144
      * Subscribe to a channels
120 145
      * Facade so we can hide provider specific methods
121
-     * @param {array} channels
146
+     * @param {array} channels grouping name
122 147
      */
123 148
     subscribe({ channels }) {
124 149
         _providerMethods.subscribe({ channels })
@@ -130,8 +155,10 @@ class Chatter {
130 155
     _listenFor({ listeners }) {
131 156
         _providerMethods.listen(listeners)
132 157
     }
133
-    //  step 2: build the this.subscriptions array from the this.groupings object
134
-    // fetch all groupings for this profile and then store them in the chatter groupings object for reference
158
+    /**
159
+     * Get all groupings for this profile and
160
+     * then store them as subscriptions
161
+     */
135 162
     async _setupAllChannels(groupings) {
136 163
         groupings.forEach(grouping => {
137 164
             this._subscriptions.push(grouping.grouping_name)

+ 28
- 30
frontend/src/utils/db.js View File

@@ -21,13 +21,23 @@ const headerTemplate = {
21 21
 class Connector {
22 22
     constructor() {
23 23
         this.apiPrefix = prefix
24
+        this._verbs = {
25
+            get: 'GET',
26
+            post: 'POST',
27
+            patch: 'PATCH',
28
+        }
24 29
     }
25
-    async get(endpoint) {
30
+    _makeHeader({ method, payload }) {
26 31
         const header = { ...headerTemplate }
27
-        header.method = 'GET'
32
+        header.method = method
33
+        if (payload) {
34
+            header.body = JSON.stringify(payload)
35
+        }
36
+        return header
37
+    }
38
+    async _tryFetch({ endpoint, header }) {
28 39
         try {
29
-            // console.log(`${remote}${endpoint}`)
30
-            let res = await fetch(`${remote}${endpoint}`, header)
40
+            const res = await fetch(`${remote}${endpoint}`, header)
31 41
             if (!res.ok) {
32 42
                 throw Error(res.statusText)
33 43
             }
@@ -37,35 +47,23 @@ class Connector {
37 47
             console.error(`[API Util]: ${error}\nroute:`, endpoint)
38 48
         }
39 49
     }
50
+    async get(endpoint) {
51
+        return await this._tryFetch({
52
+            endpoint,
53
+            header: this._makeHeader(this._verbs.get),
54
+        })
55
+    }
40 56
     async post(endpoint, payload = {}) {
41
-        const header = { ...headerTemplate }
42
-        header.method = 'POST'
43
-        header.body = JSON.stringify(payload)
44
-        try {
45
-            let res = await fetch(`${remote}${endpoint}`, header)
46
-            if (!res.ok) {
47
-                throw Error(res.statusText)
48
-            }
49
-            const jsonRes = await res.json()
50
-            return jsonRes.data
51
-        } catch (error) {
52
-            console.error(error)
53
-        }
57
+        return await this._tryFetch({
58
+            endpoint,
59
+            header: this._makeHeader(this._verbs.post, payload),
60
+        })
54 61
     }
55 62
     async patch(endpoint, payload = {}) {
56
-        const header = { ...headerTemplate }
57
-        header.method = 'PATCH'
58
-        header.body = JSON.stringify(payload)
59
-        try {
60
-            let res = await fetch(`${remote}${endpoint}`, header)
61
-            if (!res.ok) {
62
-                throw Error(res.statusText)
63
-            }
64
-            const jsonRes = await res.json()
65
-            return jsonRes.data
66
-        } catch (error) {
67
-            console.error(error)
68
-        }
63
+        return await this._tryFetch({
64
+            endpoint,
65
+            header: this._makeHeader(this._verbs.patch, payload),
66
+        })
69 67
     }
70 68
 
71 69
     /** !: DEV ONLY */

+ 0
- 4
frontend/src/utils/index.js View File

@@ -1,5 +1,4 @@
1 1
 import Joi from 'joi'
2
-import { Connector } from './db.js'
3 2
 import { SurveyFactory } from './survey.js'
4 3
 import { possible } from './lang.js'
5 4
 import { pidMixin, profileMixin } from './mixins.js'
@@ -31,8 +30,6 @@ const possibleZipcodes = [
31 30
     '97075',
32 31
 ]
33 32
 
34
-const api = new Connector('kittens')
35
-
36 33
 const validatorMapping = {
37 34
     'input-string': Joi.string(),
38 35
     'tag-cloud': Joi.string(),
@@ -133,7 +130,6 @@ const randomSurveyResponses = count => {
133 130
 }
134 131
 
135 132
 export {
136
-    api,
137 133
     validatorMapping,
138 134
     surveyFactory,
139 135
     makeKebob,

+ 9
- 34
frontend/src/utils/lang.js View File

@@ -17,7 +17,7 @@ const allSteps = {
17 17
         distance: 'distance',
18 18
         blurb: 'blurb',
19 19
         image: 'image',
20
-    }
20
+    },
21 21
 }
22 22
 
23 23
 const aspectResponses = {
@@ -29,7 +29,7 @@ const aspectResponses = {
29 29
         mostly: 'mostly',
30 30
         often: 'often',
31 31
         everytime: 'everytime',
32
-    }
32
+    },
33 33
 }
34 34
 
35 35
 const possible = {}
@@ -55,27 +55,11 @@ possible.usa = {
55 55
         'casually_browsing',
56 56
     ],
57 57
     // key 11
58
-    presence: [
59
-        'remote',
60
-        'in_person',
61
-        'hybrid',
62
-        'flexible'
63
-    ],
58
+    presence: ['remote', 'in_person', 'hybrid', 'flexible'],
64 59
     // key 10
65
-    duration: [
66
-        'full-time',
67
-        'part-time',
68
-        'contract',
69
-        'flexible',
70
-    ],
60
+    duration: ['full-time', 'part-time', 'contract', 'flexible'],
71 61
     // Experience and roles concat, key: 14
72
-    experience: [
73
-        'associate',
74
-        'junior',
75
-        'mid-level',
76
-        'senior',
77
-        'staff',
78
-    ],
62
+    experience: ['associate', 'junior', 'mid-level', 'senior', 'staff'],
79 63
     roles: {
80 64
         type: [
81 65
             'back-end',
@@ -96,21 +80,12 @@ possible.usa = {
96 80
             'manager',
97 81
             'technician',
98 82
         ],
99
-        candidate: [
100
-            'hiring_manager',
101
-            'recruiter',
102
-        ]
83
+        candidate: ['hiring_manager', 'recruiter'],
103 84
     },
104
-    pronouns: [
105
-        'she/her',
106
-        'she/they',
107
-        'he/him',
108
-        'he/they',
109
-        'they/them',
110
-    ],
85
+    pronouns: ['she/her', 'she/they', 'he/him', 'he/they', 'they/them'],
111 86
     image: [],
112 87
     zipcode: [],
113
-    blurb: []
88
+    blurb: [],
114 89
 }
115 90
 
116
-export { allSteps, aspectResponses, possible }
91
+export { allSteps, aspectResponses, possible, DELIMITER }

+ 23
- 14
frontend/src/views/ChatView.vue View File

@@ -1,17 +1,20 @@
1 1
 <template lang="pug">
2 2
 main.view--chat
3 3
     header.mb6(v-if='profile && grouping')
4
-        h3 chatting with: {{ target.profile_id }}
5
-        p {{ grouping.profile.user_name }}
6
-        p {{ grouping.profile.user_email }}
7
-        p(v-if='!grouping._loading') {{ grouping.revealed }}
4
+        h3 chatting with:
5
+        p {{ target.profile_id }} | {{ grouping.profile.user_name }} | {{ grouping.profile.user_email }}
6
+
7
+        h3 logged in as:
8
+        p {{ profile.id }} | {{ profile._profile.user_name }} | {{ profile._profile.user_email }}
8 9
         //- p subscriptions: {{ profile.chatter.subscriptions }}
10
+        hr
9 11
 
10
-        .w-flex.column
11
-            p reveal: {{ grouping.revealed }}
12
+        .w-flex.column(v-if='!grouping._loading')
13
+            p you revealed: {{ grouping.revealed[profile.id.value].map(tag => tag.description) }}
12 14
             .w-flex.row
13
-                button(@click='reveal(7)') my name
14
-                button(@click='reveal(8)') my email
15
+                button(@click='reveal(7)') reveal my name
16
+                button(@click='reveal(8)') reveal my email
17
+            p they revealed: {{ grouping.revealed[target.profile_id].map(tag => tag.description) }}
15 18
 
16 19
     article
17 20
         template(v-if='isLoading')
@@ -22,7 +25,7 @@ main.view--chat
22 25
                 template(v-for='chatmessage in messages')
23 26
                     li(
24 27
                         :class='[{ me: chatmessage.publisher == profile.id.value }, chatmessage.publisher]'
25
-                        v-if='chatmessage.message.description'
28
+                        v-if='chatmessage.message && chatmessage.message.description'
26 29
                     )
27 30
                         ChatBubble(
28 31
                             :message='chatmessage.message.description'
@@ -41,7 +44,7 @@ main.view--chat
41 44
     MainNav
42 45
 </template>
43 46
 
44
-<script>
47
+<script lang="js">
45 48
 import ChatBubble from '../components/ChatBubble.vue'
46 49
 import { currentProfile } from '../services'
47 50
 import { mixins } from '../utils'
@@ -54,19 +57,25 @@ export default {
54 57
         target: null,
55 58
         toSend: '',
56 59
         messages: [],
57
-        openDrawer: null,
58 60
         grouping: null,
59 61
     }),
60 62
     watch: {
61
-        profile() {
63
+        async profile() {
62 64
             this.loadTargetProfile()
65
+            currentProfile._loading.value = true
66
+            this.messages = await currentProfile.chatter.getHistory(this.grouping.grouping_name)
63 67
             currentProfile.chatter.setOnMessage(this._onMessage)
68
+            currentProfile._loading.value = false
64 69
         },
65 70
     },
66
-    created() {
71
+    async created() {
67 72
         this.loadTargetProfile()
68
-        currentProfile.chatter.setOnMessage(this._onMessage)
73
+        currentProfile._loading.value = true
69 74
         this.grouping = this.getGrouping()
75
+        this.messages = await currentProfile.chatter.getHistory(this.grouping.grouping_name)
76
+        console.log('this.messages :>> ', this.messages)
77
+        currentProfile.chatter.setOnMessage(this._onMessage)
78
+        currentProfile._loading.value = false
70 79
     },
71 80
     methods: {
72 81
         async reveal(tagId) {

Loading…
Cancel
Save