title: $:/plugins/Gk0Wk/CPL-Repo/views/plugins/comments
created: 20260101000000000
modified: 20260101000000000
tags: $:/tags/ViewTemplate
type: text/vnd.tiddlywiki

\whitespace trim


<!-- Comment Section for Plugin Pages -->
<$list filter="[all[current]tag[$:/tags/PluginWiki]has[cpl.title]]" variable="pluginTiddler">

<$let pluginTitle={{!!cpl.title}} userStatus={{{ [[$:/temp/CPL-Server/user-status]get[text]else[anonymous]] }}} serverType={{{ [[$:/temp/CPL-Repo/server-type]get[text]else[unknown]] }}} chinese={{{ [[$:/language]get[text]removeprefix[$:/languages/]else[en-GB]search[zh]then[yes]else[no]] }}} githubClientId={{{ [[$:/temp/CPL-Server/github-client-id]get[text]] }}} isAdmin={{{ [[$:/temp/CPL-Server/is-admin]get[text]else[no]] }}}>

<!-- Only show comment section for server mirrors -->
<$list filter="[<serverType>match[server]]" variable="isServerMirror">

<div class="cpl-comment-section">
  <h3 class="cpl-comment-title">
    💬 <$text text={{{ [<chinese>match[yes]then[评论]else[Comments]] }}}/>
  </h3>

  <!-- Login Status -->
  <$list filter="[<userStatus>match[authenticated]]" variable="isLoggedIn">
    <!-- Logged in: Show user info and comment form -->
    <$let userJson={{{ [[$:/temp/CPL-Server/user]get[text]else[{}]] }}} username={{{ [<userJson>jsonget[username]else[User]] }}} avatar={{{ [<userJson>jsonget[avatar]] }}}>
    <div class="cpl-comment-user-card">
      <$list filter="[<avatar>!is[blank]]" variable="hasAvatar">
        <img src=<<avatar>> class="cpl-comment-avatar" alt="avatar"/>
      </$list>
      <span><strong><$text text=<<username>>/></strong></span>
      <$button class="cpl-comment-logout-button" tooltip={{{ [<chinese>match[yes]then[退出当前 GitHub 账号]else[Sign out of the current GitHub account]] }}}>
        <$action-sendmessage $message="tm-http-request"
          url={{{ [[$:/temp/CPL-Server/api-base]get[text]addsuffix[/cpl/auth/logout]] }}}
          method="POST" header-Content-Type="application/json" body="{}"
          oncompletion="""
            <$action-deletetiddler $tiddler='$:/temp/CPL-Server/user'/>
            <$action-setfield $tiddler='$:/temp/CPL-Server/user-status' text='anonymous'/>
            <$action-setfield $tiddler='$:/temp/CPL-Server/is-admin' text='no'/>
            <$action-setfield $tiddler='$:/temp/CPL-Server/auth-refresh-token' text={{{ [<now '0hh:0mm:0ss.XYZ'] ] }}}/>
            """/>
        <$text text={{{ [<chinese>match[yes]then[退出登录]else[Logout]] }}}/>
      </$button>
    </div>
    </$let>

    <!-- Comment Form -->
    <div class="cpl-comment-form">
      <$edit-text tiddler="$:/temp/CPL-Server/comment-draft" field="text" class="cpl-comment-textarea" tag="textarea" placeholder={{{ [<chinese>match[yes]then[使用 Wikitext 写评论...]else[Write a comment using wikitext...]] }}}/>
      <div class="cpl-comment-form-actions">
        <span class="cpl-comment-hint"><$text text={{{ [<chinese>match[yes]then[支持 Wikitext]else[Wikitext supported]] }}}/></span>
        <$let statusTitle={{{ [[$:/temp/CPL-Server/comment-status/]addsuffix<pluginTitle>] }}} draftText={{$:/temp/CPL-Server/comment-draft}}>
        <$button class="tc-btn-primary cpl-comment-submit-button">
          <$action-setfield $tiddler=<<statusTitle>> text="submitting" />
          <$action-sendmessage $message="tm-http-request"
            url={{{ [[$:/temp/CPL-Server/api-base]get[text]addsuffix[/cpl/comments/]] [<pluginTitle>encodeuricomponent[]] +[join[]] }}}
            method="POST" header-Content-Type="application/json"
            body={{{ [[{}]jsonset[content],<draftText>] }}}
            var-pluginTitle=<<pluginTitle>>
            var-statusTitle=<<statusTitle>>
            oncompletion="""
              <$list filter='[<status>compare:number:gteq[200]compare:number:lt[300]]'>
              <$action-setfield $tiddler='$:/temp/CPL-Server/comment-draft' text=''/>
              <$action-setfield $tiddler=<<statusTitle>> text='success'/>
              <$action-sendmessage $message='tm-http-request'
                url={{{ [[$:/temp/CPL-Server/api-base]get[text]addsuffix[/cpl/comments/]] [<pluginTitle>encodeuricomponent[]] +[join[]] }}}
                method='GET'
                var-refreshTiddler={{{ [<pluginTitle>addprefix[$:/temp/CPL-Server/comments/]] }}}
                var-refreshPluginTitle=<<pluginTitle>>
                oncompletion='<$action-setfield $tiddler=<<refreshTiddler>> text=<<data>> type="application/json" plugin-title=<<refreshPluginTitle>>/>'/>
              <$action-sendmessage $message='tm-http-request'
                url={{{ [[$:/temp/CPL-Server/api-base]get[text]addsuffix[/cpl/comments/all-recent]] }}}
                method='GET'
                oncompletion='<$action-setfield $tiddler="$:/temp/CPL-Server/all-recent-comments" text=<<data>> type="application/json"/>'/>
              </$list>
              <$list filter='[<status>compare:number:lt[200]] [<status>compare:number:gteq[300]] +[limit[1]]'>
              <$action-setfield $tiddler=<<statusTitle>> text={{{ [<data>jsonget[error]!is[blank]] [<error>!is[blank]] [<statusText>!is[blank]] +[limit[1]addprefix[error: ]] }}}/>
              </$list>
              """/>
          <$text text={{{ [<chinese>match[yes]then[提交评论]else[Submit Comment]] }}}/>
        </$button>
        </$let>
      </div>
      <$let commentStatus={{{ [[$:/temp/CPL-Server/comment-status/]addsuffix<pluginTitle>] }}}>
        <$list filter="[<commentStatus>get[text]prefix[error:]]" variable="hasError">
          <div class="cpl-comment-status cpl-comment-status-error">
            ✗ <$text text={{{ [<commentStatus>get[text]removeprefix[error: ]] }}}/>
          </div>
        </$list>
        <$list filter="[<commentStatus>get[text]match[success]]" variable="hasSuccess">
          <div class="cpl-comment-status cpl-comment-status-success">
            ✓ <$text text={{{ [<chinese>match[yes]then[评论已提交]else[Comment submitted]] }}}/>
          </div>
        </$list>
      </$let>
    </div>
  </$list>

  <$list filter="[<userStatus>!match[authenticated]]" variable="notLoggedIn">
    <!-- Not logged in: Show login button -->
    <div class="cpl-comment-login-prompt">
      <p class="cpl-comment-login-text">
        <$text text={{{ [<chinese>match[yes]then[登录后即可发表评论]else[Login to post comments]] }}}/>
      </p>
      <$list filter="[<githubClientId>!is[blank]]" variable="hasGithubOAuth">
        <$button class="tc-btn-primary cpl-comment-login-button">
          <$action-sendmessage $message="cpl-github-login"/>
          <$text text={{{ [<chinese>match[yes]then[使用 GitHub 登录]else[Login with GitHub]] }}}/>
        </$button>
      </$list>
      <$list filter="[<githubClientId>is[blank]]" variable="missingGithubOAuth">
        <p class="cpl-comment-login-text cpl-status-muted">
          <$text text={{{ [<chinese>match[yes]then[GitHub 登录暂未配置。]else[GitHub login is not configured.]] }}}/>
        </p>
      </$list>
    </div>
  </$list>

  <!-- Comments List -->
  <$let commentsDataTitle={{{ [<pluginTitle>addprefix[$:/temp/CPL-Server/comments/]] }}}>
  <$tiddler tiddler=<<commentsDataTitle>>>
  <div class="cpl-comments-list">
      <$list filter="[<currentTiddler>get[text]jsonindexes[comments]]" variable="idx">
      <$let commentUsername={{{ [<currentTiddler>get[text]jsonget[comments],<idx>,[username]else[Anonymous]] }}} commentContent={{{ [<currentTiddler>get[text]jsonget[comments],<idx>,[content]] }}} commentDate={{{ [<currentTiddler>get[text]jsonget[comments],<idx>,[createdAt]] }}} commentAvatar={{{ [<currentTiddler>get[text]jsonget[comments],<idx>,[avatar]] }}} commentStatus={{{ [<currentTiddler>get[text]jsonget[comments],<idx>,[status]else[approved]] }}} commentId={{{ [<currentTiddler>get[text]jsonget[comments],<idx>,[id]] }}}}>
            <div class="cpl-comment-item">
              <div class="cpl-comment-meta">
                <$list filter="[<commentAvatar>!is[blank]]" variable="hasAvatar">
                  <img src=<<commentAvatar>> class="cpl-comment-avatar-small" alt=""/>
                </$list>
                <strong><$text text=<<commentUsername>>/></strong>
                <span class="cpl-comment-date"><$text text=<<commentDate>>/></span>
              </div>
              <div class="cpl-comment-body">
                <$text text=<<commentContent>>/>
              </div>
            </div>
          </$let>
        </$list>
      <$list filter="[<currentTiddler>get[text]jsonindexes[comments]count[]match[0]]" variable="noComments">
        <div class="cpl-comment-empty">
          <$text text={{{ [<chinese>match[yes]then[暂无评论，来发表第一条吧！]else[No comments yet. Be the first!]] }}}/>
        </div>
      </$list>
  </div>
  </$tiddler>
  </$let>
</div>

</$list>
<!-- End server mirror check -->

</$let>
</$list>
