Skip to content

Commit

Permalink
Simplify code part I
Browse files Browse the repository at this point in the history
Uses classes in an effort to make code more readable
and organize things.
While the code compiles, it has not been tested and very likely doesn't work.
Fixes for the bugs that may occur will be pushed in the upcoming days
  • Loading branch information
double-beep authored May 24, 2024
1 parent 3fec67c commit c64a7ec
Show file tree
Hide file tree
Showing 19 changed files with 1,260 additions and 1,449 deletions.
444 changes: 31 additions & 413 deletions src/AdvancedFlagging.ts

Large diffs are not rendered by default.

34 changes: 15 additions & 19 deletions src/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { flagCategories } from './FlagTypes';
import { Store } from './UserscriptTools/Store';
import { CachedFlag, updateFlagTypes } from './shared';
import { Flags } from './FlagTypes';

import {
Store,
Cached,
FlagNames,
cachedFlagTypes,
CachedCategory,
cachedCategories,
cachedConfiguration,
displayStacksToast
} from './shared';
CachedFlag
} from './UserscriptTools/Store';
import { Flags } from './FlagTypes';

import { FlagNames, displayStacksToast } from './shared';

import { buildConfigurationOverlay } from './modals/config';
import { setupCommentsAndFlagsModal } from './modals/comments/main';
Expand Down Expand Up @@ -52,7 +48,7 @@ export function cacheFlags(): void {
Store.set<CachedFlag[]>(Cached.FlagTypes, flagTypesToCache);

// also update the variable to prevent breaking the config modal
cachedFlagTypes.push(...flagTypesToCache);
Store.flagTypes.push(...flagTypesToCache);
}

function cacheCategories(): void {
Expand All @@ -68,33 +64,33 @@ function cacheCategories(): void {

Store.set<CachedCategory[]>(Cached.FlagCategories, categories);

cachedCategories.push(...categories);
Store.categories.push(...categories);
}

function setupDefaults(): void {
// if there's no downvote property, then the user
// is probably using an older version of AF
// clear and re-save
if (!cachedFlagTypes.length
|| !('downvote' in cachedFlagTypes[0])) {
if (!Store.flagTypes.length
|| !('downvote' in Store.flagTypes[0])) {
cacheFlags();
}

if (!cachedCategories.length
|| !('appliesTo' in cachedCategories[0])) {
if (!Store.categories.length
|| !('appliesTo' in Store.categories[0])) {
cacheCategories();
}

// PostOther can be replaced with PlagiarizedContent
// for "Plagiarism" flag type.
cachedFlagTypes.forEach(cachedFlag => {
Store.flagTypes.forEach(cachedFlag => {
// Plagiarism and Bad Attribution flag types,
// filter by id because names can be edited by the user
if (cachedFlag.id !== 3 && cachedFlag.id !== 5) return;

cachedFlag.reportType = FlagNames.Plagiarism;
});
updateFlagTypes();
Store.updateFlagTypes();
}

export function setupConfiguration(): void {
Expand Down Expand Up @@ -133,7 +129,7 @@ export function setupConfiguration(): void {
// or is using an older version of AF,
// prompt to submit
const propertyDoesNotExist = !Object.prototype.hasOwnProperty.call(
cachedConfiguration,
Store.config,
Cached.Configuration.addAuthorName
);

Expand Down
6 changes: 3 additions & 3 deletions src/UserscriptTools/ChatApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store } from './Store';
import { Cached, getSentMessage, debugMode } from '../shared';
import { Store, Cached } from './Store';
import { getSentMessage } from '../shared';

export class ChatApi {
private static getExpiryDate(): Date {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ChatApi {
private async sendRequestToChat(message: string, roomId: number): Promise<boolean> {
const url = `${this.chatRoomUrl}/chats/${roomId}/messages/new`;

if (debugMode) {
if (Store.dryRun) {
console.log('Send', message, `to ${roomId} via`, url);

return Promise.resolve(true);
Expand Down
42 changes: 28 additions & 14 deletions src/UserscriptTools/CopyPastorAPI.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ChatApi } from './ChatApi';
import {
isStackOverflow,
username,
getSentMessage,
FlagTypeFeedbacks,
debugMode
AllFeedbacks
} from '../shared';
import { getAllPostIds } from './sotools';
import { createBotIcon, displayToaster } from '../AdvancedFlagging';
import { displayToaster, page } from '../AdvancedFlagging';
import { Store } from './Store';
import Reporter from './Reporter';
import Page from './Page';

interface CopyPastorFindTargetResponseItem {
post_id: string;
Expand Down Expand Up @@ -35,23 +36,24 @@ interface CopyPastorData {
const copypastorServer = 'https://copypastor.sobotics.org';
const copypastorKey = 'wgixsmuiz8q8px9kyxgwf8l71h7a41uugfh5rkyj';

export class CopyPastorAPI {
export class CopyPastorAPI extends Reporter {
private static copypastorIds: Partial<CopyPastorData> = {};

public name: keyof FlagTypeFeedbacks = 'Guttenberg';
public copypastorId: number;
public repost: boolean;
public targetUrl: string;
public icon?: HTMLDivElement;

constructor(
private readonly answerId: number,
id: number,
) {
super('Guttenberg', id);

const {
copypastorId = 0,
repost = false,
target_url: targetUrl = ''
} = CopyPastorAPI.copypastorIds[this.answerId] || {};
} = CopyPastorAPI.copypastorIds[this.id] || {};

this.copypastorId = copypastorId;
this.repost = repost;
Expand All @@ -61,9 +63,9 @@ export class CopyPastorAPI {
}

public static async getAllCopyPastorIds(): Promise<void> {
if (!isStackOverflow) return;
if (!Page.isStackOverflow) return;

const postUrls = getAllPostIds(false, true); // postIds as URLs excluding questions
const postUrls = page.getAllPostIds(false, true); // postIds as URLs excluding questions

if (!postUrls.length) return; // make sure the array isn't empty

Expand Down Expand Up @@ -113,7 +115,7 @@ export class CopyPastorAPI {
});
}

public sendFeedback(feedback: string): Promise<string> {
public override sendFeedback(feedback: string): Promise<string> {
const chatId = new ChatApi().getChatUserId();

if (!this.copypastorId) {
Expand All @@ -138,7 +140,7 @@ export class CopyPastorAPI {
return new Promise<string>((resolve, reject) => {
const url = `${copypastorServer}/feedback/create`;

if (debugMode) {
if (Store.dryRun) {
console.log('Feedback to Guttenberg via', url, data);

reject('Didn\'t send feedback: debug mode');
Expand All @@ -161,11 +163,23 @@ export class CopyPastorAPI {
});
}

public override canBeReported(): boolean {
return false;
}

public override wasReported(): boolean {
// post was reported if copypastorId is not falsy
return Boolean(this.copypastorId);
}

public override canSendFeedback(feedback: AllFeedbacks): boolean {
return this.wasReported() && Boolean(feedback);
}

private getIcon(): HTMLDivElement | undefined {
if (!this.copypastorId) return;

const icon = createBotIcon(
'Guttenberg',
const icon = this.createBotIcon(
`${copypastorServer}/posts/${this.copypastorId}`
);

Expand Down
40 changes: 26 additions & 14 deletions src/UserscriptTools/GenericBotAPI.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import {
isStackOverflow,
username,
FlagTypeFeedbacks,
debugMode
} from '../shared';
import { username, FlagTypeFeedbacks, AllFeedbacks } from '../shared';
import Page from './Page';
import Reporter from './Reporter';
import { Store } from './Store';

const genericBotKey = 'Cm45BSrt51FR3ju';
const genericBotSuccess = 'Post tracked with Generic Bot';
const genericBotFailure = 'Server refused to track the post';

export class GenericBotAPI {
private readonly answerId: number;
export class GenericBotAPI extends Reporter {
public name: keyof FlagTypeFeedbacks = 'Generic Bot';

constructor(answerId: number) {
this.answerId = answerId;
private readonly deleted: boolean;

constructor(id: number, deleted: boolean) {
super('Generic Bot', id);

this.deleted = deleted;
}

// TODO create icon

// Ask Floern what this does
// https://github.com/SOBotics/Userscripts/blob/master/GenericBot/flagtracker.user.js#L32-L40
private computeContentHash(postContent: string): number {
Expand All @@ -34,18 +37,18 @@ export class GenericBotAPI {
public sendFeedback(trackPost: string): Promise<string> {
const flaggerName = encodeURIComponent(username || '');

if (!trackPost || !isStackOverflow || !flaggerName) {
if (!trackPost || !Page.isStackOverflow || !flaggerName) {
return Promise.resolve('');
}

const answer = document.querySelector(`#answer-${this.answerId} .js-post-body`);
const answer = document.querySelector(`#answer-${this.id} .js-post-body`);
const answerBody = answer?.innerHTML.trim() || '';
const contentHash = this.computeContentHash(answerBody);

const url = 'https://so.floern.com/api/trackpost.php';
const payload = {
key: genericBotKey,
postId: this.answerId,
postId: this.id,
contentHash,
flagger: flaggerName,
};
Expand All @@ -54,7 +57,7 @@ export class GenericBotAPI {
.map(item => item.join('='))
.join('&');

if (debugMode) {
if (Store.dryRun) {
console.log('Track post via', url, payload);

return Promise.resolve('');
Expand All @@ -80,4 +83,13 @@ export class GenericBotAPI {
});
});
}

public override showOnPopover(): boolean {
// Generic Bot only works on SO
return Page.isStackOverflow;
}

public override canSendFeedback(feedback: AllFeedbacks): boolean {
return feedback === 'track' && !this.deleted && Page.isStackOverflow;
}
}
Loading

0 comments on commit c64a7ec

Please sign in to comment.