Skip to content

Commit

Permalink
Fix group-chat-direct-message spec
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Oct 6, 2024
1 parent 2861605 commit 80b1385
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 1 addition & 3 deletions frontend/controller/actions/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,7 @@ export default (sbp('sbp/selectors/register', {
const partnerIDs = params.data.memberIDs
.filter(memberID => memberID !== rootGetters.ourIdentityContractId)
.map(memberID => rootGetters.ourContactProfilesById[memberID].contractID)
// NOTE: 'rootState.currentGroupId' could be changed while waiting for the sbp functions to be proceeded
// So should save it as a constant variable 'currentGroupId', and use it which can't be changed
const currentGroupId = rootState.currentGroupId
const currentGroupId = params.currentGroupId

const message = await sbp('gi.actions/chatroom/create', {
data: {
Expand Down
1 change: 0 additions & 1 deletion frontend/controller/serviceworkers/sw-primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ sbp('sbp/selectors/register', {
'appLogs/save': () => {}
})


self.addEventListener('install', function (event) {
console.debug('[sw] install')
event.waitUntil(self.skipWaiting())
Expand Down
34 changes: 30 additions & 4 deletions frontend/views/containers/chatroom/DMMixin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sbp from '@sbp/sbp'
import { mapGetters } from 'vuex'
import { mapGetters, mapState } from 'vuex'
import { logExceptNavigationDuplicated } from '@view-utils/misc.js'
import { L } from '@common/common.js'

Expand All @@ -11,23 +11,49 @@ const DMMixin: Object = {
'ourGroupDirectMessages',
'ourIdentityContractId',
'ourGroupDirectMessageFromUserIds'
])
]),
...mapState(['currentGroupId'])
},
methods: {
async createDirectMessage (memberIDs: string | string[]) {
if (typeof memberIDs === 'string') {
memberIDs = [memberIDs]
}
try {
const identityContractID = this.ourIdentityContractId
await sbp('gi.actions/identity/createDirectMessage', {
contractID: this.ourIdentityContractId,
contractID: identityContractID,
currentGroupId: this.currentGroupId,
data: { memberIDs },
hooks: {
onprocessed: (message) => {
const dmID = message.decryptedValue().data.contractID
// The logic for updating paths will not work until the DM chatroom
// has been synced
sbp('chelonia/queueInvocation', dmID, () => this.redirect(dmID))

// Sometimes, the state may not be ready (it needs to be copied from the SW
// to Vuex). In this case, we try again after a short delay.
// The specific issue is that the browsing-side state is updated in response
// to the EVENT_HANDLED event. That may happen after the onprocessed hook
// TODO: Figure out a better way of doing this that doesn't require a timeout (for example, doing this directly is the GroupChat.vue file
// `if (!this.isJoinedChatRoom(chatRoomID)) {` check)

let attemptCount = 0
const setCurrentChatRoomId = () => {
// Re-grab the state as it could be a stale reference
const rootState = sbp('state/vuex/state')
if (!rootState[dmID]?.members?.[identityContractID]) {
if (++attemptCount > 5) {
console.warn('[createDirectMessage] Given up on redirect after 5 attempts', { identityContractID, dmID })
return
}
setTimeout(setCurrentChatRoomId, 5 + 5 * attemptCount)
} else {
this.redirect(dmID)
}
}

sbp('chelonia/queueInvocation', dmID, setCurrentChatRoomId)
}
}
})
Expand Down

0 comments on commit 80b1385

Please sign in to comment.