Skip to content

Commit

Permalink
Improved streaming and fixed issues with Question
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Dec 29, 2023
1 parent 1ea6fff commit 3cac1be
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/backend/src/core/GlobalEventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ export interface NoteEventTypes {
deletedAt: Date;
};
updated: {
updatedAt: Date | null;
cw: string | null;
text: string | null;
emojis: string[] | null;
files: Packed<'DriveFile'>[] | null;
poll: string[] | null;
};
reacted: {
reaction: string;
Expand Down
13 changes: 12 additions & 1 deletion packages/backend/src/core/NoteUpdateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { concat } from "@/misc/prelude/array.js";
import { extractHashtags } from "@/misc/extract-hashtags.js";
import { extractCustomEmojisFromMfm } from "@/misc/extract-custom-emojis-from-mfm.js";
import util from 'util';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';

type MinimumUser = {
id: MiUser['id'];
Expand Down Expand Up @@ -62,6 +63,7 @@ export class NoteUpdateService implements OnApplicationShutdown {
private notesRepository: NotesRepository,

private userEntityService: UserEntityService,
private noteEntityService: NoteEntityService,
private globalEventService: GlobalEventService,
private relayService: RelayService,
private apDeliverManagerService: ApDeliverManagerService,
Expand Down Expand Up @@ -215,7 +217,16 @@ export class NoteUpdateService implements OnApplicationShutdown {
if (!silent) {
if (this.userEntityService.isLocalUser(user)) this.activeUsersChart.write(user);

this.globalEventService.publishNoteStream(note.id, 'updated', { cw: note.cw, text: note.text });
const pack = await this.noteEntityService.pack(note);

this.globalEventService.publishNoteStream(note.id, 'updated', {
updatedAt: note.updatedAt,
cw: note.cw,
text: note.text,
emojis: note.emojis,
files: pack.files ? pack.files : null,
poll: pack.poll ? pack.poll : null,
});

//#region AP deliver
if (this.userEntityService.isLocalUser(user)) {
Expand Down
23 changes: 17 additions & 6 deletions packages/backend/src/core/activitypub/ApInboxService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,23 @@ export class ApInboxService {
if (isActor(object)) {
await this.apPersonService.updatePerson(actor.uri, resolver, object);
return 'ok: Person updated';
} else if (getApType(object) === 'Question') {
await this.apQuestionService.updateQuestion(object, resolver).catch(err => console.error(err));
return 'ok: Question updated';
} else if (getApType(object) === 'Note') {
await this.updateNote(resolver, actor, object, false, activity);
return 'ok: Note updated';
} else if (getApType(object) === 'Question' || getApType(object) === 'Note') {
const uri = typeof object === 'string' ? object : object.id;
if (uri == null) throw new Error('uri is null');

// URIがこのサーバーを指しているならスキップ
if (uri.startsWith(this.config.url + '/')) throw new Error('uri points local');

//#region このサーバーに既に登録されているか
const note = await this.notesRepository.findOneBy({ uri });
if (note == null) throw new Error('Note is not registed');
if (object.updated && (!note.updatedAt || new Date(object.updated) > note.updatedAt)) {
await this.updateNote(resolver, actor, object, false, activity);
return 'ok: Note updated';
} else {
await this.apQuestionService.updateQuestion(object, note, resolver).catch(err => console.error(err));
return 'ok: Question updated';
}
} else {
return `skip: Unknown type: ${getApType(object)}`;
}
Expand Down
14 changes: 2 additions & 12 deletions packages/backend/src/core/activitypub/models/ApQuestionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import type { NotesRepository, PollsRepository } from '@/models/_.js';
import type { MiNote, NotesRepository, PollsRepository } from '@/models/_.js';

Check failure on line 8 in packages/backend/src/core/activitypub/models/ApQuestionService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

Multiple spaces found before 'MiNote'
import type { Config } from '@/config.js';
import type { IPoll } from '@/models/Poll.js';
import type Logger from '@/logger.js';
Expand Down Expand Up @@ -65,17 +65,7 @@ export class ApQuestionService {
* @returns true if updated
*/
@bindThis
public async updateQuestion(value: string | IObject, resolver?: Resolver): Promise<boolean> {
const uri = typeof value === 'string' ? value : value.id;
if (uri == null) throw new Error('uri is null');

// URIがこのサーバーを指しているならスキップ
if (uri.startsWith(this.config.url + '/')) throw new Error('uri points local');

//#region このサーバーに既に登録されているか
const note = await this.notesRepository.findOneBy({ uri });
if (note == null) throw new Error('Question is not registed');

public async updateQuestion(value: string | IObject, note: MiNote, resolver?: Resolver): Promise<boolean> {
const poll = await this.pollsRepository.findOneBy({ noteId: note.id });
if (poll == null) throw new Error('Question is not registed');
//#endregion
Expand Down
5 changes: 4 additions & 1 deletion packages/frontend/src/scripts/use-note-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ export function useNoteCapture(props: {
}

case 'updated': {
note.value.updatedAt = new Date().toISOString();
note.value.updatedAt = body.updatedAt;
note.value.cw = body.cw;
note.value.text = body.text;
note.value.emojis = body.emojis;
note.value.files = body.files;
note.value.poll = body.poll;
break;
}

Expand Down

0 comments on commit 3cac1be

Please sign in to comment.