server: fix various type errors in services

This commit is contained in:
Norm 2022-12-07 14:59:08 -05:00
parent b8fb7a38cc
commit 3dec9a47f0
9 changed files with 21 additions and 18 deletions

View file

@ -6,7 +6,7 @@ import { isUserRelated } from '@/misc/is-user-related.js';
import { publishAntennaStream, publishMainStream } from '@/services/stream.js';
import { User } from '@/models/entities/user.js';
export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { id: User['id']; }) {
export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { id: User['id']; }): Promise<void> {
// 通知しない設定になっているか、自分自身の投稿なら既読にする
const read = !antenna.notify || (antenna.userId === noteUser.id);

View file

@ -9,7 +9,7 @@ export async function createNotification(
notifieeId: User['id'],
type: Notification['type'],
data: Partial<Notification>,
) {
): Promise<Notification | null> {
if (data.notifierId && (notifieeId === data.notifierId)) {
return null;
}

View file

@ -81,6 +81,7 @@ type NodeInfo = {
nodeName?: any;
nodeDescription?: any;
description?: any;
themeColor?: any;
maintainer?: {
name?: any;
email?: any;
@ -125,7 +126,8 @@ async function fetchNodeinfo(instance: Instance): Promise<NodeInfo> {
return info as NodeInfo;
} catch (e) {
logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${e.message}`);
const message = e instanceof Error ? e.message : e;
logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${message}`);
throw e;
}

View file

@ -2,7 +2,7 @@ import { ModerationLogs } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
import { User } from '@/models/entities/user.js';
export async function insertModerationLog(moderator: { id: User['id'] }, type: string, info?: Record<string, any>) {
export async function insertModerationLog(moderator: { id: User['id'] }, type: string, info?: Record<string, any>): Promise<void> {
await ModerationLogs.insert({
id: genId(),
createdAt: new Date(),

View file

@ -16,7 +16,7 @@ type pushNotificationsTypes = {
};
// Reduce the content of the push message because of the character limit
function truncateNotification(notification: Packed<'Notification'>): any {
function truncateNotification(notification: Packed<'Notification'>): Record<string, any> {
if (notification.note) {
return {
...notification,
@ -37,7 +37,7 @@ function truncateNotification(notification: Packed<'Notification'>): any {
return notification;
}
export async function pushNotification<T extends keyof pushNotificationsTypes>(userId: string, type: T, body: pushNotificationsTypes[T]) {
export async function pushNotification<T extends keyof pushNotificationsTypes>(userId: string, type: T, body: pushNotificationsTypes[T]): Promise<void> {
const meta = await fetchMeta();
// Register key pair information

View file

@ -36,7 +36,7 @@ export async function getRelayActor(): Promise<ILocalUser> {
return created as ILocalUser;
}
export async function addRelay(inbox: string) {
export async function addRelay(inbox: string): Promise<Relay> {
const relay = await Relays.insert({
id: genId(),
inbox,
@ -51,7 +51,7 @@ export async function addRelay(inbox: string) {
return relay;
}
export async function removeRelay(inbox: string) {
export async function removeRelay(inbox: string): Promise<void> {
const relay = await Relays.findOneBy({
inbox,
});
@ -69,12 +69,12 @@ export async function removeRelay(inbox: string) {
await Relays.delete(relay.id);
}
export async function listRelay() {
export async function listRelay(): Promise<Relay[]> {
const relays = await Relays.find();
return relays;
}
export async function relayAccepted(id: string) {
export async function relayAccepted(id: string): Promise<string> {
const result = await Relays.update(id, {
status: 'accepted',
});
@ -82,7 +82,7 @@ export async function relayAccepted(id: string) {
return JSON.stringify(result);
}
export async function relayRejected(id: string) {
export async function relayRejected(id: string): Promise<string> {
const result = await Relays.update(id, {
status: 'rejected',
});
@ -90,11 +90,11 @@ export async function relayRejected(id: string) {
return JSON.stringify(result);
}
export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any) {
export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any): Promise<void> {
if (activity == null) return;
const relays = await relaysCache.fetch(null);
if (relays.length === 0) return;
if (relays == null || relays.length === 0) return;
// TODO
//const copy = structuredClone(activity);

View file

@ -8,7 +8,7 @@ import { User } from '@/models/entities/user.js';
import { Users, Followings } from '@/models/index.js';
import { publishInternalEvent } from '@/services/stream.js';
export async function doPostUnsuspend(user: User) {
export async function doPostUnsuspend(user: User): Promise<void> {
publishInternalEvent('userChangeSuspendedState', { id: user.id, isSuspended: false });
if (Users.isLocalUser(user)) {

View file

@ -16,7 +16,7 @@ export async function updateUsertags(user: User, tags: string[]): Promise<void>
await updateHashtag(user, tag, true, true);
}
for (const tag of (user.tags || []).filter(x => !tags.includes(x))) {
for (const tag of user.tags.filter(x => !tags.includes(x))) {
await updateHashtag(user, tag, true, false);
}
}

View file

@ -1,3 +1,4 @@
import { IsNull } from 'typeorm';
import { CacheableLocalUser, CacheableUser, ILocalUser } from '@/models/entities/user.js';
import { Users } from '@/models/index.js';
import { Cache } from '@/misc/cache.js';
@ -5,15 +6,15 @@ import { subscriber } from '@/db/redis.js';
export const userByIdCache = new Cache<CacheableUser>(
Infinity,
(id) => Users.findOneBy({ id }).then(x => x ?? undefined),
async (id) => id ? (await Users.findOneBy({ id }) ?? undefined) : undefined,
);
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser>(
Infinity,
(token) => Users.findOneBy({ token }).then(x => x ?? undefined),
async (token) => token ? (await Users.findOneBy({ token, host: IsNull() }) as ILocalUser | null ?? undefined) : undefined,
);
export const uriPersonCache = new Cache<CacheableUser>(
Infinity,
(uri) => Users.findOneBy({ uri }).then(x => x ?? undefined),
async (uri) => uri ? (await Users.findOneBy({ uri }) ?? undefined) : undefined,
);
subscriber.on('message', async (_, data) => {