forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Apply embed specific tag on subjects for video, image and external (b…
…luesky-social#2703) * ✨ Refactor subject tagging to facilitate video content tagging * ♻️ Refactor tag check * ✅ Fix tagging logic * ♻️ Refactor content tagger and fix image content type check * ✨ Add embed tag check for video and external * ✨ Add tagging for both media and image embed
- Loading branch information
Showing
9 changed files
with
336 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ModerationService } from '../mod-service' | ||
import { ModSubject } from '../mod-service/subject' | ||
import { ModerationSubjectStatusRow } from '../mod-service/types' | ||
|
||
export abstract class ContentTagger { | ||
constructor( | ||
protected subject: ModSubject, | ||
protected subjectStatus: ModerationSubjectStatusRow | null, | ||
protected moderationService: ModerationService, | ||
) {} | ||
|
||
protected abstract tagPrefix: string | ||
|
||
protected abstract isApplicable(): boolean | ||
protected abstract buildTags(): Promise<string[]> | ||
|
||
async getTags(): Promise<string[]> { | ||
if (!this.isApplicable()) { | ||
return [] | ||
} | ||
|
||
return this.buildTags() | ||
} | ||
|
||
protected tagAlreadyExists(): boolean { | ||
return Boolean( | ||
this.subjectStatus?.tags?.some((tag) => tag.startsWith(this.tagPrefix)), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
AppBskyEmbedImages, | ||
AppBskyEmbedRecordWithMedia, | ||
AppBskyFeedPost, | ||
AppBskyEmbedVideo, | ||
AppBskyEmbedExternal, | ||
} from '@atproto/api' | ||
import { langLogger as log } from '../logger' | ||
import { ContentTagger } from './content-tagger' | ||
import { ids } from '../lexicon/lexicons' | ||
|
||
export class EmbedTagger extends ContentTagger { | ||
tagPrefix = 'embed:' | ||
|
||
isApplicable(): boolean { | ||
return ( | ||
!!this.subjectStatus && | ||
!this.tagAlreadyExists() && | ||
this.subject.isRecord() && | ||
this.subject.parsedUri.collection === ids.AppBskyFeedPost | ||
) | ||
} | ||
|
||
async buildTags(): Promise<string[]> { | ||
try { | ||
const recordValue = await this.getRecordValue() | ||
if (!recordValue) { | ||
return [] | ||
} | ||
const tags: string[] = [] | ||
if (AppBskyFeedPost.isRecord(recordValue)) { | ||
const embedContent = AppBskyEmbedRecordWithMedia.isMain( | ||
recordValue.embed, | ||
) | ||
? recordValue.embed.media | ||
: recordValue.embed | ||
|
||
if (AppBskyEmbedImages.isMain(embedContent)) { | ||
tags.push(`${this.tagPrefix}image`) | ||
} | ||
|
||
if (AppBskyEmbedVideo.isMain(embedContent)) { | ||
tags.push(`${this.tagPrefix}video`) | ||
} | ||
|
||
if (AppBskyEmbedExternal.isMain(embedContent)) { | ||
tags.push(`${this.tagPrefix}external`) | ||
} | ||
} | ||
return tags | ||
} catch (err) { | ||
log.error({ subject: this.subject, err }, 'Error getting record langs') | ||
return [] | ||
} | ||
} | ||
|
||
async getRecordValue(): Promise<Record<string, unknown> | undefined> { | ||
if (!this.subject.isRecord()) { | ||
return undefined | ||
} | ||
const recordByUri = await this.moderationService.views.fetchRecords([ | ||
this.subject, | ||
]) | ||
|
||
const record = recordByUri.get(this.subject.uri) | ||
return record?.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ModerationService } from '../mod-service' | ||
import { ModSubject } from '../mod-service/subject' | ||
import { langLogger as log } from '../logger' | ||
import { ContentTagger } from './content-tagger' | ||
import { LanguageTagger } from './language-tagger' | ||
import { EmbedTagger } from './embed-tagger' | ||
import { ModerationSubjectStatusRow } from '../mod-service/types' | ||
|
||
export class TagService { | ||
private taggers: ContentTagger[] | ||
|
||
constructor( | ||
private subject: ModSubject, | ||
protected subjectStatus: ModerationSubjectStatusRow | null, | ||
private taggerDid: string, | ||
private moderationService: ModerationService, | ||
) { | ||
this.taggers = [ | ||
new LanguageTagger(subject, subjectStatus, moderationService), | ||
new EmbedTagger(subject, subjectStatus, moderationService), | ||
// Add more taggers as needed | ||
] | ||
} | ||
|
||
async evaluateForSubject() { | ||
try { | ||
const tags: string[] = [] | ||
|
||
await Promise.all( | ||
this.taggers.map(async (tagger) => { | ||
try { | ||
const newTags = await tagger.getTags() | ||
if (newTags.length) tags.push(...newTags) | ||
} catch (e) { | ||
// Don't let one tagger error stop the rest from running | ||
log.error( | ||
{ subject: this.subject, err: e }, | ||
'Error applying tagger', | ||
) | ||
} | ||
}), | ||
) | ||
|
||
if (tags.length > 0) { | ||
await this.moderationService.logEvent({ | ||
event: { | ||
$type: 'tools.ozone.moderation.defs#modEventTag', | ||
add: tags, | ||
remove: [], | ||
}, | ||
subject: this.subject, | ||
createdBy: this.taggerDid, | ||
}) | ||
} | ||
} catch (err) { | ||
log.error({ subject: this.subject, err }, 'Error tagging subject') | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.