2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2023-07-10 09:53:00 +00:00
|
|
|
import Clutter from 'gi://Clutter';
|
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import St from 'gi://St';
|
|
|
|
|
|
|
|
let Tpl = null;
|
|
|
|
let Tp = null;
|
2017-03-10 17:23:20 +00:00
|
|
|
try {
|
2023-07-10 09:53:00 +00:00
|
|
|
({default: Tp} = await import('gi://TelepathyGlib'));
|
|
|
|
({default: Tpl} = await import('gi://TelepathyLogger'));
|
2019-12-19 19:50:37 +00:00
|
|
|
|
2022-02-10 23:09:54 +00:00
|
|
|
Gio._promisify(Tp.Channel.prototype, 'close_async');
|
|
|
|
Gio._promisify(Tp.TextChannel.prototype, 'send_message_async');
|
|
|
|
Gio._promisify(Tp.ChannelDispatchOperation.prototype, 'claim_with_async');
|
|
|
|
Gio._promisify(Tpl.LogManager.prototype, 'get_filtered_events_async');
|
2023-07-10 06:34:52 +00:00
|
|
|
} catch {
|
|
|
|
console.debug('Skipping chat support, telepathy not found');
|
2017-03-10 17:23:20 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import * as History from '../../misc/history.js';
|
|
|
|
import * as Main from '../main.js';
|
|
|
|
import * as MessageList from '../messageList.js';
|
|
|
|
import * as MessageTray from '../messageTray.js';
|
|
|
|
import * as Params from '../../misc/params.js';
|
|
|
|
import * as Util from '../../misc/util.js';
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2019-08-19 19:38:51 +00:00
|
|
|
const HAVE_TP = Tp != null && Tpl != null;
|
2017-03-10 17:23:20 +00:00
|
|
|
|
2010-02-22 19:23:36 +00:00
|
|
|
// See Notification.appendMessage
|
2023-07-10 09:53:00 +00:00
|
|
|
const SCROLLBACK_IMMEDIATE_TIME = 3 * 60; // 3 minutes
|
|
|
|
const SCROLLBACK_RECENT_TIME = 15 * 60; // 15 minutes
|
|
|
|
const SCROLLBACK_RECENT_LENGTH = 20;
|
|
|
|
const SCROLLBACK_IDLE_LENGTH = 5;
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2011-03-07 22:10:53 +00:00
|
|
|
// See Source._displayPendingMessages
|
2023-07-10 09:53:00 +00:00
|
|
|
const SCROLLBACK_HISTORY_LINES = 10;
|
2011-03-07 22:10:53 +00:00
|
|
|
|
2011-07-07 12:42:07 +00:00
|
|
|
// See Notification._onEntryChanged
|
2023-07-10 09:53:00 +00:00
|
|
|
const COMPOSING_STOP_TIMEOUT = 5;
|
2011-07-07 12:42:07 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const CHAT_EXPAND_LINES = 12;
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const NotificationDirection = {
|
2010-11-28 15:14:34 +00:00
|
|
|
SENT: 'chat-sent',
|
2019-08-20 21:43:54 +00:00
|
|
|
RECEIVED: 'chat-received',
|
2010-11-28 15:14:34 +00:00
|
|
|
};
|
2010-04-16 19:18:32 +00:00
|
|
|
|
2020-03-20 19:31:45 +00:00
|
|
|
const ChatMessage = HAVE_TP ? GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'message-type': GObject.ParamSpec.int(
|
|
|
|
'message-type', 'message-type', 'message-type',
|
|
|
|
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
|
|
|
|
Math.min(...Object.values(Tp.ChannelTextMessageType)),
|
|
|
|
Math.max(...Object.values(Tp.ChannelTextMessageType)),
|
|
|
|
Tp.ChannelTextMessageType.NORMAL),
|
|
|
|
'text': GObject.ParamSpec.string(
|
|
|
|
'text', 'text', 'text',
|
|
|
|
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
|
|
|
|
null),
|
|
|
|
'sender': GObject.ParamSpec.string(
|
|
|
|
'sender', 'sender', 'sender',
|
|
|
|
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
|
|
|
|
null),
|
|
|
|
'timestamp': GObject.ParamSpec.int64(
|
|
|
|
'timestamp', 'timestamp', 'timestamp',
|
|
|
|
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
|
|
|
|
0, Number.MAX_SAFE_INTEGER, 0),
|
|
|
|
'direction': GObject.ParamSpec.string(
|
|
|
|
'direction', 'direction', 'direction',
|
|
|
|
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
|
|
|
|
null),
|
|
|
|
},
|
|
|
|
}, class ChatMessageClass extends GObject.Object {
|
|
|
|
static newFromTpMessage(tpMessage, direction) {
|
|
|
|
return new ChatMessage({
|
|
|
|
'message-type': tpMessage.get_message_type(),
|
|
|
|
'text': tpMessage.to_text()[0],
|
|
|
|
'sender': tpMessage.sender.alias,
|
|
|
|
'timestamp': direction === NotificationDirection.RECEIVED
|
|
|
|
? tpMessage.get_received_timestamp() : tpMessage.get_sent_timestamp(),
|
|
|
|
direction,
|
|
|
|
});
|
|
|
|
}
|
2011-03-07 22:10:53 +00:00
|
|
|
|
2020-03-20 19:31:45 +00:00
|
|
|
static newFromTplTextEvent(tplTextEvent) {
|
|
|
|
let direction =
|
|
|
|
tplTextEvent.get_sender().get_entity_type() === Tpl.EntityType.SELF
|
|
|
|
? NotificationDirection.SENT : NotificationDirection.RECEIVED;
|
|
|
|
|
|
|
|
return new ChatMessage({
|
|
|
|
'message-type': tplTextEvent.get_message_type(),
|
|
|
|
'text': tplTextEvent.get_message(),
|
|
|
|
'sender': tplTextEvent.get_sender().get_alias(),
|
|
|
|
'timestamp': tplTextEvent.get_timestamp(),
|
|
|
|
direction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}) : null;
|
2011-03-07 22:10:53 +00:00
|
|
|
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class TelepathyComponent {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor() {
|
2017-03-10 17:23:20 +00:00
|
|
|
this._client = null;
|
|
|
|
|
|
|
|
if (!HAVE_TP)
|
|
|
|
return; // Telepathy isn't available
|
|
|
|
|
|
|
|
this._client = new TelepathyClient();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2017-03-10 17:23:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
enable() {
|
2017-03-10 17:23:20 +00:00
|
|
|
if (!this._client)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._client.register();
|
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
throw new Error(`Could not register Telepathy client. Error: ${e}`);
|
2017-03-10 17:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._client.account_manager.is_prepared(Tp.AccountManager.get_feature_quark_core()))
|
|
|
|
this._client.account_manager.prepare_async(null, null);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2017-03-10 17:23:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
disable() {
|
2017-03-10 17:23:20 +00:00
|
|
|
if (!this._client)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._client.unregister();
|
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|
2017-03-10 17:23:20 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const TelepathyClient = HAVE_TP ? GObject.registerClass(
|
2017-10-31 01:23:39 +00:00
|
|
|
class TelepathyClient extends Tp.BaseClient {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init() {
|
2011-06-30 09:53:44 +00:00
|
|
|
// channel path -> ChatSource
|
|
|
|
this._chatSources = {};
|
2011-07-07 12:42:07 +00:00
|
|
|
this._chatState = Tp.ChannelChatState.ACTIVE;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2011-09-14 15:01:48 +00:00
|
|
|
// account path -> AccountNotification
|
|
|
|
this._accountNotifications = {};
|
|
|
|
|
2011-09-12 12:33:02 +00:00
|
|
|
// Define features we want
|
|
|
|
this._accountManager = Tp.AccountManager.dup();
|
|
|
|
let factory = this._accountManager.get_factory();
|
|
|
|
factory.add_account_features([Tp.Account.get_feature_quark_connection()]);
|
|
|
|
factory.add_connection_features([Tp.Connection.get_feature_quark_contact_list()]);
|
|
|
|
factory.add_channel_features([Tp.Channel.get_feature_quark_contacts()]);
|
2020-03-27 13:18:34 +00:00
|
|
|
factory.add_contact_features([
|
|
|
|
Tp.ContactFeature.ALIAS,
|
|
|
|
Tp.ContactFeature.AVATAR_DATA,
|
|
|
|
Tp.ContactFeature.PRESENCE,
|
|
|
|
Tp.ContactFeature.SUBSCRIPTION_STATES,
|
|
|
|
]);
|
2011-09-12 12:33:02 +00:00
|
|
|
|
2011-02-07 16:34:08 +00:00
|
|
|
// Set up a SimpleObserver, which will call _observeChannels whenever a
|
|
|
|
// channel matching its filters is detected.
|
|
|
|
// The second argument, recover, means _observeChannels will be run
|
|
|
|
// for any existing channel as well.
|
2020-03-29 21:51:13 +00:00
|
|
|
super._init({
|
|
|
|
name: 'GnomeShell',
|
|
|
|
account_manager: this._accountManager,
|
|
|
|
uniquify_name: true,
|
|
|
|
});
|
2016-09-19 21:42:55 +00:00
|
|
|
|
|
|
|
// We only care about single-user text-based chats
|
|
|
|
let filter = {};
|
|
|
|
filter[Tp.PROP_CHANNEL_CHANNEL_TYPE] = Tp.IFACE_CHANNEL_TYPE_TEXT;
|
|
|
|
filter[Tp.PROP_CHANNEL_TARGET_HANDLE_TYPE] = Tp.HandleType.CONTACT;
|
|
|
|
|
|
|
|
this.set_observer_recover(true);
|
|
|
|
this.add_observer_filter(filter);
|
|
|
|
this.add_approver_filter(filter);
|
|
|
|
this.add_handler_filter(filter);
|
2011-02-07 16:34:08 +00:00
|
|
|
|
2020-08-19 09:26:11 +00:00
|
|
|
// Allow other clients (such as Empathy) to preempt our channels if
|
2011-06-24 12:29:23 +00:00
|
|
|
// needed
|
2016-09-19 21:42:55 +00:00
|
|
|
this.set_delegated_channels_callback(
|
2017-12-02 00:27:35 +00:00
|
|
|
this._delegatedChannelsCb.bind(this));
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-06-24 12:29:23 +00:00
|
|
|
|
2019-02-01 13:36:37 +00:00
|
|
|
vfunc_observe_channels(...args) {
|
2019-01-31 14:08:00 +00:00
|
|
|
let [account, conn, channels, dispatchOp_, requests_, context] = args;
|
2011-02-07 16:34:08 +00:00
|
|
|
let len = channels.length;
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
let channel = channels[i];
|
2019-01-31 14:08:00 +00:00
|
|
|
let [targetHandle_, targetHandleType] = channel.get_handle();
|
2011-02-07 16:34:08 +00:00
|
|
|
|
2012-08-14 19:21:10 +00:00
|
|
|
if (channel.get_invalidated())
|
2019-01-29 19:36:54 +00:00
|
|
|
continue;
|
2012-06-05 10:39:30 +00:00
|
|
|
|
2011-02-07 16:34:08 +00:00
|
|
|
/* Only observe contact text channels */
|
2019-08-19 19:38:51 +00:00
|
|
|
if (!(channel instanceof Tp.TextChannel) ||
|
2023-08-07 00:51:19 +00:00
|
|
|
targetHandleType !== Tp.HandleType.CONTACT)
|
2019-01-29 19:36:54 +00:00
|
|
|
continue;
|
2011-02-07 16:34:08 +00:00
|
|
|
|
2011-09-12 12:33:02 +00:00
|
|
|
this._createChatSource(account, conn, channel, channel.get_target_contact());
|
2011-02-07 16:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
context.accept();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-02-09 10:35:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_createChatSource(account, conn, channel, contact) {
|
2011-06-30 09:53:44 +00:00
|
|
|
if (this._chatSources[channel.get_object_path()])
|
2011-02-09 10:35:50 +00:00
|
|
|
return;
|
|
|
|
|
2016-09-19 21:42:55 +00:00
|
|
|
let source = new ChatSource(account, conn, channel, contact, this);
|
2011-02-09 10:35:50 +00:00
|
|
|
|
2011-06-30 09:53:44 +00:00
|
|
|
this._chatSources[channel.get_object_path()] = source;
|
2017-10-31 00:38:18 +00:00
|
|
|
source.connect('destroy', () => {
|
|
|
|
delete this._chatSources[channel.get_object_path()];
|
|
|
|
});
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-04-05 11:28:11 +00:00
|
|
|
|
2019-02-01 13:36:37 +00:00
|
|
|
vfunc_handle_channels(...args) {
|
2019-01-31 14:08:00 +00:00
|
|
|
let [account, conn, channels, requests_, userActionTime_, context] = args;
|
2011-12-15 11:24:38 +00:00
|
|
|
this._handlingChannels(account, conn, channels, true);
|
2011-07-17 19:25:42 +00:00
|
|
|
context.accept();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-07-17 19:25:42 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_handlingChannels(account, conn, channels, notify) {
|
2011-04-05 11:28:11 +00:00
|
|
|
let len = channels.length;
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
let channel = channels[i];
|
|
|
|
|
|
|
|
// We can only handle text channel, so close any other channel
|
|
|
|
if (!(channel instanceof Tp.TextChannel)) {
|
2019-12-19 19:50:37 +00:00
|
|
|
channel.close_async();
|
2011-04-05 11:28:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:21:10 +00:00
|
|
|
if (channel.get_invalidated())
|
2019-01-29 19:36:54 +00:00
|
|
|
continue;
|
2012-06-05 10:39:30 +00:00
|
|
|
|
2011-12-15 11:24:38 +00:00
|
|
|
// 'notify' will be true when coming from an actual HandleChannels
|
|
|
|
// call, and not when from a successful Claim call. The point is
|
|
|
|
// we don't want to notify for a channel we just claimed which
|
|
|
|
// has no new messages (for example, a new channel which only has
|
|
|
|
// a delivery notification). We rely on _displayPendingMessages()
|
|
|
|
// and _messageReceived() to notify for new messages.
|
|
|
|
|
|
|
|
// But we should still notify from HandleChannels because the
|
|
|
|
// Telepathy spec states that handlers must foreground channels
|
|
|
|
// in HandleChannels calls which are already being handled.
|
|
|
|
|
2016-09-19 21:42:55 +00:00
|
|
|
if (notify && this.is_handling_channel(channel)) {
|
2011-04-05 11:28:11 +00:00
|
|
|
// We are already handling the channel, display the source
|
2011-06-30 09:53:44 +00:00
|
|
|
let source = this._chatSources[channel.get_object_path()];
|
2011-04-05 11:28:11 +00:00
|
|
|
if (source)
|
2019-05-13 21:32:31 +00:00
|
|
|
source.showNotification();
|
2011-04-05 11:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-04-05 11:28:11 +00:00
|
|
|
|
2019-02-01 13:36:37 +00:00
|
|
|
vfunc_add_dispatch_operation(...args) {
|
|
|
|
let [account, conn, channels, dispatchOp, context] = args;
|
2011-06-30 12:05:41 +00:00
|
|
|
let channel = channels[0];
|
2011-07-11 11:09:45 +00:00
|
|
|
let chanType = channel.get_channel_type();
|
|
|
|
|
2012-08-14 19:21:10 +00:00
|
|
|
if (channel.get_invalidated()) {
|
2020-03-29 21:51:13 +00:00
|
|
|
context.fail(new Tp.Error({
|
|
|
|
code: Tp.Error.INVALID_ARGUMENT,
|
|
|
|
message: 'Channel is invalidated',
|
|
|
|
}));
|
2012-06-05 10:39:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (chanType === Tp.IFACE_CHANNEL_TYPE_TEXT) {
|
2011-07-11 11:09:45 +00:00
|
|
|
this._approveTextChannel(account, conn, channel, dispatchOp, context);
|
2019-08-20 00:51:42 +00:00
|
|
|
} else {
|
2020-03-29 21:51:13 +00:00
|
|
|
context.fail(new Tp.Error({
|
|
|
|
code: Tp.Error.INVALID_ARGUMENT,
|
|
|
|
message: 'Unsupported channel type',
|
|
|
|
}));
|
2019-08-20 00:51:42 +00:00
|
|
|
}
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-07-11 11:09:45 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
async _approveTextChannel(account, conn, channel, dispatchOp, context) {
|
2019-01-31 14:08:00 +00:00
|
|
|
let [targetHandle_, targetHandleType] = channel.get_handle();
|
2011-06-30 12:05:41 +00:00
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (targetHandleType !== Tp.HandleType.CONTACT) {
|
2020-03-29 21:51:13 +00:00
|
|
|
context.fail(new Tp.Error({
|
|
|
|
code: Tp.Error.INVALID_ARGUMENT,
|
|
|
|
message: 'Unsupported handle type',
|
|
|
|
}));
|
2015-03-03 08:27:48 +00:00
|
|
|
return;
|
2011-06-30 12:05:41 +00:00
|
|
|
}
|
2015-03-03 08:27:48 +00:00
|
|
|
|
|
|
|
context.accept();
|
2019-12-19 19:50:37 +00:00
|
|
|
|
|
|
|
// Approve private text channels right away as we are going to handle it
|
|
|
|
try {
|
|
|
|
await dispatchOp.claim_with_async(this);
|
|
|
|
this._handlingChannels(account, conn, [channel], false);
|
|
|
|
} catch (err) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Failed to claim channel: ${err}`);
|
2019-12-19 19:50:37 +00:00
|
|
|
}
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2011-06-30 12:05:41 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
_delegatedChannelsCb(_client, _channels) {
|
2011-06-24 12:29:23 +00:00
|
|
|
// Nothing to do as we don't make a distinction between observed and
|
|
|
|
// handled channels.
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-03-10 17:23:20 +00:00
|
|
|
}) : null;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const ChatSource = HAVE_TP ? GObject.registerClass(
|
2019-05-13 21:32:31 +00:00
|
|
|
class ChatSource extends MessageTray.Source {
|
|
|
|
_init(account, conn, channel, contact, client) {
|
2011-02-08 10:22:46 +00:00
|
|
|
this._account = account;
|
2011-02-08 12:17:29 +00:00
|
|
|
this._contact = contact;
|
2011-04-05 11:28:11 +00:00
|
|
|
this._client = client;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2020-03-23 19:59:31 +00:00
|
|
|
super._init(contact.get_alias());
|
|
|
|
|
2012-05-25 09:15:57 +00:00
|
|
|
this.isChat = true;
|
2011-05-27 09:39:50 +00:00
|
|
|
this._pendingMessages = [];
|
|
|
|
|
2011-02-08 10:22:46 +00:00
|
|
|
this._conn = conn;
|
|
|
|
this._channel = channel;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2011-08-22 14:23:50 +00:00
|
|
|
this._notifyTimeoutId = 0;
|
2010-11-28 15:14:34 +00:00
|
|
|
|
2011-02-08 13:50:06 +00:00
|
|
|
this._presence = contact.get_presence_type();
|
2010-04-16 21:24:34 +00:00
|
|
|
|
2021-08-15 22:36:59 +00:00
|
|
|
this._channel.connectObject(
|
|
|
|
'invalidated', this._channelClosed.bind(this),
|
|
|
|
'message-sent', this._messageSent.bind(this),
|
|
|
|
'message-received', this._messageReceived.bind(this),
|
|
|
|
'pending-message-removed', this._pendingRemoved.bind(this), this);
|
2010-08-05 17:09:27 +00:00
|
|
|
|
2021-08-15 22:36:59 +00:00
|
|
|
this._contact.connectObject(
|
|
|
|
'notify::alias', this._updateAlias.bind(this),
|
|
|
|
'notify::avatar-file', this._updateAvatarIcon.bind(this),
|
|
|
|
'presence-changed', this._presenceChanged.bind(this), this);
|
2011-02-08 15:19:01 +00:00
|
|
|
|
2011-02-28 18:14:07 +00:00
|
|
|
// Add ourselves as a source.
|
|
|
|
Main.messageTray.add(this);
|
|
|
|
|
2011-03-07 22:10:53 +00:00
|
|
|
this._getLogMessages();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-08 12:17:29 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_ensureNotification() {
|
2015-04-10 11:18:00 +00:00
|
|
|
if (this._notification)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._notification = new ChatNotification(this);
|
2021-08-15 22:36:59 +00:00
|
|
|
this._notification.connectObject(
|
|
|
|
'activated', this.open.bind(this),
|
|
|
|
'destroy', () => (this._notification = null),
|
|
|
|
'updated', () => {
|
|
|
|
if (this._banner && this._banner.expanded)
|
|
|
|
this._ackMessages();
|
|
|
|
}, this);
|
2015-04-10 11:18:00 +00:00
|
|
|
this.pushNotification(this._notification);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-04-10 11:18:00 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_createPolicy() {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._account.protocol_name === 'irc')
|
2015-07-26 15:33:05 +00:00
|
|
|
return new MessageTray.NotificationApplicationPolicy('org.gnome.Polari');
|
2013-10-13 20:01:56 +00:00
|
|
|
return new MessageTray.NotificationApplicationPolicy('empathy');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-11-04 01:11:08 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
createBanner() {
|
2015-02-17 03:19:07 +00:00
|
|
|
this._banner = new ChatNotificationBanner(this._notification);
|
|
|
|
|
|
|
|
// We ack messages when the user expands the new notification
|
2021-08-15 22:36:59 +00:00
|
|
|
this._banner.connectObject(
|
|
|
|
'expanded', this._ackMessages.bind(this),
|
|
|
|
'destroy', () => (this._banner = null), this);
|
2015-02-17 03:19:07 +00:00
|
|
|
|
|
|
|
return this._banner;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateAlias() {
|
2011-02-28 18:15:02 +00:00
|
|
|
let oldAlias = this.title;
|
2011-10-03 18:48:49 +00:00
|
|
|
let newAlias = this._contact.get_alias();
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (oldAlias === newAlias)
|
2011-10-03 18:48:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
this.setTitle(newAlias);
|
2015-04-10 11:18:00 +00:00
|
|
|
if (this._notification)
|
|
|
|
this._notification.appendAliasChange(oldAlias, newAlias);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
getIcon() {
|
2011-02-08 13:05:20 +00:00
|
|
|
let file = this._contact.get_avatar_file();
|
2019-08-20 00:51:42 +00:00
|
|
|
if (file)
|
2023-08-06 22:40:20 +00:00
|
|
|
return new Gio.FileIcon({file});
|
2019-08-20 00:51:42 +00:00
|
|
|
else
|
2023-08-06 22:40:20 +00:00
|
|
|
return new Gio.ThemedIcon({name: 'avatar-default'});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-21 20:46:08 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
getSecondaryIcon() {
|
2012-09-14 14:33:52 +00:00
|
|
|
let iconName;
|
2012-07-05 17:47:13 +00:00
|
|
|
let presenceType = this._contact.get_presence_type();
|
|
|
|
|
|
|
|
switch (presenceType) {
|
2019-02-01 12:21:00 +00:00
|
|
|
case Tp.ConnectionPresenceType.AVAILABLE:
|
|
|
|
iconName = 'user-available';
|
|
|
|
break;
|
|
|
|
case Tp.ConnectionPresenceType.BUSY:
|
|
|
|
iconName = 'user-busy';
|
|
|
|
break;
|
|
|
|
case Tp.ConnectionPresenceType.OFFLINE:
|
|
|
|
iconName = 'user-offline';
|
|
|
|
break;
|
|
|
|
case Tp.ConnectionPresenceType.HIDDEN:
|
|
|
|
iconName = 'user-invisible';
|
|
|
|
break;
|
|
|
|
case Tp.ConnectionPresenceType.AWAY:
|
|
|
|
iconName = 'user-away';
|
|
|
|
break;
|
|
|
|
case Tp.ConnectionPresenceType.EXTENDED_AWAY:
|
|
|
|
iconName = 'user-idle';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
iconName = 'user-offline';
|
|
|
|
}
|
2023-08-06 22:40:20 +00:00
|
|
|
return new Gio.ThemedIcon({name: iconName});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-07-05 17:47:13 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateAvatarIcon() {
|
2012-07-23 04:07:51 +00:00
|
|
|
this.iconUpdated();
|
2021-08-13 00:39:34 +00:00
|
|
|
if (this._notification) {
|
2023-08-06 23:45:22 +00:00
|
|
|
this._notification.update(
|
|
|
|
this._notification.title,
|
|
|
|
this._notification.bannerBodyText,
|
|
|
|
{gicon: this.getIcon()});
|
2019-08-20 00:51:42 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
open() {
|
2015-03-11 00:04:38 +00:00
|
|
|
Main.overview.hide();
|
|
|
|
Main.panel.closeCalendar();
|
|
|
|
|
2013-10-14 03:20:50 +00:00
|
|
|
if (this._client.is_handling_channel(this._channel)) {
|
2015-03-02 09:34:06 +00:00
|
|
|
// We are handling the channel, try to pass it to Empathy or Polari
|
|
|
|
// (depending on the channel type)
|
2019-05-15 19:32:29 +00:00
|
|
|
// We don't check if either app is available - mission control will
|
2015-03-02 09:34:06 +00:00
|
|
|
// fallback to something else if activation fails
|
|
|
|
|
|
|
|
let target;
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._channel.connection.protocol_name === 'irc')
|
2015-03-02 09:34:06 +00:00
|
|
|
target = 'org.freedesktop.Telepathy.Client.Polari';
|
|
|
|
else
|
|
|
|
target = 'org.freedesktop.Telepathy.Client.Empathy.Chat';
|
|
|
|
this._client.delegate_channels_async([this._channel], global.get_current_time(), target, null);
|
2013-10-14 03:20:50 +00:00
|
|
|
} else {
|
|
|
|
// We are not the handler, just ask to present the channel
|
|
|
|
let dbus = Tp.DBusDaemon.dup();
|
|
|
|
let cd = Tp.ChannelDispatcher.new(dbus);
|
|
|
|
|
|
|
|
cd.present_channel_async(this._channel, global.get_current_time(), null);
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-04-08 14:55:22 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
async _getLogMessages() {
|
2011-03-07 22:10:53 +00:00
|
|
|
let logManager = Tpl.LogManager.dup_singleton();
|
|
|
|
let entity = Tpl.Entity.new_from_tp_contact(this._contact, Tpl.EntityType.CONTACT);
|
2012-05-25 18:55:20 +00:00
|
|
|
|
2019-12-19 19:50:37 +00:00
|
|
|
const [events] = await logManager.get_filtered_events_async(
|
|
|
|
this._account, entity,
|
|
|
|
Tpl.EventTypeMask.TEXT, SCROLLBACK_HISTORY_LINES,
|
|
|
|
null);
|
2011-03-07 22:10:53 +00:00
|
|
|
|
2020-03-20 19:31:45 +00:00
|
|
|
let logMessages = events.map(e => ChatMessage.newFromTplTextEvent(e));
|
2015-04-10 11:18:00 +00:00
|
|
|
this._ensureNotification();
|
2011-03-07 22:10:53 +00:00
|
|
|
|
2011-03-23 19:25:42 +00:00
|
|
|
let pendingTpMessages = this._channel.get_pending_messages();
|
2011-05-27 08:52:08 +00:00
|
|
|
let pendingMessages = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < pendingTpMessages.length; i++) {
|
|
|
|
let message = pendingTpMessages[i];
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (message.get_message_type() === Tp.ChannelTextMessageType.DELIVERY_REPORT)
|
2011-05-27 08:52:08 +00:00
|
|
|
continue;
|
|
|
|
|
2020-03-20 19:31:45 +00:00
|
|
|
pendingMessages.push(ChatMessage.newFromTpMessage(message,
|
|
|
|
NotificationDirection.RECEIVED));
|
2011-05-27 09:39:50 +00:00
|
|
|
|
|
|
|
this._pendingMessages.push(message);
|
2011-05-27 08:52:08 +00:00
|
|
|
}
|
2011-03-23 19:25:42 +00:00
|
|
|
|
2012-08-06 15:28:55 +00:00
|
|
|
this.countUpdated();
|
2011-06-27 12:31:53 +00:00
|
|
|
|
2011-03-24 11:09:04 +00:00
|
|
|
let showTimestamp = false;
|
|
|
|
|
2011-03-23 19:25:42 +00:00
|
|
|
for (let i = 0; i < logMessages.length; i++) {
|
|
|
|
let logMessage = logMessages[i];
|
|
|
|
let isPending = false;
|
|
|
|
|
|
|
|
// Skip any log messages that are also in pendingMessages
|
|
|
|
for (let j = 0; j < pendingMessages.length; j++) {
|
|
|
|
let pending = pendingMessages[j];
|
2023-08-07 00:51:19 +00:00
|
|
|
if (logMessage.timestamp === pending.timestamp && logMessage.text === pending.text) {
|
2011-03-23 19:25:42 +00:00
|
|
|
isPending = true;
|
|
|
|
break;
|
2011-03-07 22:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2011-03-24 11:09:04 +00:00
|
|
|
if (!isPending) {
|
|
|
|
showTimestamp = true;
|
2011-03-24 11:11:17 +00:00
|
|
|
this._notification.appendMessage(logMessage, true, ['chat-log-message']);
|
2011-03-24 11:09:04 +00:00
|
|
|
}
|
2011-02-08 15:19:01 +00:00
|
|
|
}
|
2011-03-07 22:10:53 +00:00
|
|
|
|
2011-03-24 11:09:04 +00:00
|
|
|
if (showTimestamp)
|
|
|
|
this._notification.appendTimestamp();
|
|
|
|
|
2011-03-23 19:25:42 +00:00
|
|
|
for (let i = 0; i < pendingMessages.length; i++)
|
|
|
|
this._notification.appendMessage(pendingMessages[i], true);
|
|
|
|
|
|
|
|
if (pendingMessages.length > 0)
|
2019-05-13 21:32:31 +00:00
|
|
|
this.showNotification();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
destroy(reason) {
|
2015-04-10 11:19:23 +00:00
|
|
|
if (this._client.is_handling_channel(this._channel)) {
|
2016-03-08 14:52:09 +00:00
|
|
|
this._ackMessages();
|
2015-04-10 11:19:23 +00:00
|
|
|
// The chat box has been destroyed so it can't
|
|
|
|
// handle the channel any more.
|
2019-12-19 19:50:37 +00:00
|
|
|
this._channel.close_async();
|
2016-09-03 20:24:29 +00:00
|
|
|
} else {
|
|
|
|
// Don't indicate any unread messages when the notification
|
|
|
|
// that represents them has been destroyed.
|
|
|
|
this._pendingMessages = [];
|
|
|
|
this.countUpdated();
|
2015-04-10 11:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep source alive while the channel is open
|
2023-08-07 00:51:19 +00:00
|
|
|
if (reason !== MessageTray.NotificationDestroyedReason.SOURCE_CLOSED)
|
2015-04-10 11:19:23 +00:00
|
|
|
return;
|
|
|
|
|
2014-01-20 22:21:14 +00:00
|
|
|
if (this._destroyed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._destroyed = true;
|
2021-08-15 22:36:59 +00:00
|
|
|
this._channel.disconnectObject(this);
|
|
|
|
this._contact.disconnectObject(this);
|
2011-02-08 15:19:01 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
super.destroy(reason);
|
|
|
|
}
|
2014-01-20 22:21:14 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_channelClosed() {
|
2014-01-20 22:21:14 +00:00
|
|
|
this.destroy(MessageTray.NotificationDestroyedReason.SOURCE_CLOSED);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2012-08-06 15:28:55 +00:00
|
|
|
/* All messages are new messages for Telepathy sources */
|
|
|
|
get count() {
|
|
|
|
return this._pendingMessages.length;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-06 15:28:55 +00:00
|
|
|
|
|
|
|
get unseenCount() {
|
|
|
|
return this.count;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-06 15:28:55 +00:00
|
|
|
|
|
|
|
get countVisible() {
|
|
|
|
return this.count > 0;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 12:31:53 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_messageReceived(channel, message) {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (message.get_message_type() === Tp.ChannelTextMessageType.DELIVERY_REPORT)
|
2011-05-05 12:19:59 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-10 11:18:00 +00:00
|
|
|
this._ensureNotification();
|
2011-05-27 09:39:50 +00:00
|
|
|
this._pendingMessages.push(message);
|
2012-08-06 15:28:55 +00:00
|
|
|
this.countUpdated();
|
2011-05-27 09:39:50 +00:00
|
|
|
|
2020-03-20 19:31:45 +00:00
|
|
|
message = ChatMessage.newFromTpMessage(message,
|
|
|
|
NotificationDirection.RECEIVED);
|
2011-03-07 22:10:53 +00:00
|
|
|
this._notification.appendMessage(message);
|
2011-08-22 14:23:50 +00:00
|
|
|
|
|
|
|
// Wait a bit before notifying for the received message, a handler
|
|
|
|
// could ack it in the meantime.
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._notifyTimeoutId !== 0)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._notifyTimeoutId);
|
|
|
|
this._notifyTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 500,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._notifyTimeout.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._notifyTimeoutId, '[gnome-shell] this._notifyTimeout');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-08-22 14:23:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_notifyTimeout() {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._pendingMessages.length !== 0)
|
2019-05-13 21:32:31 +00:00
|
|
|
this.showNotification();
|
2011-08-22 14:23:50 +00:00
|
|
|
|
|
|
|
this._notifyTimeoutId = 0;
|
|
|
|
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-11-28 15:14:34 +00:00
|
|
|
// This is called for both messages we send from
|
|
|
|
// our client and other clients as well.
|
2019-01-31 14:08:10 +00:00
|
|
|
_messageSent(channel, message, _flags, _token) {
|
2015-04-10 11:18:00 +00:00
|
|
|
this._ensureNotification();
|
2020-03-20 19:31:45 +00:00
|
|
|
message = ChatMessage.newFromTpMessage(message,
|
|
|
|
NotificationDirection.SENT);
|
2011-03-07 22:10:53 +00:00
|
|
|
this._notification.appendMessage(message);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-04-16 21:24:34 +00:00
|
|
|
|
2019-05-13 21:32:31 +00:00
|
|
|
showNotification() {
|
|
|
|
super.showNotification(this._notification);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
respond(text) {
|
2011-02-26 14:38:25 +00:00
|
|
|
let type;
|
2023-08-07 00:51:19 +00:00
|
|
|
if (text.slice(0, 4) === '/me ') {
|
2011-02-26 14:38:25 +00:00
|
|
|
type = Tp.ChannelTextMessageType.ACTION;
|
|
|
|
text = text.slice(4);
|
|
|
|
} else {
|
|
|
|
type = Tp.ChannelTextMessageType.NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = Tp.ClientMessage.new_text(type, text);
|
2019-12-19 19:50:37 +00:00
|
|
|
this._channel.send_message_async(msg, 0);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-04-16 21:24:34 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
setChatState(state) {
|
2011-07-07 12:42:07 +00:00
|
|
|
// We don't want to send COMPOSING every time a letter is typed into
|
|
|
|
// the entry. We send the state only when it changes. Telepathy/Empathy
|
|
|
|
// might change it behind our back if the user is using both
|
|
|
|
// gnome-shell's entry and the Empathy conversation window. We could
|
|
|
|
// keep track of it with the ChatStateChanged signal but it is good
|
|
|
|
// enough right now.
|
2023-08-07 00:51:19 +00:00
|
|
|
if (state !== this._chatState) {
|
2019-01-29 19:36:54 +00:00
|
|
|
this._chatState = state;
|
|
|
|
this._channel.set_chat_state_async(state, null);
|
2011-07-07 12:42:07 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-07 12:42:07 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
_presenceChanged(_contact, _presence, _status, _message) {
|
2019-08-20 00:51:42 +00:00
|
|
|
if (this._notification) {
|
2023-08-06 23:45:22 +00:00
|
|
|
this._notification.update(
|
|
|
|
this._notification.title,
|
|
|
|
this._notification.bannerBodyText,
|
|
|
|
{secondaryGIcon: this.getSecondaryIcon()});
|
2019-08-20 00:51:42 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-05-27 09:39:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_pendingRemoved(channel, message) {
|
2011-05-27 09:39:50 +00:00
|
|
|
let idx = this._pendingMessages.indexOf(message);
|
|
|
|
|
2011-06-27 12:31:53 +00:00
|
|
|
if (idx >= 0) {
|
2011-05-27 09:39:50 +00:00
|
|
|
this._pendingMessages.splice(idx, 1);
|
2012-08-06 15:28:55 +00:00
|
|
|
this.countUpdated();
|
2011-06-27 12:31:53 +00:00
|
|
|
}
|
2015-03-17 20:08:05 +00:00
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._pendingMessages.length === 0 &&
|
2015-03-17 20:08:05 +00:00
|
|
|
this._banner && !this._banner.expanded)
|
|
|
|
this._banner.hide();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-05-27 09:39:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_ackMessages() {
|
2011-05-27 09:39:50 +00:00
|
|
|
// Don't clear our messages here, tp-glib will send a
|
|
|
|
// 'pending-message-removed' for each one.
|
2012-11-26 21:30:20 +00:00
|
|
|
this._channel.ack_all_pending_messages_async(null);
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
2019-05-13 21:32:31 +00:00
|
|
|
}) : null;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2020-03-20 19:06:55 +00:00
|
|
|
const ChatNotificationMessage = HAVE_TP ? GObject.registerClass(
|
|
|
|
class ChatNotificationMessage extends GObject.Object {
|
|
|
|
_init(props = {}) {
|
|
|
|
super._init();
|
|
|
|
this.set(props);
|
|
|
|
}
|
|
|
|
}) : null;
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const ChatNotification = HAVE_TP ? GObject.registerClass({
|
2019-05-13 21:32:31 +00:00
|
|
|
Signals: {
|
2023-08-06 22:40:20 +00:00
|
|
|
'message-removed': {param_types: [ChatNotificationMessage.$gtype]},
|
|
|
|
'message-added': {param_types: [ChatNotificationMessage.$gtype]},
|
|
|
|
'timestamp-changed': {param_types: [ChatNotificationMessage.$gtype]},
|
2019-08-20 21:43:54 +00:00
|
|
|
},
|
2019-05-13 21:32:31 +00:00
|
|
|
}, class ChatNotification extends MessageTray.Notification {
|
|
|
|
_init(source) {
|
|
|
|
super._init(source, source.title, null,
|
2023-08-06 22:40:20 +00:00
|
|
|
{secondaryGIcon: source.getSecondaryIcon()});
|
2015-02-17 03:19:07 +00:00
|
|
|
this.setUrgency(MessageTray.Urgency.HIGH);
|
2010-12-22 07:41:11 +00:00
|
|
|
this.setResident(true);
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2015-02-17 03:19:07 +00:00
|
|
|
this.messages = [];
|
2010-11-25 14:29:41 +00:00
|
|
|
this._timestampTimeoutId = 0;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
destroy(reason) {
|
2015-04-09 16:44:50 +00:00
|
|
|
if (this._timestampTimeoutId)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._timestampTimeoutId);
|
2015-04-09 16:44:50 +00:00
|
|
|
this._timestampTimeoutId = 0;
|
2017-10-31 01:19:44 +00:00
|
|
|
super.destroy(reason);
|
|
|
|
}
|
2015-04-09 16:44:50 +00:00
|
|
|
|
2011-03-07 22:10:53 +00:00
|
|
|
/**
|
2023-08-07 01:15:51 +00:00
|
|
|
* @param {object} message - An object with the properties
|
|
|
|
* {string} message.text - the body of the message,
|
|
|
|
* {Tp.ChannelTextMessageType} message.messageType - the type
|
|
|
|
* {string} message.sender - the name of the sender,
|
|
|
|
* {number} message.timestamp - the time the message was sent
|
|
|
|
* {NotificationDirection} message.direction - a #NotificationDirection
|
2019-09-12 15:26:08 +00:00
|
|
|
*
|
2023-08-07 01:15:51 +00:00
|
|
|
* @param {bool} noTimestamp - Whether to add a timestamp. If %true,
|
2019-10-17 16:41:52 +00:00
|
|
|
* no timestamp will be added, regardless of the difference since
|
|
|
|
* the last timestamp
|
2011-03-07 22:10:53 +00:00
|
|
|
*/
|
2017-10-31 00:03:21 +00:00
|
|
|
appendMessage(message, noTimestamp) {
|
2011-03-07 22:10:53 +00:00
|
|
|
let messageBody = GLib.markup_escape_text(message.text, -1);
|
2011-08-24 14:33:44 +00:00
|
|
|
let styles = [message.direction];
|
2011-03-07 22:10:53 +00:00
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (message.messageType === Tp.ChannelTextMessageType.ACTION) {
|
2011-03-07 22:10:53 +00:00
|
|
|
let senderAlias = GLib.markup_escape_text(message.sender, -1);
|
2022-02-07 14:14:06 +00:00
|
|
|
messageBody = `<i>${senderAlias}</i> ${messageBody}`;
|
2011-02-26 14:38:25 +00:00
|
|
|
styles.push('chat-action');
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (message.direction === NotificationDirection.RECEIVED) {
|
2020-03-29 21:51:13 +00:00
|
|
|
this.update(this.source.title, messageBody, {
|
|
|
|
datetime: GLib.DateTime.new_from_unix_local(message.timestamp),
|
|
|
|
bannerMarkup: true,
|
|
|
|
});
|
2019-08-20 00:51:42 +00:00
|
|
|
}
|
2011-05-15 09:07:23 +00:00
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
let group = message.direction === NotificationDirection.RECEIVED
|
2019-08-19 19:38:51 +00:00
|
|
|
? 'received' : 'sent';
|
2011-08-24 14:33:44 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._append({
|
|
|
|
body: messageBody,
|
|
|
|
group,
|
|
|
|
styles,
|
|
|
|
timestamp: message.timestamp,
|
|
|
|
noTimestamp,
|
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_filterMessages() {
|
2015-02-17 03:19:07 +00:00
|
|
|
if (this.messages.length < 1)
|
2011-05-25 17:18:56 +00:00
|
|
|
return;
|
|
|
|
|
2015-02-17 03:19:07 +00:00
|
|
|
let lastMessageTime = this.messages[0].timestamp;
|
2019-08-19 19:38:51 +00:00
|
|
|
let currentTime = Date.now() / 1000;
|
2011-05-25 17:18:56 +00:00
|
|
|
|
|
|
|
// Keep the scrollback from growing too long. If the most
|
|
|
|
// recent message (before the one we just added) is within
|
|
|
|
// SCROLLBACK_RECENT_TIME, we will keep
|
|
|
|
// SCROLLBACK_RECENT_LENGTH previous messages. Otherwise
|
|
|
|
// we'll keep SCROLLBACK_IDLE_LENGTH messages.
|
|
|
|
|
2019-08-19 19:38:51 +00:00
|
|
|
let maxLength = lastMessageTime < currentTime - SCROLLBACK_RECENT_TIME
|
2019-08-19 19:33:15 +00:00
|
|
|
? SCROLLBACK_IDLE_LENGTH : SCROLLBACK_RECENT_LENGTH;
|
2011-05-25 17:18:56 +00:00
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
let filteredHistory = this.messages.filter(item => item.realMessage);
|
2011-05-25 17:18:56 +00:00
|
|
|
if (filteredHistory.length > maxLength) {
|
|
|
|
let lastMessageToKeep = filteredHistory[maxLength];
|
2015-02-17 03:19:07 +00:00
|
|
|
let expired = this.messages.splice(this.messages.indexOf(lastMessageToKeep));
|
2011-05-25 17:18:56 +00:00
|
|
|
for (let i = 0; i < expired.length; i++)
|
2015-02-17 03:19:07 +00:00
|
|
|
this.emit('message-removed', expired[i]);
|
2011-05-25 17:18:56 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-05-25 17:18:56 +00:00
|
|
|
|
2011-08-24 14:33:44 +00:00
|
|
|
/**
|
2023-08-07 01:15:51 +00:00
|
|
|
* @param {object} props - An object with the properties:
|
|
|
|
* {string} props.body - The text of the message.
|
|
|
|
* {string} props.group - The group of the message, one of:
|
2011-08-24 14:33:44 +00:00
|
|
|
* 'received', 'sent', 'meta'.
|
2023-08-07 01:15:51 +00:00
|
|
|
* {string[]} props.styles - Style class names for the message to have.
|
|
|
|
* {number} props.timestamp - The timestamp of the message.
|
|
|
|
* {bool} props.noTimestamp - suppress timestamp signal?
|
2011-08-24 14:33:44 +00:00
|
|
|
*/
|
2017-10-31 00:03:21 +00:00
|
|
|
_append(props) {
|
2019-08-19 19:38:51 +00:00
|
|
|
let currentTime = Date.now() / 1000;
|
2020-03-29 21:51:13 +00:00
|
|
|
props = Params.parse(props, {
|
|
|
|
body: null,
|
|
|
|
group: null,
|
|
|
|
styles: [],
|
|
|
|
timestamp: currentTime,
|
|
|
|
noTimestamp: false,
|
|
|
|
});
|
2023-08-06 22:40:20 +00:00
|
|
|
const {noTimestamp} = props;
|
2020-03-20 19:06:55 +00:00
|
|
|
delete props.noTimestamp;
|
2010-11-25 14:29:41 +00:00
|
|
|
|
|
|
|
// Reset the old message timeout
|
|
|
|
if (this._timestampTimeoutId)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._timestampTimeoutId);
|
2015-04-09 16:44:50 +00:00
|
|
|
this._timestampTimeoutId = 0;
|
2010-11-25 14:29:41 +00:00
|
|
|
|
2020-03-20 19:06:55 +00:00
|
|
|
let message = new ChatNotificationMessage({
|
|
|
|
realMessage: props.group !== 'meta',
|
|
|
|
showTimestamp: false,
|
|
|
|
...props,
|
|
|
|
});
|
2010-11-25 14:29:41 +00:00
|
|
|
|
2015-02-17 03:19:07 +00:00
|
|
|
this.messages.unshift(message);
|
|
|
|
this.emit('message-added', message);
|
2011-08-24 14:33:44 +00:00
|
|
|
|
2020-03-20 19:06:55 +00:00
|
|
|
if (!noTimestamp) {
|
2015-02-17 03:19:07 +00:00
|
|
|
let timestamp = props.timestamp;
|
2014-04-10 17:26:52 +00:00
|
|
|
if (timestamp < currentTime - SCROLLBACK_IMMEDIATE_TIME) {
|
2011-03-07 22:10:53 +00:00
|
|
|
this.appendTimestamp();
|
2014-04-10 17:26:52 +00:00
|
|
|
} else {
|
2011-03-07 22:10:53 +00:00
|
|
|
// Schedule a new timestamp in SCROLLBACK_IMMEDIATE_TIME
|
|
|
|
// from the timestamp of the message.
|
2019-08-19 18:50:33 +00:00
|
|
|
this._timestampTimeoutId = GLib.timeout_add_seconds(
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
2011-03-07 22:10:53 +00:00
|
|
|
SCROLLBACK_IMMEDIATE_TIME - (currentTime - timestamp),
|
2017-12-02 00:27:35 +00:00
|
|
|
this.appendTimestamp.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._timestampTimeoutId, '[gnome-shell] this.appendTimestamp');
|
|
|
|
}
|
2011-03-07 22:10:53 +00:00
|
|
|
}
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2011-05-25 17:18:56 +00:00
|
|
|
this._filterMessages();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-02-22 19:23:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
appendTimestamp() {
|
2013-11-08 03:48:51 +00:00
|
|
|
this._timestampTimeoutId = 0;
|
|
|
|
|
2015-02-17 03:19:07 +00:00
|
|
|
this.messages[0].showTimestamp = true;
|
|
|
|
this.emit('timestamp-changed', this.messages[0]);
|
2011-05-25 17:18:56 +00:00
|
|
|
|
|
|
|
this._filterMessages();
|
|
|
|
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-11-25 14:29:41 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
appendAliasChange(oldAlias, newAlias) {
|
2011-03-15 13:45:53 +00:00
|
|
|
oldAlias = GLib.markup_escape_text(oldAlias, -1);
|
|
|
|
newAlias = GLib.markup_escape_text(newAlias, -1);
|
|
|
|
|
|
|
|
/* Translators: this is the other person changing their old IM name to their new
|
|
|
|
IM name. */
|
2020-03-24 04:02:20 +00:00
|
|
|
const message = `<i>${
|
|
|
|
_('%s is now known as %s').format(oldAlias, newAlias)}</i>`;
|
2011-08-24 14:33:44 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._append({
|
|
|
|
body: message,
|
|
|
|
group: 'meta',
|
|
|
|
styles: ['chat-meta-message'],
|
|
|
|
});
|
2011-05-25 17:18:56 +00:00
|
|
|
|
|
|
|
this._filterMessages();
|
2015-02-17 03:19:07 +00:00
|
|
|
}
|
2019-05-13 21:32:31 +00:00
|
|
|
}) : null;
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const ChatLineBox = GObject.registerClass(
|
2017-10-31 01:23:39 +00:00
|
|
|
class ChatLineBox extends St.BoxLayout {
|
2017-10-31 00:03:21 +00:00
|
|
|
vfunc_get_preferred_height(forWidth) {
|
2017-10-31 01:23:39 +00:00
|
|
|
let [, natHeight] = super.vfunc_get_preferred_height(forWidth);
|
2015-02-17 03:19:07 +00:00
|
|
|
return [natHeight, natHeight];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const ChatNotificationBanner = GObject.registerClass(
|
2019-07-16 09:24:13 +00:00
|
|
|
class ChatNotificationBanner extends MessageTray.NotificationBanner {
|
|
|
|
_init(notification) {
|
|
|
|
super._init(notification);
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._responseEntry = new St.Entry({
|
|
|
|
style_class: 'chat-response',
|
|
|
|
x_expand: true,
|
|
|
|
can_focus: true,
|
|
|
|
});
|
2017-12-02 00:27:35 +00:00
|
|
|
this._responseEntry.clutter_text.connect('activate', this._onEntryActivated.bind(this));
|
|
|
|
this._responseEntry.clutter_text.connect('text-changed', this._onEntryChanged.bind(this));
|
2015-02-17 03:19:07 +00:00
|
|
|
this.setActionArea(this._responseEntry);
|
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
this._responseEntry.clutter_text.connect('key-focus-in', () => {
|
2015-02-17 03:19:07 +00:00
|
|
|
this.focused = true;
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
|
|
|
this._responseEntry.clutter_text.connect('key-focus-out', () => {
|
2015-02-17 03:19:07 +00:00
|
|
|
this.focused = false;
|
|
|
|
this.emit('unfocused');
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._scrollArea = new St.ScrollView({
|
|
|
|
style_class: 'chat-scrollview vfade',
|
|
|
|
vscrollbar_policy: St.PolicyType.AUTOMATIC,
|
|
|
|
hscrollbar_policy: St.PolicyType.NEVER,
|
|
|
|
visible: this.expanded,
|
|
|
|
});
|
|
|
|
this._contentArea = new St.BoxLayout({
|
|
|
|
style_class: 'chat-body',
|
|
|
|
vertical: true,
|
|
|
|
});
|
2015-02-17 03:19:07 +00:00
|
|
|
this._scrollArea.add_actor(this._contentArea);
|
|
|
|
|
|
|
|
this.setExpandedBody(this._scrollArea);
|
|
|
|
this.setExpandedLines(CHAT_EXPAND_LINES);
|
|
|
|
|
|
|
|
this._lastGroup = null;
|
|
|
|
|
|
|
|
// Keep track of the bottom position for the current adjustment and
|
|
|
|
// force a scroll to the bottom if things change while we were at the
|
|
|
|
// bottom
|
|
|
|
this._oldMaxScrollValue = this._scrollArea.vscroll.adjustment.value;
|
2017-10-31 00:38:18 +00:00
|
|
|
this._scrollArea.vscroll.adjustment.connect('changed', adjustment => {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (adjustment.value === this._oldMaxScrollValue)
|
2015-02-17 03:19:07 +00:00
|
|
|
this.scrollTo(St.Side.BOTTOM);
|
|
|
|
this._oldMaxScrollValue = Math.max(adjustment.lower, adjustment.upper - adjustment.page_size);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2023-08-06 22:40:20 +00:00
|
|
|
this._inputHistory = new History.HistoryManager({entry: this._responseEntry.clutter_text});
|
2015-02-17 03:19:07 +00:00
|
|
|
|
|
|
|
this._composingTimeoutId = 0;
|
|
|
|
|
|
|
|
this._messageActors = new Map();
|
|
|
|
|
2021-08-15 22:36:59 +00:00
|
|
|
this.notification.connectObject(
|
|
|
|
'timestamp-changed', (n, message) => this._updateTimestamp(message),
|
|
|
|
'message-added', (n, message) => this._addMessage(message),
|
|
|
|
'message-removed', (n, message) => {
|
2015-02-17 03:19:07 +00:00
|
|
|
let actor = this._messageActors.get(message);
|
|
|
|
if (this._messageActors.delete(message))
|
|
|
|
actor.destroy();
|
2021-08-15 22:36:59 +00:00
|
|
|
}, this);
|
2015-02-17 03:19:07 +00:00
|
|
|
|
|
|
|
for (let i = this.notification.messages.length - 1; i >= 0; i--)
|
|
|
|
this._addMessage(this.notification.messages[i]);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
scrollTo(side) {
|
2015-02-17 03:19:07 +00:00
|
|
|
let adjustment = this._scrollArea.vscroll.adjustment;
|
2023-08-07 00:51:19 +00:00
|
|
|
if (side === St.Side.TOP)
|
2015-02-17 03:19:07 +00:00
|
|
|
adjustment.value = adjustment.lower;
|
2023-08-07 00:51:19 +00:00
|
|
|
else if (side === St.Side.BOTTOM)
|
2015-02-17 03:19:07 +00:00
|
|
|
adjustment.value = adjustment.upper;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
hide() {
|
2015-03-17 20:08:05 +00:00
|
|
|
this.emit('done-displaying');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-03-17 20:08:05 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_addMessage(message) {
|
2019-07-16 09:24:13 +00:00
|
|
|
let body = new MessageList.URLHighlighter(message.body, true, true);
|
2015-02-17 03:19:07 +00:00
|
|
|
|
|
|
|
let styles = message.styles;
|
|
|
|
for (let i = 0; i < styles.length; i++)
|
|
|
|
body.add_style_class_name(styles[i]);
|
|
|
|
|
|
|
|
let group = message.group;
|
2023-08-07 00:51:19 +00:00
|
|
|
if (group !== this._lastGroup) {
|
2015-02-17 03:19:07 +00:00
|
|
|
this._lastGroup = group;
|
|
|
|
body.add_style_class_name('chat-new-group');
|
|
|
|
}
|
|
|
|
|
|
|
|
let lineBox = new ChatLineBox();
|
|
|
|
lineBox.add(body);
|
|
|
|
this._contentArea.add_actor(lineBox);
|
|
|
|
this._messageActors.set(message, lineBox);
|
|
|
|
|
|
|
|
this._updateTimestamp(message);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-02-17 03:19:07 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateTimestamp(message) {
|
2015-02-17 03:19:07 +00:00
|
|
|
let actor = this._messageActors.get(message);
|
|
|
|
if (!actor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (actor.get_n_children() > 1)
|
|
|
|
actor.get_child_at_index(1).destroy();
|
|
|
|
|
|
|
|
if (message.showTimestamp) {
|
|
|
|
let lastMessageTime = message.timestamp;
|
|
|
|
let lastMessageDate = new Date(lastMessageTime * 1000);
|
|
|
|
|
|
|
|
let timeLabel = Util.createTimeLabel(lastMessageDate);
|
|
|
|
timeLabel.style_class = 'chat-meta-message';
|
|
|
|
timeLabel.x_expand = timeLabel.y_expand = true;
|
|
|
|
timeLabel.x_align = timeLabel.y_align = Clutter.ActorAlign.END;
|
|
|
|
|
|
|
|
actor.add_actor(timeLabel);
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-28 18:15:02 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onEntryActivated() {
|
2010-02-22 19:23:36 +00:00
|
|
|
let text = this._responseEntry.get_text();
|
2023-08-07 00:51:19 +00:00
|
|
|
if (text === '')
|
2010-02-22 19:23:36 +00:00
|
|
|
return;
|
|
|
|
|
2011-02-20 08:41:51 +00:00
|
|
|
this._inputHistory.addItem(text);
|
|
|
|
|
2010-11-28 15:14:34 +00:00
|
|
|
// Telepathy sends out the Sent signal for us.
|
|
|
|
// see Source._messageSent
|
2010-02-22 19:23:36 +00:00
|
|
|
this._responseEntry.set_text('');
|
2015-02-17 03:19:07 +00:00
|
|
|
this.notification.source.respond(text);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-07 12:42:07 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_composingStopTimeout() {
|
2011-07-07 12:42:07 +00:00
|
|
|
this._composingTimeoutId = 0;
|
|
|
|
|
2015-02-17 03:19:07 +00:00
|
|
|
this.notification.source.setChatState(Tp.ChannelChatState.PAUSED);
|
2011-07-07 12:42:07 +00:00
|
|
|
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-07 12:42:07 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onEntryChanged() {
|
2011-07-07 12:42:07 +00:00
|
|
|
let text = this._responseEntry.get_text();
|
|
|
|
|
|
|
|
// If we're typing, we want to send COMPOSING.
|
|
|
|
// If we empty the entry, we want to send ACTIVE.
|
|
|
|
// If we've stopped typing for COMPOSING_STOP_TIMEOUT
|
|
|
|
// seconds, we want to send PAUSED.
|
|
|
|
|
|
|
|
// Remove composing timeout.
|
|
|
|
if (this._composingTimeoutId > 0) {
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._composingTimeoutId);
|
2011-07-07 12:42:07 +00:00
|
|
|
this._composingTimeoutId = 0;
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (text !== '') {
|
2015-02-17 03:19:07 +00:00
|
|
|
this.notification.source.setChatState(Tp.ChannelChatState.COMPOSING);
|
2011-07-07 12:42:07 +00:00
|
|
|
|
2019-08-19 18:50:33 +00:00
|
|
|
this._composingTimeoutId = GLib.timeout_add_seconds(
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
2011-07-07 12:42:07 +00:00
|
|
|
COMPOSING_STOP_TIMEOUT,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._composingStopTimeout.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._composingTimeoutId, '[gnome-shell] this._composingStopTimeout');
|
2011-07-07 12:42:07 +00:00
|
|
|
} else {
|
2015-02-17 03:19:07 +00:00
|
|
|
this.notification.source.setChatState(Tp.ChannelChatState.ACTIVE);
|
2011-07-07 12:42:07 +00:00
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|
2011-06-30 12:05:41 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export const Component = TelepathyComponent;
|