Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import (
// "saturday": false,
// "sunday": false,
// },
// "currentlyAtRC": false,
// "currentlyAtRC": false,
// "openPairings": 0,
// }

type Recurser struct {
Expand All @@ -41,8 +42,16 @@ type Recurser struct {
schedule map[string]interface{}

// isSubscribed really means "did they already have an entry in the database"
isSubscribed bool
isSubscribed bool

currentlyAtRC bool

// openPairings is the number of pairings we've made since the user's last
// chat interaction.
//
// The count increments each time we send a pairing message. It resets to
// zero any time we get a chat message from the user.
openPairings int64
}

func (r *Recurser) ConvertToMap() map[string]interface{} {
Expand All @@ -53,6 +62,7 @@ func (r *Recurser) ConvertToMap() map[string]interface{} {
"isSkippingTomorrow": r.isSkippingTomorrow,
"schedule": r.schedule,
"currentlyAtRC": r.currentlyAtRC,
"openPairings": r.openPairings,
}
}

Expand All @@ -63,6 +73,15 @@ func parseDoc(doc *firestore.DocumentSnapshot) (Recurser, error) {
}
m := doc.Data()

var openPairings int64
if v, ok := m["openPairings"]; ok {
if i, ok := v.(int64); ok {
openPairings = i
} else {
return Recurser{}, fmt.Errorf("invalid openPairings (int64): %#+v", v)
}
}

// isSubscribed is missing here because it's not in the map
return Recurser{
id: id,
Expand All @@ -73,6 +92,7 @@ func parseDoc(doc *firestore.DocumentSnapshot) (Recurser, error) {
isSkippingTomorrow: m["isSkippingTomorrow"].(bool),
schedule: m["schedule"].(map[string]interface{}),
currentlyAtRC: m["currentlyAtRC"].(bool),
openPairings: openPairings,
}, nil
}

Expand Down
79 changes: 71 additions & 8 deletions pairing_bot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -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:"
Copy link
Collaborator

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!"

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

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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!
Expand Down Expand Up @@ -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
}
Loading