Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c85eff59eb | ||
|
|
1134871cbc | ||
|
|
749e4726ff | ||
|
|
57a571ea87 | ||
|
|
3a4249da11 | ||
|
|
dfbcfaa525 | ||
|
|
4a43b3611c | ||
|
|
95c2915723 | ||
|
|
960de3e545 |
@@ -173,6 +173,7 @@ gem 'pgvector'
|
|||||||
# Convert Website HTML to Markdown
|
# Convert Website HTML to Markdown
|
||||||
gem 'reverse_markdown'
|
gem 'reverse_markdown'
|
||||||
|
|
||||||
|
gem 'iso-639'
|
||||||
gem 'ruby-openai'
|
gem 'ruby-openai'
|
||||||
|
|
||||||
gem 'shopify_api'
|
gem 'shopify_api'
|
||||||
|
|||||||
@@ -378,6 +378,8 @@ GEM
|
|||||||
io-console (0.6.0)
|
io-console (0.6.0)
|
||||||
irb (1.7.2)
|
irb (1.7.2)
|
||||||
reline (>= 0.3.6)
|
reline (>= 0.3.6)
|
||||||
|
iso-639 (0.3.8)
|
||||||
|
csv
|
||||||
jbuilder (2.11.5)
|
jbuilder (2.11.5)
|
||||||
actionview (>= 5.0.0)
|
actionview (>= 5.0.0)
|
||||||
activesupport (>= 5.0.0)
|
activesupport (>= 5.0.0)
|
||||||
@@ -913,6 +915,7 @@ DEPENDENCIES
|
|||||||
hashie
|
hashie
|
||||||
html2text
|
html2text
|
||||||
image_processing
|
image_processing
|
||||||
|
iso-639
|
||||||
jbuilder
|
jbuilder
|
||||||
json_refs
|
json_refs
|
||||||
json_schemer
|
json_schemer
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class Api::V1::Accounts::SearchController < Api::V1::Accounts::BaseController
|
|||||||
SearchService.new(
|
SearchService.new(
|
||||||
current_user: Current.user,
|
current_user: Current.user,
|
||||||
current_account: Current.account,
|
current_account: Current.account,
|
||||||
|
current_account_user: Current.account_user,
|
||||||
search_type: search_type,
|
search_type: search_type,
|
||||||
params: params
|
params: params
|
||||||
).perform
|
).perform
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ const attachment = computed(() => {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<BaseBubble class="bg-transparent" data-bubble-name="audio">
|
<BaseBubble class="bg-transparent" data-bubble-name="audio">
|
||||||
<AudioChip :attachment="attachment" class="p-2 text-n-slate-12" />
|
<AudioChip
|
||||||
|
:attachment="attachment"
|
||||||
|
class="p-2 text-n-slate-12 skip-context-menu"
|
||||||
|
/>
|
||||||
</BaseBubble>
|
</BaseBubble>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ const downloadAttachment = async () => {
|
|||||||
</div>
|
</div>
|
||||||
<div v-else class="relative group rounded-lg overflow-hidden">
|
<div v-else class="relative group rounded-lg overflow-hidden">
|
||||||
<img
|
<img
|
||||||
|
class="skip-context-menu"
|
||||||
:src="attachment.dataUrl"
|
:src="attachment.dataUrl"
|
||||||
:width="attachment.width"
|
:width="attachment.width"
|
||||||
:height="attachment.height"
|
:height="attachment.height"
|
||||||
@@ -63,8 +64,9 @@ const downloadAttachment = async () => {
|
|||||||
@error="handleError"
|
@error="handleError"
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
class="inset-0 p-2 absolute bg-gradient-to-tl from-n-slate-12/30 dark:from-n-slate-1/50 via-transparent to-transparent hidden group-hover:flex items-end justify-end gap-1.5"
|
class="inset-0 p-2 pointer-events-none absolute bg-gradient-to-tl from-n-slate-12/30 dark:from-n-slate-1/50 via-transparent to-transparent hidden group-hover:flex"
|
||||||
>
|
/>
|
||||||
|
<div class="absolute right-2 bottom-2 hidden group-hover:flex gap-2">
|
||||||
<Button xs solid slate icon="i-lucide-expand" class="opacity-60" />
|
<Button xs solid slate icon="i-lucide-expand" class="opacity-60" />
|
||||||
<Button
|
<Button
|
||||||
xs
|
xs
|
||||||
|
|||||||
@@ -41,13 +41,13 @@ const onVideoLoadError = () => {
|
|||||||
<div v-if="content" v-dompurify-html="formattedContent" class="mb-2" />
|
<div v-if="content" v-dompurify-html="formattedContent" class="mb-2" />
|
||||||
<img
|
<img
|
||||||
v-if="!hasImgStoryError"
|
v-if="!hasImgStoryError"
|
||||||
class="rounded-lg max-w-80"
|
class="rounded-lg max-w-80 skip-context-menu"
|
||||||
:src="attachment.dataUrl"
|
:src="attachment.dataUrl"
|
||||||
@error="onImageLoadError"
|
@error="onImageLoadError"
|
||||||
/>
|
/>
|
||||||
<video
|
<video
|
||||||
v-else-if="!hasVideoStoryError"
|
v-else-if="!hasVideoStoryError"
|
||||||
class="rounded-lg max-w-80"
|
class="rounded-lg max-w-80 skip-context-menu"
|
||||||
controls
|
controls
|
||||||
:src="attachment.dataUrl"
|
:src="attachment.dataUrl"
|
||||||
@error="onVideoLoadError"
|
@error="onVideoLoadError"
|
||||||
|
|||||||
@@ -35,13 +35,13 @@ const isReel = computed(() => {
|
|||||||
<div class="relative group rounded-lg overflow-hidden">
|
<div class="relative group rounded-lg overflow-hidden">
|
||||||
<div
|
<div
|
||||||
v-if="isReel"
|
v-if="isReel"
|
||||||
class="absolute p-2 flex items-start justify-end right-0"
|
class="absolute p-2 flex items-start justify-end right-0 pointer-events-none"
|
||||||
>
|
>
|
||||||
<Icon icon="i-lucide-instagram" class="text-white shadow-lg" />
|
<Icon icon="i-lucide-instagram" class="text-white shadow-lg" />
|
||||||
</div>
|
</div>
|
||||||
<video
|
<video
|
||||||
controls
|
controls
|
||||||
class="rounded-lg"
|
class="rounded-lg skip-context-menu"
|
||||||
:src="attachment.dataUrl"
|
:src="attachment.dataUrl"
|
||||||
:class="{
|
:class="{
|
||||||
'max-w-48': isReel,
|
'max-w-48': isReel,
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ const handleError = () => {
|
|||||||
</div>
|
</div>
|
||||||
<img
|
<img
|
||||||
v-else
|
v-else
|
||||||
class="object-cover w-full h-full"
|
class="object-cover w-full h-full skip-context-menu"
|
||||||
:src="attachment.dataUrl"
|
:src="attachment.dataUrl"
|
||||||
@error="handleError"
|
@error="handleError"
|
||||||
/>
|
/>
|
||||||
|
|||||||
+174
-172
@@ -166,188 +166,190 @@ onMounted(() => {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<woot-modal
|
<Teleport to="body">
|
||||||
v-model:show="show"
|
<woot-modal
|
||||||
full-width
|
v-model:show="show"
|
||||||
:show-close-button="false"
|
full-width
|
||||||
:on-close="onClose"
|
:show-close-button="false"
|
||||||
>
|
:on-close="onClose"
|
||||||
<div
|
|
||||||
class="bg-n-background flex flex-col h-[inherit] w-[inherit] overflow-hidden select-none"
|
|
||||||
@click="onClose"
|
|
||||||
>
|
>
|
||||||
<header
|
<div
|
||||||
class="z-10 flex items-center justify-between w-full h-16 px-6 py-2 bg-n-background border-b border-n-weak"
|
class="bg-n-background flex flex-col h-[inherit] w-[inherit] overflow-hidden select-none"
|
||||||
@click.stop
|
@click="onClose"
|
||||||
>
|
>
|
||||||
<div
|
<header
|
||||||
v-if="senderDetails"
|
class="z-10 flex items-center justify-between w-full h-16 px-6 py-2 bg-n-background border-b border-n-weak"
|
||||||
class="flex items-center min-w-[15rem] shrink-0"
|
@click.stop
|
||||||
>
|
>
|
||||||
<Thumbnail
|
|
||||||
v-if="senderDetails.avatar"
|
|
||||||
:username="senderDetails.name"
|
|
||||||
:src="senderDetails.avatar"
|
|
||||||
class="flex-shrink-0"
|
|
||||||
/>
|
|
||||||
<div class="flex flex-col ml-2 rtl:ml-0 rtl:mr-2 overflow-hidden">
|
|
||||||
<h3 class="text-base leading-5 m-0 font-medium">
|
|
||||||
<span
|
|
||||||
class="overflow-hidden text-n-slate-12 whitespace-nowrap text-ellipsis"
|
|
||||||
>
|
|
||||||
{{ senderDetails.name }}
|
|
||||||
</span>
|
|
||||||
</h3>
|
|
||||||
<span
|
|
||||||
class="text-xs text-n-slate-11 whitespace-nowrap text-ellipsis"
|
|
||||||
>
|
|
||||||
{{ readableTime }}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div
|
|
||||||
class="flex-1 mx-2 px-2 truncate text-sm font-medium text-center text-n-slate-12"
|
|
||||||
>
|
|
||||||
<span v-dompurify-html="fileNameFromDataUrl" class="truncate" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center gap-2 ml-2 shrink-0">
|
|
||||||
<NextButton
|
|
||||||
v-if="isImage"
|
|
||||||
icon="i-lucide-zoom-in"
|
|
||||||
slate
|
|
||||||
ghost
|
|
||||||
@click="onZoom(0.1)"
|
|
||||||
/>
|
|
||||||
<NextButton
|
|
||||||
v-if="isImage"
|
|
||||||
icon="i-lucide-zoom-out"
|
|
||||||
slate
|
|
||||||
ghost
|
|
||||||
@click="onZoom(-0.1)"
|
|
||||||
/>
|
|
||||||
<NextButton
|
|
||||||
v-if="isImage"
|
|
||||||
icon="i-lucide-rotate-ccw"
|
|
||||||
slate
|
|
||||||
ghost
|
|
||||||
@click="onRotate('counter-clockwise')"
|
|
||||||
/>
|
|
||||||
<NextButton
|
|
||||||
v-if="isImage"
|
|
||||||
icon="i-lucide-rotate-cw"
|
|
||||||
slate
|
|
||||||
ghost
|
|
||||||
@click="onRotate('clockwise')"
|
|
||||||
/>
|
|
||||||
<NextButton
|
|
||||||
icon="i-lucide-download"
|
|
||||||
slate
|
|
||||||
ghost
|
|
||||||
:is-loading="isDownloading"
|
|
||||||
:disabled="isDownloading"
|
|
||||||
@click="onClickDownload"
|
|
||||||
/>
|
|
||||||
<NextButton icon="i-lucide-x" slate ghost @click="onClose" />
|
|
||||||
</div>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<main class="flex items-stretch flex-1 h-full overflow-hidden">
|
|
||||||
<div class="flex items-center justify-center w-16 shrink-0">
|
|
||||||
<NextButton
|
|
||||||
v-if="hasMoreThanOneAttachment"
|
|
||||||
icon="i-lucide-chevron-left"
|
|
||||||
class="z-10"
|
|
||||||
blue
|
|
||||||
faded
|
|
||||||
lg
|
|
||||||
:disabled="activeImageIndex === 0"
|
|
||||||
@click.stop="
|
|
||||||
onClickChangeAttachment(
|
|
||||||
allAttachments[activeImageIndex - 1],
|
|
||||||
activeImageIndex - 1
|
|
||||||
)
|
|
||||||
"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex-1 flex items-center justify-center overflow-hidden">
|
|
||||||
<div
|
<div
|
||||||
v-if="isImage"
|
v-if="senderDetails"
|
||||||
:style="imageWrapperStyle"
|
class="flex items-center min-w-[15rem] shrink-0"
|
||||||
class="flex items-center justify-center origin-center"
|
|
||||||
:class="{
|
|
||||||
// Adjust dimensions when rotated 90/270 degrees to maintain visibility
|
|
||||||
// and prevent image from overflowing container in different aspect ratios
|
|
||||||
'w-[calc(100dvh-8rem)] h-[calc(100dvw-7rem)]':
|
|
||||||
activeImageRotation % 180 !== 0,
|
|
||||||
'size-full': activeImageRotation % 180 === 0,
|
|
||||||
}"
|
|
||||||
>
|
>
|
||||||
<img
|
<Thumbnail
|
||||||
ref="imageRef"
|
v-if="senderDetails.avatar"
|
||||||
:key="activeAttachment.message_id"
|
:username="senderDetails.name"
|
||||||
:src="activeAttachment.data_url"
|
:src="senderDetails.avatar"
|
||||||
:style="imageStyle"
|
class="flex-shrink-0"
|
||||||
class="max-h-full max-w-full object-contain duration-100 ease-in-out transform select-none"
|
/>
|
||||||
@click.stop
|
<div class="flex flex-col ml-2 rtl:ml-0 rtl:mr-2 overflow-hidden">
|
||||||
@dblclick.stop="onDoubleClickZoomImage"
|
<h3 class="text-base leading-5 m-0 font-medium">
|
||||||
@wheel.prevent.stop="onWheelImageZoom"
|
<span
|
||||||
@mousemove="onMouseMove"
|
class="overflow-hidden text-n-slate-12 whitespace-nowrap text-ellipsis"
|
||||||
@mouseleave="onMouseLeave"
|
>
|
||||||
|
{{ senderDetails.name }}
|
||||||
|
</span>
|
||||||
|
</h3>
|
||||||
|
<span
|
||||||
|
class="text-xs text-n-slate-11 whitespace-nowrap text-ellipsis"
|
||||||
|
>
|
||||||
|
{{ readableTime }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
class="flex-1 mx-2 px-2 truncate text-sm font-medium text-center text-n-slate-12"
|
||||||
|
>
|
||||||
|
<span v-dompurify-html="fileNameFromDataUrl" class="truncate" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center gap-2 ml-2 shrink-0">
|
||||||
|
<NextButton
|
||||||
|
v-if="isImage"
|
||||||
|
icon="i-lucide-zoom-in"
|
||||||
|
slate
|
||||||
|
ghost
|
||||||
|
@click="onZoom(0.1)"
|
||||||
|
/>
|
||||||
|
<NextButton
|
||||||
|
v-if="isImage"
|
||||||
|
icon="i-lucide-zoom-out"
|
||||||
|
slate
|
||||||
|
ghost
|
||||||
|
@click="onZoom(-0.1)"
|
||||||
|
/>
|
||||||
|
<NextButton
|
||||||
|
v-if="isImage"
|
||||||
|
icon="i-lucide-rotate-ccw"
|
||||||
|
slate
|
||||||
|
ghost
|
||||||
|
@click="onRotate('counter-clockwise')"
|
||||||
|
/>
|
||||||
|
<NextButton
|
||||||
|
v-if="isImage"
|
||||||
|
icon="i-lucide-rotate-cw"
|
||||||
|
slate
|
||||||
|
ghost
|
||||||
|
@click="onRotate('clockwise')"
|
||||||
|
/>
|
||||||
|
<NextButton
|
||||||
|
icon="i-lucide-download"
|
||||||
|
slate
|
||||||
|
ghost
|
||||||
|
:is-loading="isDownloading"
|
||||||
|
:disabled="isDownloading"
|
||||||
|
@click="onClickDownload"
|
||||||
|
/>
|
||||||
|
<NextButton icon="i-lucide-x" slate ghost @click="onClose" />
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main class="flex items-stretch flex-1 h-full overflow-hidden">
|
||||||
|
<div class="flex items-center justify-center w-16 shrink-0">
|
||||||
|
<NextButton
|
||||||
|
v-if="hasMoreThanOneAttachment"
|
||||||
|
icon="i-lucide-chevron-left"
|
||||||
|
class="z-10"
|
||||||
|
blue
|
||||||
|
faded
|
||||||
|
lg
|
||||||
|
:disabled="activeImageIndex === 0"
|
||||||
|
@click.stop="
|
||||||
|
onClickChangeAttachment(
|
||||||
|
allAttachments[activeImageIndex - 1],
|
||||||
|
activeImageIndex - 1
|
||||||
|
)
|
||||||
|
"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<video
|
<div class="flex-1 flex items-center justify-center overflow-hidden">
|
||||||
v-if="isVideo"
|
<div
|
||||||
:key="activeAttachment.message_id"
|
v-if="isImage"
|
||||||
:src="activeAttachment.data_url"
|
:style="imageWrapperStyle"
|
||||||
controls
|
class="flex items-center justify-center origin-center"
|
||||||
playsInline
|
:class="{
|
||||||
class="max-h-full max-w-full object-contain"
|
// Adjust dimensions when rotated 90/270 degrees to maintain visibility
|
||||||
@click.stop
|
// and prevent image from overflowing container in different aspect ratios
|
||||||
/>
|
'w-[calc(100dvh-8rem)] h-[calc(100dvw-7rem)]':
|
||||||
|
activeImageRotation % 180 !== 0,
|
||||||
|
'size-full': activeImageRotation % 180 === 0,
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
ref="imageRef"
|
||||||
|
:key="activeAttachment.message_id"
|
||||||
|
:src="activeAttachment.data_url"
|
||||||
|
:style="imageStyle"
|
||||||
|
class="max-h-full max-w-full object-contain duration-100 ease-in-out transform select-none"
|
||||||
|
@click.stop
|
||||||
|
@dblclick.stop="onDoubleClickZoomImage"
|
||||||
|
@wheel.prevent.stop="onWheelImageZoom"
|
||||||
|
@mousemove="onMouseMove"
|
||||||
|
@mouseleave="onMouseLeave"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<audio
|
<video
|
||||||
v-if="isAudio"
|
v-if="isVideo"
|
||||||
:key="activeAttachment.message_id"
|
:key="activeAttachment.message_id"
|
||||||
controls
|
:src="activeAttachment.data_url"
|
||||||
class="w-full max-w-md"
|
controls
|
||||||
@click.stop
|
playsInline
|
||||||
>
|
class="max-h-full max-w-full object-contain"
|
||||||
<source :src="`${activeAttachment.data_url}?t=${Date.now()}`" />
|
@click.stop
|
||||||
</audio>
|
/>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center justify-center w-16 shrink-0">
|
<audio
|
||||||
<NextButton
|
v-if="isAudio"
|
||||||
v-if="hasMoreThanOneAttachment"
|
:key="activeAttachment.message_id"
|
||||||
icon="i-lucide-chevron-right"
|
controls
|
||||||
class="z-10"
|
class="w-full max-w-md"
|
||||||
blue
|
@click.stop
|
||||||
faded
|
>
|
||||||
lg
|
<source :src="`${activeAttachment.data_url}?t=${Date.now()}`" />
|
||||||
:disabled="activeImageIndex === allAttachments.length - 1"
|
</audio>
|
||||||
@click.stop="
|
</div>
|
||||||
onClickChangeAttachment(
|
|
||||||
allAttachments[activeImageIndex + 1],
|
|
||||||
activeImageIndex + 1
|
|
||||||
)
|
|
||||||
"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<footer
|
<div class="flex items-center justify-center w-16 shrink-0">
|
||||||
class="z-10 flex items-center justify-center h-12 border-t border-n-weak"
|
<NextButton
|
||||||
>
|
v-if="hasMoreThanOneAttachment"
|
||||||
<div
|
icon="i-lucide-chevron-right"
|
||||||
class="rounded-md flex items-center justify-center px-3 py-1 bg-n-slate-3 text-n-slate-12 text-sm font-medium"
|
class="z-10"
|
||||||
|
blue
|
||||||
|
faded
|
||||||
|
lg
|
||||||
|
:disabled="activeImageIndex === allAttachments.length - 1"
|
||||||
|
@click.stop="
|
||||||
|
onClickChangeAttachment(
|
||||||
|
allAttachments[activeImageIndex + 1],
|
||||||
|
activeImageIndex + 1
|
||||||
|
)
|
||||||
|
"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer
|
||||||
|
class="z-10 flex items-center justify-center h-12 border-t border-n-weak"
|
||||||
>
|
>
|
||||||
{{ `${activeImageIndex + 1} / ${allAttachments.length}` }}
|
<div
|
||||||
</div>
|
class="rounded-md flex items-center justify-center px-3 py-1 bg-n-slate-3 text-n-slate-12 text-sm font-medium"
|
||||||
</footer>
|
>
|
||||||
</div>
|
{{ `${activeImageIndex + 1} / ${allAttachments.length}` }}
|
||||||
</woot-modal>
|
</div>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</woot-modal>
|
||||||
|
</Teleport>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -128,6 +128,14 @@ class Account < ApplicationRecord
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def locale_english_name
|
||||||
|
# the locale can also be something like pt_BR, en_US, fr_FR, etc.
|
||||||
|
# the format is `<locale_code>_<country_code>`
|
||||||
|
# we need to extract the language code from the locale
|
||||||
|
account_locale = locale&.split('_')&.first
|
||||||
|
ISO_639.find(account_locale)&.english_name&.downcase || 'english'
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def notify_creation
|
def notify_creation
|
||||||
|
|||||||
@@ -48,4 +48,8 @@ module AssignmentHandler
|
|||||||
create_assignee_change_activity(user_name)
|
create_assignee_change_activity(user_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self_assign?(assignee_id)
|
||||||
|
assignee_id.present? && Current.user&.id == assignee_id
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -125,6 +125,10 @@ class Conversation < ApplicationRecord
|
|||||||
last_message_in_messaging_window?(messaging_window)
|
last_message_in_messaging_window?(messaging_window)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def language
|
||||||
|
additional_attributes&.dig('conversation_language')
|
||||||
|
end
|
||||||
|
|
||||||
def last_activity_at
|
def last_activity_at
|
||||||
self[:last_activity_at] || created_at
|
self[:last_activity_at] || created_at
|
||||||
end
|
end
|
||||||
@@ -257,10 +261,6 @@ class Conversation < ApplicationRecord
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self_assign?(assignee_id)
|
|
||||||
assignee_id.present? && Current.user&.id == assignee_id
|
|
||||||
end
|
|
||||||
|
|
||||||
def load_attributes_created_by_db_triggers
|
def load_attributes_created_by_db_triggers
|
||||||
# Display id is set via a trigger in the database
|
# Display id is set via a trigger in the database
|
||||||
# So we need to specifically fetch it after the record is created
|
# So we need to specifically fetch it after the record is created
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class SearchService
|
class SearchService
|
||||||
pattr_initialize [:current_user!, :current_account!, :params!, :search_type!]
|
pattr_initialize [:current_user!, :current_account!, :current_account_user!, :params!, :search_type!]
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
case search_type
|
case search_type
|
||||||
@@ -25,13 +25,16 @@ class SearchService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter_conversations
|
def filter_conversations
|
||||||
@conversations = current_account.conversations.where(inbox_id: accessable_inbox_ids)
|
query = current_account.conversations
|
||||||
.joins('INNER JOIN contacts ON conversations.contact_id = contacts.id')
|
# admins can see all conversations anyway, so no need to add the heavy IN clause
|
||||||
.where("cast(conversations.display_id as text) ILIKE :search OR contacts.name ILIKE :search OR contacts.email
|
query = query.where(inbox_id: accessable_inbox_ids) unless @current_account_user.administrator?
|
||||||
ILIKE :search OR contacts.phone_number ILIKE :search OR contacts.identifier ILIKE :search", search: "%#{search_query}%")
|
|
||||||
.order('conversations.created_at DESC')
|
@conversations = query.joins('INNER JOIN contacts ON conversations.contact_id = contacts.id')
|
||||||
.page(params[:page])
|
.where("cast(conversations.display_id as text) ILIKE :search OR contacts.name ILIKE :search OR contacts.email
|
||||||
.per(15)
|
ILIKE :search OR contacts.phone_number ILIKE :search OR contacts.identifier ILIKE :search", search: "%#{search_query}%")
|
||||||
|
.order('conversations.created_at DESC')
|
||||||
|
.page(params[:page])
|
||||||
|
.per(15)
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_messages
|
def filter_messages
|
||||||
@@ -47,16 +50,7 @@ class SearchService
|
|||||||
|
|
||||||
if search_query.present?
|
if search_query.present?
|
||||||
# Use the @@ operator with to_tsquery for better GIN index utilization
|
# Use the @@ operator with to_tsquery for better GIN index utilization
|
||||||
# Convert search query to tsquery format with prefix matching
|
base_query.where('content @@ to_tsquery(?)', search_query)
|
||||||
|
|
||||||
# Use this if we wanna match splitting the words
|
|
||||||
# split_query = search_query.split.map { |term| "#{term} | #{term}:*" }.join(' & ')
|
|
||||||
|
|
||||||
# This will do entire sentence matching using phrase distance operator
|
|
||||||
tsquery = search_query.split.join(' <-> ')
|
|
||||||
|
|
||||||
# Apply the text search using the GIN index
|
|
||||||
base_query.where('content @@ to_tsquery(?)', tsquery)
|
|
||||||
.reorder('created_at DESC')
|
.reorder('created_at DESC')
|
||||||
.page(params[:page])
|
.page(params[:page])
|
||||||
.per(15)
|
.per(15)
|
||||||
@@ -76,12 +70,14 @@ class SearchService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def message_base_query
|
def message_base_query
|
||||||
current_account.messages.where(inbox_id: accessable_inbox_ids)
|
query = current_account.messages
|
||||||
.where('created_at >= ?', 3.months.ago)
|
# admins can see all conversations anyway, so no need to add the heavy IN clause
|
||||||
|
query = query.where(inbox_id: accessable_inbox_ids) unless @current_account_user.administrator?
|
||||||
|
query.where('created_at >= ?', 3.months.ago)
|
||||||
end
|
end
|
||||||
|
|
||||||
def use_gin_search
|
def use_gin_search
|
||||||
current_account.feature_enabled?('search_with_gin')
|
search_query.split.size == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_contacts
|
def filter_contacts
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ module Enterprise::Api::V1::Accounts::ConversationsController
|
|||||||
response = Captain::Copilot::ChatService.new(
|
response = Captain::Copilot::ChatService.new(
|
||||||
assistant,
|
assistant,
|
||||||
previous_messages: copilot_params[:previous_messages],
|
previous_messages: copilot_params[:previous_messages],
|
||||||
conversation_history: @conversation.to_llm_text
|
conversation_history: @conversation.to_llm_text,
|
||||||
|
language: @conversation.account.locale_english_name
|
||||||
).generate_response(copilot_params[:message])
|
).generate_response(copilot_params[:message])
|
||||||
|
|
||||||
render json: { message: response['response'] }
|
render json: { message: response['response'] }
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class Captain::Copilot::ChatService < Llm::BaseOpenAiService
|
|||||||
@assistant = assistant
|
@assistant = assistant
|
||||||
@conversation_history = config[:conversation_history]
|
@conversation_history = config[:conversation_history]
|
||||||
@previous_messages = config[:previous_messages] || []
|
@previous_messages = config[:previous_messages] || []
|
||||||
|
@language = config[:language] || 'english'
|
||||||
@messages = [system_message, conversation_history_context] + @previous_messages
|
@messages = [system_message, conversation_history_context] + @previous_messages
|
||||||
@response = ''
|
@response = ''
|
||||||
end
|
end
|
||||||
@@ -27,7 +28,7 @@ class Captain::Copilot::ChatService < Llm::BaseOpenAiService
|
|||||||
def system_message
|
def system_message
|
||||||
{
|
{
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content: Captain::Llm::SystemPromptsService.copilot_response_generator(@assistant.config['product_name'])
|
content: Captain::Llm::SystemPromptsService.copilot_response_generator(@assistant.config['product_name'], @language)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ class Captain::Llm::ContactNotesService < Llm::BaseOpenAiService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def chat_parameters
|
def chat_parameters
|
||||||
prompt = Captain::Llm::SystemPromptsService.notes_generator
|
account_language = @conversation.account.locale_english_name
|
||||||
|
prompt = Captain::Llm::SystemPromptsService.notes_generator(account_language)
|
||||||
|
|
||||||
{
|
{
|
||||||
model: @model,
|
model: @model,
|
||||||
response_format: { type: 'json_object' },
|
response_format: { type: 'json_object' },
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ class Captain::Llm::ConversationFaqService < Llm::BaseOpenAiService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def chat_parameters
|
def chat_parameters
|
||||||
prompt = Captain::Llm::SystemPromptsService.conversation_faq_generator
|
account_language = @conversation.account.locale_english_name
|
||||||
|
prompt = Captain::Llm::SystemPromptsService.conversation_faq_generator(account_language)
|
||||||
|
|
||||||
{
|
{
|
||||||
model: @model,
|
model: @model,
|
||||||
response_format: { type: 'json_object' },
|
response_format: { type: 'json_object' },
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class Captain::Llm::SystemPromptsService
|
|||||||
SYSTEM_PROMPT_MESSAGE
|
SYSTEM_PROMPT_MESSAGE
|
||||||
end
|
end
|
||||||
|
|
||||||
def copilot_response_generator(product_name)
|
def copilot_response_generator(product_name, language)
|
||||||
<<~SYSTEM_PROMPT_MESSAGE
|
<<~SYSTEM_PROMPT_MESSAGE
|
||||||
[Identity]
|
[Identity]
|
||||||
You are Captain, a helpful and friendly copilot assistant for support agents using the product #{product_name}. Your primary role is to assist support agents by retrieving information, compiling accurate responses, and guiding them through customer interactions.
|
You are Captain, a helpful and friendly copilot assistant for support agents using the product #{product_name}. Your primary role is to assist support agents by retrieving information, compiling accurate responses, and guiding them through customer interactions.
|
||||||
@@ -67,6 +67,7 @@ class Captain::Llm::SystemPromptsService
|
|||||||
|
|
||||||
[Response Guidelines]
|
[Response Guidelines]
|
||||||
- Use natural, polite, and conversational language that is clear and easy to follow. Keep sentences short and use simple words.
|
- Use natural, polite, and conversational language that is clear and easy to follow. Keep sentences short and use simple words.
|
||||||
|
- Reply in the language the agent is using, if you're not able to detect the language, reply in #{language}.
|
||||||
- Provide brief and relevant responses—typically one or two sentences unless a more detailed explanation is necessary.
|
- Provide brief and relevant responses—typically one or two sentences unless a more detailed explanation is necessary.
|
||||||
- Do not use your own training data or assumptions to answer queries. Base responses strictly on the provided information.
|
- Do not use your own training data or assumptions to answer queries. Base responses strictly on the provided information.
|
||||||
- If the query is unclear, ask concise clarifying questions instead of making assumptions.
|
- If the query is unclear, ask concise clarifying questions instead of making assumptions.
|
||||||
|
|||||||
@@ -2,17 +2,71 @@ require 'rails_helper'
|
|||||||
|
|
||||||
RSpec.describe Captain::Copilot::ChatService do
|
RSpec.describe Captain::Copilot::ChatService do
|
||||||
let(:account) { create(:account, custom_attributes: { plan_name: 'startups' }) }
|
let(:account) { create(:account, custom_attributes: { plan_name: 'startups' }) }
|
||||||
let(:inbox) { create(:inbox, account: account) }
|
|
||||||
let(:assistant) { create(:captain_assistant, account: account) }
|
|
||||||
let(:captain_inbox_association) { create(:captain_inbox, captain_assistant: assistant, inbox: inbox) }
|
let(:captain_inbox_association) { create(:captain_inbox, captain_assistant: assistant, inbox: inbox) }
|
||||||
|
|
||||||
let(:mock_captain_agent) { instance_double(Captain::Agent) }
|
let(:mock_captain_agent) { instance_double(Captain::Agent) }
|
||||||
let(:mock_captain_tool) { instance_double(Captain::Tool) }
|
let(:mock_captain_tool) { instance_double(Captain::Tool) }
|
||||||
let(:mock_openai_client) { instance_double(OpenAI::Client) }
|
let(:mock_openai_client) { instance_double(OpenAI::Client) }
|
||||||
|
let(:inbox) { create(:inbox, account: account) }
|
||||||
|
let(:assistant) { create(:captain_assistant, account: account) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key')
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#initialize' do
|
||||||
|
it 'sets default language to english when not specified' do
|
||||||
|
service = described_class.new(assistant, { previous_messages: [], conversation_history: '' })
|
||||||
|
expect(service.instance_variable_get(:@language)).to eq('english')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses the specified language when provided' do
|
||||||
|
service = described_class.new(assistant, {
|
||||||
|
previous_messages: [],
|
||||||
|
conversation_history: '',
|
||||||
|
language: 'spanish'
|
||||||
|
})
|
||||||
|
expect(service.instance_variable_get(:@language)).to eq('spanish')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#generate_response' do
|
||||||
|
before do
|
||||||
|
allow(OpenAI::Client).to receive(:new).and_return(mock_openai_client)
|
||||||
|
allow(mock_openai_client).to receive(:chat).and_return({ choices: [{ message: { content: '{ "result": "Hey" }' } }] }.with_indifferent_access)
|
||||||
|
|
||||||
|
allow(Captain::Agent).to receive(:new).and_return(mock_captain_agent)
|
||||||
|
allow(mock_captain_agent).to receive(:execute).and_return(true)
|
||||||
|
allow(mock_captain_agent).to receive(:register_tool).and_return(true)
|
||||||
|
|
||||||
|
allow(Captain::Tool).to receive(:new).and_return(mock_captain_tool)
|
||||||
|
allow(mock_captain_tool).to receive(:register_method).and_return(true)
|
||||||
|
|
||||||
|
allow(account).to receive(:increment_response_usage).and_return(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'increments usage' do
|
||||||
|
described_class.new(assistant, { previous_messages: ['Hello'], conversation_history: 'Hi' }).generate_response('Hey')
|
||||||
|
expect(account).to have_received(:increment_response_usage).once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'includes language in system message' do
|
||||||
|
service = described_class.new(assistant, {
|
||||||
|
previous_messages: [],
|
||||||
|
conversation_history: '',
|
||||||
|
language: 'spanish'
|
||||||
|
})
|
||||||
|
|
||||||
|
allow(Captain::Llm::SystemPromptsService).to receive(:copilot_response_generator)
|
||||||
|
.with(assistant.config['product_name'], 'spanish')
|
||||||
|
.and_return('Spanish system prompt')
|
||||||
|
|
||||||
|
system_message = service.send(:system_message)
|
||||||
|
expect(system_message[:content]).to eq('Spanish system prompt')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#execute' do
|
describe '#execute' do
|
||||||
before do
|
before do
|
||||||
create(:installation_config) { create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key') }
|
|
||||||
allow(OpenAI::Client).to receive(:new).and_return(mock_openai_client)
|
allow(OpenAI::Client).to receive(:new).and_return(mock_openai_client)
|
||||||
allow(mock_openai_client).to receive(:chat).and_return({ choices: [{ message: { content: '{ "result": "Hey" }' } }] }.with_indifferent_access)
|
allow(mock_openai_client).to receive(:chat).and_return({ choices: [{ message: { content: '{ "result": "Hey" }' } }] }.with_indifferent_access)
|
||||||
|
|
||||||
|
|||||||
@@ -145,5 +145,25 @@ RSpec.describe Captain::Llm::ConversationFaqService do
|
|||||||
{ role: 'user', content: conversation.to_llm_text }
|
{ role: 'user', content: conversation.to_llm_text }
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when conversation has different language' do
|
||||||
|
let(:account) { create(:account, locale: 'fr') }
|
||||||
|
let(:conversation) do
|
||||||
|
create(:conversation, account: account,
|
||||||
|
first_reply_created_at: Time.zone.now)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'includes system prompt with correct language' do
|
||||||
|
allow(Captain::Llm::SystemPromptsService).to receive(:conversation_faq_generator)
|
||||||
|
.with('french')
|
||||||
|
.and_return('system prompt in french')
|
||||||
|
|
||||||
|
params = service.send(:chat_parameters)
|
||||||
|
|
||||||
|
expect(params[:messages]).to include(
|
||||||
|
{ role: 'system', content: 'system prompt in french' }
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -108,4 +108,30 @@ RSpec.describe Account do
|
|||||||
expect(ActiveRecord::Base.connection.execute(query).count).to eq(0)
|
expect(ActiveRecord::Base.connection.execute(query).count).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'locale' do
|
||||||
|
it 'returns correct language if the value is set' do
|
||||||
|
account = create(:account, locale: 'fr')
|
||||||
|
expect(account.locale).to eq('fr')
|
||||||
|
expect(account.locale_english_name).to eq('french')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns english if the value is not set' do
|
||||||
|
account = create(:account, locale: nil)
|
||||||
|
expect(account.locale).to be_nil
|
||||||
|
expect(account.locale_english_name).to eq('english')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns english if the value is empty string' do
|
||||||
|
account = create(:account, locale: '')
|
||||||
|
expect(account.locale).to be_nil
|
||||||
|
expect(account.locale_english_name).to eq('english')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns correct language if the value has country code' do
|
||||||
|
account = create(:account, locale: 'pt_BR')
|
||||||
|
expect(account.locale).to eq('pt_BR')
|
||||||
|
expect(account.locale_english_name).to eq('portuguese')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe SearchService do
|
describe SearchService do
|
||||||
subject(:search) { described_class.new(current_user: user, current_account: account, params: params, search_type: search_type) }
|
subject(:search) do
|
||||||
|
described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
|
end
|
||||||
|
|
||||||
let(:search_type) { 'all' }
|
let(:search_type) { 'all' }
|
||||||
let!(:account) { create(:account) }
|
let!(:account) { create(:account) }
|
||||||
@@ -26,25 +29,33 @@ describe SearchService do
|
|||||||
|
|
||||||
it 'returns all for all' do
|
it 'returns all for all' do
|
||||||
search_type = 'all'
|
search_type = 'all'
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
expect(search.perform.keys).to match_array(%i[contacts messages conversations])
|
expect(search.perform.keys).to match_array(%i[contacts messages conversations])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns contacts for contacts' do
|
it 'returns contacts for contacts' do
|
||||||
search_type = 'Contact'
|
search_type = 'Contact'
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
expect(search.perform.keys).to match_array(%i[contacts])
|
expect(search.perform.keys).to match_array(%i[contacts])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns messages for messages' do
|
it 'returns messages for messages' do
|
||||||
search_type = 'Message'
|
search_type = 'Message'
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
expect(search.perform.keys).to match_array(%i[messages])
|
expect(search.perform.keys).to match_array(%i[messages])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns conversations for conversations' do
|
it 'returns conversations for conversations' do
|
||||||
search_type = 'Conversation'
|
search_type = 'Conversation'
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
expect(search.perform.keys).to match_array(%i[conversations])
|
expect(search.perform.keys).to match_array(%i[conversations])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -61,7 +72,8 @@ describe SearchService do
|
|||||||
harry4 = create(:contact, identifier: 'Potter1235', account_id: account.id, last_activity_at: 2.minutes.ago)
|
harry4 = create(:contact, identifier: 'Potter1235', account_id: account.id, last_activity_at: 2.minutes.ago)
|
||||||
|
|
||||||
params = { q: 'Potter ' }
|
params = { q: 'Potter ' }
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Contact')
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Contact')
|
||||||
expect(search.perform[:contacts].map(&:id)).to eq([harry4.id, harry3.id, harry2.id, harry.id])
|
expect(search.perform[:contacts].map(&:id)).to eq([harry4.id, harry3.id, harry2.id, harry.id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -75,17 +87,19 @@ describe SearchService do
|
|||||||
# random messsage in inbox with out access
|
# random messsage in inbox with out access
|
||||||
create(:message, account: account, inbox: create(:inbox, account: account), content: 'Harry Potter is a wizard')
|
create(:message, account: account, inbox: create(:inbox, account: account), content: 'Harry Potter is a wizard')
|
||||||
params = { q: 'Harry' }
|
params = { q: 'Harry' }
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Message')
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Message')
|
||||||
expect(search.perform[:messages].map(&:id)).to eq([message2.id, message.id])
|
expect(search.perform[:messages].map(&:id)).to eq([message2.id, message.id])
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with feature flag for search type' do
|
context 'with different search methods' do
|
||||||
let(:params) { { q: 'Harry' } }
|
|
||||||
let(:search_type) { 'Message' }
|
let(:search_type) { 'Message' }
|
||||||
|
|
||||||
it 'uses LIKE search when search_with_gin feature is disabled' do
|
it 'uses LIKE search for multi-word queries' do
|
||||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
params = { q: 'Harry Potter' }
|
||||||
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
search_service = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
|
|
||||||
expect(search_service).to receive(:filter_messages_with_like).and_call_original
|
expect(search_service).to receive(:filter_messages_with_like).and_call_original
|
||||||
expect(search_service).not_to receive(:filter_messages_with_gin)
|
expect(search_service).not_to receive(:filter_messages_with_gin)
|
||||||
@@ -93,9 +107,11 @@ describe SearchService do
|
|||||||
search_service.perform
|
search_service.perform
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'uses GIN search when search_with_gin feature is enabled' do
|
it 'uses GIN search for single-word queries' do
|
||||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
params = { q: 'Harry' }
|
||||||
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
search_service = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
|
|
||||||
expect(search_service).to receive(:filter_messages_with_gin).and_call_original
|
expect(search_service).to receive(:filter_messages_with_gin).and_call_original
|
||||||
expect(search_service).not_to receive(:filter_messages_with_like)
|
expect(search_service).not_to receive(:filter_messages_with_like)
|
||||||
@@ -103,24 +119,39 @@ describe SearchService do
|
|||||||
search_service.perform
|
search_service.perform
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns same results regardless of search type' do
|
it 'returns same results for single word regardless of search method' do
|
||||||
# Create test messages
|
# Create test messages
|
||||||
message3 = create(:message, account: account, inbox: inbox, content: 'Harry is a wizard apprentice')
|
message3 = create(:message, account: account, inbox: inbox, content: 'Harry is a wizard apprentice')
|
||||||
|
params = { q: 'Harry' }
|
||||||
|
|
||||||
# Test with GIN search
|
# Test with GIN search (single word)
|
||||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
gin_search = described_class.new(current_user: user, current_account: account,
|
||||||
gin_search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
|
allow(gin_search).to receive(:use_gin_search).and_return(true)
|
||||||
gin_results = gin_search.perform[:messages].map(&:id)
|
gin_results = gin_search.perform[:messages].map(&:id)
|
||||||
|
|
||||||
# Test with LIKE search
|
# Test with LIKE search (forcing LIKE for comparison)
|
||||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
like_search = described_class.new(current_user: user, current_account: account,
|
||||||
like_search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
|
allow(like_search).to receive(:use_gin_search).and_return(false)
|
||||||
like_results = like_search.perform[:messages].map(&:id)
|
like_results = like_search.perform[:messages].map(&:id)
|
||||||
|
|
||||||
# Both search types should return the same messages
|
# Both search types should return the same messages
|
||||||
expect(gin_results).to match_array(like_results)
|
expect(gin_results).to match_array(like_results)
|
||||||
expect(gin_results).to include(message.id, message2.id, message3.id)
|
expect(gin_results).to include(message.id, message2.id, message3.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'handles empty search queries' do
|
||||||
|
params = { q: '' }
|
||||||
|
search_service = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: search_type)
|
||||||
|
|
||||||
|
results = search_service.perform[:messages]
|
||||||
|
expect(results).to be_a(ActiveRecord::Relation)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -131,7 +162,8 @@ describe SearchService do
|
|||||||
create(:conversation, contact: random, inbox: inbox, account: account)
|
create(:conversation, contact: random, inbox: inbox, account: account)
|
||||||
conv2 = create(:conversation, contact: harry, inbox: inbox, account: account)
|
conv2 = create(:conversation, contact: harry, inbox: inbox, account: account)
|
||||||
params = { q: 'Harry' }
|
params = { q: 'Harry' }
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Conversation')
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Conversation')
|
||||||
expect(search.perform[:conversations].map(&:id)).to eq([conv2.id, conversation.id])
|
expect(search.perform[:conversations].map(&:id)).to eq([conv2.id, conversation.id])
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -139,28 +171,86 @@ describe SearchService do
|
|||||||
random = create(:contact, account_id: account.id, name: 'random', email: 'random@random.test', identifier: 'random')
|
random = create(:contact, account_id: account.id, name: 'random', email: 'random@random.test', identifier: 'random')
|
||||||
new_converstion = create(:conversation, contact: random, inbox: inbox, account: account)
|
new_converstion = create(:conversation, contact: random, inbox: inbox, account: account)
|
||||||
params = { q: new_converstion.display_id }
|
params = { q: new_converstion.display_id }
|
||||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Conversation')
|
search = described_class.new(current_user: user, current_account: account,
|
||||||
|
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Conversation')
|
||||||
expect(search.perform[:conversations].map(&:id)).to include new_converstion.id
|
expect(search.perform[:conversations].map(&:id)).to include new_converstion.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#use_gin_search' do
|
describe '#use_gin_search' do
|
||||||
let(:params) { { q: 'test' } }
|
it 'returns true for single-word queries' do
|
||||||
|
params = { q: 'test' }
|
||||||
it 'checks if the account has the search_with_gin feature enabled' do
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
expect(account).to receive(:feature_enabled?).with('search_with_gin')
|
params: params, search_type: 'Message')
|
||||||
search.send(:use_gin_search)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns true when search_with_gin feature is enabled' do
|
|
||||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
|
||||||
expect(search.send(:use_gin_search)).to be true
|
expect(search.send(:use_gin_search)).to be true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns false when search_with_gin feature is disabled' do
|
it 'returns false for multi-word queries' do
|
||||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
params = { q: 'test query' }
|
||||||
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: 'Message')
|
||||||
|
expect(search.send(:use_gin_search)).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns false for empty queries' do
|
||||||
|
params = { q: '' }
|
||||||
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: 'Message')
|
||||||
|
expect(search.send(:use_gin_search)).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns false for nil queries' do
|
||||||
|
params = { q: nil }
|
||||||
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: params, search_type: 'Message')
|
||||||
expect(search.send(:use_gin_search)).to be false
|
expect(search.send(:use_gin_search)).to be false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#accessable_inbox_ids' do
|
||||||
|
it 'returns inbox ids assigned to the user' do
|
||||||
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: {}, search_type: 'Message')
|
||||||
|
expect(search.send(:accessable_inbox_ids)).to eq([inbox.id])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#message_base_query' do
|
||||||
|
it 'returns messages from the last 3 months' do
|
||||||
|
old_message = create(:message, account: account, inbox: inbox, content: 'old message', created_at: 4.months.ago)
|
||||||
|
recent_message = create(:message, account: account, inbox: inbox, content: 'recent message', created_at: 2.months.ago)
|
||||||
|
|
||||||
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: {}, search_type: 'Message')
|
||||||
|
base_query = search.send(:message_base_query)
|
||||||
|
|
||||||
|
expect(base_query).to include(recent_message)
|
||||||
|
expect(base_query).not_to include(old_message)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'filters by inbox_id for non-administrators' do
|
||||||
|
other_inbox = create(:inbox, account: account)
|
||||||
|
other_inbox_message = create(:message, account: account, inbox: other_inbox, content: 'other inbox message')
|
||||||
|
|
||||||
|
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||||
|
params: {}, search_type: 'Message')
|
||||||
|
base_query = search.send(:message_base_query)
|
||||||
|
|
||||||
|
expect(base_query).not_to include(other_inbox_message)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not filter by inbox_id for administrators' do
|
||||||
|
# Create a new user with administrator role
|
||||||
|
admin_user = create(:user, account: account, role: :administrator)
|
||||||
|
other_inbox = create(:inbox, account: account)
|
||||||
|
other_inbox_message = create(:message, account: account, inbox: other_inbox, content: 'other inbox message')
|
||||||
|
|
||||||
|
search = described_class.new(current_user: admin_user, current_account: account,
|
||||||
|
current_account_user: admin_user.account_users.find_by(account: account), params: {}, search_type: 'Message')
|
||||||
|
base_query = search.send(:message_base_query)
|
||||||
|
|
||||||
|
expect(base_query).to include(other_inbox_message)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user