dialog.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type {TelegramClient} from "../../client/TelegramClient";
  2. import {Api} from "../api";
  3. import type {Entity} from "../../define";
  4. import {getDisplayName, getInputPeer, getPeerId} from "../../Utils";
  5. import {Draft} from "./draft";
  6. export class Dialog {
  7. private _client: TelegramClient;
  8. private dialog: Api.Dialog;
  9. private pinned: boolean;
  10. private folderId: Api.int | undefined;
  11. private archived: boolean;
  12. private message: Api.Message;
  13. private date: Api.int;
  14. private entity: Entity | undefined;
  15. private inputEntity: Api.TypeInputPeer;
  16. private id?: number;
  17. private name?: string;
  18. private title?: string;
  19. private unreadCount: Api.int;
  20. private unreadMentionsCount: Api.int;
  21. private draft: Draft;
  22. private isUser: boolean;
  23. private isGroup: boolean;
  24. private isChannel: boolean;
  25. constructor(client: TelegramClient, dialog: Api.Dialog, entities: Map<number, Entity>, message: Api.Message) {
  26. this._client = client;
  27. this.dialog = dialog;
  28. this.pinned = !!(dialog.pinned);
  29. this.folderId = dialog.folderId;
  30. this.archived = dialog.folderId != undefined;
  31. this.message = message;
  32. this.date = this.message.date;
  33. this.entity = entities.get(getPeerId(dialog.peer));
  34. this.inputEntity = getInputPeer(this.entity);
  35. if (this.entity) {
  36. this.id = getPeerId(this.entity); // ^ May be InputPeerSelf();
  37. this.name = this.title = getDisplayName(this.entity);
  38. }
  39. this.unreadCount = dialog.unreadCount;
  40. this.unreadMentionsCount = dialog.unreadMentionsCount;
  41. if (!this.entity){
  42. throw new Error("Entity not found for dialog");
  43. }
  44. this.draft = new Draft(client, this.entity, this.dialog.draft);
  45. this.isUser = this.entity instanceof Api.User;
  46. this.isGroup = !!((this.entity instanceof Api.Chat && this.entity instanceof Api.ChatForbidden) || (this.entity instanceof Api.Channel && this.entity.megagroup));
  47. this.isChannel = this.entity instanceof Api.Channel;
  48. }
  49. // TODO implement rest
  50. }