blob: c44d7f8874576149cccbd9b8908c8afd06ce6794 [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 {
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010013 getEvent() {
14 return kViewThreadResponse;
15 }
16
17 getWaitForCurrentInfoOptions() {
18 return {
19 interval: kIntervalInMs,
20 timeout: kTimeoutInMs,
21 };
22 }
23
Adrià Vilanova Martínez4b2582d2023-11-16 01:56:04 +010024 setUpDefaultInfoValue() {
25 this.info = {
26 thread: new ThreadModel(),
27 messages: [],
28 id: -1,
29 timestamp: 0,
30 };
31 }
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010032
Adrià Vilanova Martínez4b2582d2023-11-16 01:56:04 +010033 updateInfoWithNewValue(e) {
34 const newThread = new ThreadModel(e.detail.body?.[1]);
35 if (newThread.getId() != this.info.thread.getId()) {
36 this.info.messages = [];
37 }
38
39 const newMessages = newThread.getAllMessagesList();
40 this.updateRecordedMessages(newMessages);
41
42 this.info.thread = newThread;
43 this.info.id = e.detail.id;
44 this.info.timestamp = Date.now();
45 }
46
47 updateRecordedMessages(newMessages) {
48 const nonUpdatedMessages = this.info.messages.filter(message => {
49 return !newMessages.some(newMessage => {
50 return message.getId() == newMessage.getId();
51 });
52 });
53 this.info.messages = nonUpdatedMessages.concat(newMessages);
54 }
55
56 async isInfoCurrent(injectionDetails) {
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010057 const currentPage = this.parseThreadUrl();
58 const isCurrentThread =
59 Date.now() - this.info.timestamp < kCurrentInfoExpiresInMs &&
Adrià Vilanova Martínez4b2582d2023-11-16 01:56:04 +010060 this.info.thread.getId() == currentPage.thread &&
61 this.info.thread.getForumId() == currentPage.forum;
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010062
63 const isMessageNode = injectionDetails.isMessageNode;
64 const messageNode = injectionDetails.messageNode;
65
66 return isCurrentThread &&
67 (!isMessageNode || this.currentThreadContainsMessage(messageNode));
68 }
69
70 parseThreadUrl() {
71 const currentPage = parseUrl(location.href);
72 if (currentPage === false)
73 throw new Error(`couldn't parse current URL: ${location.href}`);
74
75 return currentPage;
76 }
77
78 currentThreadContainsMessage(messageNode) {
79 const messageId = MessageExtraInfoService.getMessageIdFromNode(messageNode);
Adrià Vilanova Martínez4b2582d2023-11-16 01:56:04 +010080 const message = MessageExtraInfoService.getMessageFromList(
81 messageId, this.info.messages);
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010082 return message !== undefined;
83 }
84}