Skip to content

armgit / ente

Public Imported from GitHub
0

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

Group Space activity actor fields

Neeraj Gupta8210bb1036ed
diff --git a/server/space/controller/friends.go b/server/space/controller/friends.go
index a9ad910d69..20007964c4 100644
--- a/server/space/controller/friends.go
+++ b/server/space/controller/friends.go
@@ -62,20 +62,11 @@ func (c *FriendsController) Add(ctx context.Context, requesterSpace *repo.SpaceR
return nil, err
}
if becameFriends {
- go c.ActivityNotifier.OnSpaceFriendAdded(
- requesterSpace.OwnerID,
- requesterSpace.SpaceID,
- requesterSpace.SpaceSlug,
- request.RequesterID,
- )
+ go c.ActivityNotifier.OnSpaceFriendAdded(spaceActivityActor(requesterSpace), request.RequesterID)
return &models.FriendStatusResponse{Status: "friend"}, nil
}
if created {
- go c.ActivityNotifier.OnSpaceFriendRequested(
- requesterSpace.OwnerID,
- requesterSpace.SpaceSlug,
- request.TargetID,
- )
+ go c.ActivityNotifier.OnSpaceFriendRequested(spaceActivityActor(requesterSpace), request.TargetID)
}
return &models.FriendStatusResponse{Status: "requested"}, nil
}
@@ -134,12 +125,7 @@ func (c *FriendsController) ConfirmRequest(ctx context.Context, targetSpace *rep
return nil, err
}
if created {
- go c.ActivityNotifier.OnSpaceFriendAdded(
- targetSpace.OwnerID,
- targetSpace.SpaceID,
- targetSpace.SpaceSlug,
- requesterID,
- )
+ go c.ActivityNotifier.OnSpaceFriendAdded(spaceActivityActor(targetSpace), requesterID)
}
return &models.FriendStatusResponse{Status: "friend"}, nil
}
diff --git a/server/space/controller/messages.go b/server/space/controller/messages.go
index 9100059441..bc9086d4f9 100644
--- a/server/space/controller/messages.go
+++ b/server/space/controller/messages.go
@@ -69,12 +69,7 @@ func (c *MessagesController) Create(ctx context.Context, senderSpace *repo.Space
}
return nil, err
}
- go c.ActivityNotifier.OnSpaceMessageSent(
- senderSpace.OwnerID,
- senderSpace.SpaceID,
- senderSpace.SpaceSlug,
- recipientSpace.OwnerID,
- )
+ go c.ActivityNotifier.OnSpaceMessageSent(spaceActivityActor(senderSpace), recipientSpace.OwnerID)
return toMessageResponse(*message), nil
}
@@ -124,12 +119,7 @@ func (c *MessagesController) ReplyToPost(ctx context.Context, senderSpace *repo.
}
return nil, err
}
- go c.ActivityNotifier.OnSpacePostReplied(
- senderSpace.OwnerID,
- senderSpace.SpaceID,
- senderSpace.SpaceSlug,
- recipientSpace.OwnerID,
- )
+ go c.ActivityNotifier.OnSpacePostReplied(spaceActivityActor(senderSpace), recipientSpace.OwnerID)
return toMessageResponse(*message), nil
}
@@ -242,12 +232,7 @@ func (c *MessagesController) SetLike(ctx context.Context, actorSpace *repo.Space
return nil, err
}
if like && changed {
- go c.ActivityNotifier.OnSpaceMessageLiked(
- actorSpace.OwnerID,
- actorSpace.SpaceID,
- actorSpace.SpaceSlug,
- recipientUserID,
- )
+ go c.ActivityNotifier.OnSpaceMessageLiked(spaceActivityActor(actorSpace), recipientUserID)
}
return &models.LikeMessageResponse{Liked: like}, nil
}
diff --git a/server/space/controller/notifications.go b/server/space/controller/notifications.go
index 80e292e841..9ea1c694d6 100644
--- a/server/space/controller/notifications.go
+++ b/server/space/controller/notifications.go
@@ -37,14 +37,28 @@ const (
var sendSpaceWebPush = webpush.SendNotificationWithContext
+type SpaceActivityActor struct {
+ UserID int64
+ SpaceID string
+ Slug string
+}
+
+func spaceActivityActor(space *repo.SpaceRecord) SpaceActivityActor {
+ return SpaceActivityActor{
+ UserID: space.OwnerID,
+ SpaceID: space.SpaceID,
+ Slug: space.SpaceSlug,
+ }
+}
+
type SpaceActivityNotifier interface {
- 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)
- OnSpaceMessageLiked(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64)
- OnSpaceFriendAdded(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64)
- OnSpaceFriendRequested(actorUserID int64, actorSlug string, recipientUserID int64)
+ OnSpacePostCreated(actor SpaceActivityActor)
+ OnSpacePostLiked(actor SpaceActivityActor, recipientUserID int64)
+ OnSpacePostReplied(actor SpaceActivityActor, recipientUserID int64)
+ OnSpaceMessageSent(actor SpaceActivityActor, recipientUserID int64)
+ OnSpaceMessageLiked(actor SpaceActivityActor, recipientUserID int64)
+ OnSpaceFriendAdded(actor SpaceActivityActor, recipientUserID int64)
+ OnSpaceFriendRequested(actor SpaceActivityActor, recipientUserID int64)
}
type SpaceWebPushSender struct {
@@ -76,16 +90,13 @@ func NewSpaceWebPushSender(
}
}
-func (n *SpaceWebPushSender) OnSpacePostCreated(
- actorUserID int64,
- actorSpaceID, actorSlug string,
-) {
+func (n *SpaceWebPushSender) OnSpacePostCreated(actor SpaceActivityActor) {
if !n.available() {
return
}
subscriptions, err := n.WebPushRepo.ListPostSubscriptions(
context.Background(),
- actorSpaceID,
+ actor.SpaceID,
)
if err != nil {
log.WithField("event", spaceActivityPostCreated).WithError(err).
@@ -93,8 +104,7 @@ func (n *SpaceWebPushSender) OnSpacePostCreated(
return
}
n.send(
- actorUserID,
- actorSlug,
+ actor,
"posted a new photo",
"Check it out",
spaceActivityPostCreated,
@@ -103,65 +113,40 @@ func (n *SpaceWebPushSender) OnSpacePostCreated(
)
}
-func (n *SpaceWebPushSender) OnSpacePostLiked(
- actorUserID int64,
- actorSpaceID, actorSlug string,
- recipientUserID int64,
-) {
- n.sendAccountActivity(actorUserID, actorSlug, "liked your post", "", spaceActivityPostLiked, conversationURL(actorSpaceID), recipientUserID)
+func (n *SpaceWebPushSender) OnSpacePostLiked(actor SpaceActivityActor, recipientUserID int64) {
+ n.sendAccountActivity(actor, "liked your post", "", spaceActivityPostLiked, conversationURL(actor.SpaceID), recipientUserID)
}
-func (n *SpaceWebPushSender) OnSpacePostReplied(
- actorUserID int64,
- actorSpaceID, actorSlug string,
- recipientUserID int64,
-) {
- n.sendAccountActivity(actorUserID, actorSlug, "replied to your post", "Check it out", spaceActivityPostReplied, conversationURL(actorSpaceID), recipientUserID)
+func (n *SpaceWebPushSender) OnSpacePostReplied(actor SpaceActivityActor, recipientUserID int64) {
+ n.sendAccountActivity(actor, "replied to your post", "Check it out", spaceActivityPostReplied, conversationURL(actor.SpaceID), recipientUserID)
}
-func (n *SpaceWebPushSender) OnSpaceMessageSent(
- actorUserID int64,
- actorSpaceID, actorSlug string,
- recipientUserID int64,
-) {
- n.sendAccountActivity(actorUserID, actorSlug, "sent you a message", "Check it out", spaceActivityMessageSent, conversationURL(actorSpaceID), recipientUserID)
+func (n *SpaceWebPushSender) OnSpaceMessageSent(actor SpaceActivityActor, recipientUserID int64) {
+ n.sendAccountActivity(actor, "sent you a message", "Check it out", spaceActivityMessageSent, conversationURL(actor.SpaceID), recipientUserID)
}
-func (n *SpaceWebPushSender) OnSpaceMessageLiked(
- actorUserID int64,
- actorSpaceID, actorSlug string,
- recipientUserID int64,
-) {
- n.sendAccountActivity(actorUserID, actorSlug, "liked a message", "View conversation", spaceActivityMessageLiked, conversationURL(actorSpaceID), recipientUserID)
+func (n *SpaceWebPushSender) OnSpaceMessageLiked(actor SpaceActivityActor, recipientUserID int64) {
+ n.sendAccountActivity(actor, "liked a message", "View conversation", spaceActivityMessageLiked, conversationURL(actor.SpaceID), recipientUserID)
}
-func (n *SpaceWebPushSender) OnSpaceFriendAdded(
- actorUserID int64,
- actorSpaceID, actorSlug string,
- recipientUserID int64,
-) {
+func (n *SpaceWebPushSender) OnSpaceFriendAdded(actor SpaceActivityActor, recipientUserID int64) {
n.sendAccountActivity(
- actorUserID,
- actorSlug,
+ actor,
"is now your friend",
- "Say hi to "+spaceActivityActorLabel(actorSlug),
+ "Say hi to "+spaceActivityActorLabel(actor.Slug),
spaceActivityFriendAdded,
- conversationURL(actorSpaceID),
+ conversationURL(actor.SpaceID),
recipientUserID,
)
}
-func (n *SpaceWebPushSender) OnSpaceFriendRequested(
- actorUserID int64,
- actorSlug string,
- recipientUserID int64,
-) {
- n.sendAccountActivity(actorUserID, actorSlug, "sent you a friend request", "Review request", spaceActivityFriendRequested, "/app/messages", recipientUserID)
+func (n *SpaceWebPushSender) OnSpaceFriendRequested(actor SpaceActivityActor, recipientUserID int64) {
+ n.sendAccountActivity(actor, "sent you a friend request", "Review request", spaceActivityFriendRequested, "/app/messages", recipientUserID)
}
func (n *SpaceWebPushSender) sendAccountActivity(
- actorUserID int64,
- actorSlug, activity, action, event, destination string,
+ actor SpaceActivityActor,
+ activity, action, event, destination string,
recipientUserID int64,
) {
if !n.available() || recipientUserID <= 0 {
@@ -176,7 +161,7 @@ func (n *SpaceWebPushSender) sendAccountActivity(
Error("Failed to list Space web push subscriptions")
return
}
- n.send(actorUserID, actorSlug, activity, action, event, destination, subscriptions)
+ n.send(actor, activity, action, event, destination, subscriptions)
}
func (n *SpaceWebPushSender) available() bool {
@@ -190,8 +175,8 @@ func (n *SpaceWebPushSender) available() bool {
}
func (n *SpaceWebPushSender) send(
- actorUserID int64,
- actorSlug, activity, action, event, destination string,
+ actor SpaceActivityActor,
+ activity, action, event, destination string,
subscriptions []repo.SpaceWebPushSubscriptionRecord,
) {
options := spaceWebPushOptions(n.config, n.HTTPClient)
@@ -204,10 +189,10 @@ func (n *SpaceWebPushSender) send(
}
limitContext, err := n.sendLimiter.Get(
context.Background(),
- strconv.FormatInt(actorUserID, 10),
+ strconv.FormatInt(actor.UserID, 10),
)
if err != nil {
- log.WithField("actor_user_id", actorUserID).WithError(err).
+ log.WithField("actor_user_id", actor.UserID).WithError(err).
Error("Failed to check Space web push rate limit")
return
}
@@ -216,7 +201,7 @@ func (n *SpaceWebPushSender) send(
}
if limitContext.Remaining == 0 {
log.WithFields(log.Fields{
- "actor_user_id": actorUserID,
+ "actor_user_id": actor.UserID,
"reset_at": limitContext.Reset,
}).Warn("Space web push rate limit reached")
}
@@ -225,7 +210,7 @@ func (n *SpaceWebPushSender) send(
for _, subscription := range subscriptions {
payload := spaceWebPushPayload{
Title: "Ente Space",
- Body: fmt.Sprintf("%s %s", spaceActivityActorLabel(actorSlug), activity),
+ Body: fmt.Sprintf("%s %s", spaceActivityActorLabel(actor.Slug), activity),
Action: action,
}
if subscription.Public {
diff --git a/server/space/controller/notifications_test.go b/server/space/controller/notifications_test.go
index 1eeec84126..c4db38e373 100644
--- a/server/space/controller/notifications_test.go
+++ b/server/space/controller/notifications_test.go
@@ -37,40 +37,40 @@ func newRecordingSpaceActivityNotifier() *recordingSpaceActivityNotifier {
return &recordingSpaceActivityNotifier{events: make(chan recordedSpaceActivity, 8)}
}
-func (n *recordingSpaceActivityNotifier) OnSpacePostCreated(actorUserID int64, actorSpaceID, actorSlug string) {
- n.record(spaceActivityPostCreated, actorUserID, actorSpaceID, actorSlug)
+func (n *recordingSpaceActivityNotifier) OnSpacePostCreated(actor SpaceActivityActor) {
+ n.record(spaceActivityPostCreated, actor)
}
-func (n *recordingSpaceActivityNotifier) OnSpacePostLiked(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64) {
- n.record(spaceActivityPostLiked, actorUserID, actorSpaceID, actorSlug, recipientUserID)
+func (n *recordingSpaceActivityNotifier) OnSpacePostLiked(actor SpaceActivityActor, recipientUserID int64) {
+ n.record(spaceActivityPostLiked, actor, recipientUserID)
}
-func (n *recordingSpaceActivityNotifier) OnSpacePostReplied(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64) {
- n.record(spaceActivityPostReplied, actorUserID, actorSpaceID, actorSlug, recipientUserID)
+func (n *recordingSpaceActivityNotifier) OnSpacePostReplied(actor SpaceActivityActor, recipientUserID int64) {
+ n.record(spaceActivityPostReplied, actor, recipientUserID)
}
-func (n *recordingSpaceActivityNotifier) OnSpaceMessageSent(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64) {
- n.record(spaceActivityMessageSent, actorUserID, actorSpaceID, actorSlug, recipientUserID)
+func (n *recordingSpaceActivityNotifier) OnSpaceMessageSent(actor SpaceActivityActor, recipientUserID int64) {
+ n.record(spaceActivityMessageSent, actor, recipientUserID)
}
-func (n *recordingSpaceActivityNotifier) OnSpaceMessageLiked(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64) {
- n.record(spaceActivityMessageLiked, actorUserID, actorSpaceID, actorSlug, recipientUserID)
+func (n *recordingSpaceActivityNotifier) OnSpaceMessageLiked(actor SpaceActivityActor, recipientUserID int64) {
+ n.record(spaceActivityMessageLiked, actor, recipientUserID)
}
-func (n *recordingSpaceActivityNotifier) OnSpaceFriendAdded(actorUserID int64, actorSpaceID, actorSlug string, recipientUserID int64) {
- n.record(spaceActivityFriendAdded, actorUserID, actorSpaceID, actorSlug, recipientUserID)
+func (n *recordingSpaceActivityNotifier) OnSpaceFriendAdded(actor SpaceActivityActor, recipientUserID int64) {
+ n.record(spaceActivityFriendAdded, actor, recipientUserID)
}
-func (n *recordingSpaceActivityNotifier) OnSpaceFriendRequested(actorUserID int64, actorSlug string, recipientUserID int64) {
- n.record(spaceActivityFriendRequested, actorUserID, "", actorSlug, recipientUserID)
+func (n *recordingSpaceActivityNotifier) OnSpaceFriendRequested(actor SpaceActivityActor, recipientUserID int64) {
+ n.record(spaceActivityFriendRequested, actor, recipientUserID)
}
-func (n *recordingSpaceActivityNotifier) record(event string, actorUserID int64, actorSpaceID, actorSlug string, recipientIDs ...int64) {
+func (n *recordingSpaceActivityNotifier) record(event string, actor SpaceActivityActor, recipientIDs ...int64) {
n.events <- recordedSpaceActivity{
event: event,
- actorUserID: actorUserID,
- actorSpaceID: actorSpaceID,
- actorSlug: actorSlug,
+ actorUserID: actor.UserID,
+ actorSpaceID: actor.SpaceID,
+ actorSlug: actor.Slug,
recipientIDs: append([]int64(nil), recipientIDs...),
}
}
@@ -149,7 +149,7 @@ func newSpaceWebPushTestConfig(t *testing.T) *SpaceWebPushConfig {
func TestUnconfiguredSpaceWebPushSenderIsUnavailable(t *testing.T) {
sender := NewSpaceWebPushSender(&repo.WebPushRepository{}, nil)
require.False(t, sender.available())
- sender.OnSpacePostReplied(1, "alice_space", "alice", 2)
+ sender.OnSpacePostReplied(SpaceActivityActor{UserID: 1, SpaceID: "alice_space", Slug: "alice"}, 2)
}
func TestSpaceWebPushDeduplicatesSharedEndpointForAccount(t *testing.T) {
@@ -169,7 +169,7 @@ func TestNewPostNotifiesWithNoAccountFriends(t *testing.T) {
notifier := newRecordingSpaceActivityNotifier()
posts := NewModule(repos, nil, notifier, nil).Posts
- posts.notifyFriendsOfNewPost(1, "space_id", "alice")
+ posts.notifyFriendsOfNewPost(SpaceActivityActor{UserID: 1, SpaceID: "space_id", Slug: "alice"})
require.Equal(t, recordedSpaceActivity{
event: spaceActivityPostCreated,
@@ -289,6 +289,7 @@ func TestFriendActivitiesOnlyOnRelationshipTransitions(t *testing.T) {
require.Equal(t, recordedSpaceActivity{
event: spaceActivityFriendRequested,
actorUserID: bobID,
+ actorSpaceID: bobSpace.SpaceID,
actorSlug: bobSpace.SpaceSlug,
recipientIDs: []int64{aliceID},
}, requireSpaceActivity(t, notifier))
@@ -336,7 +337,7 @@ func TestSpaceWebPushSenderUsesGenericPayloadAndPrunesDeadEndpoint(t *testing.T)
}
sender := NewSpaceWebPushSender(repos.WebPush, config)
- sender.OnSpacePostReplied(1, "alice_space", "alice", recipientID)
+ sender.OnSpacePostReplied(SpaceActivityActor{UserID: 1, SpaceID: "alice_space", Slug: "alice"}, recipientID)
require.Equal(t, spaceWebPushPayload{
Title: "Ente Space",
Body: "@alice replied to your post",
@@ -361,8 +362,7 @@ func TestPublicPostPushCarriesOnlyOpaqueLocalRouteTarget(t *testing.T) {
sender := NewSpaceWebPushSender(nil, config)
sender.send(
- 2,
- "alice",
+ SpaceActivityActor{UserID: 2, Slug: "alice"},
"posted a new photo",
"Check it out",
spaceActivityPostCreated,
@@ -403,8 +403,7 @@ func TestSpaceWebPushSendRateAppliesOncePerActivity(t *testing.T) {
}
}
NewSpaceWebPushSender(nil, config).send(
- 3,
- "alice",
+ SpaceActivityActor{UserID: 3, Slug: "alice"},
"posted a new photo",
"Check it out",
spaceActivityPostCreated,
diff --git a/server/space/controller/posts.go b/server/space/controller/posts.go
index 2ba535fc7d..3f40232f3d 100644
--- a/server/space/controller/posts.go
+++ b/server/space/controller/posts.go
@@ -90,12 +90,12 @@ func (c *PostsController) Create(ctx context.Context, space *repo.SpaceRecord, r
space.SpaceID, space.OwnerID, postCount, repo.MaxPostsPerSpace,
))
}
- c.notifyFriendsOfNewPost(space.OwnerID, space.SpaceID, space.SpaceSlug)
+ c.notifyFriendsOfNewPost(spaceActivityActor(space))
return &models.CreatePostResponse{PostID: postID}, nil
}
-func (c *PostsController) notifyFriendsOfNewPost(ownerID int64, spaceID, spaceSlug string) {
- go c.ActivityNotifier.OnSpacePostCreated(ownerID, spaceID, spaceSlug)
+func (c *PostsController) notifyFriendsOfNewPost(actor SpaceActivityActor) {
+ go c.ActivityNotifier.OnSpacePostCreated(actor)
}
func (c *PostsController) postResponses(ctx context.Context, posts []repo.SpacePostRecord, includeAuthor bool) ([]models.PostResponse, error) {
@@ -228,12 +228,7 @@ func (c *PostsController) SetLike(ctx context.Context, actorSpace *repo.SpaceRec
return nil, err
}
if like && created {
- go c.ActivityNotifier.OnSpacePostLiked(
- actorSpace.OwnerID,
- actorSpace.SpaceID,
- actorSpace.SpaceSlug,
- post.OwnerID,
- )
+ go c.ActivityNotifier.OnSpacePostLiked(spaceActivityActor(actorSpace), post.OwnerID)
}
return &models.LikePostResponse{Liked: like}, nil
}
diff --git a/server/space/controller/test_helpers_test.go b/server/space/controller/test_helpers_test.go
index 343dac87c5..8de5b1fe50 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) {}
-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(SpaceActivityActor) {}
+func (noopSpaceActivityNotifier) OnSpacePostLiked(SpaceActivityActor, int64) {}
+func (noopSpaceActivityNotifier) OnSpacePostReplied(SpaceActivityActor, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceMessageSent(SpaceActivityActor, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceMessageLiked(SpaceActivityActor, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceFriendAdded(SpaceActivityActor, int64) {}
+func (noopSpaceActivityNotifier) OnSpaceFriendRequested(SpaceActivityActor, int64) {}
func testSpaceBytes(value string) []byte {
return []byte(value)