Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: commands shouldn't be in ASC order #614

Merged
merged 8 commits into from
Apr 19, 2024
Merged
3 changes: 3 additions & 0 deletions src/lodash.uniq.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'lodash' {
declare function uniq<T>(s: T[]): T[]
}
PaulDremanovich marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/store/modules/bot-commands/bot-commands-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ import { BotCommand, BotCommandsState } from '@/store/modules/bot-commands/types
export const actions: ActionTree<BotCommandsState, RootState> = {
addBotCommand({ commit }, value: BotCommand): void {
commit('addCommand', value)
},
initBotCommands({ commit }, value: { partnerId: string; commands: string[] }): void {
commit('initCommands', value)
}
}
8 changes: 8 additions & 0 deletions src/store/modules/bot-commands/bot-commands-mutations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MutationTree } from 'vuex'
import { BotCommand, BotCommandsState } from '@/store/modules/bot-commands/types.ts'
import { uniq } from 'lodash'

export const mutations: MutationTree<BotCommandsState> = {
addCommand(state, botCommand: BotCommand): void {
Expand All @@ -15,5 +16,12 @@ export const mutations: MutationTree<BotCommandsState> = {
botCommands.push(commandValue)
}
state.commands[botCommand.partnerId] = botCommands
},

initCommands(state, { partnerId, commands }: { partnerId: string; commands: string[] }): void {
const botCommands = state.commands[partnerId]
if (!botCommands) {
PaulDremanovich marked this conversation as resolved.
Show resolved Hide resolved
state.commands[partnerId] = uniq(commands)
PaulDremanovich marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
14 changes: 14 additions & 0 deletions src/store/modules/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,20 @@ const actions = {
.then(({ messages, lastOffset }) => {
dispatch('unshiftMessages', messages)

if (messages && offset === 0) {
dispatch(
'botCommands/initBotCommands',
{
partnerId: contactId,
commands: messages
.filter((item) => item.message.startsWith('/'))
PaulDremanovich marked this conversation as resolved.
Show resolved Hide resolved
.map((item) => item.message.trim())
.reverse()
},
{ root: true }
)
}

if (messages.length <= 0) {
commit('setChatOffset', { contactId, offset: -1 }) // no more messages
} else {
Expand Down
Loading