1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

GO-5303 Event added: spaceAutoWidgetAdded

This commit is contained in:
Roman Khafizianov 2025-03-28 17:36:22 +01:00
parent 2451192001
commit 0e8cd67a20
No known key found for this signature in database
GPG key ID: F07A7D55A2684852
11 changed files with 1462 additions and 1385 deletions

View file

@ -273,6 +273,6 @@ func (s *service) createFavoriteWidget(spc clientspace.Space) error {
}
return cache.DoState(s.objectGetter, widgetObjectId, func(st *state.State, w widget.Widget) (err error) {
return w.AddAutoWidget(st, widget.DefaultWidgetFavorite, widget.DefaultWidgetFavorite, "", model.BlockContentWidget_CompactList)
return w.AddAutoWidget(st, widget.DefaultWidgetFavorite, widget.DefaultWidgetFavorite, "", model.BlockContentWidget_CompactList, "Favorites")
})
}

View file

@ -100,7 +100,7 @@ func (p *Archive) autoInstallBinWidget() error {
st := sb.NewState()
if w, ok := sb.(widget.Widget); ok {
// We rely on AddAutoWidget to check if the widget was already installed/removed before
err = w.AddAutoWidget(st, widget.DefaultWidgetBin, widget.DefaultWidgetBin, "", model.BlockContentWidget_Link)
err = w.AddAutoWidget(st, widget.DefaultWidgetBin, widget.DefaultWidgetBin, "", model.BlockContentWidget_Link, "Bin")
if err != nil {
return err
}

View file

@ -385,6 +385,11 @@ func (sb *smartBlock) changeResolvedLayoutForObjects(msgs []simple.EventMessage,
return nil
}
func (sb *smartBlock) getTypeDetails() (*domain.Details, error) {
typeId := sb.LocalDetails().GetString(bundle.RelationKeyType)
return sb.spaceIndex.GetDetails(typeId)
}
func getLayoutFromMessages(msgs []simple.EventMessage) (layout int64, found bool) {
for _, ev := range msgs {
if amend := ev.Msg.GetObjectDetailsAmend(); amend != nil {

View file

@ -8,6 +8,7 @@ import (
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/core/block/simple"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/event"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
@ -27,7 +28,8 @@ type Widget interface {
CreateBlock(s *state.State, req *pb.RpcBlockCreateWidgetRequest) (string, error)
// AddAutoWidget adds a widget block. If widget with the same targetId was installed/removed before, it will not be added again.
// blockId is optional and used to protect from multi-device conflicts.
AddAutoWidget(s *state.State, targetId, blockId, viewId string, layout model.BlockContentWidgetLayout) error
// if eventName is empty no event is produced
AddAutoWidget(s *state.State, targetId, blockId, viewId string, layout model.BlockContentWidgetLayout, eventName string) error
}
type widget struct {
@ -71,7 +73,7 @@ func NewWidget(sb smartblock.SmartBlock) Widget {
}
}
func (w *widget) AddAutoWidget(st *state.State, targetId, widgetBlockId, viewId string, layout model.BlockContentWidgetLayout) error {
func (w *widget) AddAutoWidget(st *state.State, targetId, widgetBlockId, viewId string, layout model.BlockContentWidgetLayout, eventName string) error {
targets := st.Details().Get(bundle.RelationKeyAutoWidgetTargets).StringList()
if slices.Contains(targets, targetId) {
return nil
@ -125,7 +127,7 @@ func (w *widget) AddAutoWidget(st *state.State, targetId, widgetBlockId, viewId
position = model.Block_Bottom
}
_, err = w.CreateBlock(st, &pb.RpcBlockCreateWidgetRequest{
_, err = w.createBlock(st, &pb.RpcBlockCreateWidgetRequest{
ContextId: st.RootId(),
ObjectLimit: 6,
WidgetLayout: layout,
@ -138,11 +140,28 @@ func (w *widget) AddAutoWidget(st *state.State, targetId, widgetBlockId, viewId
TargetBlockId: targetId,
}},
},
}, true)
if err != nil {
return err
}
msg := event.NewMessage(w.SpaceID(), &pb.EventMessageValueOfSpaceAutoWidgetAdded{
SpaceAutoWidgetAdded: &pb.EventSpaceAutoWidgetAdded{
TargetId: targetId,
TargetName: eventName,
WidgetBlockId: widgetBlockId,
},
})
return err
w.SendEvent([]*pb.EventMessage{msg})
return nil
}
func (w *widget) CreateBlock(s *state.State, req *pb.RpcBlockCreateWidgetRequest) (string, error) {
return w.createBlock(s, req, false)
}
func (w *widget) createBlock(s *state.State, req *pb.RpcBlockCreateWidgetRequest, isAutoAdded bool) (string, error) {
if req.Block.Content == nil {
return "", fmt.Errorf("block has no content")
}
@ -169,9 +188,10 @@ func (w *widget) CreateBlock(s *state.State, req *pb.RpcBlockCreateWidgetRequest
},
Content: &model.BlockContentOfWidget{
Widget: &model.BlockContentWidget{
Layout: req.WidgetLayout,
Limit: req.ObjectLimit,
ViewId: req.ViewId,
Layout: req.WidgetLayout,
Limit: req.ObjectLimit,
ViewId: req.ViewId,
AutoAdded: isAutoAdded,
},
},
})

View file

@ -5,8 +5,6 @@ import (
"fmt"
"slices"
"github.com/google/uuid"
"github.com/anyproto/anytype-heart/core/block/cache"
"github.com/anyproto/anytype-heart/core/block/editor/basic"
"github.com/anyproto/anytype-heart/core/block/editor/smartblock"
@ -97,6 +95,7 @@ func (s *Service) CreateTypeWidgetIfMissing(ctx context.Context, spaceId string,
return err
}
widgetObjectId := space.DerivedIDs().Widgets
spaceIndex := s.objectStore.SpaceIndex(space.Id())
widgetDetails, err := s.objectStore.SpaceIndex(space.Id()).GetDetails(widgetObjectId)
if err == nil {
keys := widgetDetails.Get(bundle.RelationKeyAutoWidgetTargets).StringList()
@ -130,8 +129,17 @@ func (s *Service) CreateTypeWidgetIfMissing(ctx context.Context, spaceId string,
// only create widget if this was the first object of this type created
return nil
}
var targetName string
typeDetails, err := spaceIndex.GetDetails(typeId)
if err == nil {
targetName = typeDetails.Get(bundle.RelationKeyPluralName).String()
if targetName == "" {
targetName = typeDetails.Get(bundle.RelationKeyName).String()
}
}
return cache.DoState(s, widgetObjectId, func(st *state.State, w widget.Widget) (err error) {
return w.AddAutoWidget(st, typeId, key.String(), addr.ObjectTypeAllViewId, model.BlockContentWidget_View)
return w.AddAutoWidget(st, typeId, key.String(), addr.ObjectTypeAllViewId, model.BlockContentWidget_View, targetName)
})
return err
}

View file

@ -1850,6 +1850,7 @@
- [Event.Process.New](#anytype-Event-Process-New)
- [Event.Process.Update](#anytype-Event-Process-Update)
- [Event.Space](#anytype-Event-Space)
- [Event.Space.AutoWidgetAdded](#anytype-Event-Space-AutoWidgetAdded)
- [Event.Space.SyncStatus](#anytype-Event-Space-SyncStatus)
- [Event.Space.SyncStatus.Update](#anytype-Event-Space-SyncStatus-Update)
- [Event.Status](#anytype-Event-Status)
@ -1966,7 +1967,6 @@
- [Metadata.Payload](#anytype-model-Metadata-Payload)
- [Metadata.Payload.IdentityPayload](#anytype-model-Metadata-Payload-IdentityPayload)
- [Notification](#anytype-model-Notification)
- [Notification.AutoTypeWidgetAdded](#anytype-model-Notification-AutoTypeWidgetAdded)
- [Notification.Export](#anytype-model-Notification-Export)
- [Notification.GalleryImport](#anytype-model-Notification-GalleryImport)
- [Notification.Import](#anytype-model-Notification-Import)
@ -28761,6 +28761,7 @@ Precondition: user A opened a block
| payloadBroadcast | [Event.Payload.Broadcast](#anytype-Event-Payload-Broadcast) | | |
| membershipUpdate | [Event.Membership.Update](#anytype-Event-Membership-Update) | | |
| spaceSyncStatusUpdate | [Event.Space.SyncStatus.Update](#anytype-Event-Space-SyncStatus-Update) | | |
| spaceAutoWidgetAdded | [Event.Space.AutoWidgetAdded](#anytype-Event-Space-AutoWidgetAdded) | | |
| p2pStatusUpdate | [Event.P2PStatus.Update](#anytype-Event-P2PStatus-Update) | | |
| importFinish | [Event.Import.Finish](#anytype-Event-Import-Finish) | | |
| chatAdd | [Event.Chat.Add](#anytype-Event-Chat-Add) | | |
@ -29227,6 +29228,23 @@ Removes document from subscription
<a name="anytype-Event-Space-AutoWidgetAdded"></a>
### Event.Space.AutoWidgetAdded
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| targetId | [string](#string) | | |
| targetName | [string](#string) | | pluralName (if exists) for types, fallback to name. Special cases for &#34;bin&#34; and &#34;favorites&#34; |
| widgetBlockId | [string](#string) | | |
<a name="anytype-Event-Space-SyncStatus"></a>
### Event.Space.SyncStatus
@ -30591,6 +30609,7 @@ Link: block to link some content from an external sources.
| layout | [Block.Content.Widget.Layout](#anytype-model-Block-Content-Widget-Layout) | | |
| limit | [int32](#int32) | | |
| viewId | [string](#string) | | |
| autoAdded | [bool](#bool) | | |
@ -31111,7 +31130,6 @@ Used to decode block meta only, without the content itself
| participantRemove | [Notification.ParticipantRemove](#anytype-model-Notification-ParticipantRemove) | | |
| participantRequestDecline | [Notification.ParticipantRequestDecline](#anytype-model-Notification-ParticipantRequestDecline) | | |
| participantPermissionsChange | [Notification.ParticipantPermissionsChange](#anytype-model-Notification-ParticipantPermissionsChange) | | |
| autoTypeWidgetAdded | [Notification.AutoTypeWidgetAdded](#anytype-model-Notification-AutoTypeWidgetAdded) | | |
| space | [string](#string) | | |
| aclHeadId | [string](#string) | | |
@ -31120,23 +31138,6 @@ Used to decode block meta only, without the content itself
<a name="anytype-model-Notification-AutoTypeWidgetAdded"></a>
### Notification.AutoTypeWidgetAdded
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| spaceId | [string](#string) | | |
| typePluralName | [string](#string) | | fallback to singular if missing |
| widgetBlockId | [string](#string) | | |
<a name="anytype-model-Notification-Export"></a>
### Notification.Export

File diff suppressed because it is too large Load diff

View file

@ -108,6 +108,7 @@ message Event {
Membership.Update membershipUpdate = 117;
Space.SyncStatus.Update spaceSyncStatusUpdate = 119;
Space.AutoWidgetAdded spaceAutoWidgetAdded = 122;
P2PStatus.Update p2pStatusUpdate = 120;
@ -1154,6 +1155,12 @@ message Event {
IncompatibleVersion = 2;
NetworkError = 3;
}
message AutoWidgetAdded {
string targetId = 1;
string targetName = 2; // pluralName (if exists) for types, fallback to name. Special cases for "bin" and "favorites"
string widgetBlockId = 3;
}
}
message P2PStatus {
message Update {

File diff suppressed because it is too large Load diff

View file

@ -593,6 +593,7 @@ message Block {
Layout layout = 1;
int32 limit = 2;
string viewId = 3;
bool autoAdded = 4;
enum Layout {
Link = 0;
@ -1055,7 +1056,6 @@ message Notification {
ParticipantRemove participantRemove = 16;
ParticipantRequestDecline participantRequestDecline = 17;
ParticipantPermissionsChange participantPermissionsChange = 18;
AutoTypeWidgetAdded autoTypeWidgetAdded = 19;
}
string space = 7;
string aclHeadId = 14;
@ -1140,12 +1140,6 @@ message Notification {
enum ActionType {
CLOSE = 0;
}
message AutoTypeWidgetAdded {
string spaceId = 1;
string typePluralName = 2; // fallback to singular if missing
string widgetBlockId = 3;
}
}
message Export {

View file

@ -405,7 +405,7 @@ func (b *builtinObjects) createWidgets(ctx session.Context, spaceId string, useC
}
if err = cache.DoStateCtx(b.objectGetter, ctx, widgetObjectID, func(s *state.State, w widget.Widget) error {
for _, targetId := range widgetTargetsToCreate {
if err := w.AddAutoWidget(s, targetId, "", addr.ObjectTypeAllViewId, model.BlockContentWidget_View); err != nil {
if err := w.AddAutoWidget(s, targetId, "", addr.ObjectTypeAllViewId, model.BlockContentWidget_View, ""); err != nil {
log.Errorf("failed to create widget block for type '%s': %v", targetId, err)
}
}