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