Add td_api::getStoryViewers.

This commit is contained in:
levlam 2023-06-18 23:25:50 +03:00
parent 8cb17eb1c3
commit 2ea17ff61a
9 changed files with 144 additions and 3 deletions

View File

@ -7283,6 +7283,12 @@ openStory story_sender_user_id:int53 story_id:int32 = Ok;
//@story_id The identifier of the story
closeStory story_sender_user_id:int53 story_id:int32 = Ok;
//@description Returns viewers of a recent outgoing story. The method can be called if story.can_get_viewers == true
//@story_id Story identifier
//@offset_viewer A viewer from which to return next viewers; pass null to get results from the beginning
//@limit The maximum number of story viewers to return
getStoryViewers story_id:int32 offset_viewer:messageViewer limit:int32 = MessageViewers;
//@description Returns information about a bot that can be added to attachment menu @bot_user_id Bot's user identifier
getAttachmentMenuBot bot_user_id:int53 = AttachmentMenuBot;

View File

@ -13,7 +13,7 @@
namespace td {
MessageViewer::MessageViewer(telegram_api::object_ptr<telegram_api::readParticipantDate> &&read_date)
: user_id_(read_date->user_id_), date_(read_date->date_) {
: MessageViewer(UserId(read_date->user_id_), read_date->date_) {
}
td_api::object_ptr<td_api::messageViewer> MessageViewer::get_message_viewer_object(
@ -26,6 +26,17 @@ StringBuilder &operator<<(StringBuilder &string_builder, const MessageViewer &vi
return string_builder << '[' << viewer.user_id_ << " at " << viewer.date_ << ']';
}
MessageViewers::MessageViewers(vector<telegram_api::object_ptr<telegram_api::storyView>> &&story_views) {
for (auto &story_view : story_views) {
UserId user_id(story_view->user_id_);
if (!user_id.is_valid()) {
LOG(ERROR) << "Receive " << user_id << " as story viewer";
continue;
}
message_viewers_.emplace_back(user_id, story_view->date_);
}
}
MessageViewers::MessageViewers(vector<telegram_api::object_ptr<telegram_api::readParticipantDate>> &&read_dates)
: message_viewers_(
transform(std::move(read_dates), [](telegram_api::object_ptr<telegram_api::readParticipantDate> &&read_date) {

View File

@ -26,6 +26,9 @@ class MessageViewer {
public:
explicit MessageViewer(telegram_api::object_ptr<telegram_api::readParticipantDate> &&read_date);
MessageViewer(UserId user_id, int32 date) : user_id_(user_id), date_(td::max(date, static_cast<int32>(0))) {
}
UserId get_user_id() const {
return user_id_;
}
@ -38,6 +41,8 @@ StringBuilder &operator<<(StringBuilder &string_builder, const MessageViewer &vi
struct MessageViewers {
vector<MessageViewer> message_viewers_;
explicit MessageViewers(vector<telegram_api::object_ptr<telegram_api::storyView>> &&story_views);
explicit MessageViewers(vector<telegram_api::object_ptr<telegram_api::readParticipantDate>> &&read_dates);
td_api::object_ptr<td_api::messageViewers> get_message_viewers_object(ContactsManager *contacts_manager) const;

View File

@ -36,6 +36,14 @@ class StoryInteractionInfo {
return view_count_ < 0;
}
bool set_view_count(int32 view_count) {
if (view_count > view_count_) {
view_count = view_count_;
return true;
}
return false;
}
td_api::object_ptr<td_api::storyInteractionInfo> get_story_interaction_info_object(Td *td) const;
};

View File

@ -16,6 +16,7 @@
#include "td/telegram/logevent/LogEventHelper.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/MessageViewer.h"
#include "td/telegram/OptionManager.h"
#include "td/telegram/StoryContent.h"
#include "td/telegram/StoryContentType.h"
@ -162,6 +163,33 @@ class ReadStoriesQuery final : public Td::ResultHandler {
}
};
class GetStoryViewsListQuery final : public Td::ResultHandler {
Promise<telegram_api::object_ptr<telegram_api::stories_storyViewsList>> promise_;
public:
explicit GetStoryViewsListQuery(Promise<telegram_api::object_ptr<telegram_api::stories_storyViewsList>> &&promise)
: promise_(std::move(promise)) {
}
void send(StoryId story_id, int32 offset_date, int64 offset_user_id, int32 limit) {
send_query(G()->net_query_creator().create(
telegram_api::stories_getStoryViewsList(story_id.get(), offset_date, offset_user_id, limit)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::stories_getStoryViewsList>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
promise_.set_value(result_ptr.move_as_ok());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class GetStoriesByIDQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
UserId user_id_;
@ -962,6 +990,66 @@ void StoryManager::read_stories_on_server(DialogId owner_dialog_id, StoryId stor
td_->create_handler<ReadStoriesQuery>(get_erase_log_event_promise(log_event_id))->send(owner_dialog_id, story_id);
}
void StoryManager::get_story_viewers(StoryId story_id, const td_api::messageViewer *offset, int32 limit,
Promise<td_api::object_ptr<td_api::messageViewers>> &&promise) {
DialogId owner_dialog_id(td_->contacts_manager_->get_my_id());
StoryFullId story_full_id{owner_dialog_id, story_id};
const Story *story = get_story(story_full_id);
if (story == nullptr) {
return promise.set_error(Status::Error(400, "Story not found"));
}
if (!story_id.is_server()) {
return promise.set_value(td_api::object_ptr<td_api::messageViewers>());
}
// TRY_STATUS_PROMISE(promise, can_get_story_viewers(story_full_id));
if (limit <= 0) {
return promise.set_error(Status::Error(400, "Parameter limit must be positive"));
}
int32 offset_date = 0;
int64 offset_user_id = 0;
if (offset != nullptr) {
offset_date = offset->view_date_;
offset_user_id = offset->user_id_;
}
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), story_id, promise = std::move(promise)](
Result<telegram_api::object_ptr<telegram_api::stories_storyViewsList>> result) mutable {
send_closure(actor_id, &StoryManager::on_get_story_viewers, story_id, std::move(result), std::move(promise));
});
td_->create_handler<GetStoryViewsListQuery>(std::move(query_promise))
->send(story_full_id.get_story_id(), offset_date, offset_user_id, limit);
}
void StoryManager::on_get_story_viewers(
StoryId story_id, Result<telegram_api::object_ptr<telegram_api::stories_storyViewsList>> r_view_list,
Promise<td_api::object_ptr<td_api::messageViewers>> &&promise) {
G()->ignore_result_if_closing(r_view_list);
if (r_view_list.is_error()) {
return promise.set_error(r_view_list.move_as_error());
}
auto view_list = r_view_list.move_as_ok();
DialogId owner_dialog_id(td_->contacts_manager_->get_my_id());
CHECK(story_id.is_server());
StoryFullId story_full_id{owner_dialog_id, story_id};
Story *story = get_story_editable(story_full_id);
if (story == nullptr) {
return promise.set_value(td_api::object_ptr<td_api::messageViewers>());
}
td_->contacts_manager_->on_get_users(std::move(view_list->users_), "on_get_story_viewers");
if (story->content_ != nullptr && story->interaction_info_.set_view_count(view_list->count_)) {
on_story_changed(story_full_id, story, true, true);
}
MessageViewers story_viewers(std::move(view_list->views_));
promise.set_value(story_viewers.get_message_viewers_object(td_->contacts_manager_.get()));
}
bool StoryManager::have_story(StoryFullId story_full_id) const {
return get_story(story_full_id) != nullptr;
}
@ -1542,12 +1630,11 @@ void StoryManager::on_get_story_views(const vector<StoryId> &story_ids,
continue;
}
bool is_changed = false;
StoryInteractionInfo interaction_info(td_, std::move(story_views->views_[i]));
CHECK(!interaction_info.is_empty());
if (story->interaction_info_ != interaction_info) {
story->interaction_info_ = std::move(interaction_info);
on_story_changed(story_full_id, story, is_changed, false);
on_story_changed(story_full_id, story, true, true);
}
}
}

View File

@ -120,6 +120,9 @@ class StoryManager final : public Actor {
void close_story(DialogId owner_dialog_id, StoryId story_id, Promise<Unit> &&promise);
void get_story_viewers(StoryId story_id, const td_api::messageViewer *offset, int32 limit,
Promise<td_api::object_ptr<td_api::messageViewers>> &&promise);
StoryId on_get_story(DialogId owner_dialog_id, telegram_api::object_ptr<telegram_api::StoryItem> &&story_item_ptr);
std::pair<int32, vector<StoryId>> on_get_stories(DialogId owner_dialog_id, vector<StoryId> &&expected_story_ids,
@ -262,6 +265,10 @@ class StoryManager final : public Actor {
void on_synchronized_archive_all_stories(bool set_archive_all_stories, Result<Unit> result);
void on_get_story_viewers(StoryId story_id,
Result<telegram_api::object_ptr<telegram_api::stories_storyViewsList>> r_view_list,
Promise<td_api::object_ptr<td_api::messageViewers>> &&promise);
std::shared_ptr<UploadMediaCallback> upload_media_callback_;
WaitFreeHashMap<StoryFullId, FileSourceId, StoryFullIdHash> story_full_id_to_file_source_id_;

View File

@ -6457,6 +6457,13 @@ void Td::on_request(uint64 id, const td_api::closeStory &request) {
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getStoryViewers &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
story_manager_->get_story_viewers(StoryId(request.story_id_), request.offset_viewer_.get(), request.limit_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getAttachmentMenuBot &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();

View File

@ -1010,6 +1010,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::closeStory &request);
void on_request(uint64 id, const td_api::getStoryViewers &request);
void on_request(uint64 id, const td_api::getAttachmentMenuBot &request);
void on_request(uint64 id, const td_api::toggleBotIsAddedToAttachmentMenu &request);

View File

@ -4068,6 +4068,14 @@ class CliClient final : public Actor {
StoryId story_id;
get_args(args, story_sender_user_id, story_id);
send_request(td_api::make_object<td_api::closeStory>(story_sender_user_id, story_id));
} else if (op == "gsv") {
StoryId story_id;
string limit;
int32 offset_date;
UserId offset_user_id;
get_args(args, story_id, limit, offset_date, offset_user_id);
send_request(td_api::make_object<td_api::getStoryViewers>(
story_id, td_api::make_object<td_api::messageViewer>(offset_user_id, offset_date), as_limit(limit)));
} else if (op == "gamb") {
UserId user_id;
get_args(args, user_id);