Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 1 | import {parseUrl} from '../../../../common/commonUtils.js'; |
| 2 | import ThreadModel from '../../../../models/Thread.js'; |
| 3 | import {kViewThreadResponse} from '../consts.js'; |
| 4 | import MessageExtraInfoService from '../services/message.js'; |
| 5 | |
| 6 | import ResponseEventBasedInfoHandler from './basedOnResponseEvent.js'; |
| 7 | |
| 8 | const kIntervalInMs = 500; |
| 9 | const kTimeoutInMs = 10 * 1000; |
| 10 | const kCurrentInfoExpiresInMs = kTimeoutInMs * 1.5; |
| 11 | |
| 12 | export default class ThreadInfoHandler extends ResponseEventBasedInfoHandler { |
Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 13 | getEvent() { |
| 14 | return kViewThreadResponse; |
| 15 | } |
| 16 | |
| 17 | getWaitForCurrentInfoOptions() { |
| 18 | return { |
| 19 | interval: kIntervalInMs, |
| 20 | timeout: kTimeoutInMs, |
| 21 | }; |
| 22 | } |
| 23 | |
Adrià Vilanova Martínez | 4b2582d | 2023-11-16 01:56:04 +0100 | [diff] [blame^] | 24 | setUpDefaultInfoValue() { |
| 25 | this.info = { |
| 26 | thread: new ThreadModel(), |
| 27 | messages: [], |
| 28 | id: -1, |
| 29 | timestamp: 0, |
| 30 | }; |
| 31 | } |
Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 32 | |
Adrià Vilanova Martínez | 4b2582d | 2023-11-16 01:56:04 +0100 | [diff] [blame^] | 33 | 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ínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 57 | const currentPage = this.parseThreadUrl(); |
| 58 | const isCurrentThread = |
| 59 | Date.now() - this.info.timestamp < kCurrentInfoExpiresInMs && |
Adrià Vilanova Martínez | 4b2582d | 2023-11-16 01:56:04 +0100 | [diff] [blame^] | 60 | this.info.thread.getId() == currentPage.thread && |
| 61 | this.info.thread.getForumId() == currentPage.forum; |
Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 62 | |
| 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ínez | 4b2582d | 2023-11-16 01:56:04 +0100 | [diff] [blame^] | 80 | const message = MessageExtraInfoService.getMessageFromList( |
| 81 | messageId, this.info.messages); |
Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 82 | return message !== undefined; |
| 83 | } |
| 84 | } |