Skip to content

Commit

Permalink
api updates for profile
Browse files Browse the repository at this point in the history
  • Loading branch information
NoodleOfDeath committed Jan 29, 2024
1 parent ffe3236 commit 08ad69a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 40 deletions.
80 changes: 55 additions & 25 deletions src/server/scripts/migrate_prefs_to_interactions.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import 'dotenv/config';

import {
Category,
CategoryInteraction,
InteractionType,
Publisher,
PublisherInteraction,
SummaryInteraction,
UserMetadata,
Expand All @@ -13,25 +15,32 @@ import { DBService } from '../src/services';

async function migratePublishers(
key: string,
type: InteractionType,
userId: number,
type: InteractionType
) {
console.log(`migrating ${key}`);
const rows = await UserMetadata.findAll({ where: { key } });
for (const row of rows) {
const userId = row.userId;
const publishers = Array.isArray(row.value) ? Object.values(row.value) : Object.keys(row.value);
for (pub of publishers) {
const publishers = typeof row.value === 'string' ? JSON.parse(row.value) as string[] : Array.isArray(row.value) ? Object.values(row.value) as string[] : Object.keys(row.value);
for (const pub of publishers) {
const publisher = await Publisher.findOne({ where: { name: pub } });
if (!publisher) {
continue;
}
const interaction = await PublisherInteraction.findOne({
revert: false,
targetId: pub,
type,
userId,
where: {
revert: false,
targetId: publisher.id,
type,
userId,
},
});
if (interaction) {
continue;
}
console.log(`creating ${type} interaction for ${userId} with ${pub}`);
await PublisherInteraction.create({
targetId: pub,
targetId: publisher.id,
type,
userId,
});
Expand All @@ -41,24 +50,32 @@ async function migratePublishers(

async function migrateCategories(
key: string,
type: InteractionType,
type: InteractionType
) {
console.log(`migrating ${key}`);
const rows = await UserMetadata.findAll({ where: { key } });
for (const row of rows) {
const userId = row.userId;
const categories = Array.isArray(row.value) ? Object.values(row.value) : Object.keys(row.value);
for (cat of categories) {
const categories = typeof row.value === 'string' ? JSON.parse(row.value) as string[] : Array.isArray(row.value) ? Object.values(row.value) as string[] : Object.keys(row.value);
for (const cat of categories) {
const category = await Category.findOne({ where: { name: cat } });
if (!category) {
continue;
}
const interaction = await CategoryInteraction.findOne({
revert: false,
targetId: cat,
type,
userId,
where: {
revert: false,
targetId: category.id,
type,
userId,
},
});
if (interaction) {
continue;
}
console.log(`creating ${type} interaction for ${userId} with ${cat}`);
await CategoryInteraction.create({
targetId: cat,
targetId: category.id,
type,
userId,
});
Expand All @@ -68,23 +85,26 @@ async function migrateCategories(

async function migrateSummaries(
key: string,
type: InteractionType,
userId: number,
type: InteractionType
) {
console.log(`migrating ${key}`);
const rows = await UserMetadata.findAll({ where: { key } });
for (const row of rows) {
const userId = row.userId;
const summaries = Array.isArray(row.value) ? Object.values(row.value) : Object.keys(row.value);
for (s of summaries) {
const summaries = typeof row.value === 'string' ? (JSON.parse(row.value) as string[]).map((k) => parseInt(k)) : Array.isArray(row.value) ? Object.values(row.value) as number[] : Object.keys(row.value).map(k => parseInt(k, 10));
for (const s of summaries) {
const interaction = await SummaryInteraction.findOne({
revert: false,
targetId: s,
type,
userId,
where: {
revert: false,
targetId: s,
type,
userId,
},
});
if (interaction) {
continue;
}
console.log(`creating ${type} interaction for ${userId} with ${s}`);
await SummaryInteraction.create({
targetId: s,
type,
Expand All @@ -96,15 +116,25 @@ async function migrateSummaries(

async function main() {
await DBService.prepare();
await Publisher.prepare();
await migratePublishers('followedPublishers', 'follow');
console.log('migrated followedPublishers');
await migratePublishers('favoritedPublishers', 'favorite');
console.log('migrated favoritedPublishers');
await migratePublishers('excludedPublishers', 'hide');
console.log('migrated excludedPublishers');
await migrateCategories('followedCategories', 'follow');
console.log('migrated followedCategories');
await migrateCategories('favoritedCategories', 'favorite');
console.log('migrated favoritedCategories');
await migrateCategories('excludedCategories', 'hide');
console.log('migrated excludedCategories');
await migrateSummaries('bookmarkedSummaries', 'bookmark');
console.log('migrated bookmarkedSummaries');
await migrateSummaries('readSummaries', 'read');
console.log('migrated readSummaries');
await migrateSummaries('removedSummaries', 'hide');
console.log('migrated removedSummaries');
}

main();
38 changes: 23 additions & 15 deletions src/server/src/api/v1/schema/user/User.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AliasPayload,
AliasType,
CalculateStreakOptions,
Category,
CategoryInteraction,
Credential,
CredentialCreationAttributes,
Expand All @@ -29,6 +30,7 @@ import {
InteractionType,
MetadataType,
Profile,
Publisher,
PublisherInteraction,
QueryFactory,
QueryOptions,
Expand Down Expand Up @@ -353,7 +355,7 @@ export class User<A extends UserAttributes = UserAttributes, B extends UserCreat
minCount,
offset,
type,
userId: null,
userId: user?.id ?? null,
};
const response = (await User.sql.query(QueryFactory.getQuery('summary_interaction_count'), {
nest: true,
Expand Down Expand Up @@ -472,7 +474,7 @@ export class User<A extends UserAttributes = UserAttributes, B extends UserCreat
type: 'bookmark',
userId: this.id,
},
})).map((i) => [i.id, true]));
})).map((i) => [i.id, { createdAt: i.createdAt, item: true }]));
profile.preferences.readSummaries = Object.fromEntries((await SummaryInteraction.findAll({
group: ['id'],
order: [['createdAt', 'desc']],
Expand All @@ -481,7 +483,7 @@ export class User<A extends UserAttributes = UserAttributes, B extends UserCreat
type: 'read',
userId: this.id,
},
})).map((i) => [i.id, true]));
})).map((i) => [i.id, { createdAt: i.createdAt, item: true }]));
profile.preferences.removedSummaries = Object.fromEntries((await SummaryInteraction.findAll({
group: ['id'],
order: [['createdAt', 'desc']],
Expand All @@ -491,60 +493,66 @@ export class User<A extends UserAttributes = UserAttributes, B extends UserCreat
userId: this.id,
},
})).map((i) => [i.id, true]));
profile.preferences.followedPublishers = Object.fromEntries((await PublisherInteraction.findAll({
profile.preferences.followedPublishers = Object.fromEntries(((await PublisherInteraction.findAll({
group: ['id'],
include: [Publisher.scope('public')],
order: [['createdAt', 'desc']],
where: {
revert: false,
type: 'follow',
userId: this.id,
},
})).map((i) => [i.id, true]));
profile.preferences.favoritedPublishers = Object.fromEntries((await PublisherInteraction.findAll({
})) as (PublisherInteraction & { publisher: Publisher })[]).map((i) => [i.publisher.name, true]));
profile.preferences.favoritedPublishers = Object.fromEntries(((await PublisherInteraction.findAll({
group: ['id'],
include: [Publisher.scope('public')],
order: [['createdAt', 'desc']],
where: {
revert: false,
type: 'favorite',
userId: this.id,
},
})).map((i) => [i.id, true]));
profile.preferences.excludedPublishers = Object.fromEntries((await PublisherInteraction.findAll({
})) as (PublisherInteraction & { publisher: Publisher })[]).map((i) => [i.publisher.name, true]));
profile.preferences.excludedPublishers = Object.fromEntries(((await PublisherInteraction.findAll({
group: ['id'],
include: [Publisher.scope('public')],
order: [['createdAt', 'desc']],
where: {
revert: false,
type: 'hide',
userId: this.id,
},
})).map((i) => [i.id, true]));
profile.preferences.followedCategories = Object.fromEntries((await CategoryInteraction.findAll({
})) as (PublisherInteraction & { publisher: Publisher })[]).map((i) => [i.publisher.name, true]));
profile.preferences.followedCategories = Object.fromEntries(((await CategoryInteraction.findAll({
group: ['id'],
include: [Category.scope('public')],
order: [['createdAt', 'desc']],
where: {
revert: false,
type: 'follow',
userId: this.id,
},
})).map((i) => [i.id, true]));
profile.preferences.favoritedCategories = Object.fromEntries((await CategoryInteraction.findAll({
})) as (CategoryInteraction & { category: Category })[]).map((i) => [i.category.name, true]));
profile.preferences.favoritedCategories = Object.fromEntries(((await CategoryInteraction.findAll({
group: ['id'],
include: [Category.scope('public')],
order: [['createdAt', 'desc']],
where: {
revert: false,
type: 'favortie',
userId: this.id,
},
})).map((i) => [i.id, true]));
profile.preferences.excludedCategories = Object.fromEntries((await CategoryInteraction.findAll({
})) as (CategoryInteraction & { category: Category })[]).map((i) => [i.category.name, true]));
profile.preferences.excludedCategories = Object.fromEntries(((await CategoryInteraction.findAll({
group: ['id'],
include: [Category.scope('public')],
order: [['createdAt', 'desc']],
where: {
revert: false,
type: 'hide',
userId: this.id,
},
})).map((i) => [i.id, true]));
})) as (CategoryInteraction & { category: Category })[]).map((i) => [i.category.name, true]));
}
profile.createdAt = this.createdAt;
const stats = await this.getStats();
Expand Down

0 comments on commit 08ad69a

Please sign in to comment.