Skip to content
45 changes: 36 additions & 9 deletions src/Common/Entities/Activity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {Entity, Column, ManyToOne, PrimaryColumn, OneToMany, Index} from "typeorm";
import {Entity, Column, ManyToOne, PrimaryColumn, OneToMany, Index, DataSource, JoinColumn} from "typeorm";
import {AuthorEntity} from "./AuthorEntity";
import {Subreddit} from "./Subreddit";
import {CMEvent} from "./CMEvent";
import {asComment, getActivityAuthorName, parseRedditFullname, redditThingTypeToPrefix} from "../../util";
import {activityReports, ActivityType, Report, SnoowrapActivity} from "../Infrastructure/Reddit";
import {ActivityReport} from "./ActivityReport";
import dayjs, {Dayjs} from "dayjs";
import {ExtendedSnoowrap} from "../../Utils/SnoowrapClients";
import {Comment, Submission} from 'snoowrap/dist/objects';

export interface ActivityEntityOptions {
id: string
Expand Down Expand Up @@ -45,7 +47,7 @@ export class Activity {
@Column({name: 'name'})
name!: string;

@ManyToOne(type => Subreddit, sub => sub.activities, {cascade: ['insert']})
@ManyToOne(type => Subreddit, sub => sub.activities, {cascade: ['insert'], eager: true})
subreddit!: Subreddit;

@Column("varchar", {length: 20})
Expand All @@ -58,17 +60,18 @@ export class Activity {
@Column("text")
permalink!: string;

@ManyToOne(type => AuthorEntity, author => author.activities, {cascade: ['insert']})
@ManyToOne(type => AuthorEntity, author => author.activities, {cascade: ['insert'], eager: true})
author!: AuthorEntity;

@OneToMany(type => CMEvent, act => act.activity) // note: we will create author property in the Photo class below
@OneToMany(type => CMEvent, act => act.activity)
actionedEvents!: CMEvent[]

@ManyToOne(type => Activity, obj => obj.comments, {nullable: true})
@ManyToOne('Activity', 'comments', {nullable: true, cascade: ['insert']})
@JoinColumn({name: 'submission_id'})
submission?: Activity;

@OneToMany(type => Activity, obj => obj.submission, {nullable: true})
comments!: Activity[];
@OneToMany('Activity', 'submission', {nullable: true})
comments?: Activity[];

@OneToMany(type => ActivityReport, act => act.activity, {cascade: ['insert'], eager: true})
reports: ActivityReport[] | undefined
Expand Down Expand Up @@ -151,10 +154,12 @@ export class Activity {
return false;
}

static fromSnoowrapActivity(subreddit: Subreddit, activity: SnoowrapActivity, lastKnownStateTimestamp?: dayjs.Dayjs | undefined) {
static async fromSnoowrapActivity(activity: SnoowrapActivity, options: fromSnoowrapOptions | undefined = {}) {

let submission: Activity | undefined;
let type: ActivityType = 'submission';
let content: string;
const subreddit = await Subreddit.fromSnoowrap(activity.subreddit, options?.db);
if(asComment(activity)) {
type = 'comment';
content = activity.body;
Expand All @@ -179,8 +184,30 @@ export class Activity {
submission
});

entity.syncReports(activity, lastKnownStateTimestamp);
entity.syncReports(activity, options.lastKnownStateTimestamp);

return entity;
}

toSnoowrap(client: ExtendedSnoowrap): SnoowrapActivity {
let act: SnoowrapActivity;
if(this.type === 'submission') {
act = new Submission({name: this.id, id: this.name}, client, false);
act.title = this.content;
} else {
act = new Comment({name: this.id, id: this.name}, client, false);
act.link_id = this.submission?.id as string;
act.body = this.content;
}
act.permalink = this.permalink;
act.subreddit = this.subreddit.toSnoowrap(client);
act.author = this.author.toSnoowrap(client);

return act;
}
}

export interface fromSnoowrapOptions {
lastKnownStateTimestamp?: dayjs.Dayjs | undefined
db?: DataSource
}
9 changes: 8 additions & 1 deletion src/Common/Entities/AuthorEntity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {Entity, Column, PrimaryColumn, OneToMany} from "typeorm";
import {Activity} from "./Activity";
import {ExtendedSnoowrap} from "../../Utils/SnoowrapClients";
import {SnoowrapActivity} from "../Infrastructure/Reddit";
import {RedditUser} from "snoowrap/dist/objects";

@Entity({name: 'Author'})
export class AuthorEntity {
Expand All @@ -11,11 +14,15 @@ export class AuthorEntity {
name!: string;

@OneToMany(type => Activity, act => act.author)
activities!: Activity[]
activities!: Promise<Activity[]>

constructor(data?: any) {
if(data !== undefined) {
this.name = data.name;
}
}

toSnoowrap(client: ExtendedSnoowrap): RedditUser {
return new RedditUser({name: this.name, id: this.id}, client, false);
}
}
39 changes: 15 additions & 24 deletions src/Common/Entities/DispatchedEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ManyToOne,
PrimaryColumn,
BeforeInsert,
AfterLoad
AfterLoad, JoinColumn
} from "typeorm";
import {
ActivityDispatch
Expand All @@ -22,15 +22,15 @@ import Comment from "snoowrap/dist/objects/Comment";
import {ColumnDurationTransformer} from "./Transformers";
import { RedditUser } from "snoowrap/dist/objects";
import {ActivitySourceTypes, DurationVal, NonDispatchActivitySourceValue, onExistingFoundBehavior} from "../Infrastructure/Atomic";
import {Activity} from "./Activity";

@Entity({name: 'DispatchedAction'})
export class DispatchedEntity extends TimeAwareRandomBaseEntity {

@Column()
activityId!: string

@Column()
author!: string
//@ManyToOne(type => Activity, obj => obj.dispatched, {cascade: ['insert'], eager: true, nullable: false})
@ManyToOne(type => Activity, undefined, {cascade: ['insert'], eager: true, nullable: false})
@JoinColumn({name: 'activityId'})
activity!: Activity

@Column({
type: 'int',
Expand Down Expand Up @@ -82,11 +82,10 @@ export class DispatchedEntity extends TimeAwareRandomBaseEntity {
}})
tardyTolerant!: boolean | Duration

constructor(data?: ActivityDispatch & { manager: ManagerEntity }) {
constructor(data?: HydratedActivityDispatch) {
super();
if (data !== undefined) {
this.activityId = data.activity.name;
this.author = getActivityAuthorName(data.activity.author);
this.activity = data.activity;
this.delay = data.delay;
this.createdAt = data.queuedAt;
this.type = data.type;
Expand Down Expand Up @@ -151,20 +150,7 @@ export class DispatchedEntity extends TimeAwareRandomBaseEntity {
}

async toActivityDispatch(client: ExtendedSnoowrap): Promise<ActivityDispatch> {
const redditThing = parseRedditFullname(this.activityId);
if(redditThing === undefined) {
throw new Error(`Could not parse reddit ID from value '${this.activityId}'`);
}
let activity: Comment | Submission;
if (redditThing?.type === 'comment') {
// @ts-ignore
activity = await client.getComment(redditThing.id);
} else {
// @ts-ignore
activity = await client.getSubmission(redditThing.id);
}
activity.author = new RedditUser({name: this.author}, client, false);
activity.id = redditThing.id;
let activity = this.activity.toSnoowrap(client);
return {
id: this.id,
queuedAt: this.createdAt,
Expand All @@ -176,8 +162,13 @@ export class DispatchedEntity extends TimeAwareRandomBaseEntity {
cancelIfQueued: this.cancelIfQueued,
identifier: this.identifier,
type: this.type,
author: this.author,
author: activity.author.name,
dryRun: this.dryRun
}
}
}

export interface HydratedActivityDispatch extends Omit<ActivityDispatch, 'activity'> {
activity: Activity
manager: ManagerEntity
}
18 changes: 17 additions & 1 deletion src/Common/Entities/Subreddit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Entity, Column, PrimaryColumn, OneToMany, Index} from "typeorm";
import {Entity, Column, PrimaryColumn, OneToMany, Index, DataSource} from "typeorm";
import {Activity} from "./Activity";
import {ExtendedSnoowrap} from "../../Utils/SnoowrapClients";
import {Subreddit as SnoowrapSubreddit} from "snoowrap/dist/objects";

export interface SubredditEntityOptions {
id: string
Expand All @@ -25,4 +27,18 @@ export class Subreddit {
this.name = data.name;
}
}

toSnoowrap(client: ExtendedSnoowrap): SnoowrapSubreddit {
return new SnoowrapSubreddit({display_name: this.name, name: this.id}, client, false);
}

static async fromSnoowrap(subreddit: SnoowrapSubreddit, db?: DataSource) {
if(db !== undefined) {
const existing = await db.getRepository(Subreddit).findOneBy({name: subreddit.display_name});
if(existing) {
return existing;
}
}
return new Subreddit({id: await subreddit.name, name: await subreddit.display_name});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm"

export class delayedReset1667415256831 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.connection.logger.logSchemaBuild('Truncating (removing) existing Dispatched Actions due to internal structural changes');
await queryRunner.clearTable('DispatchedAction');
await queryRunner.changeColumn('DispatchedAction', 'author', new TableColumn({
name: 'author',
type: 'varchar',
length: '150',
isNullable: true
}));
}

public async down(queryRunner: QueryRunner): Promise<void> {
}

}
4 changes: 2 additions & 2 deletions src/Subreddit/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ export class Manager extends EventEmitter implements RunningStates {
let shouldPersistReports = false;

if (existingEntity === null) {
activityEntity = Activity.fromSnoowrapActivity(this.managerEntity.subreddit, activity, lastKnownStateTimestamp);
activityEntity = await Activity.fromSnoowrapActivity(activity, {lastKnownStateTimestamp, db: this.resources.database});
// always persist if activity is not already persisted and any reports exist
if (item.num_reports > 0) {
shouldPersistReports = true;
Expand Down Expand Up @@ -1189,7 +1189,7 @@ export class Manager extends EventEmitter implements RunningStates {
// @ts-ignore
const subProxy = await this.client.getSubmission((item as Comment).link_id);
const sub = await this.resources.getActivity(subProxy);
subActivity = await this.activityRepo.save(Activity.fromSnoowrapActivity(this.managerEntity.subreddit, sub));
subActivity = await this.activityRepo.save(await Activity.fromSnoowrapActivity(sub, {db: this.resources.database}));
}
event.activity.submission = subActivity;

Expand Down
Loading