diff --git a/pkg/models/posts.go b/pkg/models/posts.go index 1132ec3..852f7fa 100644 --- a/pkg/models/posts.go +++ b/pkg/models/posts.go @@ -14,6 +14,8 @@ type Post struct { DislikedAccounts []PostDislike `json:"disliked_accounts"` RepostTo *Post `json:"repost_to" gorm:"foreignKey:RepostID"` ReplyTo *Post `json:"reply_to" gorm:"foreignKey:ReplyID"` + PinnedAt *time.Time `json:"pinned_at"` + EditedAt *time.Time `json:"edited_at"` PublishedAt time.Time `json:"published_at"` RepostID *uint `json:"repost_id"` ReplyID *uint `json:"reply_id"` diff --git a/pkg/server/posts_api.go b/pkg/server/posts_api.go index 0e6f4d6..adcd0d9 100644 --- a/pkg/server/posts_api.go +++ b/pkg/server/posts_api.go @@ -13,16 +13,22 @@ import ( func listPost(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) + authorId := c.QueryInt("authorId", 0) + + tx := database.C.Where(&models.Post{RealmID: nil}).Order("created_at desc") + + if authorId > 0 { + tx = tx.Where(&models.Post{AuthorID: uint(authorId)}) + } var count int64 - if err := database.C. - Where(&models.Post{RealmID: nil}). + if err := tx. Model(&models.Post{}). Count(&count).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - posts, err := services.ListPost(take, offset) + posts, err := services.ListPost(tx, take, offset) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/server/startup.go b/pkg/server/startup.go index fa3f3c3..1df69c7 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -57,7 +57,8 @@ func NewServer() { api := A.Group("/api").Name("API") { api.Get("/users/me", auth, getUserinfo) - api.Get("/users/:accountId/follow", auth, doFollowAccount) + api.Get("/users/:accountId", getOthersInfo) + api.Post("/users/:accountId/follow", auth, doFollowAccount) api.Get("/auth", doLogin) api.Get("/auth/callback", doPostLogin) diff --git a/pkg/server/users_api.go b/pkg/server/users_api.go index 2cb26d1..f285f3d 100644 --- a/pkg/server/users_api.go +++ b/pkg/server/users_api.go @@ -20,6 +20,19 @@ func getUserinfo(c *fiber.Ctx) error { return c.JSON(data) } +func getOthersInfo(c *fiber.Ctx) error { + accountId, _ := c.ParamsInt("accountId", 0) + + var data models.Account + if err := database.C. + Where(&models.Account{BaseModel: models.BaseModel{ID: uint(accountId)}}). + First(&data).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return c.JSON(data) +} + func doFollowAccount(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) id, _ := c.ParamsInt("accountId", 0) diff --git a/pkg/services/posts.go b/pkg/services/posts.go index a483c9e..a4b4265 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -10,13 +10,11 @@ import ( "gorm.io/gorm" ) -func ListPost(take int, offset int) ([]*models.Post, error) { +func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) { var posts []*models.Post - if err := database.C. - Where(&models.Post{RealmID: nil}). + if err := tx. Limit(take). Offset(offset). - Order("created_at desc"). Preload("Author"). Find(&posts).Error; err != nil { return posts, err diff --git a/pkg/view/src/components/NameCard.module.css b/pkg/view/src/components/NameCard.module.css new file mode 100644 index 0000000..932298a --- /dev/null +++ b/pkg/view/src/components/NameCard.module.css @@ -0,0 +1,3 @@ +.description { + color: var(--fallback-bc, oklch(var(--bc)/.8)); +} \ No newline at end of file diff --git a/pkg/view/src/components/NameCard.tsx b/pkg/view/src/components/NameCard.tsx new file mode 100644 index 0000000..bc9fdfa --- /dev/null +++ b/pkg/view/src/components/NameCard.tsx @@ -0,0 +1,58 @@ +import { createSignal } from "solid-js"; + +import styles from "./NameCard.module.css"; + +export default function NameCard(props: { accountId: number, onError: (messasge: string | null) => void }) { + const [info, setInfo] = createSignal(null); + + const [_, setLoading] = createSignal(true); + + async function readInfo() { + setLoading(true); + const res = await fetch(`/api/users/${props.accountId}`); + if (res.status !== 200) { + props.onError(await res.text()); + } else { + setInfo(await res.json()); + props.onError(null); + } + setLoading(false); + } + + readInfo(); + + return ( +
+ + +
+
+ avatar +
+
+ +
+
+ +
+
+ +
+

{info()?.name}

+

{info()?.description}

+
+

+ + Joined at {new Date(info()?.created_at).toLocaleString()} +

+
+
+
+ ); +} \ No newline at end of file diff --git a/pkg/view/src/components/PostItem.tsx b/pkg/view/src/components/PostItem.tsx index 8bb8be6..f8deff6 100644 --- a/pkg/view/src/components/PostItem.tsx +++ b/pkg/view/src/components/PostItem.tsx @@ -24,22 +24,24 @@ export default function PostItem(props: { post: any, onError: (message: string | return (
-
-
- +

{props.post.title}

@@ -48,7 +50,7 @@ export default function PostItem(props: { post: any, onError: (message: string |
-
+
+
+
-
-