blob: 96f69de107f228c261edb597bae53eaf25d89e1e [file] [log] [blame]
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +01001import {waitFor} from 'poll-until-promise';
2
3import {CCApi} from '../../../../common/api.js';
4import {parseUrl} from '../../../../common/commonUtils.js';
5import {getAuthUser} from '../../../../common/communityConsoleUtils.js';
6
7export default class Thread {
8 constructor(forumId, threadId) {
9 this.forumId = forumId;
10 this.threadId = threadId;
11 this._details = null;
12 }
13
14 static fromUrl(url) {
15 const rawThread = parseUrl(url);
16 if (!rawThread) return null;
17
18 return new Thread(rawThread.forum, rawThread.thread);
19 }
20
21 loadThreadDetails() {
22 if (this._details) return Promise.resolve(true);
23
24 return waitFor(
25 () => {
26 return CCApi(
27 'ViewForum', {
28 1: '0', // forumID,
29 // options
30 2: {
31 3: false, // withMessages
32 5: true, // withUserProfile
33 6: false, // withUserReadState
34 7: false, // withStickyThreads
35 9: false, // withRequestorProfile
36 10: false, // withPromotedMessages
37 11: false, // withExpertResponder
38 12: `forum:${this.forumId} thread:${
39 this.threadId}`, // forumViewFilters
40 16: false, // withThreadNotes
41 17: false, // withExpertReplyingIndicator
42 },
43 },
44 /* authenticated = */ true, getAuthUser())
45 .then(res => {
46 if (res?.['1']?.['2']?.length < 1)
47 throw new Error(
48 `Couldn't retrieve thread details (forum: ${
49 this.forumId}, thread: ${this.thread}).`);
50
51 return res?.['1']?.['2']?.[0];
52 });
53 },
54 {
55 interval: 500,
56 timeout: 2000,
57 })
58 .then(thread => {
59 this._details = thread;
60 return true;
61 });
62 }
63
64 get opName() {
65 return this._details?.['4']?.['1']?.['1'];
66 }
67
68 get opUserId() {
69 return this._details?.['4']?.['3'];
70 }
71
72 get forumTitle() {
73 return this._details?.['23'];
74 }
75
76 get isRead() {
77 return !!this._details?.['6'];
78 }
79
80 get isStarred() {
81 return !!this._details?.['7']?.['1'];
82 }
83
84 get numMessages() {
85 return this._details?.['8'];
86 }
87
88 get numAnswers() {
89 return this._details?.['15'];
90 }
91
92 get numSuggestedAnswers() {
93 return this._details?.['32'];
94 }
95
96 get title() {
97 return this._details?.['2']?.['9'];
98 }
99
Adrià Vilanova Martínez6c4739a2022-11-07 00:11:53 +0100100 get lastMessageId() {
101 return this._details?.['2']?.['10'];
102 }
103
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +0100104 get payload() {
105 return this._details?.['2']?.['13'];
106 }
107
108 // Accessors in the style of
109 // https://support.google.com/communities/answer/9147001.
110 get op_name() {
111 return this.opName;
112 }
113
114 get forum_name() {
115 return this.forumTitle;
116 }
117}