-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathchat_history_item.rs
More file actions
162 lines (143 loc) · 4.69 KB
/
chat_history_item.rs
File metadata and controls
162 lines (143 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use gtk::glib;
use gtk::glib::DateTime;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use crate::tdlib::Message;
#[derive(Clone, Debug, glib::Boxed)]
#[boxed_type(name = "ContentChatHistoryItemType")]
pub(crate) enum ChatHistoryItemType {
Message(Message),
DayDivider(DateTime),
}
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, glib::Enum)]
#[enum_type(name = "MessageStyle")]
pub(crate) enum MessageStyle {
#[default]
Single,
First,
Last,
Center,
}
mod imp {
use super::*;
use once_cell::sync::{Lazy, OnceCell};
use std::cell::Cell;
#[derive(Debug, Default)]
pub(crate) struct ChatHistoryItem {
pub(super) type_: OnceCell<ChatHistoryItemType>,
pub(super) style: Cell<MessageStyle>,
}
#[glib::object_subclass]
impl ObjectSubclass for ChatHistoryItem {
const NAME: &'static str = "ContentChatHistoryItem";
type Type = super::ChatHistoryItem;
}
impl ObjectImpl for ChatHistoryItem {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![
glib::ParamSpecBoxed::builder::<ChatHistoryItemType>("type")
.write_only()
.construct_only()
.build(),
glib::ParamSpecEnum::builder::<MessageStyle>("style").build(),
]
});
PROPERTIES.as_ref()
}
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
"style" => self.style.get().into(),
_ => unimplemented!(),
}
}
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
match pspec.name() {
"type" => {
let type_ = value.get::<ChatHistoryItemType>().unwrap();
self.type_.set(type_).unwrap();
}
"style" => {
self.style.set(value.get().unwrap());
}
_ => unimplemented!(),
}
}
}
}
glib::wrapper! {
pub(crate) struct ChatHistoryItem(ObjectSubclass<imp::ChatHistoryItem>);
}
impl ChatHistoryItem {
pub(crate) fn for_message(message: Message) -> Self {
let type_ = ChatHistoryItemType::Message(message);
glib::Object::builder().property("type", type_).build()
}
pub(crate) fn for_day_divider(day: DateTime) -> Self {
let type_ = ChatHistoryItemType::DayDivider(day);
glib::Object::builder().property("type", type_).build()
}
pub(crate) fn type_(&self) -> &ChatHistoryItemType {
self.imp().type_.get().unwrap()
}
pub(crate) fn style(&self) -> MessageStyle {
self.imp().style.get()
}
pub(crate) fn set_style(&self, style: MessageStyle) {
self.imp().style.set(style);
}
pub(crate) fn is_groupable(&self) -> bool {
if let Some(message) = self.message() {
use tdlib::enums::MessageContent::*;
matches!(
message.content().0,
MessageText(_)
| MessageAnimation(_)
| MessageAudio(_)
| MessageDocument(_)
| MessagePhoto(_)
| MessageSticker(_)
| MessageVideo(_)
| MessageVideoNote(_)
| MessageVoiceNote(_)
| MessageLocation(_)
| MessageVenue(_)
| MessageContact(_)
| MessageAnimatedEmoji(_)
| MessageDice(_)
| MessageGame(_)
| MessagePoll(_)
| MessageInvoice(_)
| MessageCall(_)
| MessageUnsupported
)
} else {
false
}
}
pub(crate) fn group_key(&self) -> Option<(bool, i64)> {
if self.is_groupable() {
self.message().map(|m| (m.is_outgoing(), m.sender().id()))
} else {
None
}
}
pub(crate) fn message(&self) -> Option<&Message> {
if let ChatHistoryItemType::Message(message) = self.type_() {
Some(message)
} else {
None
}
}
pub(crate) fn message_timestamp(&self) -> Option<DateTime> {
if let ChatHistoryItemType::Message(message) = self.type_() {
Some(
glib::DateTime::from_unix_utc(message.date().into())
.and_then(|t| t.to_local())
.unwrap(),
)
} else {
None
}
}
}