Skip to content

Commit

Permalink
GO-4012 merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cheggaaa committed Sep 9, 2024
2 parents 0c8b6b0 + 6278134 commit d7a9d3c
Show file tree
Hide file tree
Showing 47 changed files with 523 additions and 1,581 deletions.
4 changes: 2 additions & 2 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# The script below adds the branch name automatically to
# every one of your commit messages. The regular expression
# below searches for linear issue key's. The issue key will
Expand All @@ -24,4 +24,4 @@ fi

if [[ "$COMMIT_TEXT" != "GO-"* ]]; then
echo "$ISSUE_ID" "$COMMIT_TEXT" > "$COMMIT_MSG_FILE"
fi
fi
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protos-swift:
@echo 'Generating swift protobuf files'
@protoc -I ./ --swift_opt=FileNaming=DropPath --swift_opt=Visibility=Public --swift_out=./dist/ios/protobuf pb/protos/*.proto pkg/lib/pb/model/protos/*.proto
@echo 'Generated swift protobuf files at ./dist/ios/pb'

protos-swift-local: protos-swift
@echo 'Clear proto files'
@rm -rf ./dist/ios/protobuf/protos
Expand Down Expand Up @@ -325,14 +325,14 @@ install-linter:
run-linter:
ifdef GOLANGCI_LINT_BRANCH
@golangci-lint run -v ./... --new-from-rev=$(GOLANGCI_LINT_BRANCH) --timeout 15m --verbose
else
else
@golangci-lint run -v ./... --new-from-rev=origin/main --timeout 15m --verbose
endif

run-linter-fix:
ifdef GOLANGCI_LINT_BRANCH
@golangci-lint run -v ./... --new-from-rev=$(GOLANGCI_LINT_BRANCH) --timeout 15m --fix
else
else
@golangci-lint run -v ./... --new-from-rev=origin/main --timeout 15m --fix
endif

Expand Down Expand Up @@ -390,4 +390,4 @@ download-tantivy-all: download-tantivy

download-tantivy-local: remove-libs
@mkdir -p $(OUTPUT_DIR)
@cp -r $(TANTIVY_GO_PATH)/libs/ $(OUTPUT_DIR)
@cp -r $(TANTIVY_GO_PATH)/libs/* $(OUTPUT_DIR)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Middleware library for Anytype, distributed as part of the Anytype clients.
- [Project workflows](docs/Flow.md)

### CLI tools
- [Usecase generator](docs/UsecaseGenerator.md)
- [Usecase validator](docs/UsecaseValidator.md)
- [Export Archive unpacker](docs/ExportArchiveUnpacker.md)

## Contribution
Expand Down
187 changes: 139 additions & 48 deletions core/block/editor/chatobject/chatobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package chatobject

import (
"context"
"errors"
"fmt"
"path/filepath"
"testing"

anystore "github.com/anyproto/any-store"
"github.com/globalsign/mgo/bson"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -38,7 +40,9 @@ func (a *accountServiceStub) AccountID() string {

type fixture struct {
*storeObject
source *mock_source.MockStore
source *mock_source.MockStore
accountServiceStub *accountServiceStub
sourceCreator string
}

const testCreator = "accountId1"
Expand All @@ -58,33 +62,35 @@ func newFixture(t *testing.T) *fixture {

object := New(sb, accountService, dbProvider, eventSender)

fx := &fixture{
storeObject: object.(*storeObject),
accountServiceStub: accountService,
sourceCreator: testCreator,
}
source := mock_source.NewMockStore(t)
source.EXPECT().ReadStoreDoc(ctx, mock.Anything, mock.Anything).Return(nil)
source.EXPECT().PushStoreChange(mock.Anything, mock.Anything).RunAndReturn(fx.applyToStore).Maybe()
fx.source = source

err = object.Init(&smartblock.InitContext{
Ctx: ctx,
Source: source,
})
require.NoError(t, err)

return &fixture{
storeObject: object.(*storeObject),
source: source,
}
return fx
}

// TODO Test ChatHandler: validate in BeforeCreate that creator from change equals to creator from message

func TestAddMessage(t *testing.T) {
ctx := context.Background()
fx := newFixture(t)
changeId := "messageId1"
fx.source.EXPECT().PushStoreChange(mock.Anything, mock.Anything).RunAndReturn(applyToStore(changeId))

inputMessage := givenMessage()
messageId, err := fx.AddMessage(ctx, inputMessage)
require.NoError(t, err)
assert.Equal(t, changeId, messageId)
assert.NotEmpty(t, messageId)

messages, err := fx.GetMessages(ctx, "", 0)
require.NoError(t, err)
Expand All @@ -96,6 +102,10 @@ func TestAddMessage(t *testing.T) {
want.Creator = testCreator

got := messages[0]
assertMessagesEqual(t, want, got)
}

func assertMessagesEqual(t *testing.T, want, got *model.ChatMessage) {
// Cleanup order id
assert.NotEmpty(t, got.OrderId)
got.OrderId = ""
Expand All @@ -106,69 +116,150 @@ func TestAddMessage(t *testing.T) {
}

func TestEditMessage(t *testing.T) {
// TODO Test attempt to edit other's message
t.Run("edit own message", func(t *testing.T) {
ctx := context.Background()
fx := newFixture(t)

// Add
inputMessage := givenMessage()

messageId, err := fx.AddMessage(ctx, inputMessage)
require.NoError(t, err)
assert.NotEmpty(t, messageId)

// Edit
editedMessage := givenMessage()
editedMessage.Message.Text = "edited text!"

err = fx.EditMessage(ctx, messageId, editedMessage)
require.NoError(t, err)

messages, err := fx.GetMessages(ctx, "", 0)
require.NoError(t, err)
require.Len(t, messages, 1)

want := editedMessage
want.Id = messageId
want.Creator = testCreator

got := messages[0]
assertMessagesEqual(t, want, got)
})

t.Run("edit other's message", func(t *testing.T) {
ctx := context.Background()
fx := newFixture(t)

// Add
inputMessage := givenMessage()

messageId, err := fx.AddMessage(ctx, inputMessage)
require.NoError(t, err)
assert.NotEmpty(t, messageId)

// Edit
editedMessage := givenMessage()
editedMessage.Message.Text = "edited text!"

fx.sourceCreator = "maliciousPerson"

err = fx.EditMessage(ctx, messageId, editedMessage)
require.Error(t, err)

// Check that nothing is changed
messages, err := fx.GetMessages(ctx, "", 0)
require.NoError(t, err)
require.Len(t, messages, 1)

want := inputMessage
want.Id = messageId
want.Creator = testCreator

got := messages[0]
assertMessagesEqual(t, want, got)
})

}

func TestToggleReaction(t *testing.T) {
ctx := context.Background()
fx := newFixture(t)

// Add
inputMessage := givenMessage()
changeId := "messageId1"
fx.source.EXPECT().PushStoreChange(mock.Anything, mock.Anything).RunAndReturn(applyToStore(changeId))
inputMessage.Reactions = nil

messageId, err := fx.AddMessage(ctx, inputMessage)
require.NoError(t, err)
assert.NotEmpty(t, messageId)

// Edit
editedMessage := givenMessage()
editedMessage.Message.Text = "edited text!"

changeId = "messageId2"
fx.source.EXPECT().PushStoreChange(mock.Anything, mock.Anything).RunAndReturn(applyToStore(changeId))
err = fx.EditMessage(ctx, messageId, editedMessage)
require.NoError(t, err)
t.Run("can toggle own reactions", func(t *testing.T) {
err = fx.ToggleMessageReaction(ctx, messageId, "👻")
require.NoError(t, err)
err = fx.ToggleMessageReaction(ctx, messageId, "🐻")
require.NoError(t, err)
err = fx.ToggleMessageReaction(ctx, messageId, "👺")
require.NoError(t, err)
err = fx.ToggleMessageReaction(ctx, messageId, "👺")
require.NoError(t, err)
})

anotherPerson := "anotherPerson"

t.Run("can't toggle someone else's reactions", func(t *testing.T) {
fx.sourceCreator = testCreator
fx.accountServiceStub.accountId = anotherPerson
err = fx.ToggleMessageReaction(ctx, messageId, "🐻")
require.Error(t, err)
})
t.Run("can toggle reactions on someone else's messages", func(t *testing.T) {
fx.sourceCreator = anotherPerson
fx.accountServiceStub.accountId = anotherPerson
err = fx.ToggleMessageReaction(ctx, messageId, "🐻")
require.NoError(t, err)
})

messages, err := fx.GetMessages(ctx, "", 0)
require.NoError(t, err)

require.Len(t, messages, 1)

want := editedMessage
want.Id = messageId
want.Creator = testCreator
got := messages[0].Reactions

got := messages[0]
// Cleanup order id
assert.NotEmpty(t, got.OrderId)
got.OrderId = ""
// Cleanup timestamp
assert.NotZero(t, got.CreatedAt)
got.CreatedAt = 0
want := &model.ChatMessageReactions{
Reactions: map[string]*model.ChatMessageReactionsIdentityList{
"👻": {
Ids: []string{testCreator},
},
"🐻": {
Ids: []string{testCreator, anotherPerson},
},
},
}
assert.Equal(t, want, got)
}

func TestToggleReaction(t *testing.T) {
// TODO Implement
}

func applyToStore(changeId string) func(ctx context.Context, params source.PushStoreChangeParams) (string, error) {
return func(ctx context.Context, params source.PushStoreChangeParams) (string, error) {
tx, err := params.State.NewTx(ctx)
if err != nil {
return "", fmt.Errorf("new tx: %w", err)
}
order := tx.NextOrder(tx.GetMaxOrder())
err = tx.ApplyChangeSet(storestate.ChangeSet{
Id: changeId,
Order: order,
Changes: params.Changes,
Creator: testCreator,
Timestamp: params.Time.Unix(),
})
if err != nil {
return "", fmt.Errorf("apply change set: %w", err)
}
return changeId, tx.Commit()
func (fx *fixture) applyToStore(ctx context.Context, params source.PushStoreChangeParams) (string, error) {
changeId := bson.NewObjectId().Hex()
tx, err := params.State.NewTx(ctx)
if err != nil {
return "", fmt.Errorf("new tx: %w", err)
}
order := tx.NextOrder(tx.GetMaxOrder())
err = tx.ApplyChangeSet(storestate.ChangeSet{
Id: changeId,
Order: order,
Changes: params.Changes,
Creator: fx.sourceCreator,
Timestamp: params.Time.Unix(),
})
if err != nil {
return "", errors.Join(tx.Rollback(), fmt.Errorf("apply change set: %w", err))
}
return changeId, tx.Commit()
}

func givenMessage() *model.ChatMessage {
Expand Down
7 changes: 7 additions & 0 deletions core/block/editor/converter/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func (c *layoutConverter) Convert(space smartblock.Space, st *state.State, fromL
return nil
}

if fromLayout == model.ObjectType_chat || fromLayout == model.ObjectType_chatDerived {
return fmt.Errorf("can't convert from chat")
}
if toLayout == model.ObjectType_chat || toLayout == model.ObjectType_chatDerived {
return fmt.Errorf("can't convert to chat")
}

if fromLayout == model.ObjectType_note && toLayout == model.ObjectType_collection {
return c.fromNoteToCollection(st)
}
Expand Down
4 changes: 2 additions & 2 deletions core/block/editor/objecttype/lastused_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestSetLastUsedDateForInitialType(t *testing.T) {
bundle.TypeKeyCollection.BundledURL(),
bundle.TypeKeyTask.BundledURL(),
bundle.TypeKeyPage.BundledURL(),
bundle.TypeKeyClassNote.BundledURL(),
bundle.TypeKeyGoal.BundledURL(),
bundle.TypeKeyAudio.BundledURL(),
}
rand.Shuffle(len(ots), func(i, j int) {
Expand All @@ -45,6 +45,6 @@ func TestSetLastUsedDateForInitialType(t *testing.T) {
assert.True(t, isLastUsedDateGreater(detailMap[bundle.TypeKeyTask.BundledURL()], detailMap[bundle.TypeKeySet.BundledURL()]))
assert.True(t, isLastUsedDateGreater(detailMap[bundle.TypeKeySet.BundledURL()], detailMap[bundle.TypeKeyCollection.BundledURL()]))
assert.True(t, isLastUsedDateGreater(detailMap[bundle.TypeKeyCollection.BundledURL()], detailMap[bundle.TypeKeyAudio.BundledURL()]))
assert.True(t, isLastUsedDateGreater(detailMap[bundle.TypeKeyCollection.BundledURL()], detailMap[bundle.TypeKeyClassNote.BundledURL()]))
assert.True(t, isLastUsedDateGreater(detailMap[bundle.TypeKeyCollection.BundledURL()], detailMap[bundle.TypeKeyGoal.BundledURL()]))
})
}
16 changes: 16 additions & 0 deletions core/block/editor/smartblock/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func (sb *smartBlock) injectLinksDetails(s *state.State) {
s.SetLocalDetail(bundle.RelationKeyLinks.String(), pbtypes.StringList(links))
}

func (sb *smartBlock) injectMentions(s *state.State) {
mentions := objectlink.DependentObjectIDs(s, sb.Space(), objectlink.Flags{
Blocks: true,
Details: false,
Relations: false,
Types: false,
Collection: false,
DataviewBlockOnlyTarget: true,
NoSystemRelations: true,
NoHiddenBundledRelations: true,
NoImages: true,
})
mentions = slice.RemoveMut(mentions, sb.Id())
s.SetLocalDetail(bundle.RelationKeyMentions.String(), pbtypes.StringList(mentions))
}

func isBacklinksChanged(msgs []simple.EventMessage) bool {
for _, msg := range msgs {
if amend, ok := msg.Msg.Value.(*pb.EventMessageValueOfObjectDetailsAmend); ok {
Expand Down
1 change: 1 addition & 0 deletions core/block/editor/smartblock/smartblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,7 @@ func (sb *smartBlock) injectDerivedDetails(s *state.State, spaceID string, sbt s
}

sb.injectLinksDetails(s)
sb.injectMentions(s)
sb.updateBackLinks(s)
}

Expand Down
Loading

0 comments on commit d7a9d3c

Please sign in to comment.