Skip to content

Commit

Permalink
Unamiguous @ALL mention (using channel ID)
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Feb 7, 2024
1 parent 312d8ca commit 86697c8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
8 changes: 4 additions & 4 deletions frontend/model/contracts/chatroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ sbp('chelonia/defineContract', {
return
}
const newMessage = createMessage({ meta, data, hash, height, state, innerSigningContractID })
const mentions = makeMentionFromUserID(me)
const mentions = makeMentionFromUserID(me, contractID)
const isMentionedMe = data.type === MESSAGE_TYPES.TEXT &&
(newMessage.text.includes(mentions.me) || newMessage.text.includes(mentions.all))

Expand Down Expand Up @@ -467,7 +467,7 @@ sbp('chelonia/defineContract', {

const isAlreadyAdded = !!sbp('state/vuex/getters')
.chatRoomUnreadMessages(contractID).find(m => m.messageHash === data.hash)
const mentions = makeMentionFromUserID(me)
const mentions = makeMentionFromUserID(me, contractID)
const isMentionedMe = data.text.includes(mentions.me) || data.text.includes(mentions.all)

if (!isAlreadyAdded) {
Expand Down Expand Up @@ -497,7 +497,7 @@ sbp('chelonia/defineContract', {
},
'gi.contracts/chatroom/deleteMessage': {
validate: actionRequireInnerSignature(objectOf({ hash: string })),
process ({ data, meta, innerSigningContractID }, { state }) {
process ({ data, meta, contractID, innerSigningContractID }, { state }) {
if (!state.onlyRenderMessage) {
return
}
Expand All @@ -510,7 +510,7 @@ sbp('chelonia/defineContract', {
if (message.replyingMessage?.hash === data.hash) {
message.replyingMessage.hash = null
message.replyingMessage.text = L('Original message was removed by {user}', {
user: makeMentionFromUserID(innerSigningContractID).me
user: makeMentionFromUserID(innerSigningContractID, contractID).me
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/model/contracts/manifests.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifests": {
"gi.contracts/chatroom": "z9brRu3VRwA9WQ6nhRbJz8KvZ3W6a43vhXrx1iES6yRsExsXzTpG",
"gi.contracts/chatroom": "z9brRu3VRMJTR6qQiaqFxvfUgZP6RJkcpwpG5gX5DdCnS83vTXJn",
"gi.contracts/group": "z9brRu3VNeHviRAc7DLPBQqyagTrpUHoXF9YdMFr1gtrgYGMzfSB",
"gi.contracts/identity": "z9brRu3VSVRddCxKg3YRMP8niKH7gwNq5TexdjvBEDfoLRv97DBh"
}
Expand Down
15 changes: 11 additions & 4 deletions frontend/model/contracts/shared/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,30 @@ export function findMessageIdx (hash: string, messages: Array<Object>): number {
return -1
}

// This function serves two purposes, depending on the forceUsername parameter
// If forceUsername is true, mentions will be like @username, @all, for display
// purposes.
// If forceUsername is false (default), mentions like @username will be converted
// to @<userID>, for internal representation purposes.
// forceUsername is used for display purposes in the UI, so that we can show
// a mention like @username instead of @userID in SendArea
export function makeMentionFromUsername (username: string, forceUsername: ?boolean): {
export function makeMentionFromUsername (username: string, channelID: string, forceUsername: ?boolean): {
me: string, all: string
} {
const rootGetters = sbp('state/vuex/getters')
// Even if forceUsername is true, we want to look up the contract ID to ensure
// that it exists, so that we know it'll later succeed.
const userID = rootGetters.ourContactProfiles[username]?.contractID
return makeMentionFromUserID(forceUsername && userID ? username : userID)
return forceUsername
? makeMentionFromUserID(userID ? username : userID, 'all')
: makeMentionFromUserID(username === 'all' ? channelID : userID, channelID)
}

export function makeMentionFromUserID (userID: string): {
export function makeMentionFromUserID (userID: string, channelID: string): {
me: string, all: string
} {
return {
me: userID ? `@${userID}` : '',
all: '@all'
all: `@${channelID}`
}
}
8 changes: 4 additions & 4 deletions frontend/views/containers/chatroom/MessageBase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default ({
convertTextToMarkdown: Boolean
},
computed: {
...mapGetters(['chatRoomMembers', 'usernameFromID']),
...mapGetters(['chatRoomMembers', 'currentChatRoomId', 'usernameFromID']),
textObjects () {
return this.generateTextObjectsFromText(this.text)
},
Expand Down Expand Up @@ -200,8 +200,8 @@ export default ({
}
]
}
const allMention = makeMentionFromUserID('').all
const possibleMentions = Object.keys(this.chatRoomMembers).map(u => makeMentionFromUserID(u).me).filter(v => !!v)
const allMention = makeMentionFromUserID('', this.currentChatRoomId).all
const possibleMentions = Object.keys(this.chatRoomMembers).map(u => makeMentionFromUserID(u, this.currentChatRoomId).me).filter(v => !!v)
return text
// We try to find all the mentions and render them as mentions instead
Expand All @@ -212,7 +212,7 @@ export default ({
.split(new RegExp(`(?<=\\s|^)(${allMention}|${possibleMentions.join('|')})(?=[^\\w\\d]|$)`))
.map(t => {
if (t === allMention) {
return { type: TextObjectType.Mention, text: t }
return { type: TextObjectType.Mention, text: t[0] + 'all' }
}
return possibleMentions.includes(t)
? { type: TextObjectType.Mention, text: t[0] + this.usernameFromID(t.slice(1)) }
Expand Down
14 changes: 7 additions & 7 deletions frontend/views/containers/chatroom/SendArea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ export default ({
const curPosition = this.$refs.textarea.selectionStart
const selection = this.ephemeral.mention.options[index]
const mentionObj = makeMentionFromUsername(selection.username || selection.memberID, true)
const mentionObj = makeMentionFromUsername(selection.username || selection.memberID, this.currentChatRoomId, true)
const mention = selection.memberID === mentionObj.all ? mentionObj.all : mentionObj.me
const value = curValue.slice(0, this.ephemeral.mention.position) +
mention + ' ' + curValue.slice(curPosition)
Expand Down Expand Up @@ -565,15 +565,15 @@ export default ({
}
/* Process mentions in the form @username => @userID */
const mentionStart = makeMentionFromUsername('').all[0]
const allMention = makeMentionFromUsername('', this.currentChatRoomId, true).all
const mentionStart = allMention[0]
const availableMentions = this.members.map(memberID => memberID.username)
msgToSend = msgToSend.replace(
// This regular expression matches all @username mentions that are
// standing alone between spaces
new RegExp(`(?<=\\s|^)${mentionStart}(${availableMentions.join('|')})(?=[^\\w\\d]|$)`, 'g'),
new RegExp(`(?<=\\s|^)${mentionStart}(${allMention.slice(1)}|${availableMentions.join('|')})(?=[^\\w\\d]|$)`, 'g'),
(_, username) => {
console.log()
return makeMentionFromUsername(username).me
return makeMentionFromUsername(username, this.currentChatRoomId).me
}
)
Expand Down Expand Up @@ -653,13 +653,13 @@ export default ({
this.updateTextWithLines()
},
startMention (keyword, position) {
const all = makeMentionFromUsername('').all
const all = makeMentionFromUsername('', this.currentChatRoomId, true).all
const availableMentions = Array.from(this.members)
// NOTE: '@all' mention should only be needed when the members are more than 3
if (availableMentions.length > 2) {
availableMentions.push({
memberID: all,
displayName: all.slice(1),
displayName: L('all'),
picture: '/assets/images/horn.png'
})
}
Expand Down

0 comments on commit 86697c8

Please sign in to comment.