mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add activities page.
This commit is contained in:
@@ -1,36 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
if [ -z "$husky_skip_init" ]; then
|
|
||||||
debug () {
|
|
||||||
if [ "$HUSKY_DEBUG" = "1" ]; then
|
|
||||||
echo "husky (debug) - $1"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
readonly hook_name="$(basename -- "$0")"
|
|
||||||
debug "starting $hook_name..."
|
|
||||||
|
|
||||||
if [ "$HUSKY" = "0" ]; then
|
|
||||||
debug "HUSKY env variable is set to 0, skipping hook"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f ~/.huskyrc ]; then
|
|
||||||
debug "sourcing ~/.huskyrc"
|
|
||||||
. ~/.huskyrc
|
|
||||||
fi
|
|
||||||
|
|
||||||
readonly husky_skip_init=1
|
|
||||||
export husky_skip_init
|
|
||||||
sh -e "$0" "$@"
|
|
||||||
exitCode="$?"
|
|
||||||
|
|
||||||
if [ $exitCode != 0 ]; then
|
|
||||||
echo "husky - $hook_name hook exited with code $exitCode (error)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $exitCode = 127 ]; then
|
|
||||||
echo "husky - command not found in PATH=$PATH"
|
|
||||||
fi
|
|
||||||
|
|
||||||
exit $exitCode
|
|
||||||
fi
|
|
@@ -1,4 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
. "$(dirname -- "$0")/_/husky.sh"
|
|
||||||
|
|
||||||
cd frontend && npm run lint && npm run format:check
|
|
@@ -13,6 +13,7 @@ import EditResourcePage from "./pages/edit_resource_page.tsx";
|
|||||||
import AboutPage from "./pages/about_page.tsx";
|
import AboutPage from "./pages/about_page.tsx";
|
||||||
import TagsPage from "./pages/tags_page.tsx";
|
import TagsPage from "./pages/tags_page.tsx";
|
||||||
import RandomPage from "./pages/random_page.tsx";
|
import RandomPage from "./pages/random_page.tsx";
|
||||||
|
import ActivitiesPage from "./pages/activities_page.tsx";
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
@@ -32,6 +33,7 @@ export default function App() {
|
|||||||
<Route path={"/about"} element={<AboutPage />} />
|
<Route path={"/about"} element={<AboutPage />} />
|
||||||
<Route path={"/tags"} element={<TagsPage />} />
|
<Route path={"/tags"} element={<TagsPage />} />
|
||||||
<Route path={"/random"} element={<RandomPage />} />
|
<Route path={"/random"} element={<RandomPage />} />
|
||||||
|
<Route path={"/activity"} element={<ActivitiesPage />} />
|
||||||
</Route>
|
</Route>
|
||||||
</Routes>
|
</Routes>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
|
@@ -2,12 +2,7 @@ import { app } from "../app.ts";
|
|||||||
import { network } from "../network/network.ts";
|
import { network } from "../network/network.ts";
|
||||||
import { useNavigate, useOutlet } from "react-router";
|
import { useNavigate, useOutlet } from "react-router";
|
||||||
import { createContext, useContext, useEffect, useState } from "react";
|
import { createContext, useContext, useEffect, useState } from "react";
|
||||||
import {
|
import { MdArrowUpward, MdOutlinePerson, MdSearch } from "react-icons/md";
|
||||||
MdArrowUpward,
|
|
||||||
MdOutlinePerson,
|
|
||||||
MdSearch,
|
|
||||||
MdSettings,
|
|
||||||
} from "react-icons/md";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import UploadingSideBar from "./uploading_side_bar.tsx";
|
import UploadingSideBar from "./uploading_side_bar.tsx";
|
||||||
import { ThemeSwitcher } from "./theme_switcher.tsx";
|
import { ThemeSwitcher } from "./theme_switcher.tsx";
|
||||||
@@ -87,20 +82,17 @@ export default function Navigator() {
|
|||||||
>
|
>
|
||||||
<a>{t("Tags")}</a>
|
<a>{t("Tags")}</a>
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li>
|
||||||
|
<a
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const menu = document.getElementById(
|
const menu = document.getElementById(
|
||||||
"navi_menu",
|
"navi_menu",
|
||||||
) as HTMLElement;
|
) as HTMLElement;
|
||||||
menu.blur();
|
menu.blur();
|
||||||
navigate("/manage");
|
navigate("/activity");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<a>{t("Settings")}</a>
|
{t("Activity")}
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<a href={"https://github.com/wgh136/nysoure"} target={"_blank"}>
|
|
||||||
{"Github"}
|
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
@@ -161,6 +153,13 @@ export default function Navigator() {
|
|||||||
>
|
>
|
||||||
<a>{t("Random")}</a>
|
<a>{t("Random")}</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li
|
||||||
|
onClick={() => {
|
||||||
|
navigate("/activity");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<a>{t("Activity")}</a>
|
||||||
|
</li>
|
||||||
<li
|
<li
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
navigate("/about");
|
navigate("/about");
|
||||||
@@ -175,16 +174,6 @@ export default function Navigator() {
|
|||||||
<SearchBar />
|
<SearchBar />
|
||||||
<UploadingSideBar />
|
<UploadingSideBar />
|
||||||
<ThemeSwitcher />
|
<ThemeSwitcher />
|
||||||
{app.isLoggedIn() && (
|
|
||||||
<button
|
|
||||||
className={"btn btn-circle btn-ghost hidden sm:inline-flex"}
|
|
||||||
onClick={() => {
|
|
||||||
navigate("/manage");
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<MdSettings size={24} />
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
<a
|
<a
|
||||||
className={"hidden sm:inline"}
|
className={"hidden sm:inline"}
|
||||||
href="https://github.com/wgh136/nysoure"
|
href="https://github.com/wgh136/nysoure"
|
||||||
@@ -284,6 +273,19 @@ function UserButton() {
|
|||||||
{t("Publish")}
|
{t("Publish")}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<a
|
||||||
|
onClick={() => {
|
||||||
|
navigate(`/manage`);
|
||||||
|
const menu = document.getElementById(
|
||||||
|
"navi_dropdown_menu",
|
||||||
|
) as HTMLUListElement;
|
||||||
|
menu.blur();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("Settings")}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a
|
<a
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
@@ -47,6 +47,7 @@ export const i18nData = {
|
|||||||
"Server": "Server",
|
"Server": "Server",
|
||||||
|
|
||||||
// Management page translations
|
// Management page translations
|
||||||
|
"Settings": "Settings",
|
||||||
"Manage": "Manage",
|
"Manage": "Manage",
|
||||||
"Storage": "Storage",
|
"Storage": "Storage",
|
||||||
"Users": "Users",
|
"Users": "Users",
|
||||||
@@ -212,6 +213,12 @@ export const i18nData = {
|
|||||||
"Mint Leaf": "Mint Leaf",
|
"Mint Leaf": "Mint Leaf",
|
||||||
"Golden Glow": "Golden Glow",
|
"Golden Glow": "Golden Glow",
|
||||||
"Random": "Random",
|
"Random": "Random",
|
||||||
|
|
||||||
|
// Activity Page
|
||||||
|
"Activity": "Activity",
|
||||||
|
"Published a resource": "Published a resource",
|
||||||
|
"Updated a resource": "Updated a resource",
|
||||||
|
"Commented on a resource": "Commented on a resource",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"zh-CN": {
|
"zh-CN": {
|
||||||
@@ -260,6 +267,7 @@ export const i18nData = {
|
|||||||
"Server": "服务器",
|
"Server": "服务器",
|
||||||
|
|
||||||
// Management page translations
|
// Management page translations
|
||||||
|
"Settings": "设置",
|
||||||
"Manage": "管理",
|
"Manage": "管理",
|
||||||
"Storage": "存储",
|
"Storage": "存储",
|
||||||
"Users": "用户",
|
"Users": "用户",
|
||||||
@@ -417,6 +425,12 @@ export const i18nData = {
|
|||||||
"Golden Glow": "微光",
|
"Golden Glow": "微光",
|
||||||
|
|
||||||
"Random": "随机",
|
"Random": "随机",
|
||||||
|
|
||||||
|
// Activity Page
|
||||||
|
"Activity": "动态",
|
||||||
|
"Published a resource": "发布了一个资源",
|
||||||
|
"Updated a resource": "更新了一个资源",
|
||||||
|
"Commented on a resource": "评论了一个资源",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"zh-TW": {
|
"zh-TW": {
|
||||||
@@ -465,6 +479,7 @@ export const i18nData = {
|
|||||||
"Server": "伺服器",
|
"Server": "伺服器",
|
||||||
|
|
||||||
// Management page translations
|
// Management page translations
|
||||||
|
"Settings": "設置",
|
||||||
"Manage": "管理",
|
"Manage": "管理",
|
||||||
"Storage": "儲存",
|
"Storage": "儲存",
|
||||||
"Users": "用戶",
|
"Users": "用戶",
|
||||||
@@ -622,6 +637,12 @@ export const i18nData = {
|
|||||||
"Golden Glow": "微光",
|
"Golden Glow": "微光",
|
||||||
|
|
||||||
"Random": "隨機",
|
"Random": "隨機",
|
||||||
|
|
||||||
|
// Activity Page
|
||||||
|
"Activity": "動態",
|
||||||
|
"Published a resource": "發布了資源",
|
||||||
|
"Updated a resource": "更新了資源",
|
||||||
|
"Commented on a resource": "評論了資源",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@@ -141,3 +141,20 @@ export enum RSort {
|
|||||||
DownloadsAsc = 4,
|
DownloadsAsc = 4,
|
||||||
DownloadsDesc = 5,
|
DownloadsDesc = 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum ActivityType {
|
||||||
|
Unknown = 0,
|
||||||
|
ResourcePublished = 1,
|
||||||
|
ResourceUpdated = 2,
|
||||||
|
ResourceCommented = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Activity {
|
||||||
|
id: number;
|
||||||
|
type: ActivityType;
|
||||||
|
user_id: number;
|
||||||
|
created_at: string;
|
||||||
|
resource?: Resource;
|
||||||
|
user?: User;
|
||||||
|
comment?: CommentWithResource;
|
||||||
|
}
|
||||||
|
@@ -17,6 +17,7 @@ import {
|
|||||||
ServerConfig,
|
ServerConfig,
|
||||||
RSort,
|
RSort,
|
||||||
TagWithCount,
|
TagWithCount,
|
||||||
|
Activity,
|
||||||
} from "./models.ts";
|
} from "./models.ts";
|
||||||
|
|
||||||
class Network {
|
class Network {
|
||||||
@@ -994,6 +995,18 @@ class Network {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getActivities(page: number = 1): Promise<PageResponse<Activity>> {
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${this.apiBaseUrl}/activity`, {
|
||||||
|
params: { page },
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
} catch (e: any) {
|
||||||
|
console.error(e);
|
||||||
|
return { success: false, message: e.toString() };
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const network = new Network();
|
export const network = new Network();
|
||||||
|
136
frontend/src/pages/activities_page.tsx
Normal file
136
frontend/src/pages/activities_page.tsx
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
import { Activity, ActivityType } from "../network/models.ts";
|
||||||
|
import { network } from "../network/network.ts";
|
||||||
|
import showToast from "../components/toast.ts";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { MdArrowRight } from "react-icons/md";
|
||||||
|
import { useNavigate } from "react-router";
|
||||||
|
import Loading from "../components/loading.tsx";
|
||||||
|
|
||||||
|
export default function ActivitiesPage() {
|
||||||
|
const [activities, setActivities] = useState<Activity[]>([]);
|
||||||
|
const pageRef = useRef(0);
|
||||||
|
const maxPageRef = useRef(1);
|
||||||
|
const isLoadingRef = useRef(false);
|
||||||
|
|
||||||
|
const fetchNextPage = useCallback(async () => {
|
||||||
|
if (isLoadingRef.current || pageRef.current >= maxPageRef.current) return;
|
||||||
|
isLoadingRef.current = true;
|
||||||
|
const response = await network.getActivities(pageRef.current);
|
||||||
|
if (response.success) {
|
||||||
|
setActivities((prev) => [...prev, ...response.data!]);
|
||||||
|
pageRef.current += 1;
|
||||||
|
maxPageRef.current = response.totalPages!;
|
||||||
|
} else {
|
||||||
|
showToast({
|
||||||
|
type: "error",
|
||||||
|
message: response.message || "Failed to load activities",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
isLoadingRef.current = false;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchNextPage();
|
||||||
|
}, [fetchNextPage]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleScroll = () => {
|
||||||
|
if (
|
||||||
|
window.innerHeight + window.scrollY >=
|
||||||
|
document.documentElement.scrollHeight - 100 &&
|
||||||
|
!isLoadingRef.current &&
|
||||||
|
pageRef.current < maxPageRef.current
|
||||||
|
) {
|
||||||
|
fetchNextPage();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener("scroll", handleScroll);
|
||||||
|
return () => window.removeEventListener("scroll", handleScroll);
|
||||||
|
}, [fetchNextPage]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={"pb-2"}>
|
||||||
|
{activities.map((activity) => (
|
||||||
|
<ActivityCard key={activity.id} activity={activity} />
|
||||||
|
))}
|
||||||
|
{pageRef.current < maxPageRef.current && <Loading />}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ActivityCard({ activity }: { activity: Activity }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const messages = [
|
||||||
|
"Unknown activity",
|
||||||
|
t("Published a resource"),
|
||||||
|
t("Updated a resource"),
|
||||||
|
t("Commented on a resource"),
|
||||||
|
];
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
let content = <></>;
|
||||||
|
|
||||||
|
if (
|
||||||
|
activity.type === ActivityType.ResourcePublished ||
|
||||||
|
activity.type === ActivityType.ResourceUpdated
|
||||||
|
) {
|
||||||
|
content = (
|
||||||
|
<div className={"mx-1"}>
|
||||||
|
<div className={"font-bold my-4"}>{activity.resource?.title}</div>
|
||||||
|
{activity.resource?.image && (
|
||||||
|
<div>
|
||||||
|
<img
|
||||||
|
className={"object-contain max-h-52 mt-2 rounded-lg"}
|
||||||
|
src={network.getImageUrl(activity.resource.image.id)}
|
||||||
|
alt={activity.resource.title}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
} else if (activity.type === ActivityType.ResourceCommented) {
|
||||||
|
content = (
|
||||||
|
<div className={"mt-2"}>
|
||||||
|
<div className={"text-sm mx-1"}>{activity.comment?.content}</div>
|
||||||
|
<div className={"flex items-center mt-1"}>
|
||||||
|
<MdArrowRight />
|
||||||
|
<span className={"text-sm text-base-content/80"}>
|
||||||
|
{activity.comment?.resource?.title}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={
|
||||||
|
"card shadow m-4 p-4 hover:shadow-md transition-shadow cursor-pointer"
|
||||||
|
}
|
||||||
|
onClick={() => {
|
||||||
|
if (
|
||||||
|
activity.type === ActivityType.ResourcePublished ||
|
||||||
|
activity.type === ActivityType.ResourceUpdated
|
||||||
|
) {
|
||||||
|
navigate(`/resources/${activity.resource?.id}`);
|
||||||
|
} else if (activity.type === ActivityType.ResourceCommented) {
|
||||||
|
navigate(`/resources/${activity.comment?.resource.id}`);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className={"flex items-center"}>
|
||||||
|
<div className={"avatar w-9 h-9"}>
|
||||||
|
<img alt={"avatar"} src={network.getUserAvatar(activity.user!)} />
|
||||||
|
</div>
|
||||||
|
<span className={"mx-2 font-bold"}>{activity.user?.username}</span>
|
||||||
|
<span className={"ml-2 badge badge-primary badge-soft"}>
|
||||||
|
{messages[activity.type]}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
{content}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
@@ -9,6 +9,7 @@ export default defineConfig({
|
|||||||
proxy: {
|
proxy: {
|
||||||
"/api": {
|
"/api": {
|
||||||
target: "http://localhost:3000",
|
target: "http://localhost:3000",
|
||||||
|
// target: "https://res.nyne.dev",
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
1
main.go
1
main.go
@@ -34,6 +34,7 @@ func main() {
|
|||||||
api.AddFileRoutes(apiG)
|
api.AddFileRoutes(apiG)
|
||||||
api.AddCommentRoutes(apiG)
|
api.AddCommentRoutes(apiG)
|
||||||
api.AddConfigRoutes(apiG)
|
api.AddConfigRoutes(apiG)
|
||||||
|
api.AddActivityRoutes(apiG)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Fatal(app.Listen(":3000"))
|
log.Fatal(app.Listen(":3000"))
|
||||||
|
33
server/api/activity.go
Normal file
33
server/api/activity.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v3"
|
||||||
|
"nysoure/server/model"
|
||||||
|
"nysoure/server/service"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleGetActivity(c fiber.Ctx) error {
|
||||||
|
pageStr := c.Query("page", "1")
|
||||||
|
page, err := strconv.Atoi(pageStr)
|
||||||
|
if err != nil {
|
||||||
|
return model.NewRequestError("Invalid page number")
|
||||||
|
}
|
||||||
|
activities, totalPages, err := service.GetActivityList(page)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if activities == nil {
|
||||||
|
activities = []model.ActivityView{}
|
||||||
|
}
|
||||||
|
return c.JSON(model.PageResponse[model.ActivityView]{
|
||||||
|
Success: true,
|
||||||
|
Data: activities,
|
||||||
|
TotalPages: totalPages,
|
||||||
|
Message: "Activities retrieved successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddActivityRoutes(router fiber.Router) {
|
||||||
|
router.Get("/activity", handleGetActivity)
|
||||||
|
}
|
53
server/dao/activity.go
Normal file
53
server/dao/activity.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package dao
|
||||||
|
|
||||||
|
import "nysoure/server/model"
|
||||||
|
|
||||||
|
func AddNewResourceActivity(userID, resourceID uint) error {
|
||||||
|
activity := &model.Activity{
|
||||||
|
UserID: userID,
|
||||||
|
Type: model.ActivityTypeNewResource,
|
||||||
|
RefID: resourceID,
|
||||||
|
}
|
||||||
|
return db.Create(activity).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddUpdateResourceActivity(userID, resourceID uint) error {
|
||||||
|
activity := &model.Activity{
|
||||||
|
UserID: userID,
|
||||||
|
Type: model.ActivityTypeUpdateResource,
|
||||||
|
RefID: resourceID,
|
||||||
|
}
|
||||||
|
return db.Create(activity).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddNewCommentActivity(userID, commentID uint) error {
|
||||||
|
activity := &model.Activity{
|
||||||
|
UserID: userID,
|
||||||
|
Type: model.ActivityTypeNewComment,
|
||||||
|
RefID: commentID,
|
||||||
|
}
|
||||||
|
return db.Create(activity).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteResourceActivity(resourceID uint) error {
|
||||||
|
return db.Where("ref_id = ? AND (type = ? OR type = ?)", resourceID, model.ActivityTypeNewResource, model.ActivityTypeUpdateResource).Delete(&model.Activity{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteCommentActivity(commentID uint) error {
|
||||||
|
return db.Where("ref_id = ? AND type = ?", commentID, model.ActivityTypeNewComment).Delete(&model.Activity{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetActivityList(offset, limit int) ([]model.Activity, int, error) {
|
||||||
|
var activities []model.Activity
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
if err := db.Model(&model.Activity{}).Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Offset(offset).Limit(limit).Order("id DESC").Find(&activities).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return activities, int(total), nil
|
||||||
|
}
|
@@ -62,3 +62,11 @@ func GetCommentsWithUser(username string, page, pageSize int) ([]model.Comment,
|
|||||||
totalPages := (int(total) + pageSize - 1) / pageSize
|
totalPages := (int(total) + pageSize - 1) / pageSize
|
||||||
return comments, totalPages, nil
|
return comments, totalPages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCommentByID(commentID uint) (*model.Comment, error) {
|
||||||
|
var comment model.Comment
|
||||||
|
if err := db.Preload("User").Preload("Resource").First(&comment, commentID).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &comment, nil
|
||||||
|
}
|
||||||
|
@@ -34,7 +34,18 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = db.AutoMigrate(&model.User{}, &model.Resource{}, &model.Image{}, &model.Tag{}, &model.Storage{}, &model.File{}, &model.UploadingFile{}, &model.Statistic{}, &model.Comment{})
|
_ = db.AutoMigrate(
|
||||||
|
&model.User{},
|
||||||
|
&model.Resource{},
|
||||||
|
&model.Image{},
|
||||||
|
&model.Tag{},
|
||||||
|
&model.Storage{},
|
||||||
|
&model.File{},
|
||||||
|
&model.UploadingFile{},
|
||||||
|
&model.Statistic{},
|
||||||
|
&model.Comment{},
|
||||||
|
&model.Activity{},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDB() *gorm.DB {
|
func GetDB() *gorm.DB {
|
||||||
|
31
server/model/activity.go
Normal file
31
server/model/activity.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ActivityType uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
ActivityTypeUnknown ActivityType = iota
|
||||||
|
ActivityTypeNewResource
|
||||||
|
ActivityTypeUpdateResource
|
||||||
|
ActivityTypeNewComment
|
||||||
|
)
|
||||||
|
|
||||||
|
type Activity struct {
|
||||||
|
gorm.Model
|
||||||
|
UserID uint `gorm:"not null"`
|
||||||
|
Type ActivityType `gorm:"not null"`
|
||||||
|
RefID uint `gorm:"not null"` // Reference ID for the resource or comment
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActivityView struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
Time time.Time `json:"time"`
|
||||||
|
Type ActivityType `json:"type"`
|
||||||
|
User UserView `json:"user"`
|
||||||
|
Comment *CommentWithResourceView `json:"comment,omitempty"`
|
||||||
|
Resource *ResourceView `json:"resource,omitempty"`
|
||||||
|
}
|
53
server/service/activity.go
Normal file
53
server/service/activity.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"nysoure/server/dao"
|
||||||
|
"nysoure/server/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetActivityList(page int) ([]model.ActivityView, int, error) {
|
||||||
|
offset := (page - 1) * pageSize
|
||||||
|
limit := pageSize
|
||||||
|
|
||||||
|
activities, total, err := dao.GetActivityList(offset, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var views []model.ActivityView
|
||||||
|
for _, activity := range activities {
|
||||||
|
user, err := dao.GetUserByID(activity.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
var comment *model.CommentWithResourceView
|
||||||
|
var resource *model.ResourceView
|
||||||
|
if activity.Type == model.ActivityTypeNewComment {
|
||||||
|
c, err := dao.GetCommentByID(activity.RefID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
comment = c.ToViewWithResource()
|
||||||
|
} else if activity.Type == model.ActivityTypeNewResource || activity.Type == model.ActivityTypeUpdateResource {
|
||||||
|
r, err := dao.GetResourceByID(activity.RefID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
rv := r.ToView()
|
||||||
|
resource = &rv
|
||||||
|
}
|
||||||
|
view := model.ActivityView{
|
||||||
|
ID: activity.ID,
|
||||||
|
User: user.ToView(),
|
||||||
|
Type: activity.Type,
|
||||||
|
Time: activity.CreatedAt,
|
||||||
|
Comment: comment,
|
||||||
|
Resource: resource,
|
||||||
|
}
|
||||||
|
views = append(views, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
totalPages := (total + pageSize - 1) / pageSize
|
||||||
|
|
||||||
|
return views, totalPages, nil
|
||||||
|
}
|
@@ -29,6 +29,10 @@ func CreateComment(content string, userID uint, resourceID uint) (*model.Comment
|
|||||||
log.Error("Error creating comment:", err)
|
log.Error("Error creating comment:", err)
|
||||||
return nil, model.NewInternalServerError("Error creating comment")
|
return nil, model.NewInternalServerError("Error creating comment")
|
||||||
}
|
}
|
||||||
|
err = dao.AddNewCommentActivity(userID, c.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Error creating comment activity:", err)
|
||||||
|
}
|
||||||
return c.ToView(), nil
|
return c.ToView(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -60,6 +60,10 @@ func CreateResource(uid uint, params *ResourceCreateParams) (uint, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error updating cached tag list:", err)
|
log.Error("Error updating cached tag list:", err)
|
||||||
}
|
}
|
||||||
|
err = dao.AddNewResourceActivity(uid, r.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("AddNewResourceActivity error: ", err)
|
||||||
|
}
|
||||||
return r.ID, nil
|
return r.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,6 +257,10 @@ func EditResource(uid, rid uint, params *ResourceCreateParams) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error updating cached tag list:", err)
|
log.Error("Error updating cached tag list:", err)
|
||||||
}
|
}
|
||||||
|
err = dao.AddUpdateResourceActivity(uid, r.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("AddUpdateResourceActivity error: ", err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user