Move functions to utils

This commit is contained in:
JC Brand 2021-06-14 12:03:27 +02:00
parent 543cf9066a
commit ba1b712fee
3 changed files with 45 additions and 43 deletions

View File

@ -1,51 +1,11 @@
import "./message";
import dayjs from 'dayjs';
import tpl_new_day from "./templates/new-day.js";
import { CustomElement } from 'shared/components/element.js';
import { _converse, api } from "@converse/headless/core";
import { api } from "@converse/headless/core";
import { getDayIndicator } from './utils.js';
import { html } from 'lit';
import { repeat } from 'lit/directives/repeat.js';
// Return a TemplateResult indicating a new day if the passed in message is
// more than a day later than its predecessor.
function getDayIndicator (model) {
const models = model.collection.models;
const idx = models.indexOf(model);
const prev_model = models[idx-1];
if (!prev_model || dayjs(model.get('time')).isAfter(dayjs(prev_model.get('time')), 'day')) {
const day_date = dayjs(model.get('time')).startOf('day');
return tpl_new_day({
'type': 'date',
'time': day_date.toISOString(),
'datestring': day_date.format("dddd MMM Do YYYY")
});
}
}
// This is set to _converse so that it can be overriden. An attempt was made to use
// a hook instead, but hook returns a promise and it forces the asynchronicity up
// to the render method.
_converse.getHats = function (model) {
if (model.get('type') === 'groupchat') {
const allowed_hats = api.settings.get('muc_hats').filter(hat => hat).map((hat) => (hat.toLowerCase()));
let vcard_roles = []
if (allowed_hats.includes('vcard_roles')) {
vcard_roles = model.vcard ? model.vcard.get('role') : null;
vcard_roles = vcard_roles ? vcard_roles.split(',').filter(hat => hat).map((hat) => ({title: hat})) : [];
}
const muc_role = model.occupant ? [model.occupant.get('role')] : [];
const muc_affiliation = model.occupant ? [model.occupant.get('affiliation')] : [];
const affiliation_role_hats = [...muc_role, ...muc_affiliation]
.filter(hat => hat).filter((hat) => (allowed_hats.includes(hat.toLowerCase())))
.map((hat) => ({title: hat}));
const hats = allowed_hats.includes('xep317') ? model.occupant?.get('hats') || [] : [];
return [...hats, ...vcard_roles, ...affiliation_role_hats];
}
return [];
}
export default class MessageHistory extends CustomElement {
static get properties () {

View File

@ -12,6 +12,7 @@ import tpl_spinner from 'templates/spinner.js';
import { CustomElement } from 'shared/components/element.js';
import { __ } from 'i18n';
import { _converse, api, converse } from '@converse/headless/core';
import { getHats } from './utils.js';
import { html } from 'lit';
import { renderAvatar } from 'shared/directives/avatar';
@ -196,7 +197,7 @@ export default class Message extends CustomElement {
return {
'pretty_time': dayjs(this.model.get('edited') || this.model.get('time')).format(format),
'has_mentions': this.hasMentions(),
'hats': _converse.getHats(this.model),
'hats': getHats(this.model),
'is_first_unread': this.chatbox.get('first_unread_id') === this.model.get('id'),
'is_me_message': this.model.isMeCommand(),
'is_retracted': this.isRetracted(),

View File

@ -1,4 +1,6 @@
import { _converse, api } from '@converse/headless/core';
import dayjs from 'dayjs';
import tpl_new_day from "./templates/new-day.js";
export function onScrolledDown (model) {
if (!model.isHidden()) {
@ -9,3 +11,42 @@ export function onScrolledDown (model) {
}
}
}
/**
* Given a message object, returns a TemplateResult indicating a new day if
* the passed in message is more than a day later than its predecessor.
* @param { _converse.Message }
*/
export function getDayIndicator (message) {
const messages = message.collection.models;
const idx = messages.indexOf(message);
const prev_message = messages[idx-1];
if (!prev_message || dayjs(message.get('time')).isAfter(dayjs(prev_message.get('time')), 'day')) {
const day_date = dayjs(message.get('time')).startOf('day');
return tpl_new_day({
'type': 'date',
'time': day_date.toISOString(),
'datestring': day_date.format("dddd MMM Do YYYY")
});
}
}
export function getHats (message) {
if (message.get('type') === 'groupchat') {
const allowed_hats = api.settings.get('muc_hats').filter(hat => hat).map((hat) => (hat.toLowerCase()));
let vcard_roles = []
if (allowed_hats.includes('vcard_roles')) {
vcard_roles = message.vcard ? message.vcard.get('role') : null;
vcard_roles = vcard_roles ? vcard_roles.split(',').filter(hat => hat).map((hat) => ({title: hat})) : [];
}
const muc_role = message.occupant ? [message.occupant.get('role')] : [];
const muc_affiliation = message.occupant ? [message.occupant.get('affiliation')] : [];
const affiliation_role_hats = [...muc_role, ...muc_affiliation]
.filter(hat => hat).filter((hat) => (allowed_hats.includes(hat.toLowerCase())))
.map((hat) => ({title: hat}));
const hats = allowed_hats.includes('xep317') ? message.occupant?.get('hats') || [] : [];
return [...hats, ...vcard_roles, ...affiliation_role_hats];
}
return [];
}