Add option for autojoining threads when opened

This commit is contained in:
Tulir Asokan
2022-10-28 23:35:31 +03:00
parent 6be531685f
commit f268ddd132
13 changed files with 219 additions and 70 deletions

View File

@@ -1,6 +1,10 @@
package main
import (
"sync"
"time"
"github.com/bwmarrin/discordgo"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-discord/database"
@@ -9,6 +13,8 @@ import (
type Thread struct {
*database.Thread
Parent *Portal
creationNoticeLock sync.Mutex
}
func (br *DiscordBridge) GetThreadByID(id string, root *database.Message) *Thread {
@@ -31,6 +37,19 @@ func (br *DiscordBridge) GetThreadByRootMXID(mxid id.EventID) *Thread {
return thread
}
func (br *DiscordBridge) GetThreadByRootOrCreationNoticeMXID(mxid id.EventID) *Thread {
br.threadsLock.Lock()
defer br.threadsLock.Unlock()
thread, ok := br.threadsByRootMXID[mxid]
if !ok {
thread, ok = br.threadsByCreationNoticeMXID[mxid]
if !ok {
return br.loadThread(br.DB.Thread.GetByMatrixRootOrCreationNoticeMsg(mxid), "", nil)
}
}
return thread
}
func (br *DiscordBridge) loadThread(dbThread *database.Thread, id string, root *database.Message) *Thread {
if dbThread == nil {
if root == nil {
@@ -49,5 +68,25 @@ func (br *DiscordBridge) loadThread(dbThread *database.Thread, id string, root *
thread.Parent = br.GetExistingPortalByID(database.NewPortalKey(thread.ParentID, ""))
br.threadsByID[thread.ID] = thread
br.threadsByRootMXID[thread.RootMXID] = thread
if thread.CreationNoticeMXID != "" {
br.threadsByCreationNoticeMXID[thread.CreationNoticeMXID] = thread
}
return thread
}
func (thread *Thread) Join(user *User) {
if user.IsInPortal(thread.ID) {
return
}
user.log.Debugfln("Joining thread %s@%s", thread.ID, thread.ParentID)
err := user.Session.ThreadJoinWithLocation(thread.ID, discordgo.ThreadJoinLocationContextMenu)
if err != nil {
user.log.Errorfln("Error joining thread %s@%s: %v", thread.ID, thread.ParentID, err)
} else {
user.MarkInPortal(database.UserPortal{
DiscordID: thread.ID,
Type: database.UserPortalTypeThread,
Timestamp: time.Now(),
})
}
}