-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: Automatically skip and unsub inactive users #78
Draft
jdkaplan
wants to merge
3
commits into
main
Choose a base branch
from
jdkaplan-auto-unsub-inactive
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.
Draft
Changes from all commits
Commits
Show all changes
3 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 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
@@ -13,9 +14,14 @@ import ( | |
|
||
const owner string = `@_**Maren Beam (SP2'19)**` | ||
const oddOneOutMessage string = "OK this is awkward.\nThere were an odd number of people in the match-set today, which means that one person couldn't get paired. Unfortunately, it was you -- I'm really sorry :(\nI promise it's not personal, it was very much random. Hopefully this doesn't happen again too soon. Enjoy your day! <3" | ||
const matchedMessage = "Hi you two! You've been matched for pairing :)\n\nHave fun!" | ||
const matchedMessage = "Hi you two! You've been matched for pairing :)\n\nHave fun!\n\nNote: In an effort to reduce the frequency of no-show partners, I'll soon start automatically unsubscribing users that I haven't heard from in a while. Please message me back so I know you're an active user (and messages in this chat count!) :heart:" | ||
const offboardedMessage = "Hi! You've been unsubscribed from Pairing Bot.\n\nThis happens at the end of every batch, and everyone is offboarded even if they're still in batch. If you'd like to re-subscribe, just send me a message that says `subscribe`.\n\nBe well! :)" | ||
const introMessage = "Hi! I'm Pairing Bot (she/her)!\n\nSend me a PM that says `subscribe` to get started :smiley:\n\n:pear::robot:\n:octopus::octopus:" | ||
const autoUnsubscribeMessage = ("Hi! I've noticed that it's been a few pairings since I last heard from you. To make sure I only pair active users together, I've automatically unsubscribed you from pairing.\n\nIt's okay though! Send me another `subscribe` message to join back in whenever you like (even now!) :heart:\n\nIf you *are* active and I unsubscribed you anyway, I'm sorry! Please re-subscribe! And then request a non-automated apology from the maintainers :robot:") | ||
|
||
// MAX_OPEN_PAIRINGS is the threshold for considering a user to be "inactive". | ||
// After this many unanswered pairings, the user is automatically unsubscribed. | ||
const MAX_OPEN_PAIRINGS = 3 | ||
|
||
var maintenanceMode = false | ||
|
||
|
@@ -66,6 +72,11 @@ func (pl *PairingLogic) handle(w http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
// Reset the open pairings count on user activity. | ||
if err := pl.resetPairingCount(ctx, hook.Message); err != nil { | ||
log.Printf("Could not reset openPairings count for user (%d): %s", hook.Message.SenderID, err) | ||
} | ||
|
||
// Don't respond to commands sent in pair-making group DMs. We can | ||
// distinguish these by checking whether there are exactly two participants | ||
// (Pairing Bot + 1). | ||
|
@@ -156,13 +167,16 @@ func (pl *PairingLogic) match(w http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
// Remove anyone who has been inactive for too many pairings. | ||
active, inactive := groupByActivity(recursersList) | ||
|
||
// message the peeps! | ||
|
||
// if there's an odd number today, message the last person in the list | ||
// and tell them they don't get a match today, then knock them off the list | ||
if len(recursersList)%2 != 0 { | ||
recurser := recursersList[len(recursersList)-1] | ||
recursersList = recursersList[:len(recursersList)-1] | ||
if len(active)%2 != 0 { | ||
recurser := active[len(active)-1] | ||
active = active[:len(active)-1] | ||
log.Printf("%s was the odd-one-out today", recurser.name) | ||
|
||
err := pl.zulip.SendUserMessage(ctx, []int64{recurser.id}, oddOneOutMessage) | ||
|
@@ -171,19 +185,26 @@ func (pl *PairingLogic) match(w http.ResponseWriter, r *http.Request) { | |
} | ||
} | ||
|
||
for i := 0; i < len(recursersList); i += 2 { | ||
rc1 := recursersList[i] | ||
rc2 := recursersList[i+1] | ||
for i := 0; i < len(active); i += 2 { | ||
rc1 := &active[i] | ||
rc2 := &active[i+1] | ||
ids := []int64{rc1.id, rc2.id} | ||
|
||
err := pl.zulip.SendUserMessage(ctx, ids, matchedMessage) | ||
if err != nil { | ||
log.Printf("Error when trying to send matchedMessage to %s and %s: %s\n", rc1.name, rc2.name, err) | ||
continue | ||
} | ||
log.Println(rc1.name, "was", "matched", "with", rc2.name) | ||
|
||
for _, rc := range []*Recurser{rc1, rc2} { | ||
if err := pl.incrementPairingCount(ctx, rc); err != nil { | ||
log.Printf("Error incrementing openPairings for user (%d): %s", rc1.id, err) | ||
} | ||
} | ||
} | ||
|
||
numRecursersPairedUp := len(recursersList) | ||
numRecursersPairedUp := len(active) | ||
|
||
log.Printf("Pairing Bot paired up %d recursers today", numRecursersPairedUp) | ||
|
||
|
@@ -193,6 +214,18 @@ func (pl *PairingLogic) match(w http.ResponseWriter, r *http.Request) { | |
if err := pl.pdb.SetNumPairings(ctx, int(timestamp), numPairings); err != nil { | ||
log.Printf("Failed to record today's pairings: %s", err) | ||
} | ||
|
||
for _, r := range inactive { | ||
if err := pl.rdb.Delete(ctx, r.id); err != nil { | ||
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. Maybe: send the message, and then only delete if we successfully sent the message? I'd hate to see someone "silently" unsubscribed if there's a bug here. |
||
log.Printf("Could not unsubscribe user (%d): %s", r.id, err) | ||
} | ||
|
||
if err := pl.zulip.SendUserMessage(ctx, []int64{r.id}, autoUnsubscribeMessage); err != nil { | ||
log.Printf("Could not send auto-unsubscribe message to user (%d): %s", r.id, err) | ||
} | ||
} | ||
|
||
log.Printf("Pairing Bot auto-unsubscribed %d inactive recursers today", len(inactive)) | ||
} | ||
|
||
// Unsubscribe people from Pairing Bot when their batch is over. They're always welcome to re-subscribe manually! | ||
|
@@ -364,3 +397,33 @@ func getWelcomeMessage() string { | |
|
||
return fmt.Sprintf(message, todayFormatted) | ||
} | ||
|
||
func (pl *PairingLogic) resetPairingCount(ctx context.Context, msg zulip.Message) error { | ||
rec, err := pl.rdb.GetByUserID(ctx, msg.SenderID, msg.SenderEmail, msg.SenderFullName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if rec.isSubscribed { | ||
rec.openPairings = 0 | ||
return pl.rdb.Set(ctx, rec.id, rec) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (pl *PairingLogic) incrementPairingCount(ctx context.Context, rc *Recurser) error { | ||
rc.openPairings++ | ||
return pl.rdb.Set(ctx, rc.id, *rc) | ||
} | ||
|
||
func groupByActivity(recursers []Recurser) (active, inactive []Recurser) { | ||
for _, r := range recursers { | ||
if r.openPairings < MAX_OPEN_PAIRINGS { | ||
active = append(active, r) | ||
} else { | ||
inactive = append(inactive, r) | ||
} | ||
} | ||
return active, inactive | ||
} |
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.
Maybe: "... in a while. Please reply (or message me separately) so I know you're an active user! ❤️ Thank you!"