refactor isPureRenote to foundkey-js

This commit is contained in:
Johann150 2023-02-04 15:58:13 +01:00
parent 9458045c8f
commit 28c11ca7af
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
9 changed files with 23 additions and 42 deletions

View file

@ -1,12 +0,0 @@
import { Note } from '@/models/entities/note.js';
export function isPureRenote(note: Note): note is Note & { renoteId: string, text: null, cw: null, fileIds: null | never[], hasPoll: false } {
return note.renoteId != null
&& note.text == null
&& note.cw == null
&& (
note.fileIds == null
|| note.fileIds.length === 0
)
&& !note.hasPoll;
}

View file

@ -1,14 +1,14 @@
import * as foundkey from 'foundkey-js';
import config from '@/config/index.js';
import { Notes } from '@/models/index.js';
import { Note } from '@/models/entities/note.js';
import { isPureRenote } from '@/misc/renote.js';
import { IActivity } from '@/remote/activitypub/types.js';
import renderNote from '@/remote/activitypub/renderer/note.js';
import renderCreate from '@/remote/activitypub/renderer/create.js';
import renderAnnounce from '@/remote/activitypub/renderer/announce.js';
export async function renderNoteOrRenoteActivity(note: Note): Promise<IActivity> {
if (isPureRenote(note)) {
if (foundkey.entities.isPureRenote(note)) {
const renote = await Notes.findOneByOrFail({ id: note.renoteId });
return renderAnnounce(renote.uri ?? `${config.url}/notes/${renote.id}`, note);
} else {

View file

@ -12,7 +12,6 @@ import { countIf } from '@/prelude/array.js';
import * as url from '@/prelude/url.js';
import { Users, Notes } from '@/models/index.js';
import { Note } from '@/models/entities/note.js';
import { isPureRenote } from '@/misc/renote.js';
import { makePaginationQuery } from '../api/common/make-pagination-query.js';
import { setResponseType } from '../activitypub.js';

View file

@ -1,5 +1,5 @@
import { In } from 'typeorm';
import { noteVisibilities } from 'foundkey-js';
import { noteVisibilities, entities } from 'foundkey-js';
import create from '@/services/note/create.js';
import { User } from '@/models/entities/user.js';
import { Users, DriveFiles, Notes, Channels, Blockings } from '@/models/index.js';
@ -7,7 +7,6 @@ import { DriveFile } from '@/models/entities/drive-file.js';
import { Note } from '@/models/entities/note.js';
import { Channel } from '@/models/entities/channel.js';
import { HOUR } from '@/const.js';
import { isPureRenote } from '@/misc/renote.js';
import config from '@/config/index.js';
import { ApiError } from '../../error.js';
import define from '../../define.js';
@ -160,7 +159,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw e;
});
if (isPureRenote(renote)) throw new ApiError('PURE_RENOTE', 'Cannot renote a pure renote.');
if (entities.isPureRenote(renote)) throw new ApiError('PURE_RENOTE', 'Cannot renote a pure renote.');
// check that the visibility is not less restrictive
if (noteVisibilities.indexOf(renote.visibility) > noteVisibilities.indexOf(ps.visibility)) {
@ -185,7 +184,7 @@ export default define(meta, paramDef, async (ps, user) => {
throw e;
});
if (isPureRenote(reply)) throw new ApiError('PURE_RENOTE', 'Cannot reply to a pure renote.');
if (entities.isPureRenote(reply)) throw new ApiError('PURE_RENOTE', 'Cannot reply to a pure renote.');
// check that the visibility is not less restrictive
if (noteVisibilities.indexOf(reply.visibility) > noteVisibilities.indexOf(ps.visibility)) {

View file

@ -1,4 +1,5 @@
import { FindOptionsWhere, In, IsNull, Not } from 'typeorm';
import * as foundkey from 'foundkey-js';
import { publishNoteStream } from '@/services/stream.js';
import renderDelete from '@/remote/activitypub/renderer/delete.js';
import renderAnnounce from '@/remote/activitypub/renderer/announce.js';
@ -12,7 +13,6 @@ import { Notes, Users, Instances } from '@/models/index.js';
import { notesChart, perUserNotesChart, instanceChart } from '@/services/chart/index.js';
import { DeliverManager } from '@/remote/activitypub/deliver-manager.js';
import { countSameRenotes } from '@/misc/count-same-renotes.js';
import { isPureRenote } from '@/misc/renote.js';
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
import { deliverToRelays } from '../relay.js';
@ -42,7 +42,7 @@ export default async function(user: { id: User['id']; uri: User['uri']; host: Us
let renote: Note | null = null;
// if deleted note is renote
if (isPureRenote(note)) {
if (foundkey.entities.isPureRenote(note)) {
renote = await Notes.findOneBy({ id: note.renoteId });
}

View file

@ -159,12 +159,7 @@ if (noteViewInterruptors.length > 0) {
});
}
const isRenote = (
note.renote != null &&
note.text == null &&
note.fileIds.length === 0 &&
note.poll == null
);
const isRenote = foundkey.entities.isPureRenote(note);
const el = ref<HTMLElement>();
const menuButton = ref<HTMLElement>();

View file

@ -148,13 +148,7 @@ if (noteViewInterruptors.length > 0) {
});
}
const isRenote = (
note.renote != null &&
note.text == null &&
note.cw == null &&
note.fileIds.length === 0 &&
note.poll == null
);
const isRenote = foundkey.entities.isPureRenote(note);
const el = ref<HTMLElement>();
const menuButton = ref<HTMLElement>();

View file

@ -16,14 +16,9 @@ export function getNoteMenu(props: {
isDeleted: Ref<boolean>;
currentClipPage?: Ref<foundkey.entities.Clip>;
}) {
const isRenote = (
props.note.renote != null &&
props.note.text == null &&
props.note.fileIds.length === 0 &&
props.note.poll == null
);
const appearNote = isRenote ? props.note.renote as foundkey.entities.Note : props.note;
const appearNote = foundkey.entities.isPureRenote(props.note)
? props.note.renote as foundkey.entities.Note
: props.note;
function del(): void {
os.confirm({

View file

@ -471,3 +471,14 @@ export type UserSorting =
| '+updatedAt'
| '-updatedAt';
export type OriginType = 'combined' | 'local' | 'remote';
export function isPureRenote(note: Note): boolean {
return note.renoteId != null
&& note.text == null
&& note.cw == null
&& (
note.fileIds == null
|| note.fileIds.length === 0
)
&& note.poll == null;
}