Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autojoin and show join leave events #587

Merged
merged 31 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
51e155c
Add autojoin and show join leave events - partial, missing e2e tests.
clemire Jul 30, 2024
5a68477
nit: remove non-logic edit.
clemire Jul 30, 2024
63498bf
Revert
clemire Jul 30, 2024
655f862
Fire off derived events on channel creation.
clemire Jul 31, 2024
3da55cc
Remove kruft.
clemire Jul 31, 2024
f62e2fd
remove kruft (2).
clemire Jul 31, 2024
e995c1c
prettier, lint.
clemire Jul 31, 2024
4fb8846
Merge branch 'main' into crystal/674-protocol-autojoin-channels
clemire Jul 31, 2024
910c33c
Remove capacity to accept events as derived, sent by node.
clemire Jul 31, 2024
4a98009
Add in-progess test case.
clemire Jul 31, 2024
d302c0d
Revert file.
clemire Jul 31, 2024
f0cc308
Fix defaults, test cases.
clemire Aug 1, 2024
a6aca75
Merge branch 'main' into crystal/674-protocol-autojoin-channels
clemire Aug 1, 2024
746d216
Extra comments for test case clarity.
clemire Aug 1, 2024
d30e348
Lint fixes.
clemire Aug 1, 2024
9579a28
Make entitlments-only.
clemire Aug 1, 2024
1836ebf
Merge branch 'main' into crystal/674-protocol-autojoin-channels
clemire Aug 1, 2024
f14ca8a
Fix protocol, minus emitting derived event with channel settings.
clemire Aug 1, 2024
713295c
PR feedback - settings in channel inception, updated tests, rule eval…
clemire Aug 1, 2024
cf786d0
channelExistsInSpace rename
clemire Aug 1, 2024
5bb11b8
refactor space chainauth
clemire Aug 1, 2024
1a1f4ab
Merge branch 'main' into crystal/674-protocol-autojoin-channels
clemire Aug 1, 2024
8f4074b
Method rename.
clemire Aug 2, 2024
0385db3
Fixed issues due to VS code not saving merge edits.
clemire Aug 2, 2024
ccdb178
Change autojoin for default to true if unspecified, updated tests.
clemire Aug 2, 2024
3ca4b4e
prettier:fix
clemire Aug 2, 2024
6c743e5
Lint fixes.
clemire Aug 2, 2024
3bcece9
prettier:fix
clemire Aug 2, 2024
4f5e55f
Requested changes.
clemire Aug 2, 2024
3b3aa7d
Merge branch 'main' into crystal/674-protocol-autojoin-channels
clemire Aug 2, 2024
5521d7d
Remove panic, continue if snapshot is malformatted during migration
clemire Aug 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/node/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func Make_SpacePayload_ChannelUpdate(
op ChannelOp,
channelId StreamId,
originEvent *EventRef,
settings *SpacePayload_ChannelSettings,
) *StreamEvent_SpacePayload {
return &StreamEvent_SpacePayload{
SpacePayload: &SpacePayload{
Expand All @@ -310,6 +311,7 @@ func Make_SpacePayload_ChannelUpdate(
Op: op,
ChannelId: channelId[:],
OriginEvent: originEvent,
Settings: settings,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions core/node/events/migrations/migrate_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type migrationFunc func(*Snapshot) *Snapshot
var MIGRATIONS = []migrationFunc{
snapshot_migration_0000,
snapshot_migration_0001,
snapshot_migration_0002,
}

func CurrentSnapshotVersion() int32 {
Expand Down
27 changes: 27 additions & 0 deletions core/node/events/migrations/snapshot_migration_0002.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package migrations

import (
. "github.com/river-build/river/core/node/protocol"
"github.com/river-build/river/core/node/shared"
)

func snapshot_migration_0002(iSnapshot *Snapshot) *Snapshot {
switch snapshot := iSnapshot.Content.(type) {
case *Snapshot_SpaceContent:
for _, channel := range snapshot.SpaceContent.Channels {
if channel.Settings == nil {
channel.Settings = &SpacePayload_ChannelSettings{}
}
channelId, err := shared.StreamIdFromBytes(channel.ChannelId)
if err != nil {
// Note: it would be better to log this error, but we have no logging
// context here at this time
continue
}
if shared.IsDefaultChannelId(channelId) {
channel.Settings.Autojoin = true
}
}
}
return iSnapshot
}
54 changes: 54 additions & 0 deletions core/node/events/migrations/snapshot_migration_0002_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package migrations

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"

. "github.com/river-build/river/core/node/protocol"

"github.com/river-build/river/core/node/shared"
)

func TestSnapshotMigration0002(t *testing.T) {
spaceId, err := shared.MakeSpaceId()
require.NoError(t, err)

defaultChannelId, err := shared.MakeDefaultChannelId(spaceId)
require.NoError(t, err)

channelId1, err := shared.MakeChannelId(spaceId)
require.NoError(t, err)

spaceSnap := &Snapshot{
Content: &Snapshot_SpaceContent{
SpaceContent: &SpacePayload_Snapshot{
Inception: &SpacePayload_Inception{
StreamId: spaceId[:],
},
Channels: []*SpacePayload_ChannelMetadata{
{
ChannelId: defaultChannelId[:],
},
{
ChannelId: channelId1[:],
},
},
},
},
}

migratedSnapshot := snapshot_migration_0002(spaceSnap)

require.Equal(t, 2, len(migratedSnapshot.GetSpaceContent().Channels))

require.True(t, bytes.Equal(migratedSnapshot.GetSpaceContent().Channels[0].ChannelId, defaultChannelId[:]))
require.True(t, bytes.Equal(migratedSnapshot.GetSpaceContent().Channels[1].ChannelId, channelId1[:]))

require.True(t, migratedSnapshot.GetSpaceContent().Channels[0].Settings.Autojoin)
require.False(t, migratedSnapshot.GetSpaceContent().Channels[1].Settings.Autojoin)

require.False(t, migratedSnapshot.GetSpaceContent().Channels[0].Settings.HideUserJoinLeaveEvents)
require.False(t, migratedSnapshot.GetSpaceContent().Channels[1].Settings.HideUserJoinLeaveEvents)
}
2 changes: 1 addition & 1 deletion core/node/events/miniblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Make_GenesisMiniblockHeader(parsedEvents []*ParsedEvent) (*MiniblockHeader,
}
}

snapshot, err := Make_GenisisSnapshot(parsedEvents)
snapshot, err := Make_GenesisSnapshot(parsedEvents)
if err != nil {
return nil, err
}
Expand Down
37 changes: 36 additions & 1 deletion core/node/events/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/river-build/river/core/node/shared"
)

func Make_GenisisSnapshot(events []*ParsedEvent) (*Snapshot, error) {
func Make_GenesisSnapshot(events []*ParsedEvent) (*Snapshot, error) {
if len(events) == 0 {
return nil, RiverError(Err_INVALID_ARGUMENT, "no events to make snapshot from")
}
Expand Down Expand Up @@ -201,9 +201,44 @@ func update_Snapshot_Space(
Op: content.Channel.Op,
OriginEvent: content.Channel.OriginEvent,
UpdatedAtEventNum: eventNum,
Settings: content.Channel.Settings,
}
if channel.Settings == nil {
if channel.Op == ChannelOp_CO_CREATED {
// Apply default channel settings for new channels when settings are not provided.
// Invariant: channel.Settings is defined for all channels in the snapshot.
channelId, err := shared.StreamIdFromBytes(content.Channel.ChannelId)
if err != nil {
return err
}
channel.Settings = &SpacePayload_ChannelSettings{
Autojoin: shared.IsDefaultChannelId(channelId),
}
} else if channel.Op == ChannelOp_CO_UPDATED {
// Find the existing channel and copy over the settings if new ones are not provided.
existingChannel, err := findChannel(snapshot.SpaceContent.Channels, content.Channel.ChannelId)
if err != nil {
return err
}
channel.Settings = existingChannel.Settings
}
}
clemire marked this conversation as resolved.
Show resolved Hide resolved
snapshot.SpaceContent.Channels = insertChannel(snapshot.SpaceContent.Channels, channel)
return nil
case *SpacePayload_UpdateChannelAutojoin_:
channel, err := findChannel(snapshot.SpaceContent.Channels, content.UpdateChannelAutojoin.ChannelId)
if err != nil {
return err
}
channel.Settings.Autojoin = content.UpdateChannelAutojoin.Autojoin
return nil
case *SpacePayload_UpdateChannelHideUserJoinLeaveEvents_:
channel, err := findChannel(snapshot.SpaceContent.Channels, content.UpdateChannelHideUserJoinLeaveEvents.ChannelId)
if err != nil {
return err
}
channel.Settings.HideUserJoinLeaveEvents = content.UpdateChannelHideUserJoinLeaveEvents.HideUserJoinLeaveEvents
return nil
case *SpacePayload_SpaceImage:
snapshot.SpaceContent.SpaceImage = &SpacePayload_SnappedSpaceImage{
Data: content.SpaceImage,
Expand Down
10 changes: 5 additions & 5 deletions core/node/events/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestMakeSnapshot(t *testing.T) {
wallet, _ := crypto.NewWallet(ctx)
streamId := UserStreamIdFromAddr(wallet.Address)
inception := make_User_Inception(wallet, streamId, t)
snapshot, err := Make_GenisisSnapshot([]*ParsedEvent{inception})
snapshot, err := Make_GenesisSnapshot([]*ParsedEvent{inception})
clemire marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
assert.Equal(
t,
Expand All @@ -161,7 +161,7 @@ func TestUpdateSnapshot(t *testing.T) {
wallet, _ := crypto.NewWallet(ctx)
streamId := UserStreamIdFromAddr(wallet.Address)
inception := make_User_Inception(wallet, streamId, t)
snapshot, err := Make_GenisisSnapshot([]*ParsedEvent{inception})
snapshot, err := Make_GenesisSnapshot([]*ParsedEvent{inception})
assert.NoError(t, err)

membership := make_User_Membership(wallet, MembershipOp_SO_JOIN, streamId, nil, t)
Expand All @@ -185,7 +185,7 @@ func TestCloneAndUpdateUserSnapshot(t *testing.T) {
wallet, _ := crypto.NewWallet(ctx)
streamId := UserStreamIdFromAddr(wallet.Address)
inception := make_User_Inception(wallet, streamId, t)
snapshot1, err := Make_GenisisSnapshot([]*ParsedEvent{inception})
snapshot1, err := Make_GenesisSnapshot([]*ParsedEvent{inception})
assert.NoError(t, err)

snapshot := proto.Clone(snapshot1).(*Snapshot)
Expand All @@ -211,7 +211,7 @@ func TestCloneAndUpdateSpaceSnapshot(t *testing.T) {
wallet, _ := crypto.NewWallet(ctx)
streamId := UserStreamIdFromAddr(wallet.Address)
inception := make_Space_Inception(wallet, streamId, t)
snapshot1, err := Make_GenisisSnapshot([]*ParsedEvent{inception})
snapshot1, err := Make_GenesisSnapshot([]*ParsedEvent{inception})
assert.NoError(t, err)
userId, err := AddressHex(inception.Event.CreatorAddress)
assert.NoError(t, err)
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestUpdateSnapshotFailsIfInception(t *testing.T) {
wallet, _ := crypto.NewWallet(ctx)
streamId := UserStreamIdFromAddr(wallet.Address)
inception := make_User_Inception(wallet, streamId, t)
snapshot, err := Make_GenisisSnapshot([]*ParsedEvent{inception})
snapshot, err := Make_GenesisSnapshot([]*ParsedEvent{inception})
assert.NoError(t, err)

err = Update_Snapshot(snapshot, inception, 0, 1)
Expand Down
Loading
Loading