Files
context-mod/src/Subreddit/ModNotes/ModUserNote.ts
FoxxMD c0d19ede39 feat(modnote): Improve handling of modnote data and implement caching
* Normalize (depopulate from snoowrap) mod note raw data so it can be constructed agnostic of source (cache or api)
* Implement cache GET for modnotes with default TTL of 60 seconds
* Refactor mod note action and implement cache PUT when new notes are added
2022-06-09 12:54:23 -04:00

49 lines
1.5 KiB
TypeScript

import {Comment, PrivateMessage, RedditUser, Submission} from "snoowrap/dist/objects";
import {ModUserNoteLabel} from "../../Common/Infrastructure/Atomic";
//import {ExtendedSnoowrap} from "../../Utils/SnoowrapClients";
import {generateSnoowrapEntityFromRedditThing, parseRedditFullname} from "../../util";
import Snoowrap from "snoowrap";
export interface ModUserNoteRaw {
note?: string | null
reddit_id?: string | null
label?: string | null
}
export class ModUserNote {
note?: string
actedOn?: RedditUser | Submission | Comment | PrivateMessage
label?: ModUserNoteLabel
constructor(data: ModUserNoteRaw | undefined, client: Snoowrap) {
const {
note,
reddit_id,
label
} = data || {};
this.note = note !== null ? note : undefined;
this.label = label !== null ? label as ModUserNoteLabel : undefined;
if (reddit_id !== null && reddit_id !== undefined) {
const thing = parseRedditFullname(reddit_id);
if (thing !== undefined) {
this.actedOn = generateSnoowrapEntityFromRedditThing(thing, client) as RedditUser | Submission | Comment;
}
}
}
toRaw(): ModUserNoteRaw {
return {
note: this.note,
reddit_id: this.actedOn !== undefined ? this.actedOn.id : undefined,
label: this.label
}
}
toJSON() {
return this.toRaw();
}
}
export default ModUserNote;