blob: c57378830ac81b9ccb2aee0d60e1fa9e2a3fb865 [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import {CCApi} from '../../common/api.js';
2import {getAuthUser} from '../../common/communityConsoleUtils.js';
3
4var authuser = getAuthUser();
5
6// Send a request to mark the current thread as read
7export function markCurrentThreadAsRead() {
8 console.debug(
9 '[forceMarkAsRead] %cTrying to mark a thread as read.',
10 'color: #1a73e8;');
11
12 var threadRegex =
13 /\/s\/community\/?.*\/forum\/([0-9]+)\/?.*\/thread\/([0-9]+)/;
14
15 var url = location.href;
16 var matches = url.match(threadRegex);
17 if (matches !== null && matches.length > 2) {
18 var forumId = matches[1];
19 var threadId = matches[2];
20
21 console.debug('[forceMarkAsRead] Thread details:', {forumId, threadId});
22
23 return CCApi(
24 'ViewThread', {
25 1: forumId,
26 2: threadId,
27 // options
28 3: {
29 // pagination
30 1: {
31 2: 0, // maxNum
32 },
33 3: false, // withMessages
34 5: false, // withUserProfile
35 6: true, // withUserReadState
36 9: false, // withRequestorProfile
37 10: false, // withPromotedMessages
38 11: false, // withExpertResponder
39 },
40 },
41 true, authuser)
42 .then(thread => {
43 if (thread?.[1]?.[6] === true) {
44 console.debug(
45 '[forceMarkAsRead] This thread is already marked as read, but marking it as read anyways.');
46 }
47
48 var lastMessageId = thread?.[1]?.[2]?.[10];
49
50 console.debug('[forceMarkAsRead] lastMessageId is:', lastMessageId);
51
52 if (lastMessageId === undefined)
53 throw new Error(
54 'Couldn\'t find lastMessageId in the ViewThread response.');
55
56 return CCApi(
57 'SetUserReadStateBulk', {
58 1: [{
59 1: forumId,
60 2: threadId,
61 3: lastMessageId,
62 }],
63 },
64 true, authuser);
65 })
66 .then(_ => {
67 console.debug(
68 '[forceMarkAsRead] %cSuccessfully set as read!',
69 'color: #1e8e3e;');
70 })
71 .catch(err => {
72 console.error(
73 '[forceMarkAsRead] Error while marking current thread as read: ',
74 err);
75 });
76 } else {
77 console.error(
78 '[forceMarkAsRead] Couldn\'t retrieve forumId and threadId from the current URL.',
79 url);
80 }
81}