-
-
Notifications
You must be signed in to change notification settings - Fork 33
chatsync: classify requests using trust state #128
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
Open
highesttt
wants to merge
1
commit into
main
Choose a base branch
from
highest/plat-38683
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,96 @@ func isXChatPortalForLogin(portal *bridgev2.Portal, loginID networkid.UserLoginI | |
| return ok && meta.IsXChatConversation() | ||
| } | ||
|
|
||
| func xchatInboxItemTrust(item *response.XChatInboxItem) *bool { | ||
| defer func() { _ = recover() }() | ||
| if item == nil { | ||
| return nil | ||
| } | ||
| encodedEvents := make([]string, 0, len(item.LatestMessageEvents)+len(item.EncodedMessageEvents)+len(item.LatestConversationKeyChangeEvents)+1) | ||
| encodedEvents = append(encodedEvents, item.LatestMessageEvents...) | ||
| encodedEvents = append(encodedEvents, item.EncodedMessageEvents...) | ||
| encodedEvents = append(encodedEvents, item.LatestConversationKeyChangeEvents...) | ||
| if item.LatestNotifiableMessageCreateEvent != "" { | ||
| encodedEvents = append(encodedEvents, item.LatestNotifiableMessageCreateEvent) | ||
| } | ||
|
|
||
| var trusted *bool | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why's this a pointer not just a bool?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the field can still be absent for older chats, so that's to have 3 states: true/false/nil |
||
| latestSequenceID := "" | ||
| for _, encoded := range encodedEvents { | ||
| decoded, err := base64.StdEncoding.DecodeString(encoded) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| var evt payload.MessageEvent | ||
| if err = payload.Decode(decoded, &evt); err != nil || evt.IsTrusted == nil { | ||
| continue | ||
| } | ||
| sequenceID := ptr.Val(evt.SequenceId) | ||
| if trusted != nil && (sequenceID == "" && latestSequenceID != "" || | ||
| sequenceID != "" && latestSequenceID != "" && compareIntStrings(sequenceID, latestSequenceID) < 0) { | ||
| continue | ||
| } | ||
| value := *evt.IsTrusted | ||
| trusted = &value | ||
| latestSequenceID = sequenceID | ||
| } | ||
| return trusted | ||
| } | ||
|
|
||
| func applyXChatTrustToChatInfo(info *bridgev2.ChatInfo, trusted bool) { | ||
| info.MessageRequest = ptr.Ptr(!trusted) | ||
| info.ExtraUpdates = bridgev2.MergeExtraUpdaters(info.ExtraUpdates, func(_ context.Context, portal *bridgev2.Portal) bool { | ||
| meta, ok := portal.Metadata.(*PortalMetadata) | ||
| if !ok || meta == nil || (meta.XChatTrusted != nil && *meta.XChatTrusted == trusted) { | ||
| return false | ||
| } | ||
| meta.XChatTrusted = ptr.Ptr(trusted) | ||
| return true | ||
| }) | ||
| } | ||
|
|
||
| func (tc *TwitterClient) syncXChatTrust( | ||
| ctx context.Context, | ||
| conversationID string, | ||
| trusted *bool, | ||
| timestamp time.Time, | ||
| streamOrder int64, | ||
| ) bool { | ||
| if trusted == nil { | ||
| return true | ||
| } | ||
| if ctx == nil { | ||
| ctx = context.TODO() | ||
| } | ||
| portalKey := tc.MakePortalKeyFromID(conversationID) | ||
| portal, err := tc.connector.br.GetPortalByKey(ctx, portalKey) | ||
| if err != nil { | ||
| zerolog.Ctx(ctx).Warn().Err(err).Msg("Failed to get XChat portal for trust update") | ||
| return false | ||
| } | ||
| meta, _ := portal.Metadata.(*PortalMetadata) | ||
| messageRequest := !*trusted | ||
| if meta != nil && meta.XChatTrusted != nil && *meta.XChatTrusted == *trusted && portal.MessageRequest == messageRequest { | ||
| return true | ||
| } | ||
|
|
||
| chatInfo := &bridgev2.ChatInfo{} | ||
| applyXChatTrustToChatInfo(chatInfo, *trusted) | ||
| if current := bridgev2.GetRemoteEventFromContext(ctx); current != nil && current.GetPortalKey().ID == portalKey.ID { | ||
| portal.UpdateInfo(ctx, chatInfo, tc.userLogin, nil, timestamp) | ||
| return true | ||
| } | ||
| return tc.queueXChatRemoteEventWithPortalRepair(ctx, conversationID, &simplevent.ChatInfoChange{ | ||
| EventMeta: simplevent.EventMeta{ | ||
| Type: bridgev2.RemoteEventChatInfoChange, | ||
| PortalKey: portalKey, | ||
| Timestamp: timestamp, | ||
| StreamOrder: streamOrder, | ||
| }, | ||
| ChatInfoChange: &bridgev2.ChatInfoChange{ChatInfo: chatInfo}, | ||
| }) | ||
| } | ||
|
|
||
| // TODO: Remove this repair after affected bridges have been reconnected | ||
| func (tc *TwitterClient) repairExistingXChatMessageRequests(ctx context.Context) error { | ||
| if tc.xchatRequestsRepaired { | ||
|
|
@@ -68,8 +158,13 @@ func (tc *TwitterClient) repairExistingXChatMessageRequests(ctx context.Context) | |
| } | ||
| xchatRooms++ | ||
|
|
||
| meta := portal.Metadata.(*PortalMetadata) | ||
| messageRequest := false | ||
| if meta.XChatTrusted != nil { | ||
| messageRequest = !*meta.XChatTrusted | ||
| } | ||
| wasMessageRequest := portal.MessageRequest | ||
| portal.MessageRequest = false | ||
| portal.MessageRequest = messageRequest | ||
| internals := (*bridgev2.PortalInternals)(portal) | ||
| stateKey, bridgeInfo := internals.GetBridgeInfo() | ||
| for _, eventType := range []event.Type{event.StateBridge, event.StateHalfShotBridge} { | ||
|
|
@@ -81,7 +176,7 @@ func (tc *TwitterClient) repairExistingXChatMessageRequests(ctx context.Context) | |
| return fmt.Errorf("repair XChat message-request room state") | ||
| } | ||
| } | ||
| if !wasMessageRequest { | ||
| if wasMessageRequest == messageRequest { | ||
| continue | ||
| } | ||
| if err = portal.Save(ctx); err != nil { | ||
|
|
@@ -102,6 +197,9 @@ func shouldEmitChatInfoUpdate(chatInfo *bridgev2.ChatInfo, portalRoomType databa | |
| if chatInfo == nil { | ||
| return false | ||
| } | ||
| if chatInfo.MessageRequest != nil || chatInfo.ExtraUpdates != nil { | ||
| return true | ||
| } | ||
|
|
||
| // DM room title/avatar are member-derived, so always emit ChatInfoChange | ||
| // for existing DM rooms to refresh stale member profile info. | ||
|
|
@@ -242,9 +340,12 @@ func (tc *TwitterClient) xchatItemToConversation(ctx context.Context, item *resp | |
|
|
||
| conv := &types.Conversation{ | ||
| ConversationID: detail.ConversationID, | ||
| Trusted: true, // XChat conversations are always trusted | ||
| Trusted: true, | ||
| Muted: detail.IsMuted, | ||
| } | ||
| if trusted := xchatInboxItemTrust(item); trusted != nil { | ||
| conv.Trusted = *trusted | ||
| } | ||
|
|
||
| // Determine conversation type based on conversation ID and metadata. | ||
| if strings.HasPrefix(detail.ConversationID, "g") || detail.GroupMetadata != nil || | ||
|
|
@@ -384,11 +485,7 @@ func (tc *TwitterClient) xchatItemToChatInfo(ctx context.Context, item *response | |
| } | ||
| } | ||
|
|
||
| // MessageRequest is true for untrusted conversations (message requests) | ||
| var messageRequest *bool | ||
| if conv != nil { | ||
| messageRequest = ptr.Ptr(!conv.Trusted) | ||
| } | ||
| trusted := xchatInboxItemTrust(item) | ||
| membersAreFull := len(detail.GroupMembersResults) > 0 && len(memberMap) > 0 | ||
| for _, memberResult := range detail.GroupMembersResults { | ||
| if memberID, _ := xchatUserFromResult(memberResult); memberID == "" { | ||
|
|
@@ -418,8 +515,10 @@ func (tc *TwitterClient) xchatItemToChatInfo(ctx context.Context, item *response | |
| TotalMemberCount: len(memberMap), | ||
| MemberMap: memberMap, | ||
| }, | ||
| CanBackfill: true, | ||
| MessageRequest: messageRequest, | ||
| CanBackfill: true, | ||
| } | ||
| if trusted != nil { | ||
| applyXChatTrustToChatInfo(info, *trusted) | ||
| } | ||
|
|
||
| if isGroup { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A blind recover looks sketchy to me, what's going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was meant to treat trust values that fail to decode as unknown instead of crashing the sync
I can replace it with a helper that converts the panic into a normal error