Skip to content

Commit

Permalink
fix(backend): Troubleshoot Messaging Not Working
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Sep 22, 2023
1 parent 7594869 commit c3f2a9b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
24 changes: 20 additions & 4 deletions packages/backend/src/core/MessagingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { MessagingMessageEntityService } from '@/core/entities/MessagingMessageEntityService.js';
import { PushNotificationService } from '@/core/PushNotificationService.js';
import { bindThis } from '@/decorators.js';
import type { UserProfilesRepository } from "@/models/_.js";
import {IMentionedRemoteUsers} from '@/models/Note.js';

Check failure on line 27 in packages/backend/src/core/MessagingService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 27 in packages/backend/src/core/MessagingService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';

@Injectable()
export class MessagingService {
Expand All @@ -33,6 +36,9 @@ export class MessagingService {
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,

@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,

@Inject(DI.messagingMessagesRepository)
private messagingMessagesRepository: MessagingMessagesRepository,

Expand All @@ -42,6 +48,7 @@ export class MessagingService {
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,

private noteEntityService: NoteEntityService,
private userEntityService: UserEntityService,
private messagingMessageEntityService: MessagingMessageEntityService,
private idService: IdService,
Expand All @@ -53,10 +60,12 @@ export class MessagingService {
}

@bindThis
public async createMessage(user: { id: MiUser['id']; host: MiUser['host']; }, recipientUser: MiUser | undefined, recipientGroup: MiUserGroup | undefined, text: string | null | undefined, file: MiDriveFile | null, uri?: string) {
public async createMessage(user: { id: MiUser['id']; host: MiUser['host']; }, recipientUser: MiUser | null, recipientGroup: MiUserGroup | null, text: string | null | undefined, file: MiDriveFile | null, uri?: string) {
const data = new Date();

const message = {
id: this.idService.genId(),
createdAt: new Date(),
id: this.idService.genId(data),
createdAt: data,
fileId: file ? file.id : null,
recipientId: recipientUser ? recipientUser.id : null,
groupId: recipientGroup ? recipientGroup.id : null,
Expand Down Expand Up @@ -125,19 +134,26 @@ export class MessagingService {
}, 2000);

if (recipientUser && this.userEntityService.isLocalUser(user) && this.userEntityService.isRemoteUser(recipientUser)) {
const profiles = await this.userProfilesRepository.findBy({ userId: In([recipientUser.id]) });
const profile = profiles.find(p => p.userId === recipientUser.id);
const url = profile != null ? profile.url : null;

const note = {
id: message.id,
createdAt: message.createdAt,
fileIds: message.fileId ? [message.fileId] : [],
text: message.text,
userId: message.userId,
visibility: 'specified',
emojis: [{}],
mentions: [recipientUser].map(u => u.id),
mentionedRemoteUsers: JSON.stringify([recipientUser].map(u => ({
uri: u.uri,
url: url,
username: u.username,
host: u.host,
}))),
} as IMentionedRemoteUsers[0]
))),
} as MiNote;

const activity = this.apRendererService.addContext(this.apRendererService.renderCreate(await this.apRendererService.renderNote(note, false, true), note));
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/core/NoteCreateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ export class NoteCreateService implements OnApplicationShutdown {

//#region AP deliver
if (this.userEntityService.isLocalUser(user)) {
(async () => {
await (async () => {
const noteActivity = await this.renderNoteOrRenoteActivity(data, note);
const dm = this.apDeliverManagerService.createDeliverManager(user, noteActivity);

Expand All @@ -670,13 +670,13 @@ export class NoteCreateService implements OnApplicationShutdown {

// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
if (data.reply && data.reply.userHost !== null) {
const u = await this.usersRepository.findOneBy({ id: data.reply.userId });
const u = await this.usersRepository.findOneBy({id: data.reply.userId});

Check failure on line 673 in packages/backend/src/core/NoteCreateService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 673 in packages/backend/src/core/NoteCreateService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'
if (u && this.userEntityService.isRemoteUser(u)) dm.addDirectRecipe(u);
}

// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
if (data.renote && data.renote.userHost !== null) {
const u = await this.usersRepository.findOneBy({ id: data.renote.userId });
const u = await this.usersRepository.findOneBy({id: data.renote.userId});

Check failure on line 679 in packages/backend/src/core/NoteCreateService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required after '{'

Check failure on line 679 in packages/backend/src/core/NoteCreateService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

A space is required before '}'
if (u && this.userEntityService.isRemoteUser(u)) dm.addDirectRecipe(u);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/core/activitypub/ApRendererService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ export class ApRendererService {
* @param orderedItems attached objects (optional)
*/
@bindThis
public renderOrderedCollection(id: string, totalItems: number, first?: string, last?: string, orderedItems?: IObject[]) {
public renderOrderedCollection(id: string | null, totalItems: number, first?: string, last?: string, orderedItems?: IObject[]) {
const page: any = {
id,
type: 'OrderedCollection',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class ApNoteService {

if (isMessaging) {
for (const recipient of visibleUsers) {
await this.messagingService.createMessage(actor, recipient, undefined, text ?? undefined, (files && files.length > 0) ? files[0] : null, object.id);
await this.messagingService.createMessage(actor, recipient, null, text ?? undefined, (files && files.length > 0) ? files[0] : null, object.id);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private messagingService: MessagingService,
) {
super(meta, paramDef, async (ps, me) => {
let recipientUser: MiUser | null;
let recipientGroup: MiUserGroup | null;
let recipientUser: MiUser | null = null;
let recipientGroup: MiUserGroup | null = null;

if (ps.userId != null) {
// Myself
Expand Down

0 comments on commit c3f2a9b

Please sign in to comment.