forked from FoundKeyGang/FoundKey
Compare commits
12 commits
native-not
...
main
Author | SHA1 | Date | |
---|---|---|---|
9f6be8d557 | |||
9d9b2da6cc | |||
d1ec058d5c | |||
131c12a30b | |||
8d6476af2a | |||
57299f0df6 | |||
b0489abd7f | |||
26f1b66c6a | |||
1d877e97f0 | |||
0571a0843c | |||
56033c26f0 | |||
80af8a143e |
23 changed files with 246 additions and 237 deletions
|
@ -190,7 +190,9 @@ charts: "Charts"
|
|||
perHour: "Per Hour"
|
||||
perDay: "Per Day"
|
||||
stopActivityDelivery: "Stop sending activities"
|
||||
stopActivityDeliveryDescription: "Local activities will not be sent to this instance. Receiving activities works as before."
|
||||
blockThisInstance: "Block this instance"
|
||||
blockThisInstanceDescription: "Local activites will not be sent to this instance. Activites from this instance will be discarded."
|
||||
operations: "Operations"
|
||||
software: "Software"
|
||||
version: "Version"
|
||||
|
|
|
@ -62,22 +62,21 @@ export function fromHtml(html: string, hashtagNames?: string[]): string {
|
|||
const rel = node.attrs.find(x => x.name === 'rel');
|
||||
const href = node.attrs.find(x => x.name === 'href');
|
||||
|
||||
// ハッシュタグ
|
||||
// hashtags
|
||||
if (hashtagNames && href && hashtagNames.map(x => x.toLowerCase()).includes(txt.toLowerCase())) {
|
||||
text += txt;
|
||||
// メンション
|
||||
// mentions
|
||||
} else if (txt.startsWith('@') && !(rel && rel.value.match(/^me /))) {
|
||||
const part = txt.split('@');
|
||||
|
||||
if (part.length === 2 && href) {
|
||||
//#region ホスト名部分が省略されているので復元する
|
||||
// restore the host name part
|
||||
const acct = `${txt}@${(new URL(href.value)).hostname}`;
|
||||
text += acct;
|
||||
//#endregion
|
||||
} else if (part.length === 3) {
|
||||
text += txt;
|
||||
}
|
||||
// その他
|
||||
// other
|
||||
} else {
|
||||
const generateLink = () => {
|
||||
if (!href && !txt) {
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
export class Cache<T> {
|
||||
public cache: Map<string | null, { date: number; value: T; }>;
|
||||
private lifetime: number;
|
||||
public fetcher: (key: string | null) => Promise<T | undefined>;
|
||||
|
||||
constructor(lifetime: Cache<never>['lifetime']) {
|
||||
constructor(lifetime: number, fetcher: Cache<T>['fetcher']) {
|
||||
this.cache = new Map();
|
||||
this.lifetime = lifetime;
|
||||
this.fetcher = fetcher;
|
||||
}
|
||||
|
||||
public set(key: string | null, value: T): void {
|
||||
|
@ -17,10 +19,13 @@ export class Cache<T> {
|
|||
public get(key: string | null): T | undefined {
|
||||
const cached = this.cache.get(key);
|
||||
if (cached == null) return undefined;
|
||||
|
||||
// discard if past the cache lifetime
|
||||
if ((Date.now() - cached.date) > this.lifetime) {
|
||||
this.cache.delete(key);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
|
@ -29,52 +34,22 @@ export class Cache<T> {
|
|||
}
|
||||
|
||||
/**
|
||||
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
||||
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
|
||||
* If the value is cached, it is returned. Otherwise the fetcher is
|
||||
* run to get the value. If the fetcher returns undefined, it is
|
||||
* returned but not cached.
|
||||
*/
|
||||
public async fetch(key: string | null, fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
|
||||
const cachedValue = this.get(key);
|
||||
if (cachedValue !== undefined) {
|
||||
if (validator) {
|
||||
if (validator(cachedValue)) {
|
||||
// Cache HIT
|
||||
return cachedValue;
|
||||
}
|
||||
} else {
|
||||
// Cache HIT
|
||||
return cachedValue;
|
||||
}
|
||||
}
|
||||
public async fetch(key: string | null): Promise<T | undefined> {
|
||||
const cached = this.get(key);
|
||||
if (cached !== undefined) {
|
||||
return cached;
|
||||
} else {
|
||||
const value = await this.fetcher(key);
|
||||
|
||||
// Cache MISS
|
||||
const value = await fetcher();
|
||||
this.set(key, value);
|
||||
return value;
|
||||
}
|
||||
// don't cache undefined
|
||||
if (value !== undefined)
|
||||
this.set(key, value);
|
||||
|
||||
/**
|
||||
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
||||
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
|
||||
*/
|
||||
public async fetchMaybe(key: string | null, fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
|
||||
const cachedValue = this.get(key);
|
||||
if (cachedValue !== undefined) {
|
||||
if (validator) {
|
||||
if (validator(cachedValue)) {
|
||||
// Cache HIT
|
||||
return cachedValue;
|
||||
}
|
||||
} else {
|
||||
// Cache HIT
|
||||
return cachedValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// Cache MISS
|
||||
const value = await fetcher();
|
||||
if (value !== undefined) {
|
||||
this.set(key, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,22 +3,26 @@ import { Note } from '@/models/entities/note.js';
|
|||
import { User } from '@/models/entities/user.js';
|
||||
import { UserListJoinings, UserGroupJoinings, Blockings } from '@/models/index.js';
|
||||
import * as Acct from '@/misc/acct.js';
|
||||
import { MINUTE } from '@/const.js';
|
||||
import { getFullApAccount } from './convert-host.js';
|
||||
import { Packed } from './schema.js';
|
||||
import { Cache } from './cache.js';
|
||||
|
||||
const blockingCache = new Cache<User['id'][]>(1000 * 60 * 5);
|
||||
const blockingCache = new Cache<User['id'][]>(
|
||||
5 * MINUTE,
|
||||
(blockerId) => Blockings.findBy({ blockerId }).then(res => res.map(x => x.blockeeId)),
|
||||
);
|
||||
|
||||
// NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている
|
||||
// designation for users you follow, list users and groups is disabled for performance reasons
|
||||
|
||||
/**
|
||||
* noteUserFollowers / antennaUserFollowing はどちらか一方が指定されていればよい
|
||||
* either noteUserFollowers or antennaUserFollowing must be specified
|
||||
*/
|
||||
export async function checkHitAntenna(antenna: Antenna, note: (Note | Packed<'Note'>), noteUser: { id: User['id']; username: string; host: string | null; }, noteUserFollowers?: User['id'][], antennaUserFollowing?: User['id'][]): Promise<boolean> {
|
||||
if (note.visibility === 'specified') return false;
|
||||
|
||||
// アンテナ作成者がノート作成者にブロックされていたらスキップ
|
||||
const blockings = await blockingCache.fetch(noteUser.id, () => Blockings.findBy({ blockerId: noteUser.id }).then(res => res.map(x => x.blockeeId)));
|
||||
// skip if the antenna creator is blocked by the note author
|
||||
const blockings = await blockingCache.fetch(noteUser.id);
|
||||
if (blockings.some(blocking => blocking === antenna.userId)) return false;
|
||||
|
||||
if (note.visibility === 'followers') {
|
||||
|
|
|
@ -1,44 +1,44 @@
|
|||
import push from 'web-push';
|
||||
import { db } from '@/db/postgre.js';
|
||||
import { Meta } from '@/models/entities/meta.js';
|
||||
import { getFetchInstanceMetadataLock } from '@/misc/app-lock.js';
|
||||
|
||||
let cache: Meta;
|
||||
|
||||
/**
|
||||
* Performs the primitive database operation to set the server configuration
|
||||
*/
|
||||
export async function setMeta(meta: Meta): Promise<void> {
|
||||
const unlock = await getFetchInstanceMetadataLock('localhost');
|
||||
|
||||
// try to mitigate older bugs where multiple meta entries may have been created
|
||||
db.manager.clear(Meta);
|
||||
db.manager.insert(Meta, meta);
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the primitive database operation to fetch server configuration.
|
||||
* Writes to `cache` instead of returning.
|
||||
*/
|
||||
async function getMeta(): Promise<void> {
|
||||
const unlock = await getFetchInstanceMetadataLock('localhost');
|
||||
|
||||
// new IDs are prioritised because multiple records may have been created due to past bugs
|
||||
cache = db.manager.findOne(Meta, {
|
||||
order: {
|
||||
id: 'DESC',
|
||||
},
|
||||
});
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
export async function fetchMeta(noCache = false): Promise<Meta> {
|
||||
if (!noCache && cache) return cache;
|
||||
|
||||
return await db.transaction(async transactionalEntityManager => {
|
||||
// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
|
||||
const metas = await transactionalEntityManager.find(Meta, {
|
||||
order: {
|
||||
id: 'DESC',
|
||||
},
|
||||
});
|
||||
await getMeta();
|
||||
|
||||
const meta = metas[0];
|
||||
|
||||
if (meta) {
|
||||
cache = meta;
|
||||
return meta;
|
||||
} else {
|
||||
// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
|
||||
const saved = await transactionalEntityManager
|
||||
.upsert(
|
||||
Meta,
|
||||
{
|
||||
id: 'x',
|
||||
},
|
||||
['id'],
|
||||
)
|
||||
.then((x) => transactionalEntityManager.findOneByOrFail(Meta, x.identifiers[0]));
|
||||
|
||||
cache = saved;
|
||||
return saved;
|
||||
}
|
||||
});
|
||||
return cache;
|
||||
}
|
||||
|
||||
setInterval(() => {
|
||||
fetchMeta(true).then(meta => {
|
||||
cache = meta;
|
||||
});
|
||||
}, 1000 * 10);
|
||||
|
|
|
@ -3,8 +3,11 @@ import { User } from '@/models/entities/user.js';
|
|||
import { UserKeypair } from '@/models/entities/user-keypair.js';
|
||||
import { Cache } from './cache.js';
|
||||
|
||||
const cache = new Cache<UserKeypair>(Infinity);
|
||||
const cache = new Cache<UserKeypair>(
|
||||
Infinity,
|
||||
(userId) => UserKeypairs.findOneByOrFail({ userId }),
|
||||
);
|
||||
|
||||
export async function getUserKeypair(userId: User['id']): Promise<UserKeypair> {
|
||||
return await cache.fetch(userId, () => UserKeypairs.findOneByOrFail({ userId }));
|
||||
return await cache.fetch(userId);
|
||||
}
|
||||
|
|
|
@ -4,14 +4,27 @@ import { Emojis } from '@/models/index.js';
|
|||
import { Emoji } from '@/models/entities/emoji.js';
|
||||
import { Note } from '@/models/entities/note.js';
|
||||
import { query } from '@/prelude/url.js';
|
||||
import { HOUR } from '@/const.js';
|
||||
import { Cache } from './cache.js';
|
||||
import { isSelfHost, toPunyNullable } from './convert-host.js';
|
||||
import { decodeReaction } from './reaction-lib.js';
|
||||
|
||||
const cache = new Cache<Emoji | null>(1000 * 60 * 60 * 12);
|
||||
/**
|
||||
* composite cache key: `${host ?? ''}:${name}`
|
||||
*/
|
||||
const cache = new Cache<Emoji | null>(
|
||||
12 * HOUR,
|
||||
async (key) => {
|
||||
const [host, name] = key.split(':');
|
||||
return (await Emojis.findOneBy({
|
||||
name,
|
||||
host: host || IsNull(),
|
||||
})) || null;
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* 添付用絵文字情報
|
||||
* Information needed to attach in ActivityPub
|
||||
*/
|
||||
type PopulatedEmoji = {
|
||||
name: string;
|
||||
|
@ -36,28 +49,22 @@ function parseEmojiStr(emojiName: string, noteUserHost: string | null) {
|
|||
|
||||
const name = match[1];
|
||||
|
||||
// ホスト正規化
|
||||
const host = toPunyNullable(normalizeHost(match[2], noteUserHost));
|
||||
|
||||
return { name, host };
|
||||
}
|
||||
|
||||
/**
|
||||
* 添付用絵文字情報を解決する
|
||||
* @param emojiName ノートやユーザープロフィールに添付された、またはリアクションのカスタム絵文字名 (:は含めない, リアクションでローカルホストの場合は@.を付ける (これはdecodeReactionで可能))
|
||||
* @param noteUserHost ノートやユーザープロフィールの所有者のホスト
|
||||
* @returns 絵文字情報, nullは未マッチを意味する
|
||||
* Resolve emoji information from ActivityPub attachment.
|
||||
* @param emojiName custom emoji names attached to notes, user profiles or in rections. Colons should not be included. Localhost is denote by @. (see also `decodeReaction`)
|
||||
* @param noteUserHost host that the content is from, to default to
|
||||
* @returns emoji information. `null` means not found.
|
||||
*/
|
||||
export async function populateEmoji(emojiName: string, noteUserHost: string | null): Promise<PopulatedEmoji | null> {
|
||||
const { name, host } = parseEmojiStr(emojiName, noteUserHost);
|
||||
if (name == null) return null;
|
||||
|
||||
const queryOrNull = async () => (await Emojis.findOneBy({
|
||||
name,
|
||||
host: host ?? IsNull(),
|
||||
})) || null;
|
||||
|
||||
const emoji = await cache.fetch(`${name} ${host}`, queryOrNull);
|
||||
const emoji = await cache.fetch(`${host ?? ''}:${name}`);
|
||||
|
||||
if (emoji == null) return null;
|
||||
|
||||
|
@ -72,7 +79,7 @@ export async function populateEmoji(emojiName: string, noteUserHost: string | nu
|
|||
}
|
||||
|
||||
/**
|
||||
* 複数の添付用絵文字情報を解決する (キャシュ付き, 存在しないものは結果から除外される)
|
||||
* Retrieve list of emojis from the cache. Uncached emoji are dropped.
|
||||
*/
|
||||
export async function populateEmojis(emojiNames: string[], noteUserHost: string | null): Promise<PopulatedEmoji[]> {
|
||||
const emojis = await Promise.all(emojiNames.map(x => populateEmoji(x, noteUserHost)));
|
||||
|
@ -103,11 +110,20 @@ export function aggregateNoteEmojis(notes: Note[]) {
|
|||
}
|
||||
|
||||
/**
|
||||
* 与えられた絵文字のリストをデータベースから取得し、キャッシュに追加します
|
||||
* Query list of emojis in bulk and add them to the cache.
|
||||
*/
|
||||
export async function prefetchEmojis(emojis: { name: string; host: string | null; }[]): Promise<void> {
|
||||
const notCachedEmojis = emojis.filter(emoji => cache.get(`${emoji.name} ${emoji.host}`) == null);
|
||||
const notCachedEmojis = emojis.filter(emoji => {
|
||||
// check if the cache has this emoji
|
||||
return cache.get(`${emoji.host ?? ''}:${emoji.name}`) == null;
|
||||
});
|
||||
|
||||
// check if there even are any uncached emoji to handle
|
||||
if (notCachedEmojis.length === 0) return;
|
||||
|
||||
// query all uncached emoji
|
||||
const emojisQuery: any[] = [];
|
||||
// group by hosts to try to reduce query size
|
||||
const hosts = new Set(notCachedEmojis.map(e => e.host));
|
||||
for (const host of hosts) {
|
||||
emojisQuery.push({
|
||||
|
@ -115,11 +131,14 @@ export async function prefetchEmojis(emojis: { name: string; host: string | null
|
|||
host: host ?? IsNull(),
|
||||
});
|
||||
}
|
||||
const _emojis = emojisQuery.length > 0 ? await Emojis.find({
|
||||
|
||||
await Emojis.find({
|
||||
where: emojisQuery,
|
||||
select: ['name', 'host', 'originalUrl', 'publicUrl'],
|
||||
}) : [];
|
||||
for (const emoji of _emojis) {
|
||||
cache.set(`${emoji.name} ${emoji.host}`, emoji);
|
||||
}
|
||||
}).then(emojis => {
|
||||
// store all emojis into the cache
|
||||
emojis.forEach(emoji => {
|
||||
cache.set(`${emoji.host ?? ''}:${emoji.name}`, emoji);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -6,13 +6,16 @@ import { Packed } from '@/misc/schema.js';
|
|||
import { awaitAll, Promiseable } from '@/prelude/await-all.js';
|
||||
import { populateEmojis } from '@/misc/populate-emojis.js';
|
||||
import { getAntennas } from '@/misc/antenna-cache.js';
|
||||
import { USER_ACTIVE_THRESHOLD, USER_ONLINE_THRESHOLD } from '@/const.js';
|
||||
import { USER_ACTIVE_THRESHOLD, USER_ONLINE_THRESHOLD, HOUR } from '@/const.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { db } from '@/db/postgre.js';
|
||||
import { Instance } from '../entities/instance.js';
|
||||
import { Notes, NoteUnreads, FollowRequests, Notifications, MessagingMessages, UserNotePinings, Followings, Blockings, Mutings, RenoteMutings, UserProfiles, UserSecurityKeys, UserGroupJoinings, Pages, Announcements, AnnouncementReads, AntennaNotes, ChannelFollowings, Instances, DriveFiles } from '../index.js';
|
||||
|
||||
const userInstanceCache = new Cache<Instance | null>(1000 * 60 * 60 * 3);
|
||||
const userInstanceCache = new Cache<Instance | null>(
|
||||
3 * HOUR,
|
||||
(host) => Instances.findOneBy({ host }).then(x => x ?? undefined),
|
||||
);
|
||||
|
||||
type IsUserDetailed<Detailed extends boolean> = Detailed extends true ? Packed<'UserDetailed'> : Packed<'UserLite'>;
|
||||
type IsMeAndIsUserDetailed<ExpectsMe extends boolean | null, Detailed extends boolean> =
|
||||
|
@ -309,17 +312,15 @@ export const UserRepository = db.getRepository(User).extend({
|
|||
isModerator: user.isModerator || falsy,
|
||||
isBot: user.isBot || falsy,
|
||||
isCat: user.isCat || falsy,
|
||||
instance: user.host ? userInstanceCache.fetch(user.host,
|
||||
() => Instances.findOneBy({ host: user.host! }),
|
||||
v => v != null,
|
||||
).then(instance => instance ? {
|
||||
name: instance.name,
|
||||
softwareName: instance.softwareName,
|
||||
softwareVersion: instance.softwareVersion,
|
||||
iconUrl: instance.iconUrl,
|
||||
faviconUrl: instance.faviconUrl,
|
||||
themeColor: instance.themeColor,
|
||||
} : undefined) : undefined,
|
||||
instance: !user.host ? undefined : userInstanceCache.fetch(user.host)
|
||||
.then(instance => !instance ? undefined : {
|
||||
name: instance.name,
|
||||
softwareName: instance.softwareName,
|
||||
softwareVersion: instance.softwareVersion,
|
||||
iconUrl: instance.iconUrl,
|
||||
faviconUrl: instance.faviconUrl,
|
||||
themeColor: instance.themeColor,
|
||||
}),
|
||||
emojis: populateEmojis(user.emojis, user.host),
|
||||
onlineStatus: this.getOnlineStatus(user),
|
||||
|
||||
|
|
|
@ -10,8 +10,14 @@ import { uriPersonCache, userByIdCache } from '@/services/user-cache.js';
|
|||
import { IObject, getApId } from './type.js';
|
||||
import { resolvePerson } from './models/person.js';
|
||||
|
||||
const publicKeyCache = new Cache<UserPublickey | null>(Infinity);
|
||||
const publicKeyByUserIdCache = new Cache<UserPublickey | null>(Infinity);
|
||||
const publicKeyCache = new Cache<UserPublickey>(
|
||||
Infinity,
|
||||
(keyId) => UserPublickeys.findOneBy({ keyId }).then(x => x ?? undefined),
|
||||
);
|
||||
const publicKeyByUserIdCache = new Cache<UserPublickey>(
|
||||
Infinity,
|
||||
(userId) => UserPublickeys.findOneBy({ userId }).then(x => x ?? undefined),
|
||||
);
|
||||
|
||||
export type UriParseResult = {
|
||||
/** wether the URI was generated by us */
|
||||
|
@ -99,13 +105,9 @@ export default class DbResolver {
|
|||
if (parsed.local) {
|
||||
if (parsed.type !== 'users') return null;
|
||||
|
||||
return await userByIdCache.fetchMaybe(parsed.id, () => Users.findOneBy({
|
||||
id: parsed.id,
|
||||
}).then(x => x ?? undefined)) ?? null;
|
||||
return await userByIdCache.fetch(parsed.id) ?? null;
|
||||
} else {
|
||||
return await uriPersonCache.fetch(parsed.uri, () => Users.findOneBy({
|
||||
uri: parsed.uri,
|
||||
}));
|
||||
return await uriPersonCache.fetch(parsed.uri) ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,20 +118,12 @@ export default class DbResolver {
|
|||
user: CacheableRemoteUser;
|
||||
key: UserPublickey;
|
||||
} | null> {
|
||||
const key = await publicKeyCache.fetch(keyId, async () => {
|
||||
const key = await UserPublickeys.findOneBy({
|
||||
keyId,
|
||||
});
|
||||
|
||||
if (key == null) return null;
|
||||
|
||||
return key;
|
||||
}, key => key != null);
|
||||
const key = await publicKeyCache.fetch(keyId);
|
||||
|
||||
if (key == null) return null;
|
||||
|
||||
return {
|
||||
user: await userByIdCache.fetch(key.userId, () => Users.findOneByOrFail({ id: key.userId })) as CacheableRemoteUser,
|
||||
user: await userByIdCache.fetch(key.userId) as CacheableRemoteUser,
|
||||
key,
|
||||
};
|
||||
}
|
||||
|
@ -145,7 +139,7 @@ export default class DbResolver {
|
|||
|
||||
if (user == null) return null;
|
||||
|
||||
const key = await publicKeyByUserIdCache.fetch(user.id, () => UserPublickeys.findOneBy({ userId: user.id }), v => v != null);
|
||||
const key = await publicKeyByUserIdCache.fetch(user.id);
|
||||
|
||||
return {
|
||||
user,
|
||||
|
|
|
@ -3,10 +3,13 @@ import { Users, AccessTokens, Apps } from '@/models/index.js';
|
|||
import { AccessToken } from '@/models/entities/access-token.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { App } from '@/models/entities/app.js';
|
||||
import { localUserByIdCache, localUserByNativeTokenCache } from '@/services/user-cache.js';
|
||||
import { userByIdCache, localUserByNativeTokenCache } from '@/services/user-cache.js';
|
||||
import isNativeToken from './common/is-native-token.js';
|
||||
|
||||
const appCache = new Cache<App>(Infinity);
|
||||
const appCache = new Cache<App>(
|
||||
Infinity,
|
||||
(id) => Apps.findOneByOrFail({ id }),
|
||||
);
|
||||
|
||||
export class AuthenticationError extends Error {
|
||||
constructor(message: string) {
|
||||
|
@ -39,8 +42,7 @@ export default async (authorization: string | null | undefined, bodyToken: strin
|
|||
const token: string = maybeToken;
|
||||
|
||||
if (isNativeToken(token)) {
|
||||
const user = await localUserByNativeTokenCache.fetch(token,
|
||||
() => Users.findOneBy({ token }) as Promise<ILocalUser | null>);
|
||||
const user = await localUserByNativeTokenCache.fetch(token);
|
||||
|
||||
if (user == null) {
|
||||
throw new AuthenticationError('unknown token');
|
||||
|
@ -64,14 +66,13 @@ export default async (authorization: string | null | undefined, bodyToken: strin
|
|||
lastUsedAt: new Date(),
|
||||
});
|
||||
|
||||
const user = await localUserByIdCache.fetch(accessToken.userId,
|
||||
() => Users.findOneBy({
|
||||
id: accessToken.userId,
|
||||
}) as Promise<ILocalUser>);
|
||||
const user = await userByIdCache.fetch(accessToken.userId);
|
||||
|
||||
// can't authorize remote users
|
||||
if (!Users.isLocalUser(user)) return [null, null];
|
||||
|
||||
if (accessToken.appId) {
|
||||
const app = await appCache.fetch(accessToken.appId,
|
||||
() => Apps.findOneByOrFail({ id: accessToken.appId! }));
|
||||
const app = await appCache.fetch(accessToken.appId);
|
||||
|
||||
return [user, {
|
||||
id: accessToken.id,
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { Meta } from '@/models/entities/meta.js';
|
||||
import { insertModerationLog } from '@/services/insert-moderation-log.js';
|
||||
import { db } from '@/db/postgre.js';
|
||||
import { fetchMeta, setMeta } from '@/misc/fetch-meta.js';
|
||||
import define from '../../define.js';
|
||||
|
||||
export const meta = {
|
||||
|
@ -375,20 +374,10 @@ export default define(meta, paramDef, async (ps, me) => {
|
|||
set.deeplIsPro = ps.deeplIsPro;
|
||||
}
|
||||
|
||||
await db.transaction(async transactionalEntityManager => {
|
||||
const metas = await transactionalEntityManager.find(Meta, {
|
||||
order: {
|
||||
id: 'DESC',
|
||||
},
|
||||
});
|
||||
|
||||
const meta = metas[0];
|
||||
|
||||
if (meta) {
|
||||
await transactionalEntityManager.update(Meta, meta.id, set);
|
||||
} else {
|
||||
await transactionalEntityManager.save(Meta, set);
|
||||
}
|
||||
const meta = await fetchMeta();
|
||||
await setMeta({
|
||||
...meta,
|
||||
...set,
|
||||
});
|
||||
|
||||
insertModerationLog(me, 'updateMeta');
|
||||
|
|
|
@ -1,28 +1,20 @@
|
|||
import { IsNull } from 'typeorm';
|
||||
import { ILocalUser } from '@/models/entities/user.js';
|
||||
import { Users } from '@/models/index.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { createSystemUser } from './create-system-user.js';
|
||||
|
||||
const ACTOR_USERNAME = 'instance.actor' as const;
|
||||
|
||||
const cache = new Cache<ILocalUser>(Infinity);
|
||||
let instanceActor = await Users.findOneBy({
|
||||
host: IsNull(),
|
||||
username: ACTOR_USERNAME,
|
||||
}) as ILocalUser | undefined;
|
||||
|
||||
export async function getInstanceActor(): Promise<ILocalUser> {
|
||||
const cached = cache.get(null);
|
||||
if (cached) return cached;
|
||||
|
||||
const user = await Users.findOneBy({
|
||||
host: IsNull(),
|
||||
username: ACTOR_USERNAME,
|
||||
}) as ILocalUser | undefined;
|
||||
|
||||
if (user) {
|
||||
cache.set(null, user);
|
||||
return user;
|
||||
if (instanceActor) {
|
||||
return instanceActor;
|
||||
} else {
|
||||
const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
||||
cache.set(null, created);
|
||||
instanceActor = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
||||
return created;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,13 +36,22 @@ import { Cache } from '@/misc/cache.js';
|
|||
import { UserProfile } from '@/models/entities/user-profile.js';
|
||||
import { getActiveWebhooks } from '@/misc/webhook-cache.js';
|
||||
import { IActivity } from '@/remote/activitypub/type.js';
|
||||
import { MINUTE } from '@/const.js';
|
||||
import { updateHashtags } from '../update-hashtag.js';
|
||||
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
|
||||
import { createNotification } from '../create-notification.js';
|
||||
import { addNoteToAntenna } from '../add-note-to-antenna.js';
|
||||
import { deliverToRelays } from '../relay.js';
|
||||
|
||||
const mutedWordsCache = new Cache<{ userId: UserProfile['userId']; mutedWords: UserProfile['mutedWords']; }[]>(1000 * 60 * 5);
|
||||
const mutedWordsCache = new Cache<{ userId: UserProfile['userId']; mutedWords: UserProfile['mutedWords']; }[]>(
|
||||
5 * MINUTE,
|
||||
() => UserProfiles.find({
|
||||
where: {
|
||||
enableWordMute: true,
|
||||
},
|
||||
select: ['userId', 'mutedWords'],
|
||||
}),
|
||||
);
|
||||
|
||||
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
|
||||
|
||||
|
@ -257,12 +266,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
|
|||
incNotesCountOfUser(user);
|
||||
|
||||
// Word mute
|
||||
mutedWordsCache.fetch(null, () => UserProfiles.find({
|
||||
where: {
|
||||
enableWordMute: true,
|
||||
},
|
||||
select: ['userId', 'mutedWords'],
|
||||
})).then(us => {
|
||||
mutedWordsCache.fetch(null).then(us => {
|
||||
for (const u of us) {
|
||||
checkWordMute(note, { id: u.userId }, u.mutedWords).then(shouldMute => {
|
||||
if (shouldMute) {
|
||||
|
|
|
@ -3,29 +3,27 @@ import { Instances } from '@/models/index.js';
|
|||
import { genId } from '@/misc/gen-id.js';
|
||||
import { toPuny } from '@/misc/convert-host.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { HOUR } from '@/const.js';
|
||||
|
||||
const cache = new Cache<Instance>(1000 * 60 * 60);
|
||||
const cache = new Cache<Instance>(
|
||||
HOUR,
|
||||
(host) => Instances.findOneBy({ host }).then(x => x ?? undefined),
|
||||
);
|
||||
|
||||
export async function registerOrFetchInstanceDoc(idnHost: string): Promise<Instance> {
|
||||
const host = toPuny(idnHost);
|
||||
|
||||
const cached = cache.get(host);
|
||||
const cached = cache.fetch(host);
|
||||
if (cached) return cached;
|
||||
|
||||
const index = await Instances.findOneBy({ host });
|
||||
// apparently a new instance
|
||||
const i = await Instances.insert({
|
||||
id: genId(),
|
||||
host,
|
||||
caughtAt: new Date(),
|
||||
lastCommunicatedAt: new Date(),
|
||||
}).then(x => Instances.findOneByOrFail(x.identifiers[0]));
|
||||
|
||||
if (index == null) {
|
||||
const i = await Instances.insert({
|
||||
id: genId(),
|
||||
host,
|
||||
caughtAt: new Date(),
|
||||
lastCommunicatedAt: new Date(),
|
||||
}).then(x => Instances.findOneByOrFail(x.identifiers[0]));
|
||||
|
||||
cache.set(host, i);
|
||||
return i;
|
||||
} else {
|
||||
cache.set(host, index);
|
||||
return index;
|
||||
}
|
||||
cache.set(host, i);
|
||||
return i;
|
||||
}
|
||||
|
|
|
@ -8,11 +8,21 @@ import { Users, Relays } from '@/models/index.js';
|
|||
import { genId } from '@/misc/gen-id.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { Relay } from '@/models/entities/relay.js';
|
||||
import { MINUTE } from '@/const.js';
|
||||
import { createSystemUser } from './create-system-user.js';
|
||||
|
||||
const ACTOR_USERNAME = 'relay.actor' as const;
|
||||
|
||||
const relaysCache = new Cache<Relay[]>(1000 * 60 * 10);
|
||||
/**
|
||||
* There is only one cache key: null.
|
||||
* A cache is only used here to have expiring storage.
|
||||
*/
|
||||
const relaysCache = new Cache<Relay[]>(
|
||||
10 * MINUTE,
|
||||
() => Relays.findBy({
|
||||
status: 'accepted',
|
||||
}),
|
||||
);
|
||||
|
||||
export async function getRelayActor(): Promise<ILocalUser> {
|
||||
const user = await Users.findOneBy({
|
||||
|
@ -83,9 +93,7 @@ export async function relayRejected(id: string) {
|
|||
export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any) {
|
||||
if (activity == null) return;
|
||||
|
||||
const relays = await relaysCache.fetch(null, () => Relays.findBy({
|
||||
status: 'accepted',
|
||||
}));
|
||||
const relays = await relaysCache.fetch(null);
|
||||
if (relays.length === 0) return;
|
||||
|
||||
// TODO
|
||||
|
|
|
@ -3,10 +3,18 @@ import { Users } from '@/models/index.js';
|
|||
import { Cache } from '@/misc/cache.js';
|
||||
import { subscriber } from '@/db/redis.js';
|
||||
|
||||
export const userByIdCache = new Cache<CacheableUser>(Infinity);
|
||||
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser | null>(Infinity);
|
||||
export const localUserByIdCache = new Cache<CacheableLocalUser>(Infinity);
|
||||
export const uriPersonCache = new Cache<CacheableUser | null>(Infinity);
|
||||
export const userByIdCache = new Cache<CacheableUser>(
|
||||
Infinity,
|
||||
(id) => Users.findOneBy({ id }).then(x => x ?? undefined),
|
||||
);
|
||||
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser>(
|
||||
Infinity,
|
||||
(token) => Users.findOneBy({ token }).then(x => x ?? undefined),
|
||||
);
|
||||
export const uriPersonCache = new Cache<CacheableUser>(
|
||||
Infinity,
|
||||
(uri) => Users.findOneBy({ uri }).then(x => x ?? undefined),
|
||||
);
|
||||
|
||||
subscriber.on('message', async (_, data) => {
|
||||
const obj = JSON.parse(data);
|
||||
|
@ -27,7 +35,6 @@ subscriber.on('message', async (_, data) => {
|
|||
}
|
||||
if (Users.isLocalUser(user)) {
|
||||
localUserByNativeTokenCache.set(user.token, user);
|
||||
localUserByIdCache.set(user.id, user);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
:spellcheck="spellcheck"
|
||||
:step="step"
|
||||
:list="id"
|
||||
:maxlength="max"
|
||||
@focus="focused = true"
|
||||
@blur="focused = false"
|
||||
@keydown="onKeydown($event)"
|
||||
|
@ -58,6 +59,7 @@ const props = defineProps<{
|
|||
manualSave?: boolean;
|
||||
small?: boolean;
|
||||
large?: boolean;
|
||||
max?: number;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
:pattern="pattern"
|
||||
:autocomplete="autocomplete ? 'on' : 'off'"
|
||||
:spellcheck="spellcheck"
|
||||
:maxlength="max"
|
||||
@focus="focused = true"
|
||||
@blur="focused = false"
|
||||
@keydown="onKeydown($event)"
|
||||
|
@ -54,6 +55,7 @@ const props = withDefaults(defineProps<{
|
|||
pre?: boolean;
|
||||
debounce?: boolean;
|
||||
manualSave?: boolean;
|
||||
max?: number;
|
||||
}>(), {
|
||||
pattern: undefined,
|
||||
placeholder: '',
|
||||
|
|
|
@ -103,7 +103,7 @@ export const apiWithDialog = ((
|
|||
promiseDialog(promise, null, (err) => {
|
||||
alert({
|
||||
type: 'error',
|
||||
text: err.message + '\n' + (err as any).id,
|
||||
text: (err.message + '\n' + (err?.endpoint ?? '') + (err?.code ?? '')).trim(),
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -141,7 +141,7 @@ export function promiseDialog<T extends Promise<any>>(
|
|||
}
|
||||
});
|
||||
|
||||
// NOTE: dynamic importすると挙動がおかしくなる(showingの変更が伝播しない)
|
||||
// NOTE: dynamic import results in strange behaviour (showing is not reactive)
|
||||
popup(MkWaitingDialog, {
|
||||
success,
|
||||
showing,
|
||||
|
|
|
@ -26,8 +26,27 @@
|
|||
|
||||
<FormSection v-if="iAmModerator">
|
||||
<template #label>Moderation</template>
|
||||
<FormSwitch v-model="suspended" class="_formBlock" @update:modelValue="toggleSuspend">{{ i18n.ts.stopActivityDelivery }}</FormSwitch>
|
||||
<FormSwitch v-model="isBlocked" class="_formBlock" @update:modelValue="toggleBlock">{{ i18n.ts.blockThisInstance }}</FormSwitch>
|
||||
<FormSwitch
|
||||
:model-value="suspended || isBlocked"
|
||||
@update:model-value="newValue => {suspended = newValue; toggleSuspend() }"
|
||||
:disabled="isBlocked"
|
||||
class="_formBlock"
|
||||
>
|
||||
{{ i18n.ts.stopActivityDelivery }}
|
||||
<template #caption>
|
||||
{{ i18n.ts.stopActivityDeliveryDescription }}
|
||||
</template>
|
||||
</FormSwitch>
|
||||
<FormSwitch
|
||||
v-model="isBlocked"
|
||||
@update:modelValue="toggleBlock"
|
||||
class="_formBlock"
|
||||
>
|
||||
{{ i18n.ts.blockThisInstance }}
|
||||
<template #caption>
|
||||
{{ i18n.ts.blockThisInstanceDescription }}
|
||||
</template>
|
||||
</FormSwitch>
|
||||
<MkButton @click="refreshMetadata"><i class="fas fa-refresh"></i> Refresh metadata</MkButton>
|
||||
</FormSection>
|
||||
|
||||
|
@ -152,7 +171,7 @@ const usersPagination = {
|
|||
offsetMode: true,
|
||||
};
|
||||
|
||||
async function fetch() {
|
||||
async function fetch(): Promise<void> {
|
||||
instance = await os.api('federation/show-instance', {
|
||||
host: props.host,
|
||||
});
|
||||
|
@ -160,21 +179,21 @@ async function fetch() {
|
|||
isBlocked = instance.isBlocked;
|
||||
}
|
||||
|
||||
async function toggleBlock(ev) {
|
||||
async function toggleBlock(): Promise<void> {
|
||||
if (meta == null) return;
|
||||
await os.api('admin/update-meta', {
|
||||
blockedHosts: isBlocked ? meta.blockedHosts.concat([instance.host]) : meta.blockedHosts.filter(x => x !== instance.host),
|
||||
});
|
||||
}
|
||||
|
||||
async function toggleSuspend(v) {
|
||||
async function toggleSuspend(): Promise<void> {
|
||||
await os.api('admin/federation/update-instance', {
|
||||
host: instance.host,
|
||||
isSuspended: suspended,
|
||||
});
|
||||
}
|
||||
|
||||
function refreshMetadata() {
|
||||
function refreshMetadata(): void {
|
||||
os.api('admin/federation/refresh-remote-instance-metadata', {
|
||||
host: instance.host,
|
||||
});
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<template #label>{{ i18n.ts._profile.name }}</template>
|
||||
</FormInput>
|
||||
|
||||
<FormTextarea v-model="profile.description" :max="500" tall manual-save class="_formBlock">
|
||||
<FormTextarea v-model="profile.description" :max="2048" tall manual-save class="_formBlock">
|
||||
<template #label>{{ i18n.ts._profile.description }}</template>
|
||||
<template #caption>{{ i18n.ts._profile.youCanIncludeHashtags }}</template>
|
||||
</FormTextarea>
|
||||
|
|
|
@ -30,14 +30,6 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
|||
const i18n = await swLang.i18n as I18n<any>;
|
||||
const { t } = i18n;
|
||||
switch (data.type) {
|
||||
/*
|
||||
case 'driveFileCreated': // TODO (Server Side)
|
||||
return [t('_notification.fileUploaded'), {
|
||||
body: body.name,
|
||||
icon: body.url,
|
||||
data
|
||||
}];
|
||||
*/
|
||||
case 'notification':
|
||||
switch (data.body.type) {
|
||||
case 'follow':
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
declare var self: ServiceWorkerGlobalScope;
|
||||
|
||||
import { createEmptyNotification, createNotification } from '@/scripts/create-notification';
|
||||
import { createNotification } from '@/scripts/create-notification';
|
||||
import { swLang } from '@/scripts/lang';
|
||||
import { swNotificationRead } from '@/scripts/notification-read';
|
||||
import { pushNotificationDataMap } from '@/types';
|
||||
|
@ -67,8 +67,6 @@ self.addEventListener('push', ev => {
|
|||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return createEmptyNotification();
|
||||
}));
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue