✨ Count replies and reposts
This commit is contained in:
		| @@ -27,4 +27,6 @@ type Post struct { | |||||||
| 	// Dynamic Calculating Values | 	// Dynamic Calculating Values | ||||||
| 	LikeCount    int64 `json:"like_count" gorm:"-"` | 	LikeCount    int64 `json:"like_count" gorm:"-"` | ||||||
| 	DislikeCount int64 `json:"dislike_count" gorm:"-"` | 	DislikeCount int64 `json:"dislike_count" gorm:"-"` | ||||||
|  | 	ReplyCount   int64 `json:"reply_count" gorm:"-"` | ||||||
|  | 	RepostCount  int64 `json:"repost_count" gorm:"-"` | ||||||
| } | } | ||||||
|   | |||||||
| @@ -49,21 +49,25 @@ func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB { | |||||||
|  |  | ||||||
| func GetPost(tx *gorm.DB) (*models.Post, error) { | func GetPost(tx *gorm.DB) (*models.Post, error) { | ||||||
| 	var post *models.Post | 	var post *models.Post | ||||||
| 	if err := PreloadRelatedPost(tx). | 	if err := PreloadRelatedPost(tx).First(&post).Error; err != nil { | ||||||
| 		First(&post).Error; err != nil { |  | ||||||
| 		return post, err | 		return post, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	var reactInfo struct { | 	var reactInfo struct { | ||||||
| 		PostID       uint | 		PostID       uint  `json:"post_id"` | ||||||
| 		LikeCount    int64 | 		LikeCount    int64 `json:"like_count"` | ||||||
| 		DislikeCount int64 | 		DislikeCount int64 `json:"dislike_count"` | ||||||
|  | 		ReplyCount   int64 `json:"reply_count"` | ||||||
|  | 		RepostCount  int64 `json:"repost_count"` | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	prefix := viper.GetString("database.prefix") | 	prefix := viper.GetString("database.prefix") | ||||||
| 	database.C.Raw(fmt.Sprintf(`SELECT t.id                         as post_id, | 	database.C.Raw(fmt.Sprintf(` | ||||||
|  | SELECT t.id                         as post_id, | ||||||
|        COALESCE(l.like_count, 0)    AS like_count, |        COALESCE(l.like_count, 0)    AS like_count, | ||||||
|        COALESCE(d.dislike_count, 0) AS dislike_count |        COALESCE(d.dislike_count, 0) AS dislike_count, | ||||||
|  |        COALESCE(r.reply_count, 0)    AS reply_count, | ||||||
|  |        COALESCE(rp.repost_count, 0)  AS repost_count | ||||||
| FROM %sposts t | FROM %sposts t | ||||||
|          LEFT JOIN (SELECT post_id, COUNT(*) AS like_count |          LEFT JOIN (SELECT post_id, COUNT(*) AS like_count | ||||||
|                     FROM %spost_likes |                     FROM %spost_likes | ||||||
| @@ -71,10 +75,20 @@ FROM %sposts t | |||||||
|          LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count |          LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count | ||||||
|                     FROM %spost_dislikes |                     FROM %spost_dislikes | ||||||
|                     GROUP BY post_id) d ON t.id = d.post_id |                     GROUP BY post_id) d ON t.id = d.post_id | ||||||
| WHERE t.id = ?`, prefix, prefix, prefix), post.ID).Scan(&reactInfo) |          LEFT JOIN (SELECT reply_id, COUNT(*) AS reply_count | ||||||
|  |                     FROM %sposts | ||||||
|  |                     WHERE reply_id IS NOT NULL | ||||||
|  |                     GROUP BY reply_id) r ON t.id = r.reply_id | ||||||
|  |          LEFT JOIN (SELECT repost_id, COUNT(*) AS repost_count | ||||||
|  |                     FROM %sposts | ||||||
|  |                     WHERE repost_id IS NOT NULL | ||||||
|  |                     GROUP BY repost_id) rp ON t.id = rp.repost_id | ||||||
|  | WHERE t.id = ?`, prefix, prefix, prefix, prefix, prefix), post.ID).Scan(&reactInfo) | ||||||
|  |  | ||||||
| 	post.LikeCount = reactInfo.LikeCount | 	post.LikeCount = reactInfo.LikeCount | ||||||
| 	post.DislikeCount = reactInfo.DislikeCount | 	post.DislikeCount = reactInfo.DislikeCount | ||||||
|  | 	post.ReplyCount = reactInfo.ReplyCount | ||||||
|  | 	post.RepostCount = reactInfo.RepostCount | ||||||
|  |  | ||||||
| 	return post, nil | 	return post, nil | ||||||
| } | } | ||||||
| @@ -97,15 +111,20 @@ func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) { | |||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	var reactInfo []struct { | 	var reactInfo []struct { | ||||||
| 		PostID       uint | 		PostID       uint  `json:"post_id"` | ||||||
| 		LikeCount    int64 | 		LikeCount    int64 `json:"like_count"` | ||||||
| 		DislikeCount int64 | 		DislikeCount int64 `json:"dislike_count"` | ||||||
|  | 		ReplyCount   int64 `json:"reply_count"` | ||||||
|  | 		RepostCount  int64 `json:"repost_count"` | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	prefix := viper.GetString("database.prefix") | 	prefix := viper.GetString("database.prefix") | ||||||
| 	database.C.Raw(fmt.Sprintf(`SELECT t.id                         as post_id, | 	database.C.Raw(fmt.Sprintf(` | ||||||
|  | SELECT t.id                         as post_id, | ||||||
|        COALESCE(l.like_count, 0)    AS like_count, |        COALESCE(l.like_count, 0)    AS like_count, | ||||||
|        COALESCE(d.dislike_count, 0) AS dislike_count |        COALESCE(d.dislike_count, 0) AS dislike_count, | ||||||
|  |        COALESCE(r.reply_count, 0)    AS reply_count, | ||||||
|  |        COALESCE(rp.repost_count, 0)  AS repost_count | ||||||
| FROM %sposts t | FROM %sposts t | ||||||
|          LEFT JOIN (SELECT post_id, COUNT(*) AS like_count |          LEFT JOIN (SELECT post_id, COUNT(*) AS like_count | ||||||
|                     FROM %spost_likes |                     FROM %spost_likes | ||||||
| @@ -113,7 +132,15 @@ FROM %sposts t | |||||||
|          LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count |          LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count | ||||||
|                     FROM %spost_dislikes |                     FROM %spost_dislikes | ||||||
|                     GROUP BY post_id) d ON t.id = d.post_id |                     GROUP BY post_id) d ON t.id = d.post_id | ||||||
| WHERE t.id IN (?)`, prefix, prefix, prefix), postIds).Scan(&reactInfo) |          LEFT JOIN (SELECT reply_id, COUNT(*) AS reply_count | ||||||
|  |                     FROM %sposts | ||||||
|  |                     WHERE reply_id IS NOT NULL | ||||||
|  |                     GROUP BY reply_id) r ON t.id = r.reply_id | ||||||
|  |          LEFT JOIN (SELECT repost_id, COUNT(*) AS repost_count | ||||||
|  |                     FROM %sposts | ||||||
|  |                     WHERE repost_id IS NOT NULL | ||||||
|  |                     GROUP BY repost_id) rp ON t.id = rp.repost_id | ||||||
|  | WHERE t.id IN ?`, prefix, prefix, prefix, prefix, prefix), postIds).Scan(&reactInfo) | ||||||
|  |  | ||||||
| 	postMap := lo.SliceToMap(posts, func(item *models.Post) (uint, *models.Post) { | 	postMap := lo.SliceToMap(posts, func(item *models.Post) (uint, *models.Post) { | ||||||
| 		return item.ID, item | 		return item.ID, item | ||||||
| @@ -123,6 +150,8 @@ WHERE t.id IN (?)`, prefix, prefix, prefix), postIds).Scan(&reactInfo) | |||||||
| 		if post, ok := postMap[info.PostID]; ok { | 		if post, ok := postMap[info.PostID]; ok { | ||||||
| 			post.LikeCount = info.LikeCount | 			post.LikeCount = info.LikeCount | ||||||
| 			post.DislikeCount = info.DislikeCount | 			post.DislikeCount = info.DislikeCount | ||||||
|  | 			post.ReplyCount = info.ReplyCount | ||||||
|  | 			post.RepostCount = info.RepostCount | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -158,9 +158,10 @@ export default function PostItem(props: { | |||||||
|                 <div class="tooltip" data-tip="Reply"> |                 <div class="tooltip" data-tip="Reply"> | ||||||
|                   <button |                   <button | ||||||
|                     type="button" |                     type="button" | ||||||
|                     class="btn btn-ghost btn-block" |                     class="indicator btn btn-ghost btn-block" | ||||||
|                     onClick={() => props.onReply && props.onReply(props.post)} |                     onClick={() => props.onReply && props.onReply(props.post)} | ||||||
|                   > |                   > | ||||||
|  |                     <span class="indicator-item badge badge-sm badge-primary">{props.post.reply_count}</span> | ||||||
|                     <i class="fa-solid fa-reply"></i> |                     <i class="fa-solid fa-reply"></i> | ||||||
|                   </button> |                   </button> | ||||||
|                 </div> |                 </div> | ||||||
| @@ -168,9 +169,10 @@ export default function PostItem(props: { | |||||||
|                 <div class="tooltip" data-tip="Repost"> |                 <div class="tooltip" data-tip="Repost"> | ||||||
|                   <button |                   <button | ||||||
|                     type="button" |                     type="button" | ||||||
|                     class="btn btn-ghost btn-block" |                     class="indicator btn btn-ghost btn-block" | ||||||
|                     onClick={() => props.onRepost && props.onRepost(props.post)} |                     onClick={() => props.onRepost && props.onRepost(props.post)} | ||||||
|                   > |                   > | ||||||
|  |                     <span class="indicator-item badge badge-sm badge-secondary">{props.post.repost_count}</span> | ||||||
|                     <i class="fa-solid fa-retweet"></i> |                     <i class="fa-solid fa-retweet"></i> | ||||||
|                   </button> |                   </button> | ||||||
|                 </div> |                 </div> | ||||||
| @@ -182,10 +184,16 @@ export default function PostItem(props: { | |||||||
|                 </div> |                 </div> | ||||||
|                 <ul tabIndex="0" class="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52"> |                 <ul tabIndex="0" class="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52"> | ||||||
|                   <li class="md:hidden"> |                   <li class="md:hidden"> | ||||||
|                     <a onClick={() => props.onReply && props.onReply(props.post)}>Reply</a> |                     <a class="flex justify-between" onClick={() => props.onReply && props.onReply(props.post)}> | ||||||
|  |                       <span>Reply</span> | ||||||
|  |                       <span class="badge badge-primary">{props.post.reply_count}</span> | ||||||
|  |                     </a> | ||||||
|                   </li> |                   </li> | ||||||
|                   <li class="md:hidden"> |                   <li class="md:hidden"> | ||||||
|                     <a onClick={() => props.onRepost && props.onRepost(props.post)}>Repost</a> |                     <a class="flex justify-between" onClick={() => props.onRepost && props.onRepost(props.post)}> | ||||||
|  |                       <span>Repost</span> | ||||||
|  |                       <span class="badge badge-secondary">{props.post.repost_count}</span> | ||||||
|  |                     </a> | ||||||
|                   </li> |                   </li> | ||||||
|                   <Show when={userinfo?.profiles?.id === props.post.author_id}> |                   <Show when={userinfo?.profiles?.id === props.post.author_id}> | ||||||
|                     <li> |                     <li> | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| debug = false | debug = true | ||||||
|  |  | ||||||
| name = "Goatplaza" | name = "Goatplaza" | ||||||
| maintainer = "SmartSheep Studio" | maintainer = "SmartSheep Studio" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user