refactor (backend): port note watch/unwatch to backend-rs

This commit is contained in:
naskya 2024-04-24 13:16:25 +09:00
parent 879d499486
commit 109884f6d8
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
9 changed files with 53 additions and 35 deletions

View file

@ -1122,6 +1122,8 @@ export interface Webhook {
latestSentAt: Date | null
latestStatus: number | null
}
export function watchNote(watcherId: string, noteAuthorId: string, noteId: string): Promise<void>
export function unwatchNote(watcherId: string, noteId: string): Promise<void>
export enum ChatEvent {
Message = 'message',
Read = 'read',

View file

@ -310,7 +310,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}
const { loadEnv, loadConfig, stringToAcct, acctToString, addNoteToAntenna, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getNoteSummary, toMastodonId, fromMastodonId, fetchMeta, metaToPugArgs, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, AntennaSrcEnum, DriveFileUsageHintEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, ChatEvent, publishToChatStream, getTimestamp, genId, genIdAt, secureRndstr } = nativeBinding
const { loadEnv, loadConfig, stringToAcct, acctToString, addNoteToAntenna, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getNoteSummary, toMastodonId, fromMastodonId, fetchMeta, metaToPugArgs, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, AntennaSrcEnum, DriveFileUsageHintEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, watchNote, unwatchNote, ChatEvent, publishToChatStream, getTimestamp, genId, genIdAt, secureRndstr } = nativeBinding
module.exports.loadEnv = loadEnv
module.exports.loadConfig = loadConfig
@ -354,6 +354,8 @@ module.exports.RelayStatusEnum = RelayStatusEnum
module.exports.UserEmojimodpermEnum = UserEmojimodpermEnum
module.exports.UserProfileFfvisibilityEnum = UserProfileFfvisibilityEnum
module.exports.UserProfileMutingnotificationtypesEnum = UserProfileMutingnotificationtypesEnum
module.exports.watchNote = watchNote
module.exports.unwatchNote = unwatchNote
module.exports.ChatEvent = ChatEvent
module.exports.publishToChatStream = publishToChatStream
module.exports.getTimestamp = getTimestamp

View file

@ -1 +1,2 @@
pub mod note;
pub mod stream;

View file

@ -0,0 +1 @@
pub mod watch;

View file

@ -0,0 +1,42 @@
use crate::database::db_conn;
use crate::model::entity::note_watching;
use crate::util::id::gen_id;
use sea_orm::{ActiveValue, ColumnTrait, DbErr, EntityTrait, ModelTrait, QueryFilter};
#[crate::export]
pub async fn watch_note(
watcher_id: &str,
note_author_id: &str,
note_id: &str,
) -> Result<(), DbErr> {
if watcher_id != note_author_id {
note_watching::Entity::insert(note_watching::ActiveModel {
id: ActiveValue::set(gen_id()),
created_at: ActiveValue::set(chrono::Local::now().naive_local()),
user_id: ActiveValue::Set(watcher_id.to_string()),
note_user_id: ActiveValue::Set(note_author_id.to_string()),
note_id: ActiveValue::Set(note_id.to_string()),
})
.exec(db_conn().await?)
.await?;
}
Ok(())
}
#[crate::export]
pub async fn unwatch_note(watcher_id: &str, note_id: &str) -> Result<(), DbErr> {
let db = db_conn().await?;
let entry = note_watching::Entity::find()
.filter(note_watching::Column::UserId.eq(watcher_id))
.filter(note_watching::Column::NoteId.eq(note_id))
.one(db)
.await?;
if let Some(entry) = entry {
entry.delete(db).await?;
}
Ok(())
}

View file

@ -1,4 +1,4 @@
import watch from "@/services/note/watch.js";
import { watchNote } from "backend-rs";
import define from "@/server/api/define.js";
import { getNote } from "@/server/api/common/getters.js";
import { ApiError } from "@/server/api/error.js";
@ -34,5 +34,5 @@ export default define(meta, paramDef, async (ps, user) => {
throw err;
});
await watch(user.id, note);
await watchNote(user.id, note.userId, note.id);
});

View file

@ -1,4 +1,4 @@
import unwatch from "@/services/note/unwatch.js";
import { unwatchNote } from "backend-rs";
import define from "@/server/api/define.js";
import { getNote } from "@/server/api/common/getters.js";
import { ApiError } from "@/server/api/error.js";
@ -34,5 +34,5 @@ export default define(meta, paramDef, async (ps, user) => {
throw err;
});
await unwatch(user.id, note);
await unwatchNote(user.id, note.id);
});

View file

@ -1,10 +0,0 @@
import type { User } from "@/models/entities/user.js";
import { NoteWatchings } from "@/models/index.js";
import type { Note } from "@/models/entities/note.js";
export default async (me: User["id"], note: Note) => {
await NoteWatchings.delete({
noteId: note.id,
userId: me,
});
};

View file

@ -1,20 +0,0 @@
import type { User } from "@/models/entities/user.js";
import type { Note } from "@/models/entities/note.js";
import { NoteWatchings } from "@/models/index.js";
import { genId } from "backend-rs";
import type { NoteWatching } from "@/models/entities/note-watching.js";
export default async (me: User["id"], note: Note) => {
// 自分の投稿はwatchできない
if (me === note.userId) {
return;
}
await NoteWatchings.insert({
id: genId(),
createdAt: new Date(),
noteId: note.id,
userId: me,
noteUserId: note.userId,
} as NoteWatching);
};