Skip to content

Commit

Permalink
Changes supporting failing chat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Oct 9, 2024
1 parent 693173f commit 068c8c1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
5 changes: 3 additions & 2 deletions frontend/controller/actions/chatroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sbp('okTurtles.events/on', MESSAGE_RECEIVE_RAW, ({
// If newMessage is undefined, it means that an existing message is being edited
newMessage
}) => {
const state = sbp('chelonia/contract/state', contractID)
const getters = sbp('state/vuex/getters')
const mentions = makeMentionFromUserID(getters.ourIdentityContractId)
const msgData = newMessage || data
Expand All @@ -42,10 +43,10 @@ sbp('okTurtles.events/on', MESSAGE_RECEIVE_RAW, ({
messageHash: msgData.hash,
height: msgData.height,
text: msgData.text,
isDMOrMention: isMentionedMe || getters.chatRoomAttributes.type === CHATROOM_TYPES.DIRECT_MESSAGE,
isDMOrMention: isMentionedMe || state.attributes?.type === CHATROOM_TYPES.DIRECT_MESSAGE,
messageType: !newMessage ? MESSAGE_TYPES.TEXT : data.type,
memberID: innerSigningContractID,
chatRoomName: getters.chatRoomAttributes.name
chatRoomName: state.attributes?.name
}).catch(e => {
console.error('[action/chatroom.js] Error on messageReceivePostEffect', e)
})
Expand Down
4 changes: 2 additions & 2 deletions frontend/controller/actions/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,8 @@ export default (sbp('sbp/selectors/register', {
// inside of the exception handler :-(
}
},
'gi.actions/group/notifyProposalStateInGeneralChatRoom': function ({ groupID, proposal }: { groupID: string, proposal: Object }) {
const { generalChatRoomId } = sbp('chelonia/rootState')[groupID]
'gi.actions/group/notifyProposalStateInGeneralChatRoom': async function ({ groupID, proposal }: { groupID: string, proposal: Object }) {
const { generalChatRoomId } = await sbp('chelonia/contract/state', groupID)
return sbp('gi.actions/chatroom/addMessage', {
contractID: generalChatRoomId,
data: { type: MESSAGE_TYPES.INTERACTIVE, proposal }
Expand Down
8 changes: 3 additions & 5 deletions frontend/controller/actions/identity-kv.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
import sbp from '@sbp/sbp'
import { KV_KEYS } from '~/frontend/utils/constants.js'
import { KV_QUEUE, ONLINE } from '~/frontend/utils/events.js'
import { KV_QUEUE, NEW_PREFERENCES, NEW_UNREAD_MESSAGES, ONLINE } from '~/frontend/utils/events.js'
import { isExpired } from '@model/notifications/utils.js'

const initNotificationStatus = (data = {}) => ({ ...data, read: false })
Expand Down Expand Up @@ -51,8 +51,7 @@ export default (sbp('sbp/selectors/register', {
'gi.actions/identity/kv/loadChatRoomUnreadMessages': () => {
return sbp('okTurtles.eventQueue/queueEvent', KV_QUEUE, async () => {
const currentChatRoomUnreadMessages = await sbp('gi.actions/identity/kv/fetchChatRoomUnreadMessages')
// TODO: Can't use state/vuex/commit
sbp('state/vuex/commit', 'setUnreadMessages', currentChatRoomUnreadMessages)
sbp('okTurtles.events/emit', NEW_UNREAD_MESSAGES, currentChatRoomUnreadMessages)
})
},
'gi.actions/identity/kv/initChatRoomUnreadMessages': ({ contractID, messageHash, createdHeight }: {
Expand Down Expand Up @@ -192,8 +191,7 @@ export default (sbp('sbp/selectors/register', {
'gi.actions/identity/kv/loadPreferences': () => {
return sbp('okTurtles.eventQueue/queueEvent', KV_QUEUE, async () => {
const preferences = await sbp('gi.actions/identity/kv/fetchPreferences')
// TODO: Can't use state/vuex/commit
sbp('state/vuex/commit', 'setPreferences', preferences)
sbp('okTurtles.events/emit', NEW_PREFERENCES, preferences)
})
},
'gi.actions/identity/kv/updateDistributionBannerVisibility': ({ contractID, hidden }: { contractID: string, hidden: boolean }) => {
Expand Down
10 changes: 9 additions & 1 deletion frontend/controller/app/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GIErrorUIRuntimeError, L, LError, LTags } from '@common/common.js'
import { cloneDeep } from '@model/contracts/shared/giLodash.js'
import sbp from '@sbp/sbp'
import Vue from 'vue'
import { LOGIN, LOGIN_COMPLETE, LOGIN_ERROR } from '~/frontend/utils/events.js'
import { LOGIN, LOGIN_COMPLETE, LOGIN_ERROR, NEW_PREFERENCES, NEW_UNREAD_MESSAGES } from '~/frontend/utils/events.js'
import { Secret } from '~/shared/domains/chelonia/Secret.js'
import { boxKeyPair, buildRegisterSaltRequest, computeCAndHc, decryptContractSalt, hash, hashPassword, randomNonce } from '~/shared/zkpp.js'
// Using relative path to crypto.js instead of ~-path to workaround some esbuild bug
Expand Down Expand Up @@ -147,6 +147,14 @@ sbp('okTurtles.events/on', LOGIN, async ({ identityContractID, encryptionParams,
}
})

sbp('okTurtles.events/on', NEW_PREFERENCES, (currentChatRoomUnreadMessages) => {
sbp('state/vuex/commit', 'setUnreadMessages', currentChatRoomUnreadMessages)
})

sbp('okTurtles.events/on', NEW_UNREAD_MESSAGES, (preferences) => {
sbp('state/vuex/commit', 'setPreferences', preferences)
})

/* Commented out as persistentActions are not being used
sbp('okTurtles.events/on', LOGOUT, (a) => {
// TODO: [SW] It may make more sense to load persistent actions in
Expand Down
4 changes: 2 additions & 2 deletions frontend/controller/serviceworkers/sw-primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { GIMessage } from '~/shared/domains/chelonia/GIMessage.js'
import { Secret } from '~/shared/domains/chelonia/Secret.js'
import { CONTRACTS_MODIFIED, CONTRACT_IS_SYNCING, EVENT_HANDLED } from '~/shared/domains/chelonia/events.js'
import { deserializer, serializer } from '~/shared/serdes/index.js'
import { ACCEPTED_GROUP, DELETED_CHATROOM, JOINED_CHATROOM, JOINED_GROUP, KV_EVENT, LEFT_CHATROOM, LEFT_GROUP, NAMESPACE_REGISTRATION, NOTIFICATION_EMITTED, NOTIFICATION_REMOVED, NOTIFICATION_STATUS_LOADED, SWITCH_GROUP } from '../../utils/events.js'
import { ACCEPTED_GROUP, DELETED_CHATROOM, JOINED_CHATROOM, JOINED_GROUP, KV_EVENT, LEFT_CHATROOM, LEFT_GROUP, NAMESPACE_REGISTRATION, NEW_PREFERENCES, NEW_UNREAD_MESSAGES, NOTIFICATION_EMITTED, NOTIFICATION_REMOVED, NOTIFICATION_STATUS_LOADED, SWITCH_GROUP } from '../../utils/events.js'

deserializer.register(GIMessage)
deserializer.register(Secret)
Expand All @@ -32,7 +32,7 @@ sbp('sbp/filters/global/add', (domain, selector, data) => {
console.debug(`[sw] [sbp] ${selector}`, data)
});

[EVENT_HANDLED, CONTRACTS_MODIFIED, CONTRACT_IS_SYNCING, LOGIN, LOGIN_ERROR, LOGOUT, ACCEPTED_GROUP, DELETED_CHATROOM, LEFT_CHATROOM, LEFT_GROUP, JOINED_CHATROOM, JOINED_GROUP, KV_EVENT, NAMESPACE_REGISTRATION, NOTIFICATION_EMITTED, NOTIFICATION_REMOVED, NOTIFICATION_STATUS_LOADED, SWITCH_GROUP, PROPOSAL_ARCHIVED].forEach(et => {
[EVENT_HANDLED, CONTRACTS_MODIFIED, CONTRACT_IS_SYNCING, LOGIN, LOGIN_ERROR, LOGOUT, ACCEPTED_GROUP, DELETED_CHATROOM, LEFT_CHATROOM, LEFT_GROUP, JOINED_CHATROOM, JOINED_GROUP, KV_EVENT, NAMESPACE_REGISTRATION, NEW_PREFERENCES, NEW_UNREAD_MESSAGES, NOTIFICATION_EMITTED, NOTIFICATION_REMOVED, NOTIFICATION_STATUS_LOADED, SWITCH_GROUP, PROPOSAL_ARCHIVED].forEach(et => {
sbp('okTurtles.events/on', et, (...args) => {
const { data } = serializer(args)
const message = {
Expand Down
25 changes: 21 additions & 4 deletions frontend/model/notifications/messageReceivePostEffect.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ async function messageReceivePostEffect ({
}): Promise<void> {
// TODO: This can't be a root getter when running in a SW
const rootGetters = await sbp('state/vuex/getters')
const isGroupDM = rootGetters.isGroupDirectMessage(contractID)
const rootState = await sbp('state/vuex/state')
const identityContractId = rootState.loggedIn?.identityContractId
if (!identityContractId) return
const isDM = rootState[identityContractId].chatRooms[contractID]
const shouldAddToUnreadMessages = isDMOrMention || [MESSAGE_TYPES.INTERACTIVE, MESSAGE_TYPES.POLL].includes(messageType)

await sbp('chelonia/contract/wait', contractID)
Expand All @@ -37,10 +40,24 @@ async function messageReceivePostEffect ({

let title = `# ${chatRoomName}`
let icon
if (isGroupDM) {

if (isDM) {
// NOTE: partner identity contract could not be synced yet
title = rootGetters.ourGroupDirectMessages[contractID].title
icon = rootGetters.ourGroupDirectMessages[contractID].picture
const members = rootState[contractID].members
const isDMToMyself = members.length === 1 && members[0] === identityContractId
const partners = members
.filter(memberID => memberID !== identityContractId)
.sort((p1, p2) => {
const p1JoinedDate = new Date(identityContractId.members[p1].joinedDate).getTime()
const p2JoinedDate = new Date(identityContractId.members[p2].joinedDate).getTime()
return p1JoinedDate - p2JoinedDate
})
const lastJoinedPartner = isDMToMyself ? identityContractId : partners[partners.length - 1]

title = isDMToMyself
? rootGetters.userDisplayNameFromID(identityContractId)
: partners.map(cID => rootGetters.userDisplayNameFromID(cID)).join(', ')
icon = rootGetters.ourContactProfilesById[lastJoinedPartner]?.picture
}
const path = `/group-chat/${contractID}`

Expand Down
3 changes: 3 additions & 0 deletions frontend/utils/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ export const CHELONIA_STATE_MODIFIED = 'chelonia-state-modified'
export const NOTIFICATION_EMITTED = 'notification-emitted'
export const NOTIFICATION_REMOVED = 'notification-removed'
export const NOTIFICATION_STATUS_LOADED = 'notification-status-loaded'

export const NEW_UNREAD_MESSAGES = 'new-unread-messages'
export const NEW_PREFERENCES = 'new-preferences'

0 comments on commit 068c8c1

Please sign in to comment.