2018-01-12 20:03:09 +00:00
|
|
|
using Xmpp;
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
namespace Dino.Entities {
|
2017-03-22 16:15:06 +00:00
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
public class Conversation : Object {
|
|
|
|
|
|
|
|
public signal void object_updated(Conversation conversation);
|
|
|
|
|
2017-03-09 14:34:32 +00:00
|
|
|
public enum Type {
|
|
|
|
CHAT,
|
2017-03-30 23:17:01 +00:00
|
|
|
GROUPCHAT,
|
2020-01-09 13:28:08 +00:00
|
|
|
GROUPCHAT_PM;
|
|
|
|
|
|
|
|
public bool is_muc_semantic() {
|
|
|
|
return this == GROUPCHAT || this == GROUPCHAT_PM;
|
|
|
|
}
|
2017-03-09 14:34:32 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public int id { get; set; }
|
2018-01-12 20:03:09 +00:00
|
|
|
public Type type_ { get; set; }
|
2017-03-02 14:37:32 +00:00
|
|
|
public Account account { get; private set; }
|
|
|
|
public Jid counterpart { get; private set; }
|
2018-01-26 19:35:29 +00:00
|
|
|
public string? nickname { get; set; }
|
2017-03-22 16:15:06 +00:00
|
|
|
public bool active { get; set; default = false; }
|
2017-03-24 21:57:05 +00:00
|
|
|
private DateTime? _last_active;
|
|
|
|
public DateTime? last_active {
|
|
|
|
get { return _last_active; }
|
|
|
|
set {
|
|
|
|
if (_last_active == null ||
|
|
|
|
(value != null && value.difference(_last_active) > 0)) {
|
|
|
|
_last_active = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 16:15:06 +00:00
|
|
|
public Encryption encryption { get; set; default = Encryption.NONE; }
|
2017-06-16 12:29:02 +00:00
|
|
|
public Message? read_up_to { get; set; }
|
2020-04-29 19:31:23 +00:00
|
|
|
public int read_up_to_item { get; set; default=-1; }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-05-30 20:31:05 +00:00
|
|
|
public enum NotifySetting { DEFAULT, ON, OFF, HIGHLIGHT }
|
|
|
|
public NotifySetting notify_setting { get; set; default = NotifySetting.DEFAULT; }
|
|
|
|
|
|
|
|
public enum Setting { DEFAULT, ON, OFF }
|
|
|
|
public Setting send_typing { get; set; default = Setting.DEFAULT; }
|
|
|
|
|
|
|
|
public Setting send_marker { get; set; default = Setting.DEFAULT; }
|
|
|
|
|
2017-03-22 16:15:06 +00:00
|
|
|
private Database? db;
|
|
|
|
|
|
|
|
public Conversation(Jid jid, Account account, Type type) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.account = account;
|
2017-03-30 23:17:01 +00:00
|
|
|
this.counterpart = jid;
|
2017-03-22 16:15:06 +00:00
|
|
|
this.type_ = type;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 03:10:53 +00:00
|
|
|
public Conversation.from_row(Database db, Qlite.Row row) throws InvalidJidError {
|
2017-03-22 16:15:06 +00:00
|
|
|
this.db = db;
|
|
|
|
|
|
|
|
id = row[db.conversation.id];
|
2018-01-12 20:03:09 +00:00
|
|
|
type_ = (Conversation.Type) row[db.conversation.type_];
|
2017-03-22 16:15:06 +00:00
|
|
|
account = db.get_account_by_id(row[db.conversation.account_id]);
|
2017-03-30 23:17:01 +00:00
|
|
|
string? resource = row[db.conversation.resource];
|
2019-07-14 11:22:13 +00:00
|
|
|
counterpart = db.get_jid_by_id(row[db.conversation.jid_id]);
|
2018-01-12 20:03:09 +00:00
|
|
|
if (type_ == Conversation.Type.GROUPCHAT_PM) counterpart = counterpart.with_resource(resource);
|
2018-01-12 20:13:46 +00:00
|
|
|
nickname = type_ == Conversation.Type.GROUPCHAT ? resource : null;
|
2017-03-22 16:15:06 +00:00
|
|
|
active = row[db.conversation.active];
|
|
|
|
int64? last_active = row[db.conversation.last_active];
|
2017-08-31 16:40:58 +00:00
|
|
|
if (last_active != null) this.last_active = new DateTime.from_unix_utc(last_active);
|
2017-03-22 16:15:06 +00:00
|
|
|
encryption = (Encryption) row[db.conversation.encryption];
|
|
|
|
int? read_up_to = row[db.conversation.read_up_to];
|
|
|
|
if (read_up_to != null) this.read_up_to = db.get_message_by_id(read_up_to);
|
2020-04-29 19:31:23 +00:00
|
|
|
read_up_to_item = row[db.conversation.read_up_to_item];
|
2017-05-30 20:31:05 +00:00
|
|
|
notify_setting = (NotifySetting) row[db.conversation.notification];
|
|
|
|
send_typing = (Setting) row[db.conversation.send_typing];
|
|
|
|
send_marker = (Setting) row[db.conversation.send_marker];
|
2017-03-22 16:15:06 +00:00
|
|
|
|
|
|
|
notify.connect(on_update);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void persist(Database db) {
|
|
|
|
this.db = db;
|
|
|
|
var insert = db.conversation.insert()
|
|
|
|
.value(db.conversation.account_id, account.id)
|
2017-03-30 23:17:01 +00:00
|
|
|
.value(db.conversation.jid_id, db.get_jid_id(counterpart))
|
2017-03-22 16:15:06 +00:00
|
|
|
.value(db.conversation.type_, type_)
|
|
|
|
.value(db.conversation.encryption, encryption)
|
2017-08-09 18:44:15 +00:00
|
|
|
.value(db.conversation.active, active)
|
|
|
|
.value(db.conversation.notification, notify_setting)
|
|
|
|
.value(db.conversation.send_typing, send_typing)
|
|
|
|
.value(db.conversation.send_marker, send_marker);
|
|
|
|
if (read_up_to != null) {
|
|
|
|
insert.value(db.conversation.read_up_to, read_up_to.id);
|
|
|
|
}
|
2020-04-29 19:31:23 +00:00
|
|
|
if (read_up_to_item != -1) {
|
|
|
|
insert.value(db.conversation.read_up_to_item, read_up_to_item);
|
|
|
|
}
|
2018-01-26 19:35:29 +00:00
|
|
|
if (nickname != null) {
|
|
|
|
insert.value(db.conversation.resource, nickname);
|
|
|
|
}
|
2017-03-30 23:17:01 +00:00
|
|
|
if (counterpart.is_full()) {
|
|
|
|
insert.value(db.conversation.resource, counterpart.resourcepart);
|
|
|
|
}
|
2017-03-22 16:15:06 +00:00
|
|
|
if (last_active != null) {
|
|
|
|
insert.value(db.conversation.last_active, (long) last_active.to_unix());
|
|
|
|
}
|
|
|
|
id = (int) insert.perform();
|
|
|
|
notify.connect(on_update);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 20:31:05 +00:00
|
|
|
public NotifySetting get_notification_setting(StreamInteractor stream_interactor) {
|
2017-08-24 15:24:22 +00:00
|
|
|
return notify_setting != NotifySetting.DEFAULT ? notify_setting : get_notification_default_setting(stream_interactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public NotifySetting get_notification_default_setting(StreamInteractor stream_interactor) {
|
2017-08-21 15:16:25 +00:00
|
|
|
if (!Application.get_default().settings.notifications) return NotifySetting.OFF;
|
2019-05-21 19:42:39 +00:00
|
|
|
|
2017-05-30 20:31:05 +00:00
|
|
|
if (type_ == Type.GROUPCHAT) {
|
2019-05-21 19:42:39 +00:00
|
|
|
if (stream_interactor.get_module(MucManager.IDENTITY).is_private_room(this.account, this.counterpart)) {
|
|
|
|
return NotifySetting.ON;
|
2019-02-13 15:13:37 +00:00
|
|
|
} else {
|
2019-05-21 19:42:39 +00:00
|
|
|
return NotifySetting.HIGHLIGHT;
|
2018-09-16 11:54:47 +00:00
|
|
|
}
|
2017-05-30 20:31:05 +00:00
|
|
|
}
|
2017-08-24 15:24:22 +00:00
|
|
|
return NotifySetting.ON;
|
2017-05-30 20:31:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 19:42:39 +00:00
|
|
|
public Setting get_send_typing_setting(StreamInteractor stream_interactor) {
|
2017-05-30 20:31:05 +00:00
|
|
|
if (send_typing != Setting.DEFAULT) return send_typing;
|
2019-05-21 19:42:39 +00:00
|
|
|
|
|
|
|
if (stream_interactor.get_module(MucManager.IDENTITY).is_public_room(this.account, this.counterpart)) return Setting.OFF;
|
|
|
|
|
2017-08-21 15:16:25 +00:00
|
|
|
return Application.get_default().settings.send_typing ? Setting.ON : Setting.OFF;
|
2017-05-30 20:31:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 19:42:39 +00:00
|
|
|
public Setting get_send_marker_setting(StreamInteractor stream_interactor) {
|
2017-05-30 20:31:05 +00:00
|
|
|
if (send_marker != Setting.DEFAULT) return send_marker;
|
2019-05-21 19:42:39 +00:00
|
|
|
|
|
|
|
if (stream_interactor.get_module(MucManager.IDENTITY).is_public_room(this.account, this.counterpart)) return Setting.OFF;
|
|
|
|
|
2017-08-21 15:16:25 +00:00
|
|
|
return Application.get_default().settings.send_marker ? Setting.ON : Setting.OFF;
|
2017-05-30 20:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
public bool equals(Conversation? conversation) {
|
|
|
|
if (conversation == null) return false;
|
|
|
|
return equals_func(this, conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool equals_func(Conversation conversation1, Conversation conversation2) {
|
2019-05-29 14:53:34 +00:00
|
|
|
return conversation1.counterpart.equals(conversation2.counterpart) && conversation1.account.equals(conversation2.account) && conversation1.type_ == conversation2.type_;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static uint hash_func(Conversation conversation) {
|
|
|
|
return conversation.counterpart.to_string().hash() ^ conversation.account.bare_jid.to_string().hash();
|
|
|
|
}
|
2017-03-22 16:15:06 +00:00
|
|
|
|
|
|
|
private void on_update(Object o, ParamSpec sp) {
|
2017-04-23 08:23:11 +00:00
|
|
|
var update = db.conversation.update().with(db.conversation.id, "=", id);
|
2017-03-24 09:40:48 +00:00
|
|
|
switch (sp.name) {
|
2017-03-24 21:57:05 +00:00
|
|
|
case "type-":
|
2017-03-24 09:40:48 +00:00
|
|
|
update.set(db.conversation.type_, type_); break;
|
|
|
|
case "encryption":
|
|
|
|
update.set(db.conversation.encryption, encryption); break;
|
2017-03-24 21:57:05 +00:00
|
|
|
case "read-up-to":
|
2017-03-30 23:17:01 +00:00
|
|
|
if (read_up_to != null) {
|
|
|
|
update.set(db.conversation.read_up_to, read_up_to.id);
|
|
|
|
} else {
|
|
|
|
update.set_null(db.conversation.read_up_to);
|
|
|
|
}
|
|
|
|
break;
|
2020-04-29 19:31:23 +00:00
|
|
|
case "read-up-to-item":
|
|
|
|
if (read_up_to_item != -1) {
|
|
|
|
update.set(db.conversation.read_up_to_item, read_up_to_item);
|
|
|
|
} else {
|
|
|
|
update.set_null(db.conversation.read_up_to_item);
|
|
|
|
}
|
|
|
|
break;
|
2018-01-26 19:35:29 +00:00
|
|
|
case "nickname":
|
|
|
|
update.set(db.conversation.resource, nickname); break;
|
2017-03-24 09:40:48 +00:00
|
|
|
case "active":
|
|
|
|
update.set(db.conversation.active, active); break;
|
2017-03-24 21:57:05 +00:00
|
|
|
case "last-active":
|
2017-03-24 09:40:48 +00:00
|
|
|
if (last_active != null) {
|
|
|
|
update.set(db.conversation.last_active, (long) last_active.to_unix());
|
|
|
|
} else {
|
|
|
|
update.set_null(db.conversation.last_active);
|
|
|
|
}
|
|
|
|
break;
|
2017-05-30 20:31:05 +00:00
|
|
|
case "notify-setting":
|
|
|
|
update.set(db.conversation.notification, notify_setting); break;
|
|
|
|
case "send-typing":
|
|
|
|
update.set(db.conversation.send_typing, send_typing); break;
|
|
|
|
case "send-marker":
|
|
|
|
update.set(db.conversation.send_marker, send_marker); break;
|
2017-03-22 16:15:06 +00:00
|
|
|
}
|
|
|
|
update.perform();
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-22 16:15:06 +00:00
|
|
|
|
2017-06-16 12:29:02 +00:00
|
|
|
}
|