Skip to content

Commit 668bcdb

Browse files
committed
feat: add 'linkType' property to new mirrors
This will allow download-only services to integrate with the mirror bot
1 parent 661a68c commit 668bcdb

7 files changed

Lines changed: 81 additions & 20 deletions

File tree

src/controllers/admin/mirroredvideos.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ router.get("/get", async (req: Request, res: Response) => {
1010
authorized(req, res, async () => {
1111
let reqData = req.body.data;
1212
let reqUrl = reqData.reqUrl;
13-
let mirroredVideo;
13+
let mirroredVideo: AvailableMirror;
1414

1515
try {
1616
mirroredVideo = await AvailableMirror.findOne({ mirrorUrl: reqUrl });
@@ -32,8 +32,9 @@ router.get("/get", async (req: Request, res: Response) => {
3232
message: `OK`,
3333
data: {
3434
id: mirroredVideo.id,
35-
url: mirroredVideo.url,
35+
url: mirroredVideo.mirrorUrl,
3636
botUsername: mirroredVideo.bot.username,
37+
linkType: mirroredVideo.linkType,
3738
createdAt: mirroredVideo.createdAt,
3839
updatedAt: mirroredVideo.updatedAt,
3940
},
@@ -76,6 +77,7 @@ router.get("/getall", async (req: Request, res: Response) => {
7677
redditPostId: mirroredVideo.redditPostId,
7778
mirrorUrl: mirroredVideo.mirrorUrl,
7879
botUsername: mirroredVideo.bot.username,
80+
linkType: mirroredVideo.linkType,
7981
createdAt: mirroredVideo.createdAt,
8082
updatedAt: mirroredVideo.updatedAt,
8183
});

src/controllers/bot/mirroredvideos.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { redditapi } from "../../services/redditapi";
77
import {
88
CreateMirrorRequest,
99
DeleteRequest,
10+
LinkType,
1011
UpdateRequest,
1112
} from "../../structures";
1213
import { hasAuthHeaders, isAuthorized, isValidRequest } from "./api";
@@ -25,6 +26,7 @@ async function createVideo(data: CreateMirrorRequest) {
2526
newMirroredVideo.redditPostId = data.redditPostId;
2627
newMirroredVideo.mirrorUrl = data.url;
2728
newMirroredVideo.bot = data.bot;
29+
newMirroredVideo.linkType = data.linkType;
2830
await newMirroredVideo.save();
2931
}
3032

@@ -63,7 +65,11 @@ router.all("/*", async (req, res, next) => {
6365
router.post("/update", async (req, res) => {
6466
const bot = res.locals.bot as RegisteredBot;
6567
const data = req.body.data as UpdateRequest;
66-
const [redditPostId, url] = [data.redditPostId, data.url];
68+
const [redditPostId, url, linkType] = [
69+
data.redditPostId,
70+
data.url,
71+
data.linkType ?? LinkType.Mirror,
72+
];
6773

6874
let commentReply: CommentReply;
6975

@@ -146,10 +152,11 @@ router.post("/update", async (req, res) => {
146152
});
147153
} else {
148154
try {
149-
createVideo({
150-
redditPostId: redditPostId,
151-
url: url,
152-
bot: bot,
155+
await createVideo({
156+
redditPostId,
157+
url,
158+
bot,
159+
linkType,
153160
});
154161
} catch (err) {
155162
req.log.error(err);

src/controllers/cron/commentreply.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { response } from "..";
99
import * as configuration from "../../configuration";
1010
import { AvailableMirror, CommentReply } from "../../entity";
1111
import { redditapi } from "../../services";
12-
import { CommentReplyStatus } from "../../structures";
12+
import { CommentReplyStatus, LinkType } from "../../structures";
1313

1414
const router: Router = Router();
1515

@@ -47,25 +47,58 @@ async function hasStickiedReplies(submissionId: string) {
4747
return false;
4848
}
4949

50+
const generateFormattedDownloads = (downloads: AvailableMirror[]) => {
51+
const formattedDownloads = [];
52+
53+
downloads.forEach((download) => {
54+
const [mirrorUrl, botUsername] = [
55+
download.mirrorUrl,
56+
download.bot.username,
57+
];
58+
59+
formattedDownloads.push();
60+
});
61+
};
62+
5063
/**
5164
* Generates a formatted comment reply string containing all available mirrors
5265
* @param mirrors An array of AvailableMirror objects
5366
*/
54-
function generateFormattedMirrors(mirrors: AvailableMirror[]): string {
55-
let formattedMirrors: string[] = [];
56-
57-
for (let i = 0; i < mirrors.length; i++) {
58-
const mirror = mirrors[i];
67+
function generateFormattedMirrors(allMirrors: AvailableMirror[]): string {
68+
const [mirrors, downloads] = [
69+
allMirrors.filter((obj) => obj.linkType === LinkType.Mirror),
70+
allMirrors.filter((obj) => obj.linkType === LinkType.Download),
71+
];
72+
const hasMirrorsAndLinks = mirrors.length > 0 && downloads.length > 0;
73+
const formattedLinks: string[] = [];
74+
75+
const appendAll = (links: AvailableMirror[]) =>
76+
links.forEach((link, idx) => {
77+
const prefix =
78+
link.linkType === null || link.linkType === LinkType.Mirror
79+
? "Mirror"
80+
: "Download";
81+
const [mirrorUrl, botUsername] = [link.mirrorUrl, link.bot.username];
82+
83+
formattedLinks.push(
84+
`* [${prefix} #${
85+
idx + 1
86+
}](${mirrorUrl}) (provided by /u/${botUsername})`
87+
);
88+
});
5989

60-
let mirrorUrl = mirror.mirrorUrl;
61-
let botUsername = mirror.bot.username;
90+
if (mirrors.length > 0) {
91+
formattedLinks.push(`**Mirrors**`, `\n`);
92+
appendAll(mirrors);
93+
}
6294

63-
formattedMirrors.push(
64-
`* [Mirror #${i + 1}](${mirrorUrl}) (provided by /u/${botUsername})`
65-
);
95+
if (downloads.length > 0) {
96+
if (hasMirrorsAndLinks) formattedLinks.push("\n");
97+
formattedLinks.push(`**Downloads**`, `\n`);
98+
appendAll(downloads);
6699
}
67100

68-
return TEMPLATE_COMMENTREPLY.replace("%s", formattedMirrors.join("\n"));
101+
return TEMPLATE_COMMENTREPLY.replace("%s", formattedLinks.join("\n"));
69102
}
70103

71104
/**

src/entity/availablemirror.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
PrimaryGeneratedColumn,
1212
UpdateDateColumn,
1313
} from "typeorm";
14-
import { CommentReplyStatus } from "../structures";
14+
import { CommentReplyStatus, LinkType } from "../structures";
1515
import { CommentReply } from "./commentreply";
1616
import { RegisteredBot } from "./registeredbot";
1717

@@ -35,6 +35,11 @@ export class AvailableMirror extends BaseEntity {
3535
})
3636
bot: RegisteredBot;
3737

38+
@Column({
39+
default: LinkType.Mirror,
40+
})
41+
linkType: LinkType;
42+
3843
@CreateDateColumn()
3944
createdAt: Date;
4045

src/structures/api.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RegisteredBot } from "../entity";
2+
import { LinkType } from "./availablemirror";
23

34
/** Data structure for API responses */
45
export interface ResponseData {
@@ -21,6 +22,9 @@ export interface CreateMirrorRequest {
2122

2223
/** The bot hosting the mirror */
2324
bot: RegisteredBot;
25+
26+
/** The type of link */
27+
linkType: LinkType;
2428
}
2529

2630
/** Data structure for video API requests */
@@ -30,6 +34,9 @@ export interface ApiRequest {
3034

3135
/** The URL to the mirrored video */
3236
url: string;
37+
38+
/** The type of link */
39+
linkType?: LinkType;
3340
}
3441

3542
/** Data structure for delete requests */

src/structures/availablemirror.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export enum LinkType {
2+
/** Indicates this link is for a mirror */
3+
Mirror = 0,
4+
/** Indicates this link is for a download */
5+
Download = 1,
6+
}

src/structures/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./api";
2+
export * from "./availablemirror";
23
export * from "./commentreply";

0 commit comments

Comments
 (0)