Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Derive Space post push recipients in one query

Anand Baburajana41a5a53b09a
diff --git a/server/space/controller/module.go b/server/space/controller/module.go
index c66ae0bb7f..6eddde47ee 100644
--- a/server/space/controller/module.go
+++ b/server/space/controller/module.go
@@ -38,7 +38,7 @@ func NewModule(
SessionsRepo: repos.Sessions,
}
spaces := &SpacesController{SpacesRepo: repos.Spaces, AssetsRepo: repos.Assets, auth: authDeps}
- posts := &PostsController{PostsRepo: repos.Posts, SpacesRepo: repos.Spaces, FriendsRepo: repos.Friends, AssetsRepo: repos.Assets, ActivityNotifier: activityNotifier, auth: authDeps}
+ posts := &PostsController{PostsRepo: repos.Posts, SpacesRepo: repos.Spaces, AssetsRepo: repos.Assets, ActivityNotifier: activityNotifier, auth: authDeps}
assets := &AssetsController{AssetsRepo: repos.Assets, SpacesRepo: repos.Spaces, auth: authDeps}
links := &LinksController{LinksRepo: repos.Links, SpacesRepo: repos.Spaces, FriendsRepo: repos.Friends, Posts: posts, Assets: assets}
return &Module{
diff --git a/server/space/controller/notifications.go b/server/space/controller/notifications.go
index 1ce97ca5bb..80e292e841 100644
--- a/server/space/controller/notifications.go
+++ b/server/space/controller/notifications.go
@@ -38,7 +38,7 @@ const (
var sendSpaceWebPush = webpush.SendNotificationWithContext
type SpaceActivityNotifier interface {
- OnSpacePostCreated(actorUserID int64, actorSpaceID, actorSlug string, recipientUserIDs []int64)
+ OnSpacePostCreated(actorUserID int64, actorSpaceID, actorSlug string)
OnSpacePostLiked(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64)
OnSpacePostReplied(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64)
OnSpaceMessageSent(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64)
@@ -79,7 +79,6 @@ func NewSpaceWebPushSender(
func (n *SpaceWebPushSender) OnSpacePostCreated(
actorUserID int64,
actorSpaceID, actorSlug string,
- recipientUserIDs []int64,
) {
if !n.available() {
return
@@ -87,7 +86,6 @@ func (n *SpaceWebPushSender) OnSpacePostCreated(
subscriptions, err := n.WebPushRepo.ListPostSubscriptions(
context.Background(),
actorSpaceID,
- uniqueUserIDs(recipientUserIDs),
)
if err != nil {
log.WithField("event", spaceActivityPostCreated).WithError(err).
@@ -332,22 +330,6 @@ func deduplicateSpaceWebPushSubscriptions(
return result
}
-func uniqueUserIDs(userIDs []int64) []int64 {
- seen := make(map[int64]struct{}, len(userIDs))
- out := make([]int64, 0, len(userIDs))
- for _, userID := range userIDs {
- if userID <= 0 {
- continue
- }
- if _, ok := seen[userID]; ok {
- continue
- }
- seen[userID] = struct{}{}
- out = append(out, userID)
- }
- return out
-}
-
func spaceActivityActorLabel(actorSlug string) string {
actorSlug = strings.TrimSpace(actorSlug)
if actorSlug == "" {
diff --git a/server/space/controller/notifications_test.go b/server/space/controller/notifications_test.go
index 244de12e96..1eeec84126 100644
--- a/server/space/controller/notifications_test.go
+++ b/server/space/controller/notifications_test.go
@@ -37,8 +37,8 @@ func newRecordingSpaceActivityNotifier() *recordingSpaceActivityNotifier {
return &recordingSpaceActivityNotifier{events: make(chan recordedSpaceActivity, 8)}
}
-func (n *recordingSpaceActivityNotifier) OnSpacePostCreated(actorUserID int64, actorSpaceID, actorSlug string, recipientUserIDs []int64) {
- n.record(spaceActivityPostCreated, actorUserID, actorSpaceID, actorSlug, recipientUserIDs...)
+func (n *recordingSpaceActivityNotifier) OnSpacePostCreated(actorUserID int64, actorSpaceID, actorSlug string) {
+ n.record(spaceActivityPostCreated, actorUserID, actorSpaceID, actorSlug)
}
func (n *recordingSpaceActivityNotifier) OnSpacePostLiked(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64) {
diff --git a/server/space/controller/posts.go b/server/space/controller/posts.go
index b42cc32590..2ba535fc7d 100644
--- a/server/space/controller/posts.go
+++ b/server/space/controller/posts.go
@@ -12,7 +12,6 @@ import (
"github.com/ente/museum/space/repo"
"github.com/ente/stacktrace"
"github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
)
const spacePostLimitWarningThreshold = 200
@@ -24,7 +23,6 @@ type SpaceAbuseNotifier interface {
type PostsController struct {
PostsRepo *repo.PostsRepository
SpacesRepo *repo.SpacesRepository
- FriendsRepo *repo.FriendsRepository
AssetsRepo *repo.AssetsRepository
ActivityNotifier SpaceActivityNotifier
AbuseNotifier SpaceAbuseNotifier
@@ -97,14 +95,7 @@ func (c *PostsController) Create(ctx context.Context, space *repo.SpaceRecord, r
}
func (c *PostsController) notifyFriendsOfNewPost(ownerID int64, spaceID, spaceSlug string) {
- go func() {
- recipientUserIDs, err := c.FriendsRepo.ListFriendOwnerIDsForSpace(context.Background(), spaceID)
- if err != nil {
- log.WithField("space_id", spaceID).WithError(err).Error("Failed to list friends for Space post notification")
- return
- }
- c.ActivityNotifier.OnSpacePostCreated(ownerID, spaceID, spaceSlug, recipientUserIDs)
- }()
+ go c.ActivityNotifier.OnSpacePostCreated(ownerID, spaceID, spaceSlug)
}
func (c *PostsController) postResponses(ctx context.Context, posts []repo.SpacePostRecord, includeAuthor bool) ([]models.PostResponse, error) {
diff --git a/server/space/controller/test_helpers_test.go b/server/space/controller/test_helpers_test.go
index 39fb035a18..343dac87c5 100644
--- a/server/space/controller/test_helpers_test.go
+++ b/server/space/controller/test_helpers_test.go
@@ -28,13 +28,13 @@ func setupPostsControllerTest(t *testing.T) (*PostsController, *spacerepo.Module
type noopSpaceActivityNotifier struct{}
-func (noopSpaceActivityNotifier) OnSpacePostCreated(int64, string, string, []int64) {}
-func (noopSpaceActivityNotifier) OnSpacePostLiked(int64, string, string, int64) {}
-func (noopSpaceActivityNotifier) OnSpacePostReplied(int64, string, string, int64) {}
-func (noopSpaceActivityNotifier) OnSpaceMessageSent(int64, string, string, int64) {}
-func (noopSpaceActivityNotifier) OnSpaceMessageLiked(int64, string, string, int64) {}
-func (noopSpaceActivityNotifier) OnSpaceFriendAdded(int64, string, string, int64) {}
-func (noopSpaceActivityNotifier) OnSpaceFriendRequested(int64, string, int64) {}
+func (noopSpaceActivityNotifier) OnSpacePostCreated(int64, string, string) {}
+func (noopSpaceActivityNotifier) OnSpacePostLiked(int64, string, string, int64) {}
+func (noopSpaceActivityNotifier) OnSpacePostReplied(int64, string, string, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceMessageSent(int64, string, string, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceMessageLiked(int64, string, string, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceFriendAdded(int64, string, string, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceFriendRequested(int64, string, int64) {}
func testSpaceBytes(value string) []byte {
return []byte(value)
diff --git a/server/space/repo/friends.go b/server/space/repo/friends.go
index bc75e75a06..f1c36d30ba 100644
--- a/server/space/repo/friends.go
+++ b/server/space/repo/friends.go
@@ -520,29 +520,6 @@ func (r *FriendsRepository) ListSharesForFriendAndSpace(ctx context.Context, fri
return out, stacktrace.Propagate(rows.Err(), "")
}
-func (r *FriendsRepository) ListFriendOwnerIDsForSpace(ctx context.Context, spaceID string) ([]int64, error) {
- rows, err := r.DB.QueryContext(ctx, `
- SELECT friend_space.owner_id
- FROM space_friend_shares s
- JOIN spaces friend_space ON friend_space.space_id = s.friend_space_id
- JOIN users friend_owner ON friend_owner.user_id = friend_space.owner_id AND friend_owner.encrypted_email IS NOT NULL
- WHERE s.space_id = $1
- `, spaceID)
- if err != nil {
- return nil, stacktrace.Propagate(err, "")
- }
- defer rows.Close()
- var out []int64
- for rows.Next() {
- var ownerID int64
- if err := rows.Scan(&ownerID); err != nil {
- return nil, stacktrace.Propagate(err, "")
- }
- out = append(out, ownerID)
- }
- return out, stacktrace.Propagate(rows.Err(), "")
-}
-
func (r *FriendsRepository) ListFriendsForSpace(ctx context.Context, spaceID string) ([]SpaceFriendRecord, error) {
query := `
SELECT ` + spaceActorSelectColumns("friend_space", "friend_avatar", "friend") + `,
diff --git a/server/space/repo/web_push.go b/server/space/repo/web_push.go
index 9aba69347a..5efc1023ec 100644
--- a/server/space/repo/web_push.go
+++ b/server/space/repo/web_push.go
@@ -205,7 +205,6 @@ func (r *WebPushRepository) ListActiveAccountSubscriptions(
func (r *WebPushRepository) ListPostSubscriptions(
ctx context.Context,
spaceID string,
- friendUserIDs []int64,
) ([]SpaceWebPushSubscriptionRecord, error) {
rows, err := r.DB.QueryContext(ctx, `
SELECT push.target_id, push.endpoint, push.p256dh, push.auth,
@@ -213,11 +212,18 @@ func (r *WebPushRepository) ListPostSubscriptions(
FROM space_web_push_subscriptions push
JOIN space_browser_sessions session
ON session.token_hash = push.session_token_hash
- AND session.expires_at > $3
+ AND session.expires_at > $2
JOIN users active_user
ON active_user.user_id = session.user_id
AND active_user.encrypted_email IS NOT NULL
- WHERE session.user_id = ANY($1)
+ WHERE EXISTS (
+ SELECT 1
+ FROM space_friend_shares friendship
+ JOIN spaces friend_space
+ ON friend_space.space_id = friendship.friend_space_id
+ WHERE friendship.space_id = $1
+ AND friend_space.owner_id = session.user_id
+ )
UNION ALL
@@ -232,8 +238,8 @@ func (r *WebPushRepository) ListPostSubscriptions(
JOIN users active_user
ON active_user.user_id = space.owner_id
AND active_user.encrypted_email IS NOT NULL
- WHERE link.space_id = $2
- `, pq.Array(friendUserIDs), spaceID, timeutil.Microseconds())
+ WHERE link.space_id = $1
+ `, spaceID, timeutil.Microseconds())
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
diff --git a/server/space/repo/web_push_test.go b/server/space/repo/web_push_test.go
index ca55717e5c..3673868023 100644
--- a/server/space/repo/web_push_test.go
+++ b/server/space/repo/web_push_test.go
@@ -16,6 +16,19 @@ func TestWebPushSubscriptionsFollowTargetsAndActiveSessions(t *testing.T) {
friendID := insertSpaceUser(t, module, "[email protected]", "friend-public")
space, err := testCreateSpace(ctx, module, ownerID, "space_push_owner", "root", "public", "secret", "nonce", "profile")
require.NoError(t, err)
+ friendSpace, err := testCreateSpace(ctx, module, friendID, "space_push_friend", "friend-root", "friend-public", "friend-secret", "friend-nonce", "friend-profile")
+ require.NoError(t, err)
+ require.NoError(t, testAddFriend(
+ ctx,
+ module,
+ friendID,
+ friendSpace.SpaceID,
+ space.SpaceID,
+ "owner-share-key",
+ space.CurrentVersion,
+ "friend-share-key",
+ friendSpace.CurrentVersion,
+ ))
link, err := module.Links.Create(
ctx,
space.SpaceID,
@@ -34,7 +47,7 @@ func TestWebPushSubscriptionsFollowTargetsAndActiveSessions(t *testing.T) {
require.NoError(t, module.Sessions.CreateBrowserSession(ctx, activeSession, friendID, "active-wrap", timeutil.NDaysFromNow(1)))
require.NoError(t, module.Sessions.CreateBrowserSession(ctx, expiredSession, friendID, "expired-wrap", timeutil.Microseconds()-1))
- activeTargetID, err := module.WebPush.UpsertAccountSubscription(
+ _, err = module.WebPush.UpsertAccountSubscription(
ctx,
activeSession,
"https://push.example/active",
@@ -42,7 +55,7 @@ func TestWebPushSubscriptionsFollowTargetsAndActiveSessions(t *testing.T) {
"active-auth",
)
require.NoError(t, err)
- _, err = module.WebPush.UpsertAccountSubscription(
+ accountTargetID, err := module.WebPush.UpsertAccountSubscription(
ctx,
activeSession,
"https://push.example/replaced",
@@ -66,7 +79,7 @@ func TestWebPushSubscriptionsFollowTargetsAndActiveSessions(t *testing.T) {
"public-auth",
)
require.NoError(t, err)
- require.NotEqual(t, activeTargetID, publicTargetID)
+ require.NotEqual(t, accountTargetID, publicTargetID)
accountSubscriptions, err := module.WebPush.ListActiveAccountSubscriptions(ctx, []int64{friendID})
require.NoError(t, err)
@@ -74,15 +87,24 @@ func TestWebPushSubscriptionsFollowTargetsAndActiveSessions(t *testing.T) {
require.Equal(t, "https://push.example/replaced", accountSubscriptions[0].Endpoint)
require.Equal(t, friendID, accountSubscriptions[0].UserID)
- postSubscriptions, err := module.WebPush.ListPostSubscriptions(ctx, space.SpaceID, nil)
- require.NoError(t, err)
- require.Equal(t, []SpaceWebPushSubscriptionRecord{{
- TargetID: publicTargetID,
- Endpoint: "https://push.example/public",
- P256dh: "public-p256dh",
- Auth: "public-auth",
- Public: true,
- }}, postSubscriptions)
+ postSubscriptions, err := module.WebPush.ListPostSubscriptions(ctx, space.SpaceID)
+ require.NoError(t, err)
+ require.ElementsMatch(t, []SpaceWebPushSubscriptionRecord{
+ {
+ TargetID: accountTargetID,
+ Endpoint: "https://push.example/replaced",
+ P256dh: "replaced-p256dh",
+ Auth: "replaced-auth",
+ UserID: friendID,
+ },
+ {
+ TargetID: publicTargetID,
+ Endpoint: "https://push.example/public",
+ P256dh: "public-p256dh",
+ Auth: "public-auth",
+ Public: true,
+ },
+ }, postSubscriptions)
require.NoError(t, module.Sessions.DeleteBrowserSession(ctx, activeSession))
var count int