Strong typing for NotificationId and NotificationGroupId.

GitOrigin-RevId: 2fe55d97bac2fa689ddb6da855ce32f62362699a
This commit is contained in:
levlam 2018-11-10 02:08:47 +03:00
parent 4605f56d3c
commit 4240b785a0
7 changed files with 138 additions and 34 deletions

View File

@ -535,6 +535,8 @@ set(TDLIB_SOURCE
td/telegram/net/SessionProxy.h
td/telegram/net/SessionMultiProxy.h
td/telegram/net/TempAuthKeyWatchdog.h
td/telegram/NotificationGroupId.h
td/telegram/NotificationId.h
td/telegram/NotificationManager.h
td/telegram/NotificationSettings.h
td/telegram/NotificationType.h

View File

@ -15,6 +15,7 @@
#include <type_traits>
namespace td {
class CallId {
public:
CallId() = default;
@ -54,4 +55,5 @@ struct CallIdHash {
inline StringBuilder &operator<<(StringBuilder &sb, const CallId call_id) {
return sb << "CallId(" << call_id.get() << ")";
}
} // namespace td

View File

@ -0,0 +1,53 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
class NotificationGroupId {
public:
NotificationGroupId() = default;
explicit NotificationGroupId(int32 group_id) : id(group_id) {
}
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::value>>
NotificationGroupId(T group_id) = delete;
bool is_valid() const {
return id > 0;
}
int32 get() const {
return id;
}
bool operator==(const NotificationGroupId &other) const {
return id == other.id;
}
private:
int32 id{0};
};
struct NotificationGroupIdHash {
std::size_t operator()(NotificationGroupId group_id) const {
return std::hash<int32>()(group_id.get());
}
};
inline StringBuilder &operator<<(StringBuilder &sb, const NotificationGroupId group_id) {
return sb << "NotificationGroup(" << group_id.get() << ")";
}
} // namespace td

View File

@ -0,0 +1,53 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
class NotificationId {
public:
NotificationId() = default;
explicit NotificationId(int32 notification_id) : id(notification_id) {
}
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::value>>
NotificationId(T notification_id) = delete;
bool is_valid() const {
return id > 0;
}
int32 get() const {
return id;
}
bool operator==(const NotificationId &other) const {
return id == other.id;
}
private:
int32 id{0};
};
struct NotificationIdHash {
std::size_t operator()(NotificationId notification_id) const {
return std::hash<int32>()(notification_id.get());
}
};
inline StringBuilder &operator<<(StringBuilder &sb, const NotificationId notification_id) {
return sb << "Notification(" << notification_id.get() << ")";
}
} // namespace td

View File

@ -13,45 +13,37 @@ namespace td {
NotificationManager::NotificationManager(Td *td) : td_(td) {
}
int32 NotificationManager::get_next_notification_id() {
return 0;
NotificationId NotificationManager::get_next_notification_id() {
return NotificationId();
}
void NotificationManager::add_notification(int32 group_id, int32 total_count, DialogId dialog_id,
DialogId notification_settings_dialog_id, bool silent, int32 notification_id,
unique_ptr<NotificationType> type) {
void NotificationManager::add_notification(NotificationGroupId group_id, int32 total_count, DialogId dialog_id,
DialogId notification_settings_dialog_id, bool silent,
NotificationId notification_id, unique_ptr<NotificationType> type) {
}
void NotificationManager::edit_notification(int32 notification_id, unique_ptr<NotificationType> type) {
void NotificationManager::edit_notification(NotificationId notification_id, unique_ptr<NotificationType> type) {
}
void NotificationManager::delete_notification(int32 notification_id) {
void NotificationManager::delete_notification(NotificationId notification_id) {
}
void NotificationManager::remove_notification(int32 notification_id, Promise<Unit> &&promise) {
if (!is_valid_notification_id(notification_id)) {
void NotificationManager::remove_notification(NotificationId notification_id, Promise<Unit> &&promise) {
if (!notification_id.is_valid()) {
return promise.set_error(Status::Error(400, "Notification identifier is invalid"));
}
promise.set_value(Unit());
}
void NotificationManager::remove_notification_group(int32 group_id, int32 max_notification_id,
void NotificationManager::remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id,
Promise<Unit> &&promise) {
if (!is_valid_group_id(group_id)) {
if (!group_id.is_valid()) {
return promise.set_error(Status::Error(400, "Group identifier is invalid"));
}
if (!is_valid_notification_id(max_notification_id)) {
if (!max_notification_id.is_valid()) {
return promise.set_error(Status::Error(400, "Notification identifier is invalid"));
}
promise.set_value(Unit());
}
bool NotificationManager::is_valid_notification_id(int32 notification_id) {
return notification_id > 0;
}
bool NotificationManager::is_valid_group_id(int32 group_id) {
return group_id > 0;
}
} // namespace td

View File

@ -7,6 +7,8 @@
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/NotificationGroupId.h"
#include "td/telegram/NotificationId.h"
#include "td/telegram/NotificationType.h"
#include "td/actor/PromiseFuture.h"
@ -21,24 +23,22 @@ class NotificationManager {
public:
explicit NotificationManager(Td *td);
int32 get_next_notification_id();
NotificationId get_next_notification_id();
void add_notification(int32 group_id, int32 total_count, DialogId dialog_id, DialogId notification_settings_dialog_id,
bool silent, int32 notification_id, unique_ptr<NotificationType> type);
void add_notification(NotificationGroupId group_id, int32 total_count, DialogId dialog_id,
DialogId notification_settings_dialog_id, bool silent, NotificationId notification_id,
unique_ptr<NotificationType> type);
void edit_notification(int32 notification_id, unique_ptr<NotificationType> type);
void edit_notification(NotificationId notification_id, unique_ptr<NotificationType> type);
void delete_notification(int32 notification_id);
void delete_notification(NotificationId notification_id);
void remove_notification(int32 notification_id, Promise<Unit> &&promise);
void remove_notification(NotificationId notification_id, Promise<Unit> &&promise);
void remove_notification_group(int32 group_id, int32 max_notification_id, Promise<Unit> &&promise);
void remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id,
Promise<Unit> &&promise);
private:
static bool is_valid_notification_id(int32 notification_id);
static bool is_valid_group_id(int32 group_id);
Td *td_;
};

View File

@ -44,6 +44,8 @@
#include "td/telegram/MessageId.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/misc.h"
#include "td/telegram/NotificationGroupId.h"
#include "td/telegram/NotificationId.h"
#include "td/telegram/NotificationManager.h"
#include "td/telegram/NotificationSettings.h"
#include "td/telegram/PasswordManager.h"
@ -5067,14 +5069,14 @@ void Td::on_request(uint64 id, td_api::getChatMessageCount &request) {
void Td::on_request(uint64 id, const td_api::removeNotification &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
notification_manager_->remove_notification(request.notification_id_, std::move(promise));
notification_manager_->remove_notification(NotificationId(request.notification_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::removeNotificationGroup &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
notification_manager_->remove_notification_group(request.notification_group_id_, request.max_notification_id_,
std::move(promise));
notification_manager_->remove_notification_group(NotificationGroupId(request.notification_group_id_),
NotificationId(request.max_notification_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::deleteMessages &request) {