Skip to content

Commit 1ad6de2

Browse files
PawBudjcbrand
authored andcommitted
AutoComplete: added converse-autocomplete suggestion to group chat query
this commit introduces autocomplete feature to the muc-list group chat dialog, previously there was only an input field which displayed hardcoded servers.
1 parent 795a9a7 commit 1ad6de2

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/plugins/muc-views/templates/add-muc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { api } from '@converse/headless/core.js';
44
import { html } from "lit";
55
import { modal_header_close_button } from "plugins/modal/templates/buttons.js"
66
import { unsafeHTML } from "lit/directives/unsafe-html.js";
7+
import { getAutoCompleteList } from "../utils.js";
78

89

910
const nickname_input = (o) => {
@@ -34,7 +35,10 @@ export default (o) => {
3435
<div class="form-group">
3536
<label for="chatroom">${o.label_room_address}:</label>
3637
${ (o.muc_roomid_policy_error_msg) ? html`<label class="roomid-policy-error">${o.muc_roomid_policy_error_msg}</label>` : '' }
37-
<input type="text" required="required" name="chatroom" class="form-control roomjid-input" placeholder="${o.chatroom_placeholder}"/>
38+
<converse-autocomplete
39+
.getAutoCompleteList="${getAutoCompleteList}"
40+
placeholder="${o.chatroom_placeholder}"
41+
name="chatroom"/>
3842
</div>
3943
${ o.muc_roomid_policy_hint ? html`<div class="form-group">${unsafeHTML(DOMPurify.sanitize(o.muc_roomid_policy_hint, {'ALLOWED_TAGS': ['b', 'br', 'em']}))}</div>` : '' }
4044
${ !api.settings.get('locked_muc_nickname') ? nickname_input(o) : '' }

src/plugins/muc-views/tests/muc-add-modal.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ describe('The "Groupchats" Add modal', function () {
3737
roomspanel.querySelector('.show-add-muc-modal').click();
3838
label_name = modal.el.querySelector('label[for="chatroom"]');
3939
expect(label_name.textContent.trim()).toBe('Groupchat address:');
40-
name_input = modal.el.querySelector('input[name="chatroom"]');
41-
expect(name_input.placeholder).toBe('[email protected]');
40+
await u.waitUntil(() => modal.el.querySelector('input[name="chatroom"]')?.placeholder === '[email protected]');
4241
})
4342
);
4443

src/plugins/muc-views/utils.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,22 @@ export function getAutoCompleteListItem (text, input) {
127127
return element;
128128
}
129129

130+
let fetched_room_jids = [];
131+
let timestamp = null;
132+
133+
async function fetchListOfRooms () {
134+
const response = await fetch('https://search.jabber.network/api/1.0/rooms');
135+
const data = await response.json();
136+
const popular_mucs = data.items.map(item => item.address);
137+
fetched_room_jids = [...new Set(popular_mucs)];
138+
}
139+
130140
export async function getAutoCompleteList () {
131-
const models = [...(await api.rooms.get()), ...(await api.contacts.get())];
132-
const jids = [...new Set(models.map(o => Strophe.getDomainFromJid(o.get('jid'))))];
133-
return jids;
141+
if (!timestamp || converse.env.dayjs().isAfter(timestamp, 'day')) {
142+
await fetchListOfRooms();
143+
timestamp = (new Date()).toISOString();
144+
}
145+
return fetched_room_jids;
134146
}
135147

136148
export async function fetchCommandForm (command) {

0 commit comments

Comments
 (0)