1 #include "ChannelChatters.hpp"
2 
3 #include "messages/Message.hpp"
4 #include "messages/MessageBuilder.hpp"
5 
6 namespace chatterino {
7 
ChannelChatters(Channel & channel)8 ChannelChatters::ChannelChatters(Channel &channel)
9     : channel_(channel)
10     , chatterColors_(ChannelChatters::maxChatterColorCount)
11 {
12 }
13 
accessChatters() const14 SharedAccessGuard<const ChatterSet> ChannelChatters::accessChatters() const
15 {
16     return this->chatters_.accessConst();
17 }
18 
addRecentChatter(const QString & user)19 void ChannelChatters::addRecentChatter(const QString &user)
20 {
21     auto chatters = this->chatters_.access();
22     chatters->addRecentChatter(user);
23 }
24 
addJoinedUser(const QString & user)25 void ChannelChatters::addJoinedUser(const QString &user)
26 {
27     auto joinedUsers = this->joinedUsers_.access();
28     joinedUsers->append(user);
29 
30     if (!this->joinedUsersMergeQueued_)
31     {
32         this->joinedUsersMergeQueued_ = true;
33 
34         QTimer::singleShot(500, &this->lifetimeGuard_, [this] {
35             auto joinedUsers = this->joinedUsers_.access();
36 
37             MessageBuilder builder(systemMessage,
38                                    "Users joined: " + joinedUsers->join(", "));
39             builder->flags.set(MessageFlag::Collapsed);
40             joinedUsers->clear();
41             this->channel_.addMessage(builder.release());
42             this->joinedUsersMergeQueued_ = false;
43         });
44     }
45 }
46 
addPartedUser(const QString & user)47 void ChannelChatters::addPartedUser(const QString &user)
48 {
49     auto partedUsers = this->partedUsers_.access();
50     partedUsers->append(user);
51 
52     if (!this->partedUsersMergeQueued_)
53     {
54         this->partedUsersMergeQueued_ = true;
55 
56         QTimer::singleShot(500, &this->lifetimeGuard_, [this] {
57             auto partedUsers = this->partedUsers_.access();
58 
59             MessageBuilder builder(systemMessage,
60                                    "Users parted: " + partedUsers->join(", "));
61             builder->flags.set(MessageFlag::Collapsed);
62             this->channel_.addMessage(builder.release());
63             partedUsers->clear();
64 
65             this->partedUsersMergeQueued_ = false;
66         });
67     }
68 }
69 
updateOnlineChatters(const std::unordered_set<QString> & chatters)70 void ChannelChatters::updateOnlineChatters(
71     const std::unordered_set<QString> &chatters)
72 {
73     auto chatters_ = this->chatters_.access();
74     chatters_->updateOnlineChatters(chatters);
75 }
76 
getUserColor(const QString & user)77 const QColor ChannelChatters::getUserColor(const QString &user)
78 {
79     const auto chatterColors = this->chatterColors_.access();
80 
81     auto lowerUser = user.toLower();
82 
83     if (!chatterColors->exists(lowerUser))
84     {
85         // Returns an invalid color so we can decide not to override `textColor`
86         return QColor();
87     }
88 
89     return QColor::fromRgb(chatterColors->get(lowerUser));
90 }
91 
setUserColor(const QString & user,const QColor & color)92 void ChannelChatters::setUserColor(const QString &user, const QColor &color)
93 {
94     const auto chatterColors = this->chatterColors_.access();
95     chatterColors->put(user.toLower(), color.rgb());
96 }
97 
98 }  // namespace chatterino
99