Skip to content

Commit

Permalink
Add poll question and poll option entities
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Jun 8, 2024
1 parent b0fb575 commit b21dd8a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions 3_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export * from "./types/0_mini_app_info.ts";
export * from "./types/0_network_statistics_entry.ts";
export * from "./types/0_opening_hours.ts";
export * from "./types/0_parse_mode.ts";
export * from "./types/0_poll_option.ts";
export * from "./types/0_price_tag.ts";
export * from "./types/0_reaction.ts";
export * from "./types/0_restriction_reason.ts";
Expand All @@ -71,7 +70,7 @@ export * from "./types/1_message_content.ts";
export * from "./types/1_message_reaction.ts";
export * from "./types/1_network_statistics.ts";
export * from "./types/1_photo.ts";
export * from "./types/1_poll.ts";
export * from "./types/1_poll_option.ts";
export * from "./types/1_reaction_count.ts";
export * from "./types/1_reply_quote.ts";
export * from "./types/1_sticker.ts";
Expand All @@ -82,6 +81,7 @@ export * from "./types/1_venue.ts";
export * from "./types/1_video.ts";
export * from "./types/1_video_note.ts";
export * from "./types/2_business_connection.ts";
export * from "./types/2_chat.ts";
export * from "./types/2_chat_member.ts";
export * from "./types/2_chosen_inline_result.ts";
export * from "./types/2_game.ts";
Expand All @@ -92,6 +92,7 @@ export * from "./types/2_invite_link.ts";
export * from "./types/2_message_interactions.ts";
export * from "./types/2_message_reaction_count.ts";
export * from "./types/2_message_reactions.ts";
export * from "./types/2_poll.ts";
export * from "./types/2_story_content.ts";
export * from "./types/2_story_interactions.ts";
export * from "./types/2_story_interactive_area.ts";
Expand All @@ -102,6 +103,5 @@ export * from "./types/4_inline_query_result.ts";
export * from "./types/4_message.ts";
export * from "./types/5_callback_query.ts";
export * from "./types/5_chat_list_item.ts";
export * from "./types/2_chat.ts";
export * from "./types/5_inline_query_answer.ts";
export * from "./types/6_update.ts";
6 changes: 5 additions & 1 deletion types/0_poll_option.ts → types/1_poll_option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@
*/

import { Api } from "../2_tl.ts";
import { constructMessageEntity, MessageEntity } from "./0_message_entity.ts";

/** A poll option. */
export interface PollOption {
/** The option's text (1-100 characters). */
text: string;
/** The entities of the option's text. */
entities: MessageEntity[];
/** Number of users that voted this option. */
voterCount: number;
}

export function constructPollOption(option: Api.PollAnswer, results: Array<Api.PollAnswerVoters>): PollOption {
return {
text: option.text,
text: option.text.text,
entities: option.text.entities?.map(constructMessageEntity).filter((v): v is MessageEntity => v != null),
voterCount: results.find((v) => v.option.every((v, i) => option.option[i] == v))?.voters ?? 0,
};
}
7 changes: 5 additions & 2 deletions types/1_poll.ts → types/2_poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import { cleanObject } from "../1_utilities.ts";
import { Api } from "../2_tl.ts";
import { constructMessageEntity, MessageEntity } from "./0_message_entity.ts";
import { constructPollOption, PollOption } from "./0_poll_option.ts";
import { constructPollOption, PollOption } from "./1_poll_option.ts";

/** A poll. */
export interface Poll {
/** The identifier of the poll. */
id: string;
/** The poll's question. */
question: string;
/** The entities of the poll's question. */
questionEntities: MessageEntity[];
/** The poll's options. */
options: PollOption[];
/** The number of users who have participated in the poll. */
Expand Down Expand Up @@ -60,7 +62,8 @@ export function constructPoll(media_: Api.messageMediaPoll): Poll {

return cleanObject({
id: String(poll.id),
question: poll.question,
question: poll.question.text,
questionEntities: poll.question.entities.map(constructMessageEntity).filter((v): v is MessageEntity => v != null),
options: poll.answers.map((v) => constructPollOption(v, media_.results.results ?? [])),
totalVoterCount: media_.results.total_voters ?? 0,
isClosed: poll.closed || false,
Expand Down
2 changes: 1 addition & 1 deletion types/4_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ import { constructDocument, Document } from "./1_document.ts";
import { constructGiveaway, Giveaway } from "./1_giveaway.ts";
import { constructMessageReaction, MessageReaction } from "./1_message_reaction.ts";
import { constructPhoto, Photo } from "./1_photo.ts";
import { constructPoll, Poll } from "./1_poll.ts";
import { constructReplyQuote, ReplyQuote } from "./1_reply_quote.ts";
import { constructSticker, Sticker, StickerSetNameGetter } from "./1_sticker.ts";
import { constructUser, User } from "./1_user.ts";
import { constructVenue, Venue } from "./1_venue.ts";
import { constructVideoNote, VideoNote } from "./1_video_note.ts";
import { constructVideo, Video } from "./1_video.ts";
import { constructGame, Game } from "./2_game.ts";
import { constructPoll, Poll } from "./2_poll.ts";
import { constructReplyMarkup, ReplyMarkup } from "./3_reply_markup.ts";

const L = getLogger("Message");
Expand Down

0 comments on commit b21dd8a

Please sign in to comment.