Normalized channel message format
ci/woodpecker/push/ociImageTag Pipeline was successful Details
ci/woodpecker/tag/ociImageTag Pipeline was successful Details

This commit is contained in:
Natty 2023-07-31 19:43:45 +02:00
parent f9ab6d7c65
commit 0409015c96
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
11 changed files with 53 additions and 58 deletions

View File

@ -65,9 +65,9 @@ export default abstract class Channel {
public onMessage?(type: string, body: any): void; public onMessage?(type: string, body: any): void;
protected withPackedNote( protected withPackedNote(
callback: (note: Packed<"Note">) => void, callback: (noteMessage: { type: "note", body: Packed<"Note"> }) => void,
): (Note) => void { ): (noteMessage: { type: "note", body: Note }) => void {
return async (note: Note) => { return async ({body: note}) => {
try { try {
// because `note` was previously JSON.stringify'ed, the fields that // because `note` was previously JSON.stringify'ed, the fields that
// were objects before are now strings and have to be restored or // were objects before are now strings and have to be restored or
@ -79,7 +79,7 @@ export default abstract class Channel {
const packed = await Notes.pack(note, this.user, { detail: true }); const packed = await Notes.pack(note, this.user, { detail: true });
callback(packed); callback({ type: "note", body: packed });
} catch (err) { } catch (err) {
if ( if (
err instanceof IdentifiableError && err instanceof IdentifiableError &&

View File

@ -34,7 +34,7 @@ export default class extends Channel {
this.subscriber.off("notesStream", this.onNote); this.subscriber.off("notesStream", this.onNote);
} }
private async onNote(note: Packed<"Note">) { private async onNote({body: note}: { type: "note", body: Packed<"Note"> }) {
if (note.visibility !== "public") return; if (note.visibility !== "public") return;
// 関係ない返信は除外 // 関係ない返信は除外

View File

@ -1,12 +1,12 @@
import Channel from "../channel.js"; import Channel from "../channel.js";
import { normalizeForSearch } from "@/misc/normalize-for-search.js"; import {normalizeForSearch} from "@/misc/normalize-for-search.js";
import { isUserRelated } from "@/misc/is-user-related.js"; import {isUserRelated} from "@/misc/is-user-related.js";
import type { Packed } from "@/misc/schema.js"; import type {Packed} from "@/misc/schema.js";
export default class extends Channel { export default class extends Channel {
public readonly chName = "hashtag";
public static shouldShare = false; public static shouldShare = false;
public static requireCredential = false; public static requireCredential = false;
public readonly chName = "hashtag";
private q: string[][]; private q: string[][];
constructor(id: string, connection: Channel["connection"]) { constructor(id: string, connection: Channel["connection"]) {
@ -23,7 +23,12 @@ export default class extends Channel {
this.subscriber.on("notesStream", this.onNote); this.subscriber.on("notesStream", this.onNote);
} }
private async onNote(note: Packed<"Note">) { public dispose() {
// Unsubscribe events
this.subscriber.off("notesStream", this.onNote);
}
private async onNote({body: note}: {type: "note", body: Packed<"Note">}) {
if (note.visibility === "hidden") return; if (note.visibility === "hidden") return;
const noteTags = note.tags const noteTags = note.tags
? note.tags.map((t: string) => t.toLowerCase()) ? note.tags.map((t: string) => t.toLowerCase())
@ -44,9 +49,4 @@ export default class extends Channel {
this.send("note", note); this.send("note", note);
} }
public dispose() {
// Unsubscribe events
this.subscriber.off("notesStream", this.onNote);
}
} }

View File

@ -27,7 +27,7 @@ export default class extends Channel {
this.subscriber.off("notesStream", this.onNote); this.subscriber.off("notesStream", this.onNote);
} }
private async onNote(note: Packed<"Note">) { private async onNote({ body: note }: { type: "note", body: Packed<"Note"> }) {
if (note.visibility === "hidden") return; if (note.visibility === "hidden") return;
// Filter away notes that are not authored by the user or any of the followed users // Filter away notes that are not authored by the user or any of the followed users

View File

@ -36,7 +36,7 @@ export default class extends Channel {
this.subscriber.off("notesStream", this.onNote); this.subscriber.off("notesStream", this.onNote);
} }
private async onNote(note: Packed<"Note">) { private async onNote({ body: note }: { type: "note", "body": Packed<"Note"> }) {
if (note.visibility === "hidden") if (note.visibility === "hidden")
return; return;

View File

@ -33,7 +33,7 @@ export default class extends Channel {
this.subscriber.off("notesStream", this.onNote); this.subscriber.off("notesStream", this.onNote);
} }
private async onNote(note: Packed<"Note">) { private async onNote({ body: note }: { type: "note", body: Packed<"Note"> }) {
if (note.user.host !== null) return; if (note.user.host !== null) return;
if (note.visibility !== "public") return; if (note.visibility !== "public") return;

View File

@ -1,14 +1,14 @@
import Channel from "../channel.js"; import Channel from "../channel.js";
import { fetchMeta } from "@/misc/fetch-meta.js"; import {fetchMeta} from "@/misc/fetch-meta.js";
import { getWordHardMute } from "@/misc/check-word-mute.js"; import {getWordHardMute} from "@/misc/check-word-mute.js";
import { isUserRelated } from "@/misc/is-user-related.js"; import {isUserRelated} from "@/misc/is-user-related.js";
import { isInstanceMuted } from "@/misc/is-instance-muted.js"; import {isInstanceMuted} from "@/misc/is-instance-muted.js";
import type { Packed } from "@/misc/schema.js"; import type {Packed} from "@/misc/schema.js";
export default class extends Channel { export default class extends Channel {
public readonly chName = "recommendedTimeline";
public static shouldShare = true; public static shouldShare = true;
public static requireCredential = true; public static requireCredential = true;
public readonly chName = "recommendedTimeline";
private withReplies: boolean; private withReplies: boolean;
constructor(id: string, connection: Channel["connection"]) { constructor(id: string, connection: Channel["connection"]) {
@ -31,7 +31,12 @@ export default class extends Channel {
this.subscriber.on("notesStream", this.onNote); this.subscriber.on("notesStream", this.onNote);
} }
private async onNote(note: Packed<"Note">) { public dispose() {
// Unsubscribe events
this.subscriber.off("notesStream", this.onNote);
}
private async onNote({ body: note }: { type: "note", body: Packed<"Note"> }) {
if (note.visibility === "hidden") return; if (note.visibility === "hidden") return;
// チャンネルの投稿ではなく、自分自身の投稿 または // チャンネルの投稿ではなく、自分自身の投稿 または
// チャンネルの投稿ではなく、その投稿のユーザーをフォローしている または // チャンネルの投稿ではなく、その投稿のユーザーをフォローしている または
@ -90,9 +95,4 @@ export default class extends Channel {
this.send("note", note); this.send("note", note);
} }
public dispose() {
// Unsubscribe events
this.subscriber.off("notesStream", this.onNote);
}
} }

View File

@ -1,15 +1,15 @@
import Channel from "../channel.js"; import Channel from "../channel.js";
import { UserListJoinings, UserLists } from "@/models/index.js"; import {UserListJoinings, UserLists} from "@/models/index.js";
import type { User } from "@/models/entities/user.js"; import type {User} from "@/models/entities/user.js";
import { isUserRelated } from "@/misc/is-user-related.js"; import {isUserRelated} from "@/misc/is-user-related.js";
import type { Packed } from "@/misc/schema.js"; import type {Packed} from "@/misc/schema.js";
export default class extends Channel { export default class extends Channel {
public readonly chName = "userList";
public static shouldShare = false; public static shouldShare = false;
public static requireCredential = false; public static requireCredential = false;
private listId: string; public readonly chName = "userList";
public listUsers: User["id"][] = []; public listUsers: User["id"][] = [];
private listId: string;
private listUsersClock: NodeJS.Timer; private listUsersClock: NodeJS.Timer;
constructor(id: string, connection: Channel["connection"]) { constructor(id: string, connection: Channel["connection"]) {
@ -37,6 +37,14 @@ export default class extends Channel {
this.listUsersClock = setInterval(this.updateListUsers, 5000); this.listUsersClock = setInterval(this.updateListUsers, 5000);
} }
public dispose() {
// Unsubscribe events
this.subscriber.off(`userListStream:${this.listId}`, this.send);
this.subscriber.off("notesStream", this.onNote);
clearInterval(this.listUsersClock);
}
private async updateListUsers() { private async updateListUsers() {
const users = await UserListJoinings.find({ const users = await UserListJoinings.find({
where: { where: {
@ -48,7 +56,7 @@ export default class extends Channel {
this.listUsers = users.map((x) => x.userId); this.listUsers = users.map((x) => x.userId);
} }
private async onNote(note: Packed<"Note">) { private async onNote({ body: note }: { type: "note", body: Packed<"Note"> }) {
if (note.visibility === "hidden") return; if (note.visibility === "hidden") return;
if (!this.listUsers.includes(note.userId)) return; if (!this.listUsers.includes(note.userId)) return;
@ -61,12 +69,4 @@ export default class extends Channel {
this.send("note", note); this.send("note", note);
} }
public dispose() {
// Unsubscribe events
this.subscriber.off(`userListStream:${this.listId}`, this.send);
this.subscriber.off("notesStream", this.onNote);
clearInterval(this.listUsersClock);
}
} }

View File

@ -213,7 +213,7 @@ export type StreamMessages = {
}; };
notes: { notes: {
name: "notesStream"; name: "notesStream";
payload: Note; payload: EventUnionFromDictionary<{ note: Note }>;
}; };
}; };

View File

@ -1,10 +1,10 @@
import type * as http from "node:http"; import type * as http from "node:http";
import { EventEmitter } from "events"; import {EventEmitter} from "events";
import type { ParsedUrlQuery } from "querystring"; import type {ParsedUrlQuery} from "querystring";
import * as websocket from "websocket"; import * as websocket from "websocket";
import { subscriber as redisClient } from "@/db/redis.js"; import {subscriber as redisClient} from "@/db/redis.js";
import { Users } from "@/models/index.js"; import {Users} from "@/models/index.js";
import MainStreamConnection from "./stream/index.js"; import MainStreamConnection from "./stream/index.js";
import authenticate from "./authenticate.js"; import authenticate from "./authenticate.js";

View File

@ -108,7 +108,7 @@ class Publisher {
}; };
public publishNotesStream = (note: Note): void => { public publishNotesStream = (note: Note): void => {
this.publish("notesStream", null, note); this.publish("notesStream", "note", note);
}; };
public publishAdminStream = <K extends keyof AdminStreamTypes>( public publishAdminStream = <K extends keyof AdminStreamTypes>(
@ -125,15 +125,10 @@ class Publisher {
private publish = ( private publish = (
channel: StreamChannels, channel: StreamChannels,
type: string | null, type: string,
value?: any, value?: any,
): void => { ): void => {
const message = const message = { type: type, body: value };
type == null
? value
: value == null
? { type: type, body: null }
: { type: type, body: value };
redisClient.publish( redisClient.publish(
config.host, config.host,