mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
111
server/core/lib/notifier/shared/comment/comment-mention.ts
Normal file
111
server/core/lib/notifier/shared/comment/comment-mention.ts
Normal file
|
@ -0,0 +1,111 @@
|
|||
import { logger } from '@server/helpers/logger.js'
|
||||
import { toSafeHtml } from '@server/helpers/markdown.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { AccountBlocklistModel } from '@server/models/account/account-blocklist.js'
|
||||
import { getServerActor } from '@server/models/application/application.js'
|
||||
import { ServerBlocklistModel } from '@server/models/server/server-blocklist.js'
|
||||
import { UserNotificationModel } from '@server/models/user/user-notification.js'
|
||||
import { UserModel } from '@server/models/user/user.js'
|
||||
import {
|
||||
MCommentOwnerVideo,
|
||||
MUserDefault,
|
||||
MUserNotifSettingAccount,
|
||||
MUserWithNotificationSetting,
|
||||
UserNotificationModelForApi
|
||||
} from '@server/types/models/index.js'
|
||||
import { UserNotificationSettingValue, UserNotificationType } from '@peertube/peertube-models'
|
||||
import { AbstractNotification } from '../common/index.js'
|
||||
|
||||
export class CommentMention extends AbstractNotification <MCommentOwnerVideo, MUserNotifSettingAccount> {
|
||||
private users: MUserDefault[]
|
||||
|
||||
private serverAccountId: number
|
||||
|
||||
private accountMutedHash: { [ id: number ]: boolean }
|
||||
private instanceMutedHash: { [ id: number ]: boolean }
|
||||
|
||||
async prepare () {
|
||||
const extractedUsernames = this.payload.extractMentions()
|
||||
logger.debug(
|
||||
'Extracted %d username from comment %s.', extractedUsernames.length, this.payload.url,
|
||||
{ usernames: extractedUsernames, text: this.payload.text }
|
||||
)
|
||||
|
||||
this.users = await UserModel.listByUsernames(extractedUsernames)
|
||||
|
||||
if (this.payload.Video.isOwned()) {
|
||||
const userException = await UserModel.loadByVideoId(this.payload.videoId)
|
||||
this.users = this.users.filter(u => u.id !== userException.id)
|
||||
}
|
||||
|
||||
// Don't notify if I mentioned myself
|
||||
this.users = this.users.filter(u => u.Account.id !== this.payload.accountId)
|
||||
|
||||
if (this.users.length === 0) return
|
||||
|
||||
this.serverAccountId = (await getServerActor()).Account.id
|
||||
|
||||
const sourceAccounts = this.users.map(u => u.Account.id).concat([ this.serverAccountId ])
|
||||
|
||||
this.accountMutedHash = await AccountBlocklistModel.isAccountMutedByAccounts(sourceAccounts, this.payload.accountId)
|
||||
this.instanceMutedHash = await ServerBlocklistModel.isServerMutedByAccounts(sourceAccounts, this.payload.Account.Actor.serverId)
|
||||
}
|
||||
|
||||
log () {
|
||||
logger.info('Notifying %d users of new comment %s.', this.users.length, this.payload.url)
|
||||
}
|
||||
|
||||
getSetting (user: MUserNotifSettingAccount) {
|
||||
const accountId = user.Account.id
|
||||
if (
|
||||
this.accountMutedHash[accountId] === true || this.instanceMutedHash[accountId] === true ||
|
||||
this.accountMutedHash[this.serverAccountId] === true || this.instanceMutedHash[this.serverAccountId] === true
|
||||
) {
|
||||
return UserNotificationSettingValue.NONE
|
||||
}
|
||||
|
||||
return user.NotificationSetting.commentMention
|
||||
}
|
||||
|
||||
getTargetUsers () {
|
||||
return this.users
|
||||
}
|
||||
|
||||
createNotification (user: MUserWithNotificationSetting) {
|
||||
const notification = UserNotificationModel.build<UserNotificationModelForApi>({
|
||||
type: UserNotificationType.COMMENT_MENTION,
|
||||
userId: user.id,
|
||||
commentId: this.payload.id
|
||||
})
|
||||
notification.VideoComment = this.payload
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
createEmail (to: string) {
|
||||
const comment = this.payload
|
||||
|
||||
const accountName = comment.Account.getDisplayName()
|
||||
const video = comment.Video
|
||||
const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath()
|
||||
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
|
||||
const commentHtml = toSafeHtml(comment.text)
|
||||
|
||||
return {
|
||||
template: 'video-comment-mention',
|
||||
to,
|
||||
subject: 'Mention on video ' + video.name,
|
||||
locals: {
|
||||
comment,
|
||||
commentHtml,
|
||||
video,
|
||||
videoUrl,
|
||||
accountName,
|
||||
action: {
|
||||
text: 'View comment',
|
||||
url: commentUrl
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
server/core/lib/notifier/shared/comment/index.ts
Normal file
2
server/core/lib/notifier/shared/comment/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
export * from './comment-mention.js'
|
||||
export * from './new-comment-for-video-owner.js'
|
|
@ -0,0 +1,76 @@
|
|||
import { logger } from '@server/helpers/logger.js'
|
||||
import { toSafeHtml } from '@server/helpers/markdown.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { isBlockedByServerOrAccount } from '@server/lib/blocklist.js'
|
||||
import { UserModel } from '@server/models/user/user.js'
|
||||
import { UserNotificationModel } from '@server/models/user/user-notification.js'
|
||||
import { MCommentOwnerVideo, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models/index.js'
|
||||
import { UserNotificationType } from '@peertube/peertube-models'
|
||||
import { AbstractNotification } from '../common/abstract-notification.js'
|
||||
|
||||
export class NewCommentForVideoOwner extends AbstractNotification <MCommentOwnerVideo> {
|
||||
private user: MUserDefault
|
||||
|
||||
async prepare () {
|
||||
this.user = await UserModel.loadByVideoId(this.payload.videoId)
|
||||
}
|
||||
|
||||
log () {
|
||||
logger.info('Notifying owner of a video %s of new comment %s.', this.user.username, this.payload.url)
|
||||
}
|
||||
|
||||
isDisabled () {
|
||||
if (this.payload.Video.isOwned() === false) return true
|
||||
|
||||
// Not our user or user comments its own video
|
||||
if (!this.user || this.payload.Account.userId === this.user.id) return true
|
||||
|
||||
return isBlockedByServerOrAccount(this.payload.Account, this.user.Account)
|
||||
}
|
||||
|
||||
getSetting (user: MUserWithNotificationSetting) {
|
||||
return user.NotificationSetting.newCommentOnMyVideo
|
||||
}
|
||||
|
||||
getTargetUsers () {
|
||||
if (!this.user) return []
|
||||
|
||||
return [ this.user ]
|
||||
}
|
||||
|
||||
createNotification (user: MUserWithNotificationSetting) {
|
||||
const notification = UserNotificationModel.build<UserNotificationModelForApi>({
|
||||
type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
|
||||
userId: user.id,
|
||||
commentId: this.payload.id
|
||||
})
|
||||
notification.VideoComment = this.payload
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
createEmail (to: string) {
|
||||
const video = this.payload.Video
|
||||
const videoUrl = WEBSERVER.URL + this.payload.Video.getWatchStaticPath()
|
||||
const commentUrl = WEBSERVER.URL + this.payload.getCommentStaticPath()
|
||||
const commentHtml = toSafeHtml(this.payload.text)
|
||||
|
||||
return {
|
||||
template: 'video-comment-new',
|
||||
to,
|
||||
subject: 'New comment on your video ' + video.name,
|
||||
locals: {
|
||||
accountName: this.payload.Account.getDisplayName(),
|
||||
accountUrl: this.payload.Account.Actor.url,
|
||||
comment: this.payload,
|
||||
commentHtml,
|
||||
video,
|
||||
videoUrl,
|
||||
action: {
|
||||
text: 'View comment',
|
||||
url: commentUrl
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue