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: user contacts menu instead of conversation participants (there c… #14143

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 91 additions & 33 deletions src/components/RightSidebar/SearchMessages/SearchMessagesTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

<script setup lang="ts">
import debounce from 'debounce'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { computed, onBeforeUnmount, onMounted, ref, watch, nextTick } from 'vue'
import type { Route } from 'vue-router'

import IconCalendarRange from 'vue-material-design-icons/CalendarRange.vue'
import IconFilter from 'vue-material-design-icons/Filter.vue'
import IconMessageOutline from 'vue-material-design-icons/MessageOutline.vue'

import { getCurrentUser } from '@nextcloud/auth'
import { showError } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'

Expand All @@ -29,10 +30,10 @@ import AvatarWrapper from '../../AvatarWrapper/AvatarWrapper.vue'
import SearchBox from '../../UIShared/SearchBox.vue'
import TransitionWrapper from '../../UIShared/TransitionWrapper.vue'

import { useArrowNavigation } from '../../../composables/useArrowNavigation.js'
import { useIsInCall } from '../../../composables/useIsInCall.js'
import { useStore } from '../../../composables/useStore.js'
import { ATTENDEE } from '../../../constants.js'
import { searchMessages } from '../../../services/coreService.ts'
import { searchMessages, getContacts } from '../../../services/coreService.ts'
import { EventBus } from '../../../services/EventBus.ts'
import {
CoreUnifiedSearchResultEntry,
Expand All @@ -46,7 +47,21 @@ const emit = defineEmits<{
(event: 'close'): void
}>()

type contactObject = {
id: string | number | null,
displayName: string | null | undefined,
isNoUser: boolean,
user: string | number | null,
isUser: boolean,
showUserStatus: boolean,
}
const contactsList = ref<contactObject[]>([])

// initialize navigation
const searchMessagesTab = ref(null)
const searchBox = ref(null)
const { initializeNavigation, resetNavigation } = useArrowNavigation(searchMessagesTab, searchBox)

const isFocused = ref(false)
const searchResults = ref<(CoreUnifiedSearchResultEntry &
{
Expand All @@ -73,24 +88,12 @@ const store = useStore()
const isInCall = useIsInCall()

const token = computed(() => store.getters.getToken())
const participantsInitialised = computed(() => store.getters.participantsInitialised(token.value))
const participants = computed<UserFilterObject>(() => {
return store.getters.participantsList(token.value)
.filter(({ actorType }) => actorType === ATTENDEE.ACTOR_TYPE.USERS) // FIXME: federated users are not supported by the search provider
.map(({ actorId, displayName, actorType }: { actorId: string; displayName: string; actorType: string}) => ({
id: actorId,
displayName,
isNoUser: actorType !== 'users',
user: actorId,
disableMenu: true,
showUserStatus: false,
}))
})
const canLoadMore = computed(() => !isSearchExhausted.value && !isFetchingResults.value && searchCursor.value !== 0)
const hasFilter = computed(() => fromUser.value || sinceDate.value || untilDate.value)

onMounted(() => {
EventBus.on('route-change', onRouteChange)
fetchContacts()
})

onBeforeUnmount(() => {
Expand All @@ -105,13 +108,60 @@ const onRouteChange = ({ from, to }: { from: Route, to: Route }): void => {
}
}

watch(searchText, (value) => {
if (value.trim().length === 0) {
searchResults.value = []
searchCursor.value = 0
isSearchExhausted.value = false
/**
* Fetch contacts list
* @param searchTerm - Search term to filter contacts
*/
async function fetchContacts(searchTerm: string = '') {
try {
const { data: { contacts } } = await getContacts(searchTerm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is what Maksim had asked me. Hmm using contacts is quite bad for multiple reasons:

  1. It's limited by share restrictions, so you might not even see everyone that is in the room
  2. Guests, federated users, emails, … are not supported

I would stay with the current participants for now. As a follow up I would then as a replacement create a new API endpoint which provides the information that you need for all message authors instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to do it together with messages/search endpoint then, since UnifiedSearch provider is also blocking guests, federated users, e.t.c

const mappedContacts = contacts.map(contact => {
return {
id: contact.id,
displayName: contact.fullName,
isNoUser: false,
user: contact.id,
isUser: contact.isUser,
showUserStatus: false,
}
})
contactsList.value.splice(0, contactsList.value.length, ...mappedContacts)
/*
* Add authenticated user to list of contacts for search filter
* If authtenicated user is searching/filtering, do not add them to the list
*/
if (!searchTerm) {
const authenticatedUser = getCurrentUser()
const currentUser = {
id: authenticatedUser?.uid || null,
isNoUser: false,
displayName: authenticatedUser?.displayName,
user: authenticatedUser?.uid || null,
isUser: true,
showUserStatus: false,
}
contactsList.value.unshift(currentUser)
}
} catch (error) {
console.error('Failed to fetch contacts:', error)
}
})
}

/**
* Handle search contacts
* @param search - Search term
* @param loading - Loading function
*/
async function handleSearchContacts(search: string, loading: (loading: boolean) => void) {
try {
loading(true)
await fetchContacts(search)
} catch (error) {
console.error('Failed to search contacts:', error)
} finally {
loading(false)
}
}

/**
* Cancel search and cleanup the search fields and results.
Expand Down Expand Up @@ -162,8 +212,9 @@ async function fetchSearchResults(isNew = true) {
isFetchingResults.value = true

try {
// cancel the previous search request
// cancel the previous search request and reset the navigation
cancelSearchFn()
resetNavigation()

const { request, cancel } = CancelableRequest(searchMessages) as SearchMessageCancelableRequest
cancelSearchFn = cancel
Expand Down Expand Up @@ -217,6 +268,7 @@ async function fetchSearchResults(isNew = true) {
}
})
)
nextTick(() => initializeNavigation())
}
} catch (exception) {
if (CancelableRequest.isCancel(exception)) {
Expand All @@ -230,18 +282,27 @@ async function fetchSearchResults(isNew = true) {
}

const debounceFetchSearchResults = debounce(fetchNewSearchResult, 250)

watch([searchText, fromUser, sinceDate, untilDate], debounceFetchSearchResults)

watch(searchText, (value) => {
if (value.trim().length === 0) {
searchResults.value = []
searchCursor.value = 0
isSearchExhausted.value = false
}
})
</script>

<template>
<div class="search-messages-tab">
<div ref="searchMessagesTab" class="search-messages-tab">
<div class="search-form">
<div class="search-form__main">
<div class="search-form__search-box-wrapper">
<SearchBox ref="searchBox"
:value.sync="searchText"
:placeholder-text="t('spreed', 'Search messages …')"
:is-focused.sync="isFocused"
@input="debounceFetchSearchResults" />
:is-focused.sync="isFocused" />
<NcButton :pressed.sync="searchDetailsOpened"
:aria-label="t('spreed', 'Search options')"
:title="t('spreed', 'Search options')"
Expand All @@ -258,9 +319,8 @@ const debounceFetchSearchResults = debounce(fetchNewSearchResult, 250)
:aria-label-combobox="t('spreed', 'From User')"
:placeholder="t('spreed', 'From User')"
user-select
:loading="!participantsInitialised"
:options="participants"
@update:modelValue="debounceFetchSearchResults" />
:options="contactsList"
@search="handleSearchContacts" />
<div class="search-form__search-detail__date-picker-wrapper">
<NcDateTimePickerNative id="search-form__search-detail__date-picker--since"
v-model="sinceDate"
Expand All @@ -270,8 +330,7 @@ const debounceFetchSearchResults = debounce(fetchNewSearchResult, 250)
:step="1"
:max="new Date()"
:aria-label="t('spreed', 'Since')"
:label="t('spreed', 'Since')"
@update:modelValue="debounceFetchSearchResults" />
:label="t('spreed', 'Since')" />
<NcDateTimePickerNative id="search-form__search-detail__date-picker--until"
v-model="untilDate"
class="search-form__search-detail__date-picker"
Expand All @@ -280,8 +339,7 @@ const debounceFetchSearchResults = debounce(fetchNewSearchResult, 250)
:max="new Date()"
:aria-label="t('spreed', 'Until')"
:label="t('spreed', 'Until')"
:minute-step="1"
@update:modelValue="debounceFetchSearchResults" />
:minute-step="1" />
</div>
</div>
</TransitionWrapper>
Expand Down
10 changes: 9 additions & 1 deletion src/services/coreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
*/

import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { generateOcsUrl, generateUrl } from '@nextcloud/router'

import { getTalkConfig, hasTalkFeature } from './CapabilitiesManager.ts'
import { SHARE } from '../constants.js'
import type {
TaskProcessingResponse,
UnifiedSearchResponse,
SearchMessagePayload,
ContactsMenuResponse,
} from '../types/index.ts'

const canInviteToFederation = hasTalkFeature('local', 'federation-v1')
Expand Down Expand Up @@ -72,9 +73,16 @@ const searchMessages = async function(params: SearchMessagePayload, options: obj
})
}

const getContacts = async function(searchText: string): Promise<ContactsMenuResponse> {
return axios.post(generateUrl('/contactsmenu/contacts'), {
filter: searchText,
})
}

export {
autocompleteQuery,
getTaskById,
deleteTaskById,
searchMessages,
getContacts,
}
65 changes: 45 additions & 20 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,37 +315,37 @@ export type setUserSettingsResponse = ApiResponse<operations['settings-set-user-

// Unified Search
export type MessageSearchResultAttributes = {
conversation: string
messageId: string
actorType: string
actorId: string
conversation: string,
messageId: string,
actorType: string,
actorId: string,
timestamp: string
}

export type CoreUnifiedSearchResultEntry = {
thumbnailUrl: string;
title: string;
subline: string;
resourceUrl: string;
icon: string;
rounded: boolean;
attributes: MessageSearchResultAttributes;
thumbnailUrl: string,
title: string,
subline: string,
resourceUrl: string,
icon: string,
rounded: boolean,
attributes: MessageSearchResultAttributes
}

export type UserFilterObject = {
id: string
displayName: string
isNoUser: boolean
user: string
disableMenu: boolean
id: string,
displayName: string,
isNoUser: boolean,
user: string,
disableMenu: boolean,
showUserStatus: boolean
}

export type CoreUnifiedSearchResult = {
name: string;
isPaginated: boolean;
entries: CoreUnifiedSearchResultEntry[];
cursor: number | string | null;
name: string,
isPaginated: boolean,
entries: CoreUnifiedSearchResultEntry[],
cursor: number | string | null,
}
export type UnifiedSearchResponse = ApiResponseUnwrapped<CoreUnifiedSearchResult>

Expand All @@ -358,3 +358,28 @@ export type SearchMessagePayload = {
limit?: number,
from?: string
}

// Contacts menu
export interface ContactsMenuEntry {
id: number | string | null,
fullName: string,
avatar: string | null,
topAction: object | null,
actions: object[],
lastMessage: '',
emailAddresses: string[],
profileTitle: string | null,
profileUrl: string | null,
status: string | null,
statusMessage: null | string,
statusMessageTimestamp: null | number,
statusIcon: null | string,
isUser: boolean,
uid: null | string
}

export type ContactsMenuResponse = {
data: {
contacts: ContactsMenuEntry[],
}
}
Loading