From d7e70384a360cda51fe23712331110a5c8b1124c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Jan 2018 11:19:25 +0100 Subject: Add mentions to comments --- server/models/video/video-comment.ts | 41 ++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'server/models') diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 66fca2484..dbb2fe429 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -3,6 +3,7 @@ import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects' import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' import { VideoComment } from '../../../shared/models/videos/video-comment.model' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' @@ -270,6 +271,30 @@ export class VideoCommentModel extends Model { }) } + static listThreadParentComments (comment: VideoCommentModel, t: Sequelize.Transaction) { + const query = { + order: [ [ 'createdAt', 'ASC' ] ], + where: { + [ Sequelize.Op.or ]: [ + { id: comment.getThreadId() }, + { originCommentId: comment.getThreadId() } + ], + id: { + [ Sequelize.Op.ne ]: comment.id + } + }, + transaction: t + } + + return VideoCommentModel + .scope([ ScopeNames.WITH_ACCOUNT ]) + .findAll(query) + } + + getThreadId (): number { + return this.originCommentId || this.id + } + isOwned () { return this.Account.isOwned() } @@ -289,7 +314,7 @@ export class VideoCommentModel extends Model { } as VideoComment } - toActivityPubObject (): VideoCommentObject { + toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject { let inReplyTo: string // New thread, so in AS we reply to the video if (this.inReplyToCommentId === null) { @@ -298,6 +323,17 @@ export class VideoCommentModel extends Model { inReplyTo = this.InReplyToVideoComment.url } + const tag: ActivityTagObject[] = [] + for (const parentComment of threadParentComments) { + const actor = parentComment.Account.Actor + + tag.push({ + type: 'Mention', + href: actor.url, + name: `@${actor.preferredUsername}@${actor.getHost()}` + }) + } + return { type: 'Note' as 'Note', id: this.url, @@ -306,7 +342,8 @@ export class VideoCommentModel extends Model { updated: this.updatedAt.toISOString(), published: this.createdAt.toISOString(), url: this.url, - attributedTo: this.Account.Actor.url + attributedTo: this.Account.Actor.url, + tag } } } -- cgit v1.2.3