server: remove favorites

This commit is contained in:
Johann150 2023-05-26 20:52:31 +02:00
parent de81fac334
commit aa7171e116
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
14 changed files with 3 additions and 268 deletions

View file

@ -33,7 +33,6 @@ import { UserGroup } from '@/models/entities/user-group.js';
import { UserGroupJoining } from '@/models/entities/user-group-joining.js';
import { UserGroupInvitation } from '@/models/entities/user-group-invitation.js';
import { Hashtag } from '@/models/entities/hashtag.js';
import { NoteFavorite } from '@/models/entities/note-favorite.js';
import { AbuseUserReport } from '@/models/entities/abuse-user-report.js';
import { RegistrationTicket } from '@/models/entities/registration-tickets.js';
import { MessagingMessage } from '@/models/entities/messaging-message.js';
@ -134,7 +133,6 @@ export const entities = [
RenoteMuting,
Blocking,
Note,
NoteFavorite,
NoteReaction,
NoteWatching,
NoteThreadMuting,

View file

@ -5,8 +5,6 @@ export const kinds = [
'write:blocks',
'read:drive',
'write:drive',
'read:favorites',
'write:favorites',
'read:following',
'write:following',
'read:messaging',

View file

@ -23,7 +23,6 @@ import { packedHashtagSchema } from '@/models/schema/hashtag.js';
import { packedPageSchema } from '@/models/schema/page.js';
import { packedUserGroupSchema } from '@/models/schema/user-group.js';
import { packedUserGroupInvitationSchema } from '@/models/schema/user-group-invitation.js';
import { packedNoteFavoriteSchema } from '@/models/schema/note-favorite.js';
import { packedChannelSchema } from '@/models/schema/channel.js';
import { packedAntennaSchema } from '@/models/schema/antenna.js';
import { packedClipSchema } from '@/models/schema/clip.js';
@ -47,7 +46,6 @@ export const refs = {
MessagingMessage: packedMessagingMessageSchema,
Note: packedNoteSchema,
NoteReaction: packedNoteReactionSchema,
NoteFavorite: packedNoteFavoriteSchema,
Notification: packedNotificationSchema,
DriveFile: packedDriveFileSchema,
DriveFolder: packedDriveFolderSchema,

View file

@ -1,35 +0,0 @@
import { PrimaryColumn, Entity, Index, JoinColumn, Column, ManyToOne } from 'typeorm';
import { id } from '../id.js';
import { Note } from './note.js';
import { User } from './user.js';
@Entity()
@Index(['userId', 'noteId'], { unique: true })
export class NoteFavorite {
@PrimaryColumn(id())
public id: string;
@Column('timestamp with time zone', {
comment: 'The created date of the NoteFavorite.',
})
public createdAt: Date;
@Index()
@Column(id())
public userId: User['id'];
@ManyToOne(() => User, {
onDelete: 'CASCADE',
})
@JoinColumn()
public user: User | null;
@Column(id())
public noteId: Note['id'];
@ManyToOne(() => Note, {
onDelete: 'CASCADE',
})
@JoinColumn()
public note: Note | null;
}

View file

@ -29,7 +29,6 @@ import { RenoteMutingRepository } from './repositories/renote-muting.js';
import { BlockingRepository } from './repositories/blocking.js';
import { NoteReactionRepository } from './repositories/note-reaction.js';
import { NotificationRepository } from './repositories/notification.js';
import { NoteFavoriteRepository } from './repositories/note-favorite.js';
import { UserPublickey } from './entities/user-publickey.js';
import { UserKeypair } from './entities/user-keypair.js';
import { AppRepository } from './repositories/app.js';
@ -64,7 +63,6 @@ export const Announcements = db.getRepository(Announcement);
export const AnnouncementReads = db.getRepository(AnnouncementRead);
export const Apps = (AppRepository);
export const Notes = (NoteRepository);
export const NoteFavorites = (NoteFavoriteRepository);
export const NoteWatchings = db.getRepository(NoteWatching);
export const NoteThreadMutings = db.getRepository(NoteThreadMuting);
export const NoteReactions = (NoteReactionRepository);

View file

@ -1,29 +0,0 @@
import { db } from '@/db/postgre.js';
import { NoteFavorite } from '@/models/entities/note-favorite.js';
import { User } from '@/models/entities/user.js';
import { Notes } from '../index.js';
export const NoteFavoriteRepository = db.getRepository(NoteFavorite).extend({
async pack(
src: NoteFavorite['id'] | NoteFavorite,
me?: { id: User['id'] } | null | undefined,
) {
const favorite = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
return {
id: favorite.id,
createdAt: favorite.createdAt.toISOString(),
noteId: favorite.noteId,
// may throw error
note: await Notes.pack(favorite.note || favorite.noteId, me),
};
},
packMany(
favorites: any[],
me: { id: User['id'] },
) {
return Promise.allSettled(favorites.map(x => this.pack(x, me)))
.then(promises => promises.flatMap(result => result.status === 'fulfilled' ? [result.value] : []));
},
});

View file

@ -1,26 +0,0 @@
export const packedNoteFavoriteSchema = {
type: 'object',
properties: {
id: {
type: 'string',
optional: false, nullable: false,
format: 'id',
example: 'xxxxxxxxxx',
},
createdAt: {
type: 'string',
optional: false, nullable: false,
format: 'date-time',
},
note: {
type: 'object',
optional: false, nullable: false,
ref: 'Note',
},
noteId: {
type: 'string',
optional: false, nullable: false,
format: 'id',
},
},
} as const;

View file

@ -162,7 +162,6 @@ import * as ep___i_exportFollowing from './endpoints/i/export-following.js';
import * as ep___i_exportMute from './endpoints/i/export-mute.js';
import * as ep___i_exportNotes from './endpoints/i/export-notes.js';
import * as ep___i_exportUserLists from './endpoints/i/export-user-lists.js';
import * as ep___i_favorites from './endpoints/i/favorites.js';
import * as ep___i_getWordMutedNotesCount from './endpoints/i/get-word-muted-notes-count.js';
import * as ep___i_importBlocking from './endpoints/i/import-blocking.js';
import * as ep___i_importFollowing from './endpoints/i/import-following.js';
@ -215,8 +214,6 @@ import * as ep___notes_clips from './endpoints/notes/clips.js';
import * as ep___notes_conversation from './endpoints/notes/conversation.js';
import * as ep___notes_create from './endpoints/notes/create.js';
import * as ep___notes_delete from './endpoints/notes/delete.js';
import * as ep___notes_favorites_create from './endpoints/notes/favorites/create.js';
import * as ep___notes_favorites_delete from './endpoints/notes/favorites/delete.js';
import * as ep___notes_featured from './endpoints/notes/featured.js';
import * as ep___notes_globalTimeline from './endpoints/notes/global-timeline.js';
import * as ep___notes_hybridTimeline from './endpoints/notes/hybrid-timeline.js';
@ -459,7 +456,6 @@ const eps = [
['i/export-mute', ep___i_exportMute],
['i/export-notes', ep___i_exportNotes],
['i/export-user-lists', ep___i_exportUserLists],
['i/favorites', ep___i_favorites],
['i/get-word-muted-notes-count', ep___i_getWordMutedNotesCount],
['i/import-blocking', ep___i_importBlocking],
['i/import-following', ep___i_importFollowing],
@ -512,8 +508,6 @@ const eps = [
['notes/conversation', ep___notes_conversation],
['notes/create', ep___notes_create],
['notes/delete', ep___notes_delete],
['notes/favorites/create', ep___notes_favorites_create],
['notes/favorites/delete', ep___notes_favorites_delete],
['notes/featured', ep___notes_featured],
['notes/global-timeline', ep___notes_globalTimeline],
['notes/hybrid-timeline', ep___notes_hybridTimeline],

View file

@ -1,44 +0,0 @@
import { NoteFavorites } from '@/models/index.js';
import define from '@/server/api/define.js';
import { makePaginationQuery } from '@/server/api/common/make-pagination-query.js';
export const meta = {
tags: ['account', 'notes', 'favorites'],
requireCredential: true,
kind: 'read:favorites',
res: {
type: 'array',
optional: false, nullable: false,
items: {
type: 'object',
optional: false, nullable: false,
ref: 'NoteFavorite',
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' },
},
required: [],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const query = makePaginationQuery(NoteFavorites.createQueryBuilder('favorite'), ps.sinceId, ps.untilId)
.andWhere('favorite.userId = :meId', { meId: user.id })
.leftJoinAndSelect('favorite.note', 'note');
const favorites = await query
.take(ps.limit)
.getMany();
return await NoteFavorites.packMany(favorites, user);
});

View file

@ -1,48 +0,0 @@
import { NoteFavorites } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
import { getNote } from '@/server/api/common/getters.js';
export const meta = {
tags: ['notes', 'favorites'],
requireCredential: true,
kind: 'write:favorites',
errors: ['NO_SUCH_NOTE', 'ALREADY_FAVORITED'],
} as const;
export const paramDef = {
type: 'object',
properties: {
noteId: { type: 'string', format: 'misskey:id' },
},
required: ['noteId'],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Get favoritee
const note = await getNote(ps.noteId, user).catch(err => {
if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError('NO_SUCH_NOTE');
throw err;
});
// if already favorited
const exist = await NoteFavorites.countBy({
noteId: note.id,
userId: user.id,
});
if (exist) throw new ApiError('ALREADY_FAVORITED');
// Create favorite
await NoteFavorites.insert({
id: genId(),
createdAt: new Date(),
noteId: note.id,
userId: user.id,
});
});

View file

@ -1,42 +0,0 @@
import { NoteFavorites } from '@/models/index.js';
import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
import { getNote } from '@/server/api/common/getters.js';
export const meta = {
tags: ['notes', 'favorites'],
requireCredential: true,
kind: 'write:favorites',
errors: ['NO_SUCH_NOTE', 'NOT_FAVORITED'],
} as const;
export const paramDef = {
type: 'object',
properties: {
noteId: { type: 'string', format: 'misskey:id' },
},
required: ['noteId'],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Get favoritee
const note = await getNote(ps.noteId, user).catch(err => {
if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError('NO_SUCH_NOTE');
throw err;
});
// if already favorited
const exist = await NoteFavorites.findOneBy({
noteId: note.id,
userId: user.id,
});
if (exist == null) throw new ApiError('NOT_FAVORITED');
// Delete favorite
await NoteFavorites.delete(exist.id);
});

View file

@ -1,4 +1,4 @@
import { NoteFavorites, NoteThreadMutings, NoteWatchings } from '@/models/index.js';
import { NoteThreadMutings, NoteWatchings } from '@/models/index.js';
import { ApiError } from '@/server/api/error.js';
import { getNote } from '@/server/api/common/getters.js';
import define from '@/server/api/define.js';
@ -12,10 +12,6 @@ export const meta = {
type: 'object',
optional: false, nullable: false,
properties: {
isFavorited: {
type: 'boolean',
optional: false, nullable: false,
},
isWatching: {
type: 'boolean',
optional: false, nullable: false,
@ -51,14 +47,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw err;
});
const [favorite, watching, threadMuting] = await Promise.all([
NoteFavorites.count({
where: {
userId: user.id,
noteId: note.id,
},
take: 1,
}),
const [watching, threadMuting] = await Promise.all([
NoteWatchings.count({
where: {
userId: user.id,
@ -76,7 +65,6 @@ export default define(meta, paramDef, async (ps, user) => {
]);
return {
isFavorited: favorite !== 0,
isWatching: watching !== 0,
isMutedThread: threadMuting !== 0,
};

View file

@ -1,4 +1,4 @@
import { DriveFiles, Followings, NoteFavorites, NoteReactions, Notes, PageLikes, PollVotes, Users } from '@/models/index.js';
import { DriveFiles, Followings, NoteReactions, Notes, PageLikes, PollVotes, Users } from '@/models/index.js';
import { awaitAll } from '@/prelude/await-all.js';
import define from '@/server/api/define.js';
import { ApiError } from '@/server/api/error.js';
@ -76,10 +76,6 @@ export const meta = {
type: 'integer',
optional: false, nullable: false,
},
noteFavoritesCount: {
type: 'integer',
optional: false, nullable: false,
},
pageLikesCount: {
type: 'integer',
optional: false, nullable: false,
@ -148,9 +144,6 @@ export default define(meta, paramDef, async (ps, me) => {
.innerJoin('reaction.note', 'note')
.where('note.userId = :userId', { userId: user.id })
.getCount(),
noteFavoritesCount: NoteFavorites.createQueryBuilder('favorite')
.where('favorite.userId = :userId', { userId: user.id })
.getCount(),
pageLikesCount: PageLikes.createQueryBuilder('like')
.where('like.userId = :userId', { userId: user.id })
.getCount(),

View file

@ -68,10 +68,6 @@ export const errors: Record<string, { message: string, httpStatusCode: number }>
message: 'That note is already added to that clip.',
httpStatusCode: 409,
},
ALREADY_FAVORITED: {
message: 'That note is already favorited.',
httpStatusCode: 409,
},
ALREADY_FOLLOWING: {
message: 'You are already following that user.',
httpStatusCode: 409,
@ -332,10 +328,6 @@ export const errors: Record<string, { message: string, httpStatusCode: number }>
message: 'That note is not added to that clip.',
httpStatusCode: 409,
},
NOT_FAVORITED: {
message: 'You have not favorited that note.',
httpStatusCode: 409,
},
NOT_FOLLOWING: {
message: 'You are not following that user.',
httpStatusCode: 409,