Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use gravatar source as comment's avatar #111

Merged
merged 16 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions packages/comment-widget/src/avatar-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { CommentVo, ReplyVo } from '@halo-dev/api-client';

abstract class AvatarPolicy {
abstract applyCommentPolicy(comment: CommentVo | undefined): string | undefined;
abstract applyReplyPolicy(reply: ReplyVo | undefined): string | undefined;
}

// TODO fetch from halo
let currentPolicy = "AnonymousUserPolicy";
let currentPolicyObj: AvatarPolicy | undefined;
const emailKind = "Email"
const emailHash = "emailHash";
const avatarProvider = "https://gravatar.com/avatar/";

class AnonymousUserPolicy extends AvatarPolicy {
applyCommentPolicy(comment: CommentVo | undefined): string | undefined {
const isAnonymous = comment?.owner.kind === emailKind;
if (isAnonymous) {
return avatarProvider + comment?.spec.owner.annotations?.[emailHash];
}
return comment?.owner.avatar;
}
applyReplyPolicy(reply: ReplyVo | undefined): string | undefined {
const isAnonymous = reply?.owner.kind === emailKind;
if (isAnonymous) {
return avatarProvider + reply?.spec.owner.annotations?.[emailHash];
}
return reply?.owner.avatar;
}
}

class AllUserPolicy extends AvatarPolicy {
applyCommentPolicy(comment: CommentVo | undefined): string | undefined {
return avatarProvider + comment?.spec.owner.annotations?.[emailHash];
}
applyReplyPolicy(reply: ReplyVo | undefined): string | undefined {
return avatarProvider + reply?.spec.owner.annotations?.[emailHash];
}
}

class NoAvatarUserPolicy extends AvatarPolicy {
applyCommentPolicy(comment: CommentVo | undefined): string | undefined {
const avatar = comment?.owner.avatar;
if (!avatar) {
return avatarProvider + comment?.spec.owner.annotations?.[emailHash];
}
return avatar;
}
applyReplyPolicy(reply: ReplyVo | undefined): string | undefined {
const avatar = reply?.owner.avatar;
if (!avatar) {
return avatarProvider + reply?.spec.owner.annotations?.[emailHash];
}
return avatar;
}
}

enum AvatarPolicyEnum {
"AnonymousUserPolicy" = "AnonymousUserPolicy",
"AllUserPolicy" = "AllUserPolicy",
"NoAvatarUserPolicy" = "NoAvatarUserPolicy"
}

function getPolicyInstance(): AvatarPolicy {
if (currentPolicyObj != undefined) {
return currentPolicyObj;
}
switch (currentPolicy) {
case AvatarPolicyEnum.AllUserPolicy: {
currentPolicyObj = new AllUserPolicy();
break;
}
case AvatarPolicyEnum.NoAvatarUserPolicy: {
currentPolicyObj = new NoAvatarUserPolicy();
break;
}
case AvatarPolicyEnum.AnonymousUserPolicy:
default:
currentPolicyObj = new AnonymousUserPolicy();
}
return currentPolicyObj;
}

export {AnonymousUserPolicy, AllUserPolicy, NoAvatarUserPolicy, getPolicyInstance}
3 changes: 2 additions & 1 deletion packages/comment-widget/src/comment-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { LS_UPVOTED_COMMENTS_KEY } from './constant';
import varStyles from './styles/var';
import { Ref, createRef, ref } from 'lit/directives/ref.js';
import { CommentReplies } from './comment-replies';
import {handleCommentAvatar} from "./user-avatar";

export class CommentItem extends LitElement {
@consume({ context: baseUrlContext })
Expand Down Expand Up @@ -103,7 +104,7 @@ export class CommentItem extends LitElement {

override render() {
return html`<base-comment-item
.userAvatar="${this.comment?.owner.avatar}"
.userAvatar="${handleCommentAvatar(this.comment)}"
.userDisplayName="${this.comment?.owner.displayName}"
.content="${this.comment?.spec.content || ''}"
.creationTime="${this.comment?.spec.creationTime}"
Expand Down
3 changes: 2 additions & 1 deletion packages/comment-widget/src/reply-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { LS_UPVOTED_REPLIES_KEY } from './constant';
import { consume } from '@lit/context';
import { baseUrlContext } from './context';
import varStyles from './styles/var';
import {handleReplyAvatar} from "./user-avatar";

export class ReplyItem extends LitElement {
@consume({ context: baseUrlContext })
Expand Down Expand Up @@ -106,7 +107,7 @@ export class ReplyItem extends LitElement {
override render() {
return html`
<base-comment-item
.userAvatar="${this.reply?.owner.avatar}"
.userAvatar="${handleReplyAvatar(this.reply)}"
.userDisplayName="${this.reply?.owner.displayName}"
.content="${this.reply?.spec.content || ''}"
.creationTime="${this.reply?.metadata.creationTimestamp ?? undefined}"
Expand Down
12 changes: 12 additions & 0 deletions packages/comment-widget/src/user-avatar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CommentVo, ReplyVo } from '@halo-dev/api-client';
import './icons/icon-loading';
import { LitElement, css, html } from 'lit';
import { property, state } from 'lit/decorators.js';
import baseStyles from './styles/base';
import varStyles from './styles/var';
import {getPolicyInstance} from "./avatar-policy";

export class UserAvatar extends LitElement {
@property({ type: String })
Expand Down Expand Up @@ -130,6 +132,16 @@ export class UserAvatar extends LitElement {

customElements.get('user-avatar') || customElements.define('user-avatar', UserAvatar);

const avatarPolicy = getPolicyInstance();

export function handleCommentAvatar(comment: CommentVo | undefined): string | undefined {
ShiinaKin marked this conversation as resolved.
Show resolved Hide resolved
return avatarPolicy.applyCommentPolicy(comment);
}

export function handleReplyAvatar(reply: ReplyVo | undefined): string | undefined {
return avatarPolicy.applyReplyPolicy(reply);
}

declare global {
interface HTMLElementTagNameMap {
'user-avatar': UserAvatar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package run.halo.comment.widget;

import lombok.RequiredArgsConstructor;
import org.pf4j.PluginWrapper;
import org.springframework.stereotype.Component;
import run.halo.app.extension.SchemeManager;
Expand All @@ -12,14 +13,20 @@
@Component
public class CommentWidgetPlugin extends BasePlugin {

public CommentWidgetPlugin(PluginWrapper wrapper) {
public final SchemeManager schemeManager;

public CommentWidgetPlugin(PluginWrapper wrapper, SchemeManager schemeManager) {
super(wrapper);
this.schemeManager = schemeManager;
}

@Override
public void start() {
schemeManager.register(CommentWidgetSetting.class);
}

@Override
public void stop() {
schemeManager.unregister(schemeManager.get(CommentWidgetSetting.class));
}
}
34 changes: 34 additions & 0 deletions src/main/java/run/halo/comment/widget/CommentWidgetSetting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package run.halo.comment.widget;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.GVK;

/**
* @author mashirot
* 2024/4/4 16:44
*/
@Data
@EqualsAndHashCode(callSuper = true)
@GVK(kind = "CommentWidget", group = "widget.comment.halo.run",
version = "v1alpha1", singular = "comment-widget-setting", plural = "comment-widget-settings")
public class CommentWidgetSetting extends AbstractExtension {
ShiinaKin marked this conversation as resolved.
Show resolved Hide resolved

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private CommentWidgetSettingSpec spec;

public static class CommentWidgetSettingSpec {
public static final String ANONYMOUS_USER_POLICY = "AnonymousUserPolicy";
public static final String ALL_USER_POLICY = "AllUserPolicy";
public static final String NO_AVATAR_USER_POLICY = "NoAvatarUserPolicy";

@Schema(requiredMode = Schema.RequiredMode.REQUIRED, defaultValue = "false")
private Boolean useAvatarProvider;
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private String avatarProvider;
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, defaultValue = ANONYMOUS_USER_POLICY)
private String avatarPolicy;
}
}