blob: ba7b9063bdfec7344e8221d9200603a1a9255d58 [file] [log] [blame]
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +01001import {parseUrl} from '../../../../common/commonUtils.js';
2import ThreadModel from '../../../../models/Thread.js';
3import {kViewThreadResponse} from '../consts.js';
4import MessageExtraInfoService from '../services/message.js';
5
6import ResponseEventBasedInfoHandler from './basedOnResponseEvent.js';
7
8const kIntervalInMs = 500;
9const kTimeoutInMs = 10 * 1000;
10const kCurrentInfoExpiresInMs = kTimeoutInMs * 1.5;
11
12export default class ThreadInfoHandler extends ResponseEventBasedInfoHandler {
13 constructor() {
14 super();
15
16 this.thread = undefined;
17 }
18
19 getEvent() {
20 return kViewThreadResponse;
21 }
22
23 getWaitForCurrentInfoOptions() {
24 return {
25 interval: kIntervalInMs,
26 timeout: kTimeoutInMs,
27 };
28 }
29
30 async isInfoCurrent(injectionDetails) {
31 this.thread = new ThreadModel(this.info.body?.[1]);
32
33 const currentPage = this.parseThreadUrl();
34 const isCurrentThread =
35 Date.now() - this.info.timestamp < kCurrentInfoExpiresInMs &&
36 this.thread.getId() == currentPage.thread &&
37 this.thread.getForumId() == currentPage.forum;
38
39 const isMessageNode = injectionDetails.isMessageNode;
40 const messageNode = injectionDetails.messageNode;
41
42 return isCurrentThread &&
43 (!isMessageNode || this.currentThreadContainsMessage(messageNode));
44 }
45
46 parseThreadUrl() {
47 const currentPage = parseUrl(location.href);
48 if (currentPage === false)
49 throw new Error(`couldn't parse current URL: ${location.href}`);
50
51 return currentPage;
52 }
53
54 currentThreadContainsMessage(messageNode) {
55 const messageId = MessageExtraInfoService.getMessageIdFromNode(messageNode);
56 const message = MessageExtraInfoService.getMessageFromThreadModel(
57 messageId, this.thread);
58 return message !== undefined;
59 }
60}