Add option for autojoining threads when opened
This commit is contained in:
39
thread.go
39
thread.go
@@ -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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user