telepathyClient: Use proper Object to wrap different tpl messages

In telepathyClient we consider messages both Tpl.TextEvents and
Tpl.Messages, and we manually create JS objects to copy the properties we
care for each one. This may lead to objects not matching the interface we
want.

Instead, use an object with construct-only properties and two factory static
methods to initialize it.

Unfortunately we need to use the ChatMessageClass for the class name or
calling the static methods would trigger a gjs error as per [1].

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1113

[1] https://gitlab.gnome.org/GNOME/gjs/-/issues/310
This commit is contained in:
Marco Trevisan (Treviño) 2020-03-20 20:31:45 +01:00 committed by Florian Müllner
parent 9a26b970f9
commit 03a46be5c7

View File

@ -39,35 +39,58 @@ var NotificationDirection = {
RECEIVED: 'chat-received', RECEIVED: 'chat-received',
}; };
function makeMessageFromTpMessage(tpMessage, direction) { const ChatMessage = HAVE_TP ? GObject.registerClass({
let [text, flags_] = tpMessage.to_text(); Properties: {
'message-type': GObject.ParamSpec.int(
let timestamp = tpMessage.get_sent_timestamp(); 'message-type', 'message-type', 'message-type',
if (timestamp == 0) GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
timestamp = tpMessage.get_received_timestamp(); Math.min(...Object.values(Tp.ChannelTextMessageType)),
Math.max(...Object.values(Tp.ChannelTextMessageType)),
return { Tp.ChannelTextMessageType.NORMAL),
messageType: tpMessage.get_message_type(), 'text': GObject.ParamSpec.string(
text, 'text', 'text', 'text',
sender: tpMessage.sender.alias, GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
timestamp, 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, direction,
}; });
} }
static newFromTplTextEvent(tplTextEvent) {
let direction =
tplTextEvent.get_sender().get_entity_type() === Tpl.EntityType.SELF
? NotificationDirection.SENT : NotificationDirection.RECEIVED;
function makeMessageFromTplEvent(event) { return new ChatMessage({
let sent = event.get_sender().get_entity_type() == Tpl.EntityType.SELF; 'message-type': tplTextEvent.get_message_type(),
let direction = sent ? NotificationDirection.SENT : NotificationDirection.RECEIVED; 'text': tplTextEvent.get_message(),
'sender': tplTextEvent.get_sender().get_alias(),
return { 'timestamp': tplTextEvent.get_timestamp(),
messageType: event.get_message_type(),
text: event.get_message(),
sender: event.get_sender().get_alias(),
timestamp: event.get_timestamp(),
direction, direction,
}; });
} }
}) : null;
var TelepathyComponent = class { var TelepathyComponent = class {
constructor() { constructor() {
@ -430,7 +453,7 @@ class ChatSource extends MessageTray.Source {
_displayPendingMessages(logManager, result) { _displayPendingMessages(logManager, result) {
let [success_, events] = logManager.get_filtered_events_finish(result); let [success_, events] = logManager.get_filtered_events_finish(result);
let logMessages = events.map(makeMessageFromTplEvent); let logMessages = events.map(e => ChatMessage.newFromTplTextEvent(e));
this._ensureNotification(); this._ensureNotification();
let pendingTpMessages = this._channel.get_pending_messages(); let pendingTpMessages = this._channel.get_pending_messages();
@ -442,7 +465,8 @@ class ChatSource extends MessageTray.Source {
if (message.get_message_type() == Tp.ChannelTextMessageType.DELIVERY_REPORT) if (message.get_message_type() == Tp.ChannelTextMessageType.DELIVERY_REPORT)
continue; continue;
pendingMessages.push(makeMessageFromTpMessage(message, NotificationDirection.RECEIVED)); pendingMessages.push(ChatMessage.newFromTpMessage(message,
NotificationDirection.RECEIVED));
this._pendingMessages.push(message); this._pendingMessages.push(message);
} }
@ -540,7 +564,8 @@ class ChatSource extends MessageTray.Source {
this._pendingMessages.push(message); this._pendingMessages.push(message);
this.countUpdated(); this.countUpdated();
message = makeMessageFromTpMessage(message, NotificationDirection.RECEIVED); message = ChatMessage.newFromTpMessage(message,
NotificationDirection.RECEIVED);
this._notification.appendMessage(message); this._notification.appendMessage(message);
// Wait a bit before notifying for the received message, a handler // Wait a bit before notifying for the received message, a handler
@ -565,7 +590,8 @@ class ChatSource extends MessageTray.Source {
// our client and other clients as well. // our client and other clients as well.
_messageSent(channel, message, _flags, _token) { _messageSent(channel, message, _flags, _token) {
this._ensureNotification(); this._ensureNotification();
message = makeMessageFromTpMessage(message, NotificationDirection.SENT); message = ChatMessage.newFromTpMessage(message,
NotificationDirection.SENT);
this._notification.appendMessage(message); this._notification.appendMessage(message);
} }