Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to add nicknames for users #2981

Merged
merged 17 commits into from
Jul 31, 2021
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unversioned

- Major: Added alias for usernames. (#137, #2981)
apa420 marked this conversation as resolved.
Show resolved Hide resolved
- Minor: Username in channel points rewards redemption messages is now clickable. (#2673, #2953)
- Minor: Channel name in `<channel> has gone offline. Exiting host mode.` messages is now clickable. (#2922)
- Minor: Added `/openurl` command. Usage: `/openurl <URL>`. Opens the provided URL in the browser. (#2461, #2926)
Expand Down
4 changes: 4 additions & 0 deletions chatterino.pro
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ SOURCES += \
src/controllers/accounts/Account.cpp \
src/controllers/accounts/AccountController.cpp \
src/controllers/accounts/AccountModel.cpp \
src/controllers/aliases/AliasesModel.cpp \
src/controllers/commands/Command.cpp \
src/controllers/commands/CommandController.cpp \
src/controllers/commands/CommandModel.cpp \
Expand Down Expand Up @@ -307,6 +308,7 @@ SOURCES += \
src/widgets/Scrollbar.cpp \
src/widgets/settingspages/AboutPage.cpp \
src/widgets/settingspages/AccountsPage.cpp \
src/widgets/settingspages/AliasesPage.cpp \
src/widgets/settingspages/CommandPage.cpp \
src/widgets/settingspages/ExternalToolsPage.cpp \
src/widgets/settingspages/FiltersPage.cpp \
Expand Down Expand Up @@ -371,6 +373,8 @@ HEADERS += \
src/controllers/accounts/Account.hpp \
src/controllers/accounts/AccountController.hpp \
src/controllers/accounts/AccountModel.hpp \
src/controllers/aliases/AliasesName.hpp \
src/controllers/aliases/AliasesModel.hpp \
src/controllers/commands/Command.hpp \
src/controllers/commands/CommandController.hpp \
src/controllers/commands/CommandModel.hpp \
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ set(SOURCE_FILES
controllers/accounts/AccountModel.cpp
controllers/accounts/AccountModel.hpp

controllers/aliases/AliasesModel.cpp
controllers/aliases/AliasesModel.hpp
controllers/aliases/AliasesName.hpp

controllers/commands/Command.cpp
controllers/commands/Command.hpp
controllers/commands/CommandController.cpp
Expand Down Expand Up @@ -412,6 +416,8 @@ set(SOURCE_FILES
widgets/settingspages/AboutPage.hpp
widgets/settingspages/AccountsPage.cpp
widgets/settingspages/AccountsPage.hpp
widgets/settingspages/AliasesPage.cpp
widgets/settingspages/AliasesPage.hpp
widgets/settingspages/CommandPage.cpp
widgets/settingspages/CommandPage.hpp
widgets/settingspages/ExternalToolsPage.cpp
Expand Down
31 changes: 31 additions & 0 deletions src/controllers/aliases/AliasesModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "AliasesModel.hpp"

#include "Application.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "singletons/Settings.hpp"
#include "util/StandardItemHelper.hpp"

namespace chatterino {

AliasesModel::AliasesModel(QObject *parent)
: SignalVectorModel<AliasesName>(2, parent)
{
}

// turn a vector item into a model row
AliasesName AliasesModel::getItemFromRow(std::vector<QStandardItem *> &row,
const AliasesName &original)
{
return AliasesName{row[0]->data(Qt::DisplayRole).toString(),
row[1]->data(Qt::DisplayRole).toString()};
}

// turns a row in the model into a vector item
void AliasesModel::getRowFromItem(const AliasesName &item,
std::vector<QStandardItem *> &row)
{
setStringItem(row[0], item.getName());
setStringItem(row[1], item.getReplace());
}

} // namespace chatterino
25 changes: 25 additions & 0 deletions src/controllers/aliases/AliasesModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <QObject>

#include "common/SignalVectorModel.hpp"
#include "controllers/aliases/AliasesName.hpp"

namespace chatterino {

class AliasesModel : public SignalVectorModel<AliasesName>
{
public:
explicit AliasesModel(QObject *parent);

protected:
// turn a vector item into a model row
virtual AliasesName getItemFromRow(std::vector<QStandardItem *> &row,
const AliasesName &original) override;

// turns a row in the model into a vector item
virtual void getRowFromItem(const AliasesName &item,
std::vector<QStandardItem *> &row) override;
};

} // namespace chatterino
77 changes: 77 additions & 0 deletions src/controllers/aliases/AliasesName.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include "controllers/accounts/AccountController.hpp"

#include "util/RapidJsonSerializeQString.hpp"
#include "util/RapidjsonHelpers.hpp"

#include <QString>
#include <pajlada/serialize.hpp>

#include <memory>

namespace chatterino {

class AliasesName
{
public:
AliasesName(const QString &name, const QString &replace)
: name_(name)
, replace_(replace)
{
}

const QString &getName() const
apa420 marked this conversation as resolved.
Show resolved Hide resolved
{
return this->name_;
}
const QString &getReplace() const
apa420 marked this conversation as resolved.
Show resolved Hide resolved
{
return this->replace_;
}

private:
QString name_;
QString replace_;
};

} // namespace chatterino

namespace pajlada {

template <>
struct Serialize<chatterino::AliasesName> {
static rapidjson::Value get(const chatterino::AliasesName &value,
rapidjson::Document::AllocatorType &a)
{
rapidjson::Value ret(rapidjson::kObjectType);

chatterino::rj::set(ret, "name", value.getName(), a);
chatterino::rj::set(ret, "replace", value.getReplace(), a);

return ret;
}
};

template <>
struct Deserialize<chatterino::AliasesName> {
static chatterino::AliasesName get(const rapidjson::Value &value,
bool *error = nullptr)
{
if (!value.IsObject())
{
PAJLADA_REPORT_ERROR(error)
return chatterino::AliasesName(QString(), QString());
}

QString _name;
QString _replace;

chatterino::rj::getSafe(value, "name", _name);
chatterino::rj::getSafe(value, "replace", _replace);

return chatterino::AliasesName(_name, _replace);
}
};

} // namespace pajlada
10 changes: 10 additions & 0 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,16 @@ void TwitchMessageBuilder::appendUsername()
break;
}

auto aliases = *getCSettings().aliasNames.readOnly().get();

for (auto const alias : aliases)
{
if (alias.getName().toLower() == this->message().loginName.toLower())
apa420 marked this conversation as resolved.
Show resolved Hide resolved
{
usernameText = alias.getReplace();
}
}
apa420 marked this conversation as resolved.
Show resolved Hide resolved

if (this->args.isSentWhisper)
{
// TODO(pajlada): Re-implement
Expand Down
2 changes: 2 additions & 0 deletions src/singletons/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ConcurrentSettings::ConcurrentSettings()
, ignoredMessages(*new SignalVector<IgnorePhrase>())
, mutedChannels(*new SignalVector<QString>())
, filterRecords(*new SignalVector<FilterRecordPtr>())
, aliasNames(*new SignalVector<AliasesName>())
, moderationActions(*new SignalVector<ModerationAction>)
{
persist(this->highlightedMessages, "/highlighting/highlights");
Expand All @@ -32,6 +33,7 @@ ConcurrentSettings::ConcurrentSettings()
persist(this->ignoredMessages, "/ignore/phrases");
persist(this->mutedChannels, "/pings/muted");
persist(this->filterRecords, "/filtering/filters");
persist(this->aliasNames, "/alias");
// tagged users?
persist(this->moderationActions, "/moderation/actions");
}
Expand Down
3 changes: 3 additions & 0 deletions src/singletons/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "BaseSettings.hpp"
#include "common/Channel.hpp"
#include "common/SignalVector.hpp"
#include "controllers/aliases/AliasesName.hpp"
#include "controllers/filters/FilterRecord.hpp"
#include "controllers/highlights/HighlightBadge.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
Expand All @@ -23,6 +24,7 @@ class HighlightBlacklistUser;
class IgnorePhrase;
class TaggedUser;
class FilterRecord;
class AliasesName;

/// Settings which are availlable for reading on all threads.
class ConcurrentSettings
Expand All @@ -37,6 +39,7 @@ class ConcurrentSettings
SignalVector<IgnorePhrase> &ignoredMessages;
SignalVector<QString> &mutedChannels;
SignalVector<FilterRecordPtr> &filterRecords;
SignalVector<AliasesName> &aliasNames;
//SignalVector<TaggedUser> &taggedUsers;
SignalVector<ModerationAction> &moderationActions;

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/dialogs/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "widgets/helper/Button.hpp"
#include "widgets/settingspages/AboutPage.hpp"
#include "widgets/settingspages/AccountsPage.hpp"
#include "widgets/settingspages/AliasesPage.hpp"
#include "widgets/settingspages/CommandPage.hpp"
#include "widgets/settingspages/ExternalToolsPage.hpp"
#include "widgets/settingspages/FiltersPage.hpp"
Expand Down Expand Up @@ -164,6 +165,7 @@ void SettingsDialog::addTabs()
this->addTab([]{return new GeneralPage;}, "General", ":/settings/about.svg");
this->ui_.tabContainer->addSpacing(16);
this->addTab([]{return new AccountsPage;}, "Accounts", ":/settings/accounts.svg", SettingsTabId::Accounts);
this->addTab([]{return new AliasesPage;}, "Aliases", ":/settings/accounts.svg");
apa420 marked this conversation as resolved.
Show resolved Hide resolved
this->ui_.tabContainer->addSpacing(16);
this->addTab([]{return new CommandPage;}, "Commands", ":/settings/commands.svg");
this->addTab([]{return new HighlightingPage;}, "Highlights", ":/settings/notifications.svg");
Expand Down
45 changes: 45 additions & 0 deletions src/widgets/settingspages/AliasesPage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "AliasesPage.hpp"

#include "controllers/aliases/AliasesModel.hpp"
#include "singletons/Settings.hpp"
#include "singletons/WindowManager.hpp"
#include "util/LayoutCreator.hpp"
#include "widgets/Window.hpp"
#include "widgets/helper/EditableModelView.hpp"

#include <QTableView>

#include <QHeaderView>

namespace chatterino {

AliasesPage::AliasesPage()
{
LayoutCreator<AliasesPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();

layout.emplace<QLabel>("Gives an alias to a user.");
apa420 marked this conversation as resolved.
Show resolved Hide resolved
EditableModelView *view =
layout
.emplace<EditableModelView>(
(new AliasesModel(nullptr))
->initialized(&getSettings()->aliasNames))
.getElement();

view->setTitles({"Name", "Replacement"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(
QHeaderView::Interactive);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
1, QHeaderView::Stretch);

view->addButtonPressed.connect([] {
getSettings()->aliasNames.append(AliasesName{"Name", "Replacement"});
});

QTimer::singleShot(1, [view] {
view->getTableView()->resizeColumnsToContents();
view->getTableView()->setColumnWidth(0, 250);
});
}

} // namespace chatterino
18 changes: 18 additions & 0 deletions src/widgets/settingspages/AliasesPage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "widgets/helper/EditableModelView.hpp"
#include "widgets/settingspages/SettingsPage.hpp"

#include <QStringListModel>

class QVBoxLayout;

namespace chatterino {

class AliasesPage : public SettingsPage
{
public:
AliasesPage();
};

} // namespace chatterino