Realm posts mixed in feed

💩 The feed api didn't respect the visibility level
This commit is contained in:
LittleSheep 2024-03-25 19:40:43 +08:00
parent fb0c7860e0
commit 562af023f1
6 changed files with 23 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"strings"
"git.solsynth.dev/hydrogen/interactive/pkg/database" "git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models" "git.solsynth.dev/hydrogen/interactive/pkg/models"
@ -24,12 +25,12 @@ func listFeed(c *fiber.Ctx) error {
take = 20 take = 20
} }
var whereCondition string var whereConditions []string
if realmId > 0 { if realmId < 0 {
whereCondition += fmt.Sprintf("feed.realm_id = %d", realmId) whereConditions = append(whereConditions, "feed.realm_id IS NULL")
} else { } else if realmId > 0 {
whereCondition += "feed.realm_id IS NULL" whereConditions = append(whereConditions, fmt.Sprintf("feed.realm_id = %d", realmId))
} }
var author models.Account var author models.Account
@ -37,10 +38,15 @@ func listFeed(c *fiber.Ctx) error {
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil { if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} else { } else {
whereCondition += fmt.Sprintf("AND feed.author_id = %d", author.ID) whereConditions = append(whereConditions, fmt.Sprintf("feed.author_id = %d", author.ID))
} }
} }
var whereStatement string
if len(whereConditions) > 0 {
whereStatement += "WHERE " + strings.Join(whereConditions, " AND ")
}
var result []*models.Feed var result []*models.Feed
userTable := viper.GetString("database.prefix") + "accounts" userTable := viper.GetString("database.prefix") + "accounts"
@ -63,11 +69,11 @@ func listFeed(c *fiber.Ctx) error {
GROUP BY article_id, moment_id) AS reactions GROUP BY article_id, moment_id) AS reactions
ON (feed.model_type = 'article' AND feed.id = reactions.article_id) OR ON (feed.model_type = 'article' AND feed.id = reactions.article_id) OR
(feed.model_type = 'moment' AND feed.id = reactions.moment_id) (feed.model_type = 'moment' AND feed.id = reactions.moment_id)
WHERE %s ORDER BY feed.created_at desc LIMIT ? OFFSET ?`, %s ORDER BY feed.created_at desc LIMIT ? OFFSET ?`,
userTable, userTable,
commentTable, commentTable,
reactionTable, reactionTable,
whereCondition, whereStatement,
), ),
database.C.Select(queryArticle).Model(&models.Article{}), database.C.Select(queryArticle).Model(&models.Article{}),
database.C.Select(queryMoment).Model(&models.Moment{}), database.C.Select(queryMoment).Model(&models.Moment{}),

View File

@ -26,6 +26,7 @@ const isOwned = computed(() => props.item?.author_id === id.userinfo.data.id)
function editPost() { function editPost() {
editor.related.edit_to = JSON.parse(JSON.stringify(props.item)) editor.related.edit_to = JSON.parse(JSON.stringify(props.item))
// eslint-disable-next-line
if (editor.show.hasOwnProperty(props.item.model_type)) { if (editor.show.hasOwnProperty(props.item.model_type)) {
// @ts-ignore // @ts-ignore
editor.show[props.item.model_type] = true editor.show[props.item.model_type] = true

View File

@ -33,8 +33,11 @@
@update="updateReactions" @update="updateReactions"
/> />
<div class="mt-1 text-xs opacity-80 flex gap-2 items-center"> <div class="mt-3 text-xs opacity-80 flex items-center">
<span>Posted at {{ new Date(props.item?.created_at).toLocaleString() }}</span> <span>Posted at {{ new Date(props.item?.created_at).toLocaleString() }}</span>
<section v-if="props.item?.realm_id">
&nbsp;·&nbsp;<span>In realm #{{ props.item?.realm_id }}</span>
</section>
</div> </div>
</div> </div>
@ -67,6 +70,7 @@ function updateReactions(symbol: string, num: number) {
if (item.reaction_list == null) { if (item.reaction_list == null) {
item.reaction_list = {} item.reaction_list = {}
} }
// eslint-disable-next-line
if (item.reaction_list.hasOwnProperty(symbol)) { if (item.reaction_list.hasOwnProperty(symbol)) {
item.reaction_list[symbol] += num item.reaction_list[symbol] += num
} else { } else {

View File

@ -92,6 +92,7 @@ async function readPost() {
readPost() readPost()
function updateReactions(symbol: string, num: number) { function updateReactions(symbol: string, num: number) {
// eslint-disable-next-line
if (post.value.reaction_list.hasOwnProperty(symbol)) { if (post.value.reaction_list.hasOwnProperty(symbol)) {
post.value.reaction_list[symbol] += num post.value.reaction_list[symbol] += num
} else { } else {

View File

@ -100,6 +100,7 @@ async function readPost() {
readPost() readPost()
function updateReactions(symbol: string, num: number) { function updateReactions(symbol: string, num: number) {
// eslint-disable-next-line
if (post.value.reaction_list.hasOwnProperty(symbol)) { if (post.value.reaction_list.hasOwnProperty(symbol)) {
post.value.reaction_list[symbol] += num post.value.reaction_list[symbol] += num
} else { } else {

View File

@ -8,7 +8,7 @@ secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi"
content = "uploads" content = "uploads"
[debug] [debug]
database = false database = true
print_routes = false print_routes = false
[identity] [identity]