Browse Source

Merge branch 'pub-sub' of fyindr/siimee into dev

tags/0.0.1^2
maeda 3 years ago
parent
commit
be4873c5fb

+ 3
- 0
frontend/src/App.vue View File

@@ -72,6 +72,9 @@ export default {
72 72
                 currentProfile.setupToaster(this.$waveui.notify)
73 73
             }
74 74
             console.log('---')
75
+            //  step 3: subscribe to the this.subscriptions array
76
+            // view current subscriptions on the currentProfile.chatter.subscriptions
77
+            console.log('subscriptions', currentProfile.chatter.subscriptions)
75 78
         },
76 79
     },
77 80
 }

+ 2
- 4
frontend/src/components/ProfileCardList.vue View File

@@ -112,13 +112,11 @@ const chat = async pid => {
112 112
     const chatter = currentProfile.chatter
113 113
     // console.log('mock sender:', pid)
114 114
     
115
-    /**  publish a new message to the chatter with the channel and the message
116
-     *  
117
-     *  title is optional
115
+    /**  publish a new message to the chatter with the channel and the message & title is optional
118 116
      */
119 117
     const res = await chatter.publish(chatter.subscriptions[1], {
120 118
         title: 'New Message',
121
-        description: 'This is the start of a chat conversation!',
119
+        description: 'This is the checking to see if we are subscribed to a channel!',
122 120
     })
123 121
     // PubNub response will be a timecode of when the message was published
124 122
     console.log('res:', res)

+ 34
- 5
frontend/src/services/chat.service.js View File

@@ -1,5 +1,8 @@
1 1
 import PubNub from 'pubnub'
2 2
 
3
+// custom services 
4
+import {fetchMembershipsByProfileId} from '../services/grouping.service'
5
+
3 6
 /**
4 7
  * Provider method holder
5 8
  * We always reference this object so
@@ -20,7 +23,6 @@ const providerMethods = {
20 23
 const setupPubnub = async uuid => {
21 24
     const publishKey = 'pub-c-73f35484-396f-47ff-b4b6-45bed079fd3b'
22 25
     const subscribeKey = 'sub-c-6cb7f5d0-94e2-11ec-b249-a68c05a281ab'
23
-    // console.log('publishKey: ' , publishKey)
24 26
     if (!uuid) return console.error('no pubnub uuid set')
25 27
 
26 28
     const pubnubClient = await new PubNub({
@@ -57,6 +59,7 @@ class Chatter {
57 59
      */
58 60
     constructor() {
59 61
         // Map of each active chat
62
+        // * this is where we will store all of our groupings on from the backend on user login...
60 63
         this.groupings = {}
61 64
 
62 65
         // Our pubnub instance
@@ -66,10 +69,11 @@ class Chatter {
66 69
         this.uuid = null
67 70
 
68 71
         // Setup the main channel
72
+        //  subscriptions array will be built dynamically from the "this.groupings" object
69 73
         this.subscriptions = [MAIN_CHANNEL, 'Channel-LosAngeles']
70 74
         this.listeners = {
71 75
             status: async e => {
72
-                await this.publish(this.subscriptions[0], testMessage)
76
+                await this.publish(this.subscriptions[2], testMessage)
73 77
                 if (e.category !== 'PNConnectedCategory') return
74 78
             },
75 79
             message: this._onMessage,
@@ -94,9 +98,16 @@ class Chatter {
94 98
     async setup(uuid) {
95 99
         this.uuid = `${uuid}`
96 100
         this.provider = await setupPubnub(this.uuid)
97
-
98
-        this._listenFor({ listeners: this.listeners })
99
-        this._subscribe(this.subscriptions)
101
+            
102
+        //  step 1: build the this.groupings object from the backend
103
+        // ? .then() to wait for the groupings to be fetched before subscribing to channels
104
+        this.getGroupingsByProfileId(this.uuid).then(() => {
105
+            this._listenFor({ listeners: this.listeners })
106
+            this._subscribe(this.subscriptions)
107
+        })
108
+        
109
+        console.log('this.subscriptions', this.subscriptions)
110
+       
100 111
     }
101 112
     /**
102 113
      * Send a message to a channel
@@ -107,6 +118,7 @@ class Chatter {
107 118
      * @return {object} timestamp
108 119
      */
109 120
     async publish(channel, message) {
121
+        console.log('publishing message to channel:', channel)
110 122
         return await providerMethods['publish']({ channel, message })
111 123
     }
112 124
     /**
@@ -124,6 +136,23 @@ class Chatter {
124 136
     _listenFor({ listeners }) {
125 137
         providerMethods['listen'](listeners)
126 138
     }
139
+    
140
+    //  step 2: build the this.subscriptions array from the this.groupings object
141
+    // fetch all groupings for this profile and then store them in the chatter groupings object for reference
142
+    async getGroupingsByProfileId(profileId) {
143
+        console.log('fetching groupings for profileId:', profileId)
144
+        const groupings = await fetchMembershipsByProfileId(profileId)
145
+        this.groupings = groupings
146
+        this.createChannelNamesByGroupings(this.groupings)
147
+    }
148
+    // building a list of channel names from the groupings object.grouping_name
149
+    createChannelNamesByGroupings(groupings) {
150
+        groupings.forEach(item => {
151
+            this.subscriptions.push(item.grouping_name)
152
+        });
153
+        
154
+        
155
+    }
127 156
 }
128 157
 
129 158
 export { Chatter }

+ 3
- 0
frontend/src/services/login.service.js View File

@@ -2,6 +2,7 @@ import { ref } from 'vue'
2 2
 import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services'
3 3
 import { surveyFactory } from '../utils'
4 4
 
5
+
5 6
 /**
6 7
  * Logged in profile state manager
7 8
  * Sort of a util and service hybrid
@@ -106,6 +107,8 @@ class Login {
106 107
         const testAccountUUID = this.id.value
107 108
         this.chatter.setup(testAccountUUID)
108 109
     }
110
+
111
+    
109 112
 }
110 113
 
111 114
 const currentProfile = new Login()

Loading…
Cancel
Save