Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions dist/AdvancedFlagging.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,14 @@
method: "GET",
url,
timeout: 1500,
onload: ({ responseText }) => {
onload: ({ responseText, status, statusText }) => {
if (status !== 200) {
reject(`Bad response from ${this.server}.
Status: ${status} ${statusText}.
Response was:
${responseText}`);
return;
}
const response = JSON.parse(responseText);
if (response.status === "failure") return;
response.posts.forEach((item) => {
Expand Down Expand Up @@ -1678,18 +1685,31 @@
chat = new ChatApi();
feedbackMessage;
reportMessage;
static getAllNattyIds(ids) {
static async getAllNattyIds(ids) {
const postIds2 = (ids ?? page.getAllPostIds(false, false)).join(",");
if (!Page.isStackOverflow || !postIds2) return Promise.resolve();
if (!Page.isStackOverflow || !postIds2) return;
try {
this.nattyIds = await this.requestNattyIds(postIds2);
} catch (error) {
displayToaster("Could not connect to Natty.", "danger");
console.error(error);
}
}
static requestNattyIds(postIds2) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: `${nattyFeedbackUrl}${postIds2}`,
onload: ({ status, responseText }) => {
if (status !== 200) reject();
onload: ({ status, responseText, statusText }) => {
if (status !== 200) {
reject(`Bad response from ${nattyFeedbackUrl}.
Status: ${status} ${statusText}.
Response was:
${responseText}`);
return;
}
const result = JSON.parse(responseText);
this.nattyIds = result.items.map(({ name }) => Number(name));
resolve();
resolve(result.items.map(({ name }) => Number(name)));
},
onerror: () => reject()
});
Expand Down
7 changes: 6 additions & 1 deletion src/UserscriptTools/CopyPastorAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export class CopyPastorAPI extends Reporter {
method: 'GET',
url,
timeout: 1500,
onload: ({ responseText }) => {
onload: ({ responseText, status, statusText }) => {
if (status !== 200) {
reject(`Bad response from ${this.server}.\nStatus: ${status} ${statusText}.\nResponse was:\n${responseText}`);
return;
}

const response = JSON.parse(responseText) as CopyPastorFindTargetResponse;

if (response.status === 'failure') return;
Expand Down
27 changes: 19 additions & 8 deletions src/UserscriptTools/NattyApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChatApi } from './ChatApi';
import { AllFeedbacks } from '../shared';
import { page } from '../AdvancedFlagging';
import { displayToaster, page } from '../AdvancedFlagging';

import WebsocketUtils from './WebsocketUtils';
import Reporter from './Reporter';
Expand Down Expand Up @@ -41,22 +41,33 @@ export class NattyAPI extends Reporter {
this.reportMessage = `@Natty report https://stackoverflow.com/a/${this.id}`;
}

public static getAllNattyIds(ids?: number[]): Promise<void> {
public static async getAllNattyIds(ids?: number[]): Promise<void> {
const postIds = (ids ?? page.getAllPostIds(false, false)).join(',');

if (!Page.isStackOverflow || !postIds) return Promise.resolve();
if (!Page.isStackOverflow || !postIds) return;

return new Promise<void>((resolve, reject) => {
try {
this.nattyIds = await this.requestNattyIds(postIds);
} catch (error) {
displayToaster('Could not connect to Natty.', 'danger');
console.error(error);
}
}

private static requestNattyIds(postIds: string): Promise<number[]> {
return new Promise<number[]>((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: `${nattyFeedbackUrl}${postIds}`,
onload: ({ status, responseText }) => {
if (status !== 200) reject();
onload: ({ status, responseText, statusText }) => {
if (status !== 200) {
reject(`Bad response from ${nattyFeedbackUrl}.\nStatus: ${status} ${statusText}.\nResponse was:\n${responseText}`);
return;
}

const result = JSON.parse(responseText) as NattyFeedback;
this.nattyIds = result.items.map(({ name }) => Number(name));

resolve();
resolve(result.items.map(({ name }) => Number(name)));
},
onerror: () => reject()
});
Expand Down