master
 1import type { CommentData, CommentProvider } from './types';
 2import { cleanCommentHtml } from './utils';
 3import { asideConfig, commentConfig } from '@/config';
 4
 5export const WalineProvider: CommentProvider = {
 6  async setup() {
 7    const walineConfig = commentConfig.waline!;
 8    const commentCount = asideConfig.recentComment.count;
 9    const apiUrl = `${walineConfig.serverURL}/api/comment?type=recent&count=${commentCount}`;
10
11    const response = await fetch(apiUrl, {
12      method: 'GET',
13    });
14    if (!response.ok) {
15      throw new Error('Failed to fetch recent comments');
16    }
17
18    const data: {
19      nick: string;
20      sticky: 0 | 1;
21      status: string;
22      link: string;
23      comment: string;
24      url: string;
25      user_id: string;
26      objectId: string;
27      browser: string;
28      os: string;
29      type: string;
30      label: string;
31      avatar: string;
32      orig: string;
33      addr: string;
34      like: number;
35      time: number;
36    }[] = (await response.json()).data;
37
38    return data.map(
39      (item): CommentData => ({
40        avatarUrl: item.avatar,
41        commentContent: cleanCommentHtml(item.comment),
42        commentUrl: `${item.url}#${item.objectId}`,
43        author: item.nick,
44        time: new Date(item.time),
45      })
46    );
47  },
48};