forked from halo-dev/plugin-comment-widget
-
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.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
15 changed files
with
294 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { CommentVo, ReplyVo } from '@halo-dev/api-client'; | ||
import { getAvatarProvider } from './providers'; | ||
|
||
abstract class AvatarPolicy { | ||
abstract applyCommentPolicy(comment: CommentVo | undefined): string | undefined; | ||
abstract applyReplyPolicy(reply: ReplyVo | undefined): string | undefined; | ||
} | ||
|
||
let policyInstance: AvatarPolicy | undefined; | ||
const emailKind = 'Email'; | ||
const emailHash = 'email-hash'; | ||
|
||
class AnonymousUserPolicy extends AvatarPolicy { | ||
applyCommentPolicy(comment: CommentVo | undefined): string | undefined { | ||
const avatarProvider = getAvatarProvider(); | ||
const isAnonymous = comment?.owner.kind === emailKind; | ||
if (isAnonymous) { | ||
return avatarProvider?.getAvatarSrc(comment?.spec.owner.annotations?.[emailHash]); | ||
} | ||
return comment?.owner.avatar; | ||
} | ||
applyReplyPolicy(reply: ReplyVo | undefined): string | undefined { | ||
const avatarProvider = getAvatarProvider(); | ||
const isAnonymous = reply?.owner.kind === emailKind; | ||
if (isAnonymous) { | ||
return avatarProvider?.getAvatarSrc(reply?.spec.owner.annotations?.[emailHash]); | ||
} | ||
return reply?.owner.avatar; | ||
} | ||
} | ||
|
||
class AllUserPolicy extends AvatarPolicy { | ||
applyCommentPolicy(comment: CommentVo | undefined): string | undefined { | ||
const avatarProvider = getAvatarProvider(); | ||
return avatarProvider?.getAvatarSrc(comment?.spec.owner.annotations?.[emailHash]); | ||
} | ||
applyReplyPolicy(reply: ReplyVo | undefined): string | undefined { | ||
const avatarProvider = getAvatarProvider(); | ||
return avatarProvider?.getAvatarSrc(reply?.spec.owner.annotations?.[emailHash]); | ||
} | ||
} | ||
|
||
class NoAvatarUserPolicy extends AvatarPolicy { | ||
applyCommentPolicy(comment: CommentVo | undefined): string | undefined { | ||
const avatarProvider = getAvatarProvider(); | ||
const isAnonymous = comment?.owner.kind === emailKind; | ||
const avatar = comment?.owner.avatar; | ||
if (isAnonymous || !avatar) { | ||
return avatarProvider?.getAvatarSrc(comment?.spec.owner.annotations?.[emailHash]); | ||
} | ||
return avatar; | ||
} | ||
applyReplyPolicy(reply: ReplyVo | undefined): string | undefined { | ||
const avatarProvider = getAvatarProvider(); | ||
const isAnonymous = reply?.owner.kind === emailKind; | ||
const avatar = reply?.owner.avatar; | ||
if (isAnonymous || !avatar) { | ||
return avatarProvider?.getAvatarSrc(reply?.spec.owner.annotations?.[emailHash]); | ||
} | ||
return avatar; | ||
} | ||
} | ||
|
||
enum AvatarPolicyEnum { | ||
ANONYMOUS_USER_POLICY = 'anonymousUser', | ||
ALL_USER_POLICY = 'allUser', | ||
NO_AVATAR_USER_POLICY = 'noAvatarUser', | ||
} | ||
|
||
function setPolicyInstance(nPolicyInstance: AvatarPolicy | undefined) { | ||
policyInstance = nPolicyInstance; | ||
} | ||
|
||
function getPolicyInstance(): AvatarPolicy | undefined { | ||
return policyInstance; | ||
} | ||
|
||
export { | ||
AnonymousUserPolicy, | ||
AllUserPolicy, | ||
NoAvatarUserPolicy, | ||
AvatarPolicyEnum, | ||
setPolicyInstance, | ||
getPolicyInstance, | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/comment-widget/src/avatar/providers/avatar-provider.ts
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,23 @@ | ||
export default abstract class AvatarProvider { | ||
private readonly _name: string; | ||
private _url: string; | ||
|
||
constructor(name: string, url: string) { | ||
this._name = name; | ||
this._url = url; | ||
} | ||
|
||
get url(): string { | ||
return this._url; | ||
} | ||
|
||
set url(value: string) { | ||
this._url = value; | ||
} | ||
|
||
get name(): string { | ||
return this._name; | ||
} | ||
|
||
abstract getAvatarSrc(emailHash: string | undefined): string; | ||
} |
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,9 @@ | ||
import AvatarProvider from './avatar-provider'; | ||
|
||
class Gravatar extends AvatarProvider { | ||
override getAvatarSrc(emailHash: string | undefined): string { | ||
return `${this.url}/avatar/${emailHash}`; | ||
} | ||
} | ||
|
||
export default new Gravatar('Gravatar', 'https://gravatar.com'); |
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,24 @@ | ||
import Gravatar from './gravatar'; | ||
import AvatarProvider from './avatar-provider'; | ||
|
||
let avatarProvider: AvatarProvider | undefined; | ||
|
||
enum AvatarProviderEnum { | ||
GRAVATAR = 'gravatar', | ||
} | ||
|
||
export function setAvatarProvider(provider: string, mirrorUrl?: string) { | ||
switch (provider) { | ||
case AvatarProviderEnum.GRAVATAR: | ||
if (mirrorUrl) { | ||
Gravatar.url = mirrorUrl; | ||
} | ||
avatarProvider = Gravatar; | ||
break; | ||
default: | ||
} | ||
} | ||
|
||
export function getAvatarProvider() { | ||
return avatarProvider; | ||
} |
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
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
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.