|
|
@@ -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 }
|