summaryrefslogtreecommitdiff
path: root/internal/server/api/helpers.go
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2025-02-04 18:52:57 +0200
committerKyren223 <Kyren223@proton.me>2025-02-04 18:52:57 +0200
commiteab8cba4bb37491d5c3b497b98faa7029e7b7300 (patch)
tree15f9994c423d18f0492f97c43bf71a0793e1e6a4 /internal/server/api/helpers.go
parent42a1ee12c1e64955e6ddd38c34b122c7c10a93f2 (diff)
Refactored the way notifications work on the server-side, next commit
fixes the client to use this new system
Diffstat (limited to 'internal/server/api/helpers.go')
-rw-r--r--internal/server/api/helpers.go51
1 files changed, 12 insertions, 39 deletions
diff --git a/internal/server/api/helpers.go b/internal/server/api/helpers.go
index 321bfd2..c4deb6c 100644
--- a/internal/server/api/helpers.go
+++ b/internal/server/api/helpers.go
@@ -9,7 +9,6 @@ import (
"github.com/kyren223/eko/internal/data"
"github.com/kyren223/eko/internal/packet"
"github.com/kyren223/eko/internal/server/session"
- "github.com/kyren223/eko/pkg/assert"
"github.com/kyren223/eko/pkg/snowflake"
)
@@ -133,55 +132,29 @@ func UserPropagate(
}
const getNotificationsQuery = `-- name: GetNotifications :many
-WITH
-entries(source, lastId) AS (
- VALUES /*SLICE:pair*/?
-),
-permitted_frequencies AS (
- SELECT f.id, m.is_admin
- FROM frequencies f
- JOIN entries e ON f.id = e.source
- LEFT JOIN members m
- ON m.user_id = ?
- AND m.network_id = f.network_id
- WHERE m.is_member = true AND (f.perms != 0 OR m.is_admin = true)
+WITH entries AS (
+ SELECT source_id, last_read
+ FROM last_read_messages
+ WHERE user_id = ?
)
SELECT
- e.source,
+ e.source_id,
CASE
WHEN COUNT(m.id) = 0 THEN NULL
ELSE SUM(CASE WHEN (m.ping = 0 OR (m.ping = 1 AND pf.is_admin = true) OR m.ping = ?) THEN 1 ELSE 0 END)
-- 0 is @everyone, 1 is @admins, otherwise it's user_id
END AS pings
FROM entries e
-LEFT JOIN messages m ON m.id > e.lastId
- AND (m.frequency_id = e.source OR
- (m.receiver_id = e.source AND m.sender_id = ?) OR
- (m.sender_id = e.source AND m.receiver_id = ?))
-JOIN permitted_frequencies pf ON e.source = pf.id
-GROUP BY e.source, e.lastId;
+LEFT JOIN messages m ON m.id > e.last_read
+ AND (m.frequency_id = e.source_id OR
+ (m.receiver_id = e.source_id AND m.sender_id = ?) OR
+ (m.sender_id = e.source_id AND m.receiver_id = ?))
+GROUP BY e.source_id, e.last_read;
`
-func getNotifications(ctx context.Context, arg *packet.GetNotifications, ping snowflake.ID) (packet.NotificationsInfo, error) {
- assert.Assert(
- len(arg.Source) == len(arg.LastReadId),
- "Source and LastReadID must match",
- "source", arg.Source, "last read id", arg.LastReadId,
- )
-
+func getNotifications(ctx context.Context, userId snowflake.ID) (packet.NotificationsInfo, error) {
query := getNotificationsQuery
- var queryParams []interface{}
- if len(arg.Source) > 0 {
- for i := 0; i < len(arg.Source); i++ {
- queryParams = append(queryParams, arg.Source[i])
- queryParams = append(queryParams, arg.LastReadId[i])
- }
- query = strings.Replace(query, "/*SLICE:pair*/?", strings.Repeat(",(?,?)", len(arg.Source))[1:], 1)
- } else {
- assert.Never("source and id must not be empty")
- }
- queryParams = append(queryParams, ping, ping, ping, ping)
- rows, err := db.QueryContext(ctx, query, queryParams...)
+ rows, err := db.QueryContext(ctx, query, userId, userId, userId, userId, userId)
if err != nil {
return packet.NotificationsInfo{}, err
}