2022-04-19 22:47:57 +02:00
|
|
|
import log from "@converse/headless/log.js";
|
2020-12-28 21:04:44 +01:00
|
|
|
import tpl_requesting_contact from "./templates/requesting_contact.js";
|
|
|
|
import tpl_roster_item from "./templates/roster_item.js";
|
2021-03-24 11:59:09 +01:00
|
|
|
import { CustomElement } from 'shared/components/element.js';
|
2020-12-28 18:41:38 +01:00
|
|
|
import { __ } from 'i18n';
|
2022-04-14 23:39:01 +02:00
|
|
|
import { _converse, api } from "@converse/headless/core";
|
2020-12-28 18:41:38 +01:00
|
|
|
|
|
|
|
|
2021-04-14 22:56:59 +02:00
|
|
|
export default class RosterContact extends CustomElement {
|
2021-01-26 14:01:37 +01:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
|
|
|
model: { type: Object }
|
2020-12-28 18:41:38 +01:00
|
|
|
}
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
|
|
|
|
2022-04-19 22:47:57 +02:00
|
|
|
initialize () {
|
|
|
|
this.listenTo(this.model, "change", () => this.requestUpdate());
|
|
|
|
this.listenTo(this.model, "highlight", () => this.requestUpdate());
|
|
|
|
this.listenTo(this.model, 'vcard:add', () => this.requestUpdate());
|
|
|
|
this.listenTo(this.model, 'vcard:change', () => this.requestUpdate());
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2022-04-14 23:39:01 +02:00
|
|
|
if (this.model.get('requesting') === true) {
|
2020-12-28 18:41:38 +01:00
|
|
|
const display_name = this.model.getDisplayName();
|
2021-01-26 14:01:37 +01:00
|
|
|
return tpl_requesting_contact(
|
2020-12-28 18:41:38 +01:00
|
|
|
Object.assign(this.model.toJSON(), {
|
|
|
|
display_name,
|
2021-01-26 14:01:37 +01:00
|
|
|
'openChat': ev => this.openChat(ev),
|
|
|
|
'acceptRequest': ev => this.acceptRequest(ev),
|
|
|
|
'declineRequest': ev => this.declineRequest(ev),
|
2020-12-28 18:41:38 +01:00
|
|
|
'desc_accept': __("Click to accept the contact request from %1$s", display_name),
|
|
|
|
'desc_decline': __("Click to decline the contact request from %1$s", display_name),
|
|
|
|
})
|
2021-01-26 14:01:37 +01:00
|
|
|
);
|
2022-04-14 23:39:01 +02:00
|
|
|
} else {
|
|
|
|
return tpl_roster_item(this, this.model);
|
2020-12-28 18:41:38 +01:00
|
|
|
}
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:41:38 +01:00
|
|
|
openChat (ev) {
|
2021-01-26 14:01:37 +01:00
|
|
|
ev?.preventDefault?.();
|
2020-12-28 18:41:38 +01:00
|
|
|
this.model.openChat();
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
2020-12-28 18:41:38 +01:00
|
|
|
|
2022-05-11 10:01:18 +02:00
|
|
|
async removeContact (ev) {
|
2021-01-26 14:01:37 +01:00
|
|
|
ev?.preventDefault?.();
|
2020-12-28 18:41:38 +01:00
|
|
|
if (!api.settings.get('allow_contact_removal')) { return; }
|
2022-05-11 10:01:18 +02:00
|
|
|
|
|
|
|
const result = await api.confirm(__("Are you sure you want to remove this contact?"));
|
|
|
|
if (!result) return;
|
2020-12-28 18:41:38 +01:00
|
|
|
|
|
|
|
try {
|
2021-01-26 14:01:37 +01:00
|
|
|
this.model.removeFromRoster();
|
2020-12-28 18:41:38 +01:00
|
|
|
if (this.model.collection) {
|
|
|
|
// The model might have already been removed as
|
|
|
|
// result of a roster push.
|
|
|
|
this.model.destroy();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error(e);
|
|
|
|
api.alert('error', __('Error'),
|
|
|
|
[__('Sorry, there was an error while trying to remove %1$s as a contact.', this.model.getDisplayName())]
|
|
|
|
);
|
|
|
|
}
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
2020-12-28 18:41:38 +01:00
|
|
|
|
|
|
|
async acceptRequest (ev) {
|
2021-01-26 14:01:37 +01:00
|
|
|
ev?.preventDefault?.();
|
2020-12-28 18:41:38 +01:00
|
|
|
|
|
|
|
await _converse.roster.sendContactAddIQ(
|
|
|
|
this.model.get('jid'),
|
|
|
|
this.model.getFullname(),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
this.model.authorize().subscribe();
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
2020-12-28 18:41:38 +01:00
|
|
|
|
2022-05-11 10:01:18 +02:00
|
|
|
async declineRequest (ev) {
|
2020-12-28 18:41:38 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2022-05-11 10:01:18 +02:00
|
|
|
const result = await api.confirm(__("Are you sure you want to decline this contact request?"));
|
|
|
|
if (result) {
|
2020-12-28 18:41:38 +01:00
|
|
|
this.model.unauthorize().destroy();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2021-01-26 14:01:37 +01:00
|
|
|
}
|
2020-12-28 18:41:38 +01:00
|
|
|
|
2021-01-26 14:01:37 +01:00
|
|
|
api.elements.define('converse-roster-contact', RosterContact);
|