|
|
@@ -49,7 +49,7 @@ const testMessage = new ChatMessage({
|
|
49
|
49
|
title: 'testing',
|
|
50
|
50
|
description: 'hello world!',
|
|
51
|
51
|
})
|
|
52
|
|
-const MAIN_CHANNEL = 'Channel-Barcelona'
|
|
|
52
|
+const MAIN_CHANNEL = 'Channel-Siimee'
|
|
53
|
53
|
|
|
54
|
54
|
/** Singleton that holds all our chat information */
|
|
55
|
55
|
class Chatter {
|
|
|
@@ -70,10 +70,10 @@ class Chatter {
|
|
70
|
70
|
|
|
71
|
71
|
// Setup the main channel
|
|
72
|
72
|
// subscriptions array will be built dynamically from the "this.groupings" object
|
|
73
|
|
- this.subscriptions = [MAIN_CHANNEL, 'Channel-LosAngeles']
|
|
|
73
|
+ this.subscriptions = [MAIN_CHANNEL]
|
|
74
|
74
|
this.listeners = {
|
|
75
|
75
|
status: async e => {
|
|
76
|
|
- await this.publish(this.subscriptions[0], testMessage)
|
|
|
76
|
+ // await this.publish(this.subscriptions[0], testMessage)
|
|
77
|
77
|
if (e.category !== 'PNConnectedCategory') return
|
|
78
|
78
|
},
|
|
79
|
79
|
message: this._onMessage,
|
|
|
@@ -100,13 +100,10 @@ class Chatter {
|
|
100
|
100
|
this.provider = await setupPubnub(this.uuid)
|
|
101
|
101
|
|
|
102
|
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)
|
|
|
103
|
+ // ? .await for the groupings to be fetched before subscribing to channels
|
|
|
104
|
+ await this._setupAllChannels(this.uuid)
|
|
|
105
|
+ this._listenFor({ listeners: this.listeners })
|
|
|
106
|
+ this.subscribe(this.subscriptions)
|
|
110
|
107
|
}
|
|
111
|
108
|
/**
|
|
112
|
109
|
* Send a message to a channel
|
|
|
@@ -118,35 +115,30 @@ class Chatter {
|
|
118
|
115
|
*/
|
|
119
|
116
|
async publish(channel, message) {
|
|
120
|
117
|
console.log('publishing message to channel:', channel)
|
|
121
|
|
- return await providerMethods['publish']({ channel, message })
|
|
|
118
|
+ return providerMethods.publish({ channel, message })
|
|
122
|
119
|
}
|
|
123
|
120
|
/**
|
|
124
|
121
|
* Subscribe to a channels
|
|
125
|
122
|
* Facade so we can hide provider specific methods
|
|
126
|
123
|
* @param {array} channels
|
|
127
|
124
|
*/
|
|
128
|
|
- _subscribe(channels) {
|
|
129
|
|
- providerMethods['subscribe']({ channels })
|
|
|
125
|
+ subscribe(channels) {
|
|
|
126
|
+ providerMethods.subscribe({ channels })
|
|
130
|
127
|
}
|
|
131
|
128
|
/**
|
|
132
|
129
|
* Listen to events and set callbacks
|
|
133
|
130
|
* Facade so we can hide provider specific methods
|
|
134
|
131
|
*/
|
|
135
|
132
|
_listenFor({ listeners }) {
|
|
136
|
|
- providerMethods['listen'](listeners)
|
|
|
133
|
+ providerMethods.listen(listeners)
|
|
137
|
134
|
}
|
|
138
|
135
|
// step 2: build the this.subscriptions array from the this.groupings object
|
|
139
|
136
|
// fetch all groupings for this profile and then store them in the chatter groupings object for reference
|
|
140
|
|
- async getGroupingsByProfileId(profileId) {
|
|
141
|
|
- console.log('fetching groupings for profileId:', profileId)
|
|
|
137
|
+ async _setupAllChannels(profileId) {
|
|
142
|
138
|
const groupings = await fetchMembershipsByProfileId(profileId)
|
|
143
|
|
- this.groupings = groupings
|
|
144
|
|
- this.createChannelNamesByGroupings(this.groupings)
|
|
145
|
|
- }
|
|
146
|
|
- // building a list of channel names from the groupings object.grouping_name
|
|
147
|
|
- createChannelNamesByGroupings(groupings) {
|
|
148
|
|
- groupings.forEach(item => {
|
|
149
|
|
- this.subscriptions.push(item.grouping_name)
|
|
|
139
|
+ console.log(`fetched groupings for profileId: ${profileId}`, groupings)
|
|
|
140
|
+ groupings.forEach(grouping => {
|
|
|
141
|
+ this.subscriptions.push(grouping.grouping_name)
|
|
150
|
142
|
})
|
|
151
|
143
|
}
|
|
152
|
144
|
stop() {
|