refactor (backend): it turns out that sending the entire note object was redundant
This commit is contained in:
parent
6349705fb2
commit
3b65ebcb3e
6 changed files with 10 additions and 94 deletions
2
packages/backend-rs/index.d.ts
vendored
2
packages/backend-rs/index.d.ts
vendored
|
@ -1098,7 +1098,7 @@ export interface Webhook {
|
|||
latestSentAt: Date | null
|
||||
latestStatus: number | null
|
||||
}
|
||||
export function addNoteToAntenna(antennaId: string, note: Note): void
|
||||
export function addNoteToAntenna(antennaId: string, noteId: string): void
|
||||
/** Initializes Cuid2 generator. Must be called before any [create_id]. */
|
||||
export function initIdGenerator(length: number, fingerprint: string): void
|
||||
export function getTimestamp(id: string): number
|
||||
|
|
|
@ -1,20 +1,23 @@
|
|||
use crate::database::{redis_conn, redis_key};
|
||||
use crate::model::entity::note;
|
||||
use crate::service::stream::{publish, Error, Stream};
|
||||
use crate::util::id::get_timestamp;
|
||||
use redis::{streams::StreamMaxlen, Commands};
|
||||
|
||||
#[crate::export]
|
||||
pub fn add_note_to_antenna(antenna_id: &str, note: ¬e::Model) -> Result<(), Error> {
|
||||
pub fn add_note_to_antenna(antenna_id: &str, note_id: &str) -> Result<(), Error> {
|
||||
redis_conn()?.xadd_maxlen(
|
||||
redis_key(format!("antennaTimeline:{}", antenna_id)),
|
||||
StreamMaxlen::Approx(200),
|
||||
format!("{}-*", get_timestamp(¬e.id)),
|
||||
&[("note", ¬e.id)],
|
||||
format!("{}-*", get_timestamp(¬e_id)),
|
||||
&[("note", ¬e_id)],
|
||||
)?;
|
||||
|
||||
let stream = Stream::Antenna {
|
||||
id: antenna_id.to_string(),
|
||||
};
|
||||
publish(&stream, Some("note"), Some(serde_json::to_string(note)?))
|
||||
publish(
|
||||
&stream,
|
||||
Some("note"),
|
||||
Some(format!("{{ \"id\": \"{}\" }}", note_id)),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -178,7 +178,6 @@
|
|||
"ts-loader": "9.5.1",
|
||||
"ts-node": "10.9.2",
|
||||
"tsconfig-paths": "4.2.0",
|
||||
"type-fest": "4.15.0",
|
||||
"typescript": "5.4.5",
|
||||
"webpack": "^5.91.0",
|
||||
"ws": "8.16.0"
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
// https://gist.github.com/tkrotoff/a6baf96eb6b61b445a9142e5555511a0
|
||||
import type { Primitive } from "type-fest";
|
||||
|
||||
type NullToUndefined<T> = T extends null
|
||||
? undefined
|
||||
: T extends Primitive | Function | Date | RegExp
|
||||
? T
|
||||
: T extends Array<infer U>
|
||||
? Array<NullToUndefined<U>>
|
||||
: T extends Map<infer K, infer V>
|
||||
? Map<K, NullToUndefined<V>>
|
||||
: T extends Set<infer U>
|
||||
? Set<NullToUndefined<U>>
|
||||
: T extends object
|
||||
? { [K in keyof T]: NullToUndefined<T[K]> }
|
||||
: unknown;
|
||||
|
||||
type UndefinedToNull<T> = T extends undefined
|
||||
? null
|
||||
: T extends Primitive | Function | Date | RegExp
|
||||
? T
|
||||
: T extends Array<infer U>
|
||||
? Array<UndefinedToNull<U>>
|
||||
: T extends Map<infer K, infer V>
|
||||
? Map<K, UndefinedToNull<V>>
|
||||
: T extends Set<infer U>
|
||||
? Set<NullToUndefined<U>>
|
||||
: T extends object
|
||||
? { [K in keyof T]: UndefinedToNull<T[K]> }
|
||||
: unknown;
|
||||
|
||||
function _nullToUndefined<T>(obj: T): NullToUndefined<T> {
|
||||
if (obj === null) {
|
||||
return undefined as any;
|
||||
}
|
||||
|
||||
if (typeof obj === "object") {
|
||||
if (obj instanceof Map) {
|
||||
obj.forEach((value, key) => obj.set(key, _nullToUndefined(value)));
|
||||
} else {
|
||||
for (const key in obj) {
|
||||
obj[key] = _nullToUndefined(obj[key]) as any;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj as any;
|
||||
}
|
||||
|
||||
function _undefinedToNull<T>(obj: T): UndefinedToNull<T> {
|
||||
if (obj === undefined) {
|
||||
return null as any;
|
||||
}
|
||||
|
||||
if (typeof obj === "object") {
|
||||
if (obj instanceof Map) {
|
||||
obj.forEach((value, key) => obj.set(key, _undefinedToNull(value)));
|
||||
} else {
|
||||
for (const key in obj) {
|
||||
obj[key] = _undefinedToNull(obj[key]) as any;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively converts all undefined values to null.
|
||||
*
|
||||
* @param obj object to convert
|
||||
* @returns a copy of the object with all its undefined values converted to null
|
||||
*/
|
||||
export function undefinedToNull<T>(obj: T) {
|
||||
return _undefinedToNull(structuredClone(obj));
|
||||
}
|
|
@ -62,7 +62,6 @@ import { Mutex } from "redis-semaphore";
|
|||
import { langmap } from "@/misc/langmap.js";
|
||||
import Logger from "@/services/logger.js";
|
||||
import { inspect } from "node:util";
|
||||
import { undefinedToNull } from "@/prelude/undefined-to-null.js";
|
||||
|
||||
const logger = new Logger("create-note");
|
||||
|
||||
|
@ -399,8 +398,7 @@ export default async (
|
|||
for (const antenna of await getAntennas()) {
|
||||
checkHitAntenna(antenna, note, user).then((hit) => {
|
||||
if (hit) {
|
||||
// TODO: do this more sanely
|
||||
addNoteToAntenna(antenna.id, undefinedToNull(note) as Note);
|
||||
addNoteToAntenna(antenna.id, note.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -521,9 +521,6 @@ importers:
|
|||
tsconfig-paths:
|
||||
specifier: 4.2.0
|
||||
version: 4.2.0
|
||||
type-fest:
|
||||
specifier: 4.15.0
|
||||
version: 4.15.0
|
||||
typescript:
|
||||
specifier: 5.4.5
|
||||
version: 5.4.5
|
||||
|
@ -16635,11 +16632,6 @@ packages:
|
|||
engines: {node: '>=8'}
|
||||
dev: true
|
||||
|
||||
/type-fest@4.15.0:
|
||||
resolution: {integrity: sha512-tB9lu0pQpX5KJq54g+oHOLumOx+pMep4RaM6liXh2PKmVRFF+/vAtUP0ZaJ0kOySfVNjF6doBWPHhBhISKdlIA==}
|
||||
engines: {node: '>=16'}
|
||||
dev: true
|
||||
|
||||
/type-is@1.6.18:
|
||||
resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==}
|
||||
engines: {node: '>= 0.6'}
|
||||
|
|
Loading…
Reference in a new issue