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 { |
| 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 | } |