Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 1 | import {waitFor} from 'poll-until-promise'; |
| 2 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 3 | import {CCApi} from '../../common/api.js'; |
| 4 | import {parseUrl} from '../../common/commonUtils.js'; |
| 5 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 6 | import AvatarsDB from './utils/AvatarsDB.js' |
| 7 | |
| 8 | export default class AvatarsHandler { |
| 9 | constructor() { |
| 10 | this.isFilterSetUp = false; |
| 11 | this.privateForums = []; |
| 12 | this.db = new AvatarsDB(); |
| 13 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 14 | |
| 15 | // Gets a list of private forums. If it is already cached, the cached list is |
| 16 | // returned; otherwise it is also computed and cached. |
| 17 | getPrivateForums() { |
| 18 | return new Promise((resolve, reject) => { |
| 19 | if (this.isFilterSetUp) return resolve(this.privateForums); |
| 20 | |
| 21 | if (!document.documentElement.hasAttribute('data-startup')) |
| 22 | return reject('[threadListAvatars] Couldn\'t get startup data.'); |
| 23 | |
| 24 | var startupData = |
| 25 | JSON.parse(document.documentElement.getAttribute('data-startup')); |
| 26 | var forums = startupData?.['1']?.['2']; |
| 27 | if (forums === undefined) |
| 28 | return reject( |
| 29 | '[threadListAvatars] Couldn\'t retrieve forums from startup data.'); |
| 30 | |
| 31 | for (var f of forums) { |
| 32 | var forumId = f?.['2']?.['1']?.['1']; |
| 33 | var forumVisibility = f?.['2']?.['18']; |
| 34 | if (forumId === undefined || forumVisibility === undefined) { |
| 35 | console.warn( |
| 36 | '[threadListAvatars] Coudln\'t retrieve forum id and/or forum visibility for the following forum:', |
| 37 | f); |
| 38 | continue; |
| 39 | } |
| 40 | |
| 41 | // forumVisibility's value 1 means "PUBLIC". |
| 42 | if (forumVisibility != 1) this.privateForums.push(forumId); |
| 43 | } |
| 44 | |
| 45 | // Forum 51488989 is marked as public but it is in fact private. |
| 46 | this.privateForums.push('51488989'); |
| 47 | |
| 48 | this.isFilterSetUp = true; |
| 49 | return resolve(this.privateForums); |
| 50 | }); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 51 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 52 | |
| 53 | // Some threads belong to private forums, and this feature will not be able to |
| 54 | // get its avatars since it makes an anonymomus call to get the contents of |
| 55 | // the thread. |
| 56 | // |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 57 | // This function returns whether the thread belongs to a known private forum. |
| 58 | isPrivateThread(thread) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 59 | return this.getPrivateForums().then(privateForums => { |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 60 | if (privateForums.includes(thread.forum)) return true; |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 61 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 62 | return this.db.isForumUnauthorized(thread.forum); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 63 | }); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 64 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 65 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 66 | // Get an object with the author of the thread, an array of the first |num| |
| 67 | // replies from the thread |thread|, and additional information about the |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 68 | // thread. It also returns whether the thread is private, in which case the |
| 69 | // previous properties will be missing. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 70 | getFirstMessages(thread, num = 15) { |
| 71 | return CCApi( |
| 72 | 'ViewThread', { |
| 73 | 1: thread.forum, |
| 74 | 2: thread.thread, |
| 75 | // options |
| 76 | 3: { |
| 77 | // pagination |
| 78 | 1: { |
| 79 | 2: num, // maxNum |
| 80 | }, |
| 81 | 3: true, // withMessages |
| 82 | 5: true, // withUserProfile |
| 83 | 10: false, // withPromotedMessages |
| 84 | 16: false, // withThreadNotes |
| 85 | 18: true, // sendNewThreadIfMoved |
| 86 | } |
| 87 | }, |
| 88 | // |authentication| is false because otherwise this would mark |
| 89 | // the thread as read as a side effect, and that would mark all |
| 90 | // threads in the list as read. |
| 91 | // |
| 92 | // Due to the fact that we have to call this endpoint |
| 93 | // anonymously, this means we can't retrieve information about |
| 94 | // threads in private forums. |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 95 | /* authentication = */ false, /* authuser = */ 0, |
| 96 | /* returnUnauthorizedStatus = */ true) |
| 97 | .then(response => { |
| 98 | if (response.unauthorized) |
| 99 | return this.db.putUnauthorizedForum(thread.forum).then(() => { |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 100 | return { |
| 101 | isPrivate: true, |
| 102 | }; |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 103 | }); |
| 104 | |
| 105 | var data = response.body; |
| 106 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 107 | var numMessages = data?.['1']?.['8']; |
| 108 | if (numMessages === undefined) |
| 109 | throw new Error( |
| 110 | 'Request to view thread doesn\'t include the number of messages'); |
| 111 | |
| 112 | var messages = numMessages == 0 ? [] : data?.['1']['3']; |
| 113 | if (messages === undefined) |
| 114 | throw new Error( |
| 115 | 'numMessages was ' + numMessages + |
| 116 | ' but the response didn\'t include any message.'); |
| 117 | |
| 118 | var author = data?.['1']?.['4']; |
| 119 | if (author === undefined) |
| 120 | throw new Error( |
| 121 | 'Author isn\'t included in the ViewThread response.'); |
| 122 | |
| 123 | return { |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 124 | isPrivate: false, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 125 | messages, |
| 126 | author, |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 127 | |
| 128 | // The following fields are useful for the cache and can be |
| 129 | // undefined, but this is checked before adding an entry to the |
| 130 | // cache. |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 131 | lastMessageId: data?.['1']?.['2']?.['10'], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 132 | }; |
| 133 | }); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 136 | // Get the following data: |
| 137 | // - |isPrivate|: whether the thread is private. |
| 138 | // - |avatars|: a list of at most |num| avatars for thread |thread| by calling |
| 139 | // the API, if |isPrivate| is false. |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 140 | getVisibleAvatarsFromServer(thread, num) { |
| 141 | return this.getFirstMessages(thread).then(result => { |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 142 | if (result.isPrivate) |
| 143 | return { |
| 144 | isPrivate: true, |
| 145 | }; |
| 146 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 147 | var messages = result.messages; |
| 148 | var author = result.author; |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 149 | var lastMessageId = result.lastMessageId; |
| 150 | |
| 151 | var avatarUrls = []; |
| 152 | |
| 153 | var authorUrl = author?.['1']?.['2']; |
| 154 | if (authorUrl !== undefined) avatarUrls.push(authorUrl); |
| 155 | |
| 156 | for (var m of messages) { |
| 157 | var url = m?.['3']?.['1']?.['2']; |
| 158 | |
| 159 | if (url === undefined) continue; |
| 160 | if (!avatarUrls.includes(url)) avatarUrls.push(url); |
| 161 | if (avatarUrls.length == 3) break; |
| 162 | } |
| 163 | |
| 164 | // Add entry to cache if all the extra metadata could be retrieved. |
Adrià Vilanova Martínez | ac9fc9e | 2021-07-22 12:45:32 +0200 | [diff] [blame] | 165 | if (lastMessageId !== undefined) |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 166 | this.db.putCacheEntry({ |
| 167 | threadId: thread.thread, |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 168 | lastMessageId, |
| 169 | avatarUrls, |
| 170 | num, |
| 171 | lastUsedTimestamp: Math.floor(Date.now() / 1000), |
| 172 | }); |
| 173 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 174 | return { |
| 175 | isPrivate: false, |
| 176 | avatars: avatarUrls, |
| 177 | }; |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 178 | }); |
| 179 | } |
| 180 | |
| 181 | // Returns an object with a cache entry that matches the request if found (via |
| 182 | // the |entry| property). The property |found| indicates whether the cache |
| 183 | // entry was found. |
Adrià Vilanova Martínez | 4cfa32f | 2021-07-22 17:29:03 +0200 | [diff] [blame] | 184 | // |
| 185 | // The |checkRecent| parameter is used to indicate whether lastUsedTimestamp |
| 186 | // must be within the last 30 seconds (which means that the thread has been |
| 187 | // checked for a potential invalidation). |
| 188 | getVisibleAvatarsFromCache(thread, num, checkRecent) { |
| 189 | return this.db.getCacheEntry(thread.thread).then(entry => { |
| 190 | if (entry === undefined || entry.num < num) |
| 191 | return { |
| 192 | found: false, |
| 193 | }; |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 194 | |
Adrià Vilanova Martínez | 4cfa32f | 2021-07-22 17:29:03 +0200 | [diff] [blame] | 195 | if (checkRecent) { |
| 196 | var now = Math.floor(Date.now() / 1000); |
| 197 | var diff = now - entry.lastUsedTimestamp; |
| 198 | if (diff > 30) |
| 199 | throw new Error( |
| 200 | 'lastUsedTimestamp isn\'t within the last 30 seconds (id: ' + |
| 201 | thread.thread + ' the difference is: ' + diff + ').'); |
| 202 | } |
| 203 | |
| 204 | return { |
| 205 | found: true, |
| 206 | entry, |
| 207 | }; |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | // Waits for the XHR interceptor to invalidate any outdated threads and |
| 212 | // returns what getVisibleAvatarsFromCache returns. If this times out, it |
| 213 | // returns the current cache entry anyways if it exists. |
| 214 | getVisibleAvatarsFromCacheAfterInvalidations(thread, num) { |
| 215 | return waitFor( |
| 216 | () => this.getVisibleAvatarsFromCache( |
| 217 | thread, num, /* checkRecent = */ true), |
| 218 | { |
| 219 | interval: 450, |
| 220 | timeout: 2 * 1000, |
| 221 | }) |
| 222 | .catch(err => { |
| 223 | console.debug( |
| 224 | '[threadListAvatars] Error while retrieving avatars from cache ' + |
| 225 | '(probably timed out waiting for lastUsedTimestamp to change):', |
| 226 | err); |
| 227 | |
| 228 | // Sometimes when going back to a thread list, the API call to load |
| 229 | // the thread list is not made, and so the previous piece of code |
| 230 | // times out waiting to intercept that API call and handle thread |
| 231 | // invalidations. |
| 232 | // |
| 233 | // If this is the case, this point will be reached. We'll assume we |
| 234 | // intercept all API calls, so reaching this point means that an API |
| 235 | // call wasn't made. Therefore, try again to get visible avatars from |
| 236 | // the cache without checking whether the entry has been checked for |
| 237 | // potential invalidation. |
| 238 | // |
| 239 | // See https://bugs.avm99963.com/p/twpowertools/issues/detail?id=10. |
| 240 | return this.getVisibleAvatarsFromCache( |
| 241 | thread, num, /* checkRecent = */ false); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 242 | }); |
| 243 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 244 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 245 | // Get an object with the following data: |
| 246 | // - |state|: 'ok' (the avatars list could be retrieved) or 'private' (the |
| 247 | // thread is private, so the avatars list could not be retrieved). |
| 248 | // - |avatars|: list of at most |num| avatars for thread |thread| |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 249 | getVisibleAvatars(thread, num = 3) { |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 250 | return this.isPrivateThread(thread).then(isPrivate => { |
| 251 | if (isPrivate) |
| 252 | return { |
| 253 | state: 'private', |
| 254 | avatars: [], |
| 255 | }; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 256 | |
Adrià Vilanova Martínez | 4cfa32f | 2021-07-22 17:29:03 +0200 | [diff] [blame] | 257 | return this.getVisibleAvatarsFromCacheAfterInvalidations(thread, num) |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 258 | .then(res => { |
Adrià Vilanova Martínez | 4cfa32f | 2021-07-22 17:29:03 +0200 | [diff] [blame] | 259 | if (!res.found) { |
| 260 | var err = new Error('Cache entry doesn\'t exist.'); |
| 261 | err.name = 'notCached'; |
| 262 | throw err; |
| 263 | } |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 264 | return { |
| 265 | state: 'ok', |
| 266 | avatars: res.entry.avatarUrls, |
| 267 | }; |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 268 | }) |
| 269 | .catch(err => { |
Adrià Vilanova Martínez | 4cfa32f | 2021-07-22 17:29:03 +0200 | [diff] [blame] | 270 | // If the name is "notCached", then this is not an actual error so |
| 271 | // don't log an error, but still get avatars from the server. |
| 272 | if (err?.name !== 'notCached') |
| 273 | console.error( |
| 274 | '[threadListAvatars] Error while accessing avatars cache:', |
| 275 | err); |
| 276 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 277 | return this.getVisibleAvatarsFromServer(thread, num).then(res => { |
| 278 | if (res.isPrivate) |
| 279 | return { |
| 280 | state: 'private', |
| 281 | avatars: [], |
| 282 | }; |
| 283 | |
| 284 | return { |
| 285 | state: 'ok', |
| 286 | avatars: res.avatars, |
| 287 | }; |
| 288 | }); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 289 | }); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 290 | }); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 291 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 292 | |
| 293 | // Inject avatars for thread summary (thread item) |node| in a thread list. |
| 294 | inject(node) { |
| 295 | var header = node.querySelector( |
| 296 | 'ec-thread-summary .main-header .panel-description a.header'); |
| 297 | if (header === null) { |
| 298 | console.error( |
| 299 | '[threadListAvatars] Header is not present in the thread item\'s DOM.'); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | var thread = parseUrl(header.href); |
| 304 | if (thread === false) { |
| 305 | console.error('[threadListAvatars] Thread\'s link cannot be parsed.'); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | this.getVisibleAvatars(thread) |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 310 | .then(res => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 311 | var avatarsContainer = document.createElement('div'); |
| 312 | avatarsContainer.classList.add('TWPT-avatars'); |
| 313 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 314 | var avatarUrls = res.avatars; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 315 | |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 316 | if (res.state == 'private') { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 317 | var avatar = document.createElement('div'); |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 318 | avatar.classList.add('TWPT-avatar-private-placeholder'); |
avm99963 | cb55248 | 2021-08-22 18:42:35 +0200 | [diff] [blame] | 319 | avatar.textContent = 'person_off'; |
| 320 | var label = chrome.i18n.getMessage( |
| 321 | 'inject_threadlistavatars_private_thread_indicator_label'); |
| 322 | avatar.setAttribute('title', label); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 323 | avatarsContainer.appendChild(avatar); |
Adrià Vilanova Martínez | 87110e9 | 2021-08-11 19:23:16 +0200 | [diff] [blame] | 324 | } else { |
| 325 | for (var i = 0; i < avatarUrls.length; ++i) { |
| 326 | var avatar = document.createElement('div'); |
| 327 | avatar.classList.add('TWPT-avatar'); |
| 328 | avatar.style.backgroundImage = 'url(\'' + avatarUrls[i] + '\')'; |
| 329 | avatarsContainer.appendChild(avatar); |
| 330 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | header.appendChild(avatarsContainer); |
| 334 | }) |
| 335 | .catch(err => { |
| 336 | console.error( |
| 337 | '[threadListAvatars] Could not retrieve avatars for thread', |
| 338 | thread, err); |
| 339 | }); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 340 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 341 | }; |