Repository navigation
Add Reddit user blocking and block-list synchronization #4
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
Fizzadar
wants to merge
2
commits into
main
Choose a base branch
from
codex/reddit-user-blocking
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "maunium.net/go/mautrix/bridgev2" | ||
| "maunium.net/go/mautrix/bridgev2/networkid" | ||
| "maunium.net/go/mautrix/bridgev2/simplevent" | ||
| ) | ||
|
|
||
| var _ bridgev2.UserBlockingNetworkAPI = (*RedditClient)(nil) | ||
|
|
||
| func (r *RedditClient) HandleMatrixBlockUser(ctx context.Context, msg *bridgev2.MatrixBlockUser) error { | ||
| if msg.Content.ReportSpam { | ||
| return errors.New("spam reporting is not supported") | ||
| } | ||
| if r.session == nil { | ||
| return errors.New("blocking requires a saved Reddit session; sign in again") | ||
| } | ||
| userID := msg.Portal.OtherUserID | ||
| if userID == "" || userID == r.userID { | ||
| return errors.New("could not identify the other Reddit user in this DM") | ||
| } | ||
| // Serialize native snapshots with mutations so an older response cannot | ||
| // overwrite the new state in the account's in-memory block list. | ||
| r.blockNetMu.Lock() | ||
| defer r.blockNetMu.Unlock() | ||
| ctx, cancel := context.WithTimeout(ctx, 15*time.Second) | ||
| defer cancel() | ||
| if err := r.session.SetUserBlocked(ctx, string(userID), msg.Content.Block); err != nil { | ||
| return err | ||
| } | ||
| r.blockMu.Lock() | ||
| if r.blockedUsers != nil { | ||
| r.blockedUsers[userID] = msg.Content.Block | ||
| } | ||
| r.blockMu.Unlock() | ||
| // bridgev2 updates and persists this portal only after this call succeeds. | ||
| return nil | ||
| } | ||
|
|
||
| func (r *RedditClient) userBlocked(userID networkid.UserID) *bool { | ||
| r.blockMu.Lock() | ||
| defer r.blockMu.Unlock() | ||
| if r.blockedUsers == nil || userID == "" { | ||
| return nil | ||
| } | ||
| blocked := r.blockedUsers[userID] | ||
| return &blocked | ||
| } | ||
|
|
||
| func (r *RedditClient) refreshBlockedUsers(ctx context.Context) ([]string, error) { | ||
| r.blockNetMu.Lock() | ||
| defer r.blockNetMu.Unlock() | ||
| ctx, cancel := context.WithTimeout(ctx, 15*time.Second) | ||
| defer cancel() | ||
| users, err := r.session.GetBlockedUsers(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| blocked := make(map[networkid.UserID]bool, len(users)) | ||
| for _, user := range users { | ||
| blocked[networkid.UserID(user)] = true | ||
| } | ||
| r.blockMu.Lock() | ||
| r.blockedUsers = blocked | ||
| r.blockMu.Unlock() | ||
| return users, nil | ||
| } | ||
|
|
||
| // Reddit's block list is outside Matrix /sync. Poll it on the existing sync | ||
| // loop and reconcile persisted DM flags, including unblocks missed offline. | ||
| func (r *RedditClient) syncBlockedUsers(ctx context.Context) error { | ||
| users, err := r.refreshBlockedUsers(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| portals := r.main.Bridge.DB.Portal | ||
| keys, err := portals.GetAllBlockedDMsOf(ctx, r.userLogin.ID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| targets := make(map[networkid.PortalKey]networkid.UserID) | ||
| for _, key := range keys { | ||
| portal, err := portals.GetByKey(ctx, key) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if portal != nil { | ||
| targets[key] = portal.OtherUserID | ||
| } | ||
| } | ||
| for _, user := range users { | ||
| dms, err := portals.GetAllDMsWith(ctx, networkid.UserID(user)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, dm := range dms { | ||
| if dm.Receiver == r.userLogin.ID { | ||
| targets[dm.PortalKey] = dm.OtherUserID | ||
| } | ||
| } | ||
| } | ||
| for key, userID := range targets { | ||
| r.userLogin.QueueRemoteEvent(&blockListChange{ | ||
| EventMeta: simplevent.EventMeta{Type: bridgev2.RemoteEventChatInfoChange, PortalKey: key}, | ||
| client: r, | ||
| userID: userID, | ||
| }) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type blockListChange struct { | ||
| simplevent.EventMeta | ||
| client *RedditClient | ||
| userID networkid.UserID | ||
| } | ||
|
|
||
| var _ bridgev2.RemoteChatInfoChange = (*blockListChange)(nil) | ||
|
|
||
| func (evt *blockListChange) GetChatInfoChange(context.Context) (*bridgev2.ChatInfoChange, error) { | ||
| // Read at handling time: a Beeper block/unblock may have completed while | ||
| // this native snapshot was waiting in the portal's event queue. | ||
| return &bridgev2.ChatInfoChange{ChatInfo: &bridgev2.ChatInfo{ | ||
| UserBlocked: evt.client.userBlocked(evt.userID), | ||
| }}, nil | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.