10000 refactor(UserPasteStore): move to typescript · cAttte/pastebin.js@e649aab · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit e649aab

Browse files
committed
refactor(UserPasteStore): move to typescript
1 parent 06c3888 commit e649aab

File tree

2 files changed

+82
-74
lines changed

2 files changed

+82
-74
lines changed

src/struct/stores/UserPasteStore.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/struct/stores/UserPasteStore.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { parseStringPromise } from "xml2js"
2+
3+
import PastebinClient from "../PastebinClient"
4+
import PastebinError from "../PastebinError"
5+
import Paste from "../Paste"
6+
import User from "../User"
7+
8+
/**
9+
* A structure that holds all of a user's cached pastes.
10+
*/
11+
module.exports = class UserPasteStore extends Map {
12+
/**
13+
* The client this store belongs to.
14+
*/
15+
client: PastebinClient
16+
/**
17+
* The user this store belongs to.
18+
*/
19+
user: User
20+
21+
/**
22+
* @param client The client the store belongs to
23+
* @param user The user the store belongs to
24+
* @param entries The entries to populate the store with
25+
*/
26+
constructor(
27+
client: PastebinClient,
28+
user: User,
29+
entries: Array<[string, Paste]> = []
30+
) {
31+
super()
32+
this.client = client
33+
this.user = user
34+
for (const [k, v] of entries) this.set(k, v)
35+
}
36+
37+
/**
38+
* Fetch this user's pastes, and store them in the cache.
39+
* @param max The maximum number of pastes to fetch
40+
*/
41+
async fetch(max: number = 50): Promise<UserPasteStore> {
42+
if (!this.client.credentials.apiKey)
43+
throw new PastebinError("API key is required to fetch a user.")
44+
if (!this.client.credentials.userKey)
45+
throw new PastebinError("User key is required to fetch a user.")
46+
47+
const body = await PastebinClient.post(PastebinClient.POST_URL, {
48+
api_dev_key: this.client.credentials.apiKey,
49+
api_user_key: this.client.credentials.userKey,
50+
api_results_limit: max,
51+
api_option: "list"
52+
})
53+
54+
if (body.toLowerCase().trim() === "no posts found.") return this
55+
const xmls = body.match(/<paste>[\s\S]*?<\/paste>/g) // uhh
56+
if (!xmls) return this
57+
58+
for (const xml of xmls) {
59+
const parsed = await parseStringPromise(xml)
60+
const paste = new Paste(this.client, {
61+
key: parsed.paste.paste_key[0],
62+
date: new Date(Number(parsed.paste.paste_date[0]) * 1000),
63+
title: parsed.paste.paste_title[0],
64+
author: this.user,
65+
size: Number(parsed.paste.paste_size[0]),
66+
expiryDate: new Date(Number(parsed.paste.paste_expire_date[0]) * 1000),
67+
privacy: Number(parsed.paste.paste_private[0]),
68+
format: parsed.paste.paste_format_short[0],
69+
hits: Number(parsed.paste.paste_hits[0])
70+
})
71+
this.set(paste.key, paste)
72+
}
73+
74+
return this
75+
}
76+
77+
set(key: string, paste: Paste) {
78+
this.client.pastes.set(key, paste)
79+
super.set(key, paste)
80+
return this
81+
}
82+
}

0 commit comments

Comments
 (0)
0