From cca17856eecbcc04171ed6ce52a4fb9ae54e02b5 Mon Sep 17 00:00:00 2001
From: Sumner Evans <sumner.evans@automattic.com>
Date: Thu, 27 Feb 2025 12:33:58 -0700
Subject: [PATCH] sync: add limit configurations

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
---
 pkg/connector/config.go           |  7 +++++++
 pkg/connector/example-config.yaml |  9 +++++++++
 pkg/connector/sync.go             | 22 ++++++++++++++++++++--
 3 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/pkg/connector/config.go b/pkg/connector/config.go
index b79f1ae..7df609a 100644
--- a/pkg/connector/config.go
+++ b/pkg/connector/config.go
@@ -31,6 +31,11 @@ var ExampleConfig string
 type Config struct {
 	DisplaynameTemplate string             `yaml:"displayname_template"`
 	displaynameTemplate *template.Template `yaml:"-"`
+
+	Sync struct {
+		UpdateLimit int `yaml:"update_limit"`
+		CreateLimit int `yaml:"create_limit"`
+	} `yaml:"sync"`
 }
 
 type umConfig Config
@@ -50,6 +55,8 @@ func (c *Config) PostProcess() (err error) {
 
 func upgradeConfig(helper up.Helper) {
 	helper.Copy(up.Str, "displayname_template")
+	helper.Copy(up.Int, "sync", "update_limit")
+	helper.Copy(up.Int, "sync", "create_limit")
 }
 
 func (lc *LinkedInConnector) GetConfig() (string, any, up.Upgrader) {
diff --git a/pkg/connector/example-config.yaml b/pkg/connector/example-config.yaml
index 8869912..c45efa8 100644
--- a/pkg/connector/example-config.yaml
+++ b/pkg/connector/example-config.yaml
@@ -2,3 +2,12 @@
 # .FirstName is replaced with the first name
 # .LastName is replaced with the last name
 displayname_template: "{{ with .Organization }}{{ . }}{{ else }}{{ .FirstName }} {{ .LastName }}{{ end }} (LinkedIn)"
+
+sync:
+    # Number of most recently active dialogs to check when syncing chats.
+    # Set to 0 to remove limit.
+    update_limit: 0
+    # Number of most recently active dialogs to create portals for when syncing
+    # chats.
+    # Set to 0 to remove limit.
+    create_limit: 10
diff --git a/pkg/connector/sync.go b/pkg/connector/sync.go
index dab3499..a8123cb 100644
--- a/pkg/connector/sync.go
+++ b/pkg/connector/sync.go
@@ -17,6 +17,7 @@ func (l *LinkedInClient) syncConversations(ctx context.Context) {
 
 	lastUsedUpdatedBefore := time.Time{}
 	updatedBefore := time.Now()
+	var updated, created int
 	for {
 		log := log.With().
 			Time("updated_before", updatedBefore).
@@ -45,13 +46,25 @@ func (l *LinkedInClient) syncConversations(ctx context.Context) {
 				updatedBefore = conv.LastActivityAt.Time
 			}
 
+			portalKey := l.makePortalKey(conv.EntityURN)
+			portal, err := l.main.Bridge.GetPortalByKey(ctx, portalKey)
+			if err != nil {
+				log.Err(err).Msg("Failed to get portal")
+				continue
+			}
+
 			meta := simplevent.EventMeta{
 				LogContext: func(c zerolog.Context) zerolog.Context {
 					return c.Str("update", "sync")
 				},
-				PortalKey:    l.makePortalKey(conv.EntityURN),
-				CreatePortal: true,
+				PortalKey:    portalKey,
+				CreatePortal: l.main.Config.Sync.CreateLimit == 0 || created <= l.main.Config.Sync.CreateLimit,
+			}
+
+			if portal == nil || portal.MXID == "" {
+				created++
 			}
+			updated++
 
 			var latestMessageTS time.Time
 			for _, msg := range conv.Messages.Elements {
@@ -64,6 +77,11 @@ func (l *LinkedInClient) syncConversations(ctx context.Context) {
 				EventMeta:       meta.WithType(bridgev2.RemoteEventChatResync),
 				LatestMessageTS: latestMessageTS,
 			})
+
+			if l.main.Config.Sync.UpdateLimit > 0 && updated >= l.main.Config.Sync.UpdateLimit {
+				log.Info().Msg("Update limit reached")
+				return
+			}
 		}
 	}
 }
-- 
GitLab