2022-10-21 14:18:02 +00:00
|
|
|
// mautrix-groupme - A Matrix-GroupMe puppeting bridge.
|
|
|
|
// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra
|
2018-08-13 20:24:44 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
2018-08-16 12:59:18 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-13 05:53:35 +00:00
|
|
|
"context"
|
2020-09-24 12:25:36 +00:00
|
|
|
"errors"
|
2019-05-15 20:04:09 +00:00
|
|
|
"fmt"
|
2020-08-22 10:07:55 +00:00
|
|
|
"net/http"
|
2023-09-04 00:26:53 +00:00
|
|
|
"sort"
|
2022-10-21 19:02:33 +00:00
|
|
|
"strings"
|
2019-05-22 20:05:58 +00:00
|
|
|
"sync"
|
2018-08-16 12:59:18 +00:00
|
|
|
"time"
|
2018-08-24 16:46:14 +00:00
|
|
|
|
2019-01-11 19:17:31 +00:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2020-08-22 10:07:55 +00:00
|
|
|
"maunium.net/go/mautrix"
|
2022-10-21 19:02:33 +00:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
|
|
|
"maunium.net/go/mautrix/bridge"
|
|
|
|
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
2020-05-08 19:32:22 +00:00
|
|
|
"maunium.net/go/mautrix/event"
|
2019-08-10 12:24:53 +00:00
|
|
|
"maunium.net/go/mautrix/format"
|
2020-05-08 19:32:22 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2019-08-10 12:24:53 +00:00
|
|
|
|
2023-09-19 03:15:46 +00:00
|
|
|
"gitea.watsonlabs.net/watsonb8/groupme-lib"
|
2022-10-21 14:48:37 +00:00
|
|
|
|
2022-10-21 14:48:03 +00:00
|
|
|
"github.com/beeper/groupme/database"
|
2022-10-21 19:02:33 +00:00
|
|
|
"github.com/beeper/groupme/groupmeext"
|
2018-08-16 12:59:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
*database.User
|
2021-02-13 05:53:35 +00:00
|
|
|
Conn *groupme.PushSubscription
|
2018-08-16 12:59:18 +00:00
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
bridge *GMBridge
|
2018-08-16 16:20:07 +00:00
|
|
|
log log.Logger
|
2018-08-16 12:59:18 +00:00
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
Admin bool
|
|
|
|
Whitelisted bool
|
|
|
|
PermissionLevel bridgeconfig.PermissionLevel
|
2019-11-10 19:22:11 +00:00
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
BridgeState *bridge.BridgeStateQueue
|
2019-05-17 20:53:57 +00:00
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
Client *groupmeext.Client
|
2019-05-17 20:53:57 +00:00
|
|
|
ConnectionErrors int
|
2023-09-15 03:35:20 +00:00
|
|
|
SpaceId string
|
2019-05-22 20:05:58 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
ChatList map[groupme.ID]*groupme.Chat
|
|
|
|
GroupList map[groupme.ID]*groupme.Group
|
|
|
|
RelationList map[groupme.ID]*groupme.User
|
2021-02-28 19:59:03 +00:00
|
|
|
|
2020-06-25 13:44:51 +00:00
|
|
|
cleanDisconnection bool
|
|
|
|
batteryWarningsSent int
|
2020-09-17 15:48:37 +00:00
|
|
|
lastReconnection int64
|
2019-07-04 12:08:58 +00:00
|
|
|
|
2019-08-30 17:57:08 +00:00
|
|
|
chatListReceived chan struct{}
|
|
|
|
syncPortalsDone chan struct{}
|
2019-08-24 21:25:29 +00:00
|
|
|
|
2020-11-06 13:52:16 +00:00
|
|
|
messageInput chan PortalMessage
|
|
|
|
messageOutput chan PortalMessage
|
2020-07-23 17:10:21 +00:00
|
|
|
|
2020-05-27 09:16:05 +00:00
|
|
|
mgmtCreateLock sync.Mutex
|
2022-10-21 19:02:33 +00:00
|
|
|
|
|
|
|
spaceCreateLock sync.Mutex
|
|
|
|
spaceMembershipChecked bool
|
2023-09-06 01:10:24 +00:00
|
|
|
|
|
|
|
syncStart chan struct{}
|
|
|
|
syncWait sync.WaitGroup
|
2018-08-28 21:40:54 +00:00
|
|
|
}
|
2018-08-26 14:08:37 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
type Chat struct {
|
|
|
|
Portal *Portal
|
|
|
|
LastMessageTime uint64
|
|
|
|
Group *groupme.Group
|
|
|
|
DM *groupme.Chat
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
type ChatList []Chat
|
2022-10-21 19:02:33 +00:00
|
|
|
|
2023-09-15 03:35:20 +00:00
|
|
|
var connectWaitGroup = sync.WaitGroup{}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (cl ChatList) Len() int {
|
|
|
|
return len(cl)
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (cl ChatList) Less(i, j int) bool {
|
|
|
|
return cl[i].LastMessageTime > cl[j].LastMessageTime
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (cl ChatList) Swap(i, j int) {
|
|
|
|
cl[i], cl[j] = cl[j], cl[i]
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Public Properties
|
2022-10-21 19:02:33 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetPermissionLevel() bridgeconfig.PermissionLevel {
|
|
|
|
return user.PermissionLevel
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetManagementRoomID() id.RoomID {
|
|
|
|
return user.ManagementRoom
|
2020-05-21 16:49:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetMXID() id.UserID {
|
|
|
|
return user.MXID
|
2020-05-21 16:49:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetGMID() groupme.ID {
|
2023-09-19 03:15:46 +00:00
|
|
|
if user.GMID == "" {
|
2023-09-20 21:06:04 +00:00
|
|
|
u, err := user.Client.MyUser(context.TODO(), user.Token)
|
2023-09-07 19:01:41 +00:00
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to get own GroupMe ID:", err)
|
|
|
|
return ""
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
user.GMID = u.ID
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return user.GMID
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetCommandState() map[string]interface{} {
|
|
|
|
return nil
|
2019-05-28 18:31:25 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetIDoublePuppet() bridge.DoublePuppet {
|
|
|
|
p := user.bridge.GetPuppetByCustomMXID(user.MXID)
|
|
|
|
if p == nil || p.CustomIntent() == nil {
|
|
|
|
return nil
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return p
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) GetIGhost() bridge.Ghost {
|
2023-09-19 03:15:46 +00:00
|
|
|
if user.GMID == "" {
|
2023-09-07 19:01:41 +00:00
|
|
|
return nil
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
p := user.bridge.GetPuppetByGMID(user.GMID)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return p
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) GetSpaceRoom() id.RoomID {
|
2023-09-15 03:35:20 +00:00
|
|
|
return user.getSpaceRoom(&user.SpaceRoom, "GroupMe", "Your GroupMe bridged chats", "")
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:16:05 +00:00
|
|
|
func (user *User) GetManagementRoom() id.RoomID {
|
|
|
|
if len(user.ManagementRoom) == 0 {
|
|
|
|
user.mgmtCreateLock.Lock()
|
|
|
|
defer user.mgmtCreateLock.Unlock()
|
|
|
|
if len(user.ManagementRoom) > 0 {
|
|
|
|
return user.ManagementRoom
|
|
|
|
}
|
2022-10-21 19:02:33 +00:00
|
|
|
creationContent := make(map[string]interface{})
|
|
|
|
if !user.bridge.Config.Bridge.FederateRooms {
|
|
|
|
creationContent["m.federate"] = false
|
|
|
|
}
|
2020-05-27 09:16:05 +00:00
|
|
|
resp, err := user.bridge.Bot.CreateRoom(&mautrix.ReqCreateRoom{
|
2022-10-21 19:02:33 +00:00
|
|
|
Topic: "GroupMe bridge notices",
|
|
|
|
IsDirect: true,
|
|
|
|
CreationContent: creationContent,
|
2020-05-27 09:16:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to auto-create management room:", err)
|
|
|
|
} else {
|
|
|
|
user.SetManagementRoom(resp.RoomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return user.ManagementRoom
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:32:22 +00:00
|
|
|
func (user *User) SetManagementRoom(roomID id.RoomID) {
|
2018-08-18 19:57:08 +00:00
|
|
|
existingUser, ok := user.bridge.managementRooms[roomID]
|
|
|
|
if ok {
|
|
|
|
existingUser.ManagementRoom = ""
|
|
|
|
existingUser.Update()
|
|
|
|
}
|
2023-09-04 23:43:32 +00:00
|
|
|
//
|
2018-08-18 19:57:08 +00:00
|
|
|
user.ManagementRoom = roomID
|
|
|
|
user.bridge.managementRooms[user.ManagementRoom] = user
|
|
|
|
user.Update()
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) HasSession() bool {
|
|
|
|
return len(user.Token) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) IsConnected() bool {
|
|
|
|
// TODO: better connection check
|
|
|
|
return user.Conn != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) IsLoggedIn() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) IsLoginInProgress() bool {
|
|
|
|
// return user.Conn != nil && user.Conn.IsLoginInProgress()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) ShouldCallSynchronously() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) GetPortalByGMID(gmid groupme.ID) *Portal {
|
|
|
|
return user.bridge.GetPortalByGMID(user.PortalKey(gmid))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public Methods
|
|
|
|
|
|
|
|
func (user *User) Login(token string) error {
|
|
|
|
user.Token = token
|
2023-09-19 03:15:46 +00:00
|
|
|
user.Update()
|
2023-09-07 19:01:41 +00:00
|
|
|
|
|
|
|
user.addToGMIDMap()
|
2023-09-20 21:06:04 +00:00
|
|
|
if user.Connect(user.Client, user.Conn) {
|
2023-09-15 21:49:25 +00:00
|
|
|
user.PostLogin()
|
2023-09-07 19:01:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("failed to connect")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) PostLogin() {
|
|
|
|
user.bridge.Metrics.TrackConnectionState(user.GMID, true)
|
|
|
|
user.bridge.Metrics.TrackLoginState(user.GMID, true)
|
|
|
|
user.bridge.Metrics.TrackBufferLength(user.MXID, 0)
|
|
|
|
user.log.Debugln("Locking processing of incoming messages and starting post-login sync")
|
|
|
|
user.syncWait.Add(1)
|
|
|
|
user.syncStart <- struct{}{}
|
|
|
|
go user.intPostLogin()
|
|
|
|
}
|
|
|
|
|
2023-09-20 21:06:04 +00:00
|
|
|
func (user *User) Connect(gmClient *groupmeext.Client, pushSub *groupme.PushSubscription) bool {
|
2023-09-15 03:35:20 +00:00
|
|
|
user.SpaceId = "GroupMe"
|
2023-09-20 21:06:04 +00:00
|
|
|
user.Client = gmClient
|
2018-08-18 19:57:08 +00:00
|
|
|
if user.Conn != nil {
|
|
|
|
return true
|
2021-02-21 05:58:50 +00:00
|
|
|
} else if len(user.Token) == 0 {
|
2018-08-18 19:57:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-02-22 03:46:17 +00:00
|
|
|
|
2023-03-20 00:31:16 +00:00
|
|
|
user.log.Debugfln("Connecting to GroupMe")
|
2022-10-21 19:02:33 +00:00
|
|
|
timeout := time.Duration(user.bridge.Config.GroupMe.ConnectionTimeout)
|
2019-05-16 15:08:30 +00:00
|
|
|
if timeout == 0 {
|
|
|
|
timeout = 20
|
|
|
|
}
|
2023-09-04 23:43:32 +00:00
|
|
|
|
2023-09-20 21:06:04 +00:00
|
|
|
user.Conn = pushSub
|
|
|
|
|
2023-09-19 03:15:46 +00:00
|
|
|
if user.GMID == "" {
|
2023-09-20 21:06:04 +00:00
|
|
|
myuser, err := user.Client.MyUser(context.TODO(), user.Token)
|
2023-09-15 21:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err) //TODO
|
|
|
|
}
|
|
|
|
user.GMID = myuser.ID
|
|
|
|
}
|
2023-09-04 23:43:32 +00:00
|
|
|
|
2023-09-21 16:58:36 +00:00
|
|
|
user.Conn.AddFullHandler(user, user.Token)
|
2021-02-13 05:53:35 +00:00
|
|
|
|
|
|
|
//TODO: typing notification?
|
2019-05-16 15:24:54 +00:00
|
|
|
return user.RestoreSession()
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 19:57:08 +00:00
|
|
|
func (user *User) RestoreSession() bool {
|
2021-02-13 05:53:35 +00:00
|
|
|
if len(user.Token) > 0 {
|
2023-09-21 16:58:36 +00:00
|
|
|
|
|
|
|
err := user.Conn.SubscribeToUser(context.TODO(), user.GMID, user.Token)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-09-20 21:06:04 +00:00
|
|
|
|
2021-02-21 05:58:50 +00:00
|
|
|
//TODO: typing notifics
|
2019-05-17 20:53:57 +00:00
|
|
|
user.ConnectionErrors = 0
|
2021-02-13 05:53:35 +00:00
|
|
|
//user.SetSession(&sess)
|
2018-08-18 19:57:08 +00:00
|
|
|
user.log.Debugln("Session restored successfully")
|
2019-05-30 14:48:22 +00:00
|
|
|
user.PostLogin()
|
2023-09-20 21:06:04 +00:00
|
|
|
|
2021-02-21 05:58:50 +00:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
user.log.Debugln("tried login but no token")
|
|
|
|
return false
|
2018-08-16 12:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) UpdateDirectChats(chats map[id.UserID][]id.RoomID) {
|
|
|
|
if !user.bridge.Config.Bridge.SyncDirectChatList {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
puppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
|
|
|
|
if puppet == nil || puppet.CustomIntent() == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
intent := puppet.CustomIntent()
|
|
|
|
method := http.MethodPatch
|
|
|
|
if chats == nil {
|
|
|
|
chats = user.getDirectChats()
|
|
|
|
method = http.MethodPut
|
|
|
|
}
|
|
|
|
user.log.Debugln("Updating m.direct list on homeserver")
|
|
|
|
var err error
|
|
|
|
if user.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareAsmux {
|
|
|
|
urlPath := intent.BuildClientURL("unstable", "com.beeper.asmux", "dms")
|
|
|
|
_, err = intent.MakeFullRequest(mautrix.FullRequest{
|
|
|
|
Method: method,
|
|
|
|
URL: urlPath,
|
|
|
|
Headers: http.Header{"X-Asmux-Auth": {user.bridge.AS.Registration.AppToken}},
|
|
|
|
RequestJSON: chats,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
existingChats := make(map[id.UserID][]id.RoomID)
|
|
|
|
err = intent.GetAccountData(event.AccountDataDirectChats.Type, &existingChats)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Failed to get m.direct list to update it:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for userID, rooms := range existingChats {
|
|
|
|
if _, ok := user.bridge.ParsePuppetMXID(userID); !ok {
|
|
|
|
// This is not a ghost user, include it in the new list
|
|
|
|
chats[userID] = rooms
|
|
|
|
} else if _, ok := chats[userID]; !ok && method == http.MethodPatch {
|
|
|
|
// This is a ghost user, but we're not replacing the whole list, so include it too
|
|
|
|
chats[userID] = rooms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = intent.SetAccountData(event.AccountDataDirectChats.Type, &chats)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Failed to update m.direct list:", err)
|
|
|
|
}
|
2019-08-24 19:39:12 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) PortalKey(gmid groupme.ID) database.PortalKey {
|
|
|
|
return database.NewPortalKey(gmid, user.GMID)
|
2018-08-28 21:40:54 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Handlers
|
|
|
|
|
|
|
|
func (user *User) HandleTextMessage(message groupme.Message) {
|
|
|
|
id := database.ParsePortalKey(message.GroupID.String())
|
|
|
|
|
|
|
|
if id == nil {
|
2023-09-20 21:06:04 +00:00
|
|
|
if user.GMID == message.UserID {
|
|
|
|
id = database.ParsePortalKey(message.UserID.String() + "+" + message.RecipientID.String())
|
|
|
|
} else {
|
|
|
|
id = database.ParsePortalKey(message.RecipientID.String() + "+" + message.UserID.String())
|
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
|
|
|
if id == nil {
|
|
|
|
user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.messageInput <- PortalMessage{*id, user, &message, uint64(message.CreatedAt.ToTime().Unix())}
|
2021-02-13 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) HandleLike(msg groupme.Message) {
|
|
|
|
user.HandleTextMessage(msg)
|
2021-02-13 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) HandleJoin(id groupme.ID) {
|
|
|
|
user.HandleChatList()
|
|
|
|
//TODO: efficient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleGroupName(group groupme.ID, newName string) {
|
|
|
|
p := user.GetPortalByGMID(group)
|
|
|
|
if p != nil {
|
|
|
|
p.UpdateName(newName, "", false)
|
|
|
|
//get more info abt actual user TODO
|
2021-02-13 05:53:35 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
//bugs atm with above?
|
|
|
|
user.HandleChatList()
|
|
|
|
|
2019-08-30 19:04:57 +00:00
|
|
|
}
|
2022-10-21 14:35:03 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) HandleGroupTopic(_ groupme.ID, _ string) {
|
|
|
|
user.HandleChatList()
|
|
|
|
}
|
2021-02-13 05:53:35 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) HandleGroupMembership(_ groupme.ID, _ string) {
|
|
|
|
user.HandleChatList()
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleGroupAvatar(_ groupme.ID, _ string) {
|
|
|
|
user.HandleChatList()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleLikeIcon(_ groupme.ID, _, _ int, _ string) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleNewNickname(groupID, userID groupme.ID, name string) {
|
|
|
|
puppet := user.bridge.GetPuppetByGMID(userID)
|
|
|
|
if puppet != nil {
|
|
|
|
puppet.UpdateName(groupme.Member{
|
|
|
|
Nickname: name,
|
|
|
|
UserID: userID,
|
|
|
|
}, false)
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
2019-05-22 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) HandleNewAvatarInGroup(groupID, userID groupme.ID, url string) {
|
|
|
|
puppet := user.bridge.GetPuppetByGMID(userID)
|
|
|
|
puppet.UpdateAvatar(user, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleMembers(_ groupme.ID, _ []groupme.Member, _ bool) {
|
|
|
|
user.HandleChatList()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleError(err error) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleJSONParseError(err error) {
|
|
|
|
user.log.Errorln("GroupMe JSON parse error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleChatList() {
|
|
|
|
chatMap := map[groupme.ID]*groupme.Group{}
|
2023-09-20 21:06:04 +00:00
|
|
|
chats, err := user.Client.IndexAllGroups(user.Token)
|
2023-09-07 19:01:41 +00:00
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("chat sync error", err) //TODO: handle
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, chat := range chats {
|
|
|
|
chatMap[chat.ID] = chat
|
|
|
|
}
|
|
|
|
user.GroupList = chatMap
|
|
|
|
|
|
|
|
dmMap := map[groupme.ID]*groupme.Chat{}
|
2023-09-20 21:06:04 +00:00
|
|
|
dms, err := user.Client.IndexAllChats(user.Token)
|
2023-09-07 19:01:41 +00:00
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("chat sync error", err) //TODO: handle
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, dm := range dms {
|
2023-09-20 21:06:04 +00:00
|
|
|
dmMap[dm.LastMessage.ConversationID] = dm
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
|
|
|
user.ChatList = dmMap
|
|
|
|
|
2023-09-12 02:58:26 +00:00
|
|
|
userMap := map[groupme.ID]*groupme.User{}
|
2023-09-20 21:06:04 +00:00
|
|
|
users, err := user.Client.IndexAllRelations(user.Token)
|
2023-09-12 02:58:26 +00:00
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Error syncing user list, continuing sync", err)
|
|
|
|
}
|
2023-09-19 03:15:46 +00:00
|
|
|
|
2023-09-12 02:58:26 +00:00
|
|
|
for _, u := range users {
|
|
|
|
puppet := user.bridge.GetPuppetByGMID(u.ID)
|
|
|
|
// "" for overall user not related to one group
|
|
|
|
puppet.Sync(user, &groupme.Member{
|
|
|
|
UserID: u.ID,
|
|
|
|
Nickname: u.Name,
|
|
|
|
ImageURL: u.AvatarURL,
|
|
|
|
}, false, false)
|
|
|
|
userMap[u.ID] = u
|
|
|
|
}
|
2023-09-19 03:15:46 +00:00
|
|
|
|
|
|
|
for _, dm := range dms {
|
|
|
|
puppet := user.bridge.GetPuppetByGMID(dm.OtherUser.ID)
|
|
|
|
|
|
|
|
puppet.Sync(user, &groupme.Member{
|
|
|
|
UserID: dm.OtherUser.ID,
|
|
|
|
Nickname: dm.OtherUser.Name,
|
|
|
|
ImageURL: dm.OtherUser.AvatarURL,
|
|
|
|
}, false, false)
|
|
|
|
userMap[dm.OtherUser.ID] = &dm.OtherUser
|
|
|
|
}
|
2023-09-12 02:58:26 +00:00
|
|
|
user.RelationList = userMap
|
2023-09-07 19:01:41 +00:00
|
|
|
|
|
|
|
user.log.Infoln("Chat list received")
|
|
|
|
user.chatListReceived <- struct{}{}
|
|
|
|
go user.syncPortals(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private Methods
|
|
|
|
|
|
|
|
func (user *User) handleMessageLoop() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-user.messageOutput:
|
|
|
|
user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput))
|
|
|
|
puppet := user.bridge.GetPuppetByGMID(msg.data.UserID)
|
|
|
|
portal := user.bridge.GetPortalByGMID(msg.chat)
|
|
|
|
if puppet != nil {
|
|
|
|
puppet.Sync(nil, &groupme.Member{
|
|
|
|
UserID: msg.data.UserID,
|
|
|
|
Nickname: msg.data.Name,
|
|
|
|
ImageURL: msg.data.AvatarURL,
|
|
|
|
}, false, false)
|
|
|
|
}
|
|
|
|
portal.messages <- msg
|
|
|
|
case <-user.syncStart:
|
|
|
|
user.log.Debugln("Processing of incoming messages is locked")
|
|
|
|
user.bridge.Metrics.TrackSyncLock(user.GMID, true)
|
|
|
|
user.syncWait.Wait()
|
|
|
|
user.bridge.Metrics.TrackSyncLock(user.GMID, false)
|
|
|
|
user.log.Debugln("Processing of incoming messages unlocked")
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) addToGMIDMap() {
|
|
|
|
user.bridge.usersLock.Lock()
|
|
|
|
user.bridge.usersByGMID[user.GMID] = user
|
|
|
|
user.bridge.usersLock.Unlock()
|
2019-05-22 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) removeFromGMIDMap() {
|
|
|
|
user.bridge.usersLock.Lock()
|
|
|
|
jidUser, ok := user.bridge.usersByGMID[user.GMID]
|
|
|
|
if ok && user == jidUser {
|
|
|
|
delete(user.bridge.usersByGMID, user.GMID)
|
|
|
|
}
|
|
|
|
user.bridge.usersLock.Unlock()
|
|
|
|
user.bridge.Metrics.TrackLoginState(user.GMID, false)
|
2019-05-22 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect bool) (ok bool) {
|
|
|
|
extraContent := make(map[string]interface{})
|
|
|
|
if isDirect {
|
|
|
|
extraContent["is_direct"] = true
|
|
|
|
}
|
|
|
|
customPuppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
|
|
|
|
if customPuppet != nil && customPuppet.CustomIntent() != nil {
|
|
|
|
extraContent["fi.mau.will_auto_accept"] = true
|
|
|
|
}
|
|
|
|
_, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: user.MXID}, extraContent)
|
|
|
|
var httpErr mautrix.HTTPError
|
|
|
|
if err != nil && errors.As(err, &httpErr) && httpErr.RespError != nil && strings.Contains(httpErr.RespError.Err, "is already in the room") {
|
|
|
|
user.bridge.StateStore.SetMembership(roomID, user.MXID, event.MembershipJoin)
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
user.log.Warnfln("Failed to invite user to %s: %v", roomID, err)
|
|
|
|
} else {
|
|
|
|
ok = true
|
|
|
|
}
|
2019-05-22 13:46:18 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
if customPuppet != nil && customPuppet.CustomIntent() != nil {
|
|
|
|
err = customPuppet.CustomIntent().EnsureJoined(roomID, appservice.EnsureJoinedParams{IgnoreCache: true})
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to auto-join %s: %v", roomID, err)
|
|
|
|
ok = false
|
|
|
|
} else {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2019-05-30 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 18:17:03 +00:00
|
|
|
func (user *User) tryAutomaticDoublePuppeting() {
|
2022-10-21 22:04:21 +00:00
|
|
|
if !user.bridge.Config.CanAutoDoublePuppet(user.MXID) {
|
2019-12-30 18:21:04 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-06 00:29:14 +00:00
|
|
|
user.log.Debugln("Checking if double puppeting needs to be enabled")
|
2022-10-21 22:04:21 +00:00
|
|
|
puppet := user.bridge.GetPuppetByGMID(user.GMID)
|
2019-12-30 18:21:04 +00:00
|
|
|
if len(puppet.CustomMXID) > 0 {
|
2020-11-06 00:29:14 +00:00
|
|
|
user.log.Debugln("User already has double-puppeting enabled")
|
2019-12-30 18:21:04 +00:00
|
|
|
// Custom puppet already enabled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
accessToken, err := puppet.loginWithSharedSecret(user.MXID)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Failed to login with shared secret:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = puppet.SwitchCustomMXID(accessToken, user.MXID)
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warnln("Failed to switch to auto-logined custom puppet:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.log.Infoln("Successfully automatically enabled custom puppet")
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:05:42 +00:00
|
|
|
func (user *User) sendBridgeNotice(formatString string, args ...interface{}) {
|
|
|
|
notice := fmt.Sprintf(formatString, args...)
|
|
|
|
_, err := user.bridge.Bot.SendNotice(user.GetManagementRoom(), notice)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnf("Failed to send bridge notice \"%s\": %v", notice, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) sendMarkdownBridgeAlert(formatString string, args ...interface{}) {
|
|
|
|
notice := fmt.Sprintf(formatString, args...)
|
|
|
|
content := format.RenderMarkdown(notice, true, false)
|
|
|
|
_, err := user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, content)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnf("Failed to send bridge alert \"%s\": %v", notice, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 21:06:04 +00:00
|
|
|
//func (user *User) postConnPing() bool {
|
|
|
|
// user.log.Debugln("Making post-connection ping")
|
|
|
|
// if !user.Conn.Connected() {
|
|
|
|
// user.log.Errorfln("Post-connection ping failed: %v. Disconnecting and then reconnecting after a second")
|
|
|
|
// user.bridge.Metrics.TrackDisconnection(user.MXID)
|
|
|
|
// go func() {
|
|
|
|
// time.Sleep(1 * time.Second)
|
|
|
|
// user.Connect()
|
|
|
|
// }()
|
|
|
|
// return false
|
|
|
|
// } else {
|
|
|
|
// user.log.Debugln("Post-connection ping OK")
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
//}
|
2020-08-05 19:06:54 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
func (user *User) intPostLogin() {
|
2023-09-06 01:10:24 +00:00
|
|
|
defer user.syncWait.Done()
|
2023-09-04 00:26:53 +00:00
|
|
|
user.lastReconnection = time.Now().Unix()
|
2023-09-15 21:49:25 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
user.Update()
|
|
|
|
|
|
|
|
user.tryAutomaticDoublePuppeting()
|
|
|
|
|
|
|
|
user.log.Debugln("Waiting for chat list receive confirmation")
|
|
|
|
user.HandleChatList()
|
|
|
|
select {
|
|
|
|
case <-user.chatListReceived:
|
|
|
|
user.log.Debugln("Chat list receive confirmation received in PostLogin")
|
2023-09-15 03:35:20 +00:00
|
|
|
case <-time.After(time.Duration(10000 /**user.bridge.Config.Bridge.ChatListWait**/) * time.Second):
|
2023-09-04 00:26:53 +00:00
|
|
|
user.log.Warnln("Timed out waiting for chat list to arrive!")
|
2023-09-20 21:06:04 +00:00
|
|
|
//user.postConnPing()
|
2023-09-04 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.log.Debugln("Waiting for portal sync complete confirmation")
|
|
|
|
select {
|
|
|
|
case <-user.syncPortalsDone:
|
|
|
|
user.log.Debugln("Post-connection portal sync complete, unlocking processing of incoming messages.")
|
|
|
|
// TODO this is too short, maybe a per-portal duration?
|
|
|
|
case <-time.After(time.Duration(1000 /**user.bridge.Config.Bridge.PortalSyncWai**/) * time.Second):
|
|
|
|
user.log.Warnln("Timed out waiting for portal sync to complete! Unlocking processing of incoming messages.")
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 13:46:18 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
// Syncs chats & group messages
|
2021-04-28 02:49:01 +00:00
|
|
|
func (user *User) syncPortals(createAll bool) {
|
2023-09-04 00:26:53 +00:00
|
|
|
user.log.Infoln("Reading chat list")
|
2022-10-21 22:04:21 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
chats := make(ChatList, 0, len(user.GroupList)+len(user.ChatList))
|
|
|
|
portalKeys := make([]database.PortalKey, 0, cap(chats))
|
2022-10-21 22:04:21 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
for _, group := range user.GroupList {
|
2022-10-21 22:04:21 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
portal := user.bridge.GetPortalByGMID(database.GroupPortalKey(group.ID))
|
2022-10-21 22:04:21 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
chats = append(chats, Chat{
|
|
|
|
Portal: portal,
|
|
|
|
LastMessageTime: uint64(group.UpdatedAt.ToTime().Unix()),
|
2023-09-07 19:01:41 +00:00
|
|
|
Group: group,
|
2023-09-04 00:26:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, dm := range user.ChatList {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal := user.bridge.GetPortalByGMID(database.NewPortalKey(dm.OtherUser.ID, user.GMID))
|
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
chats = append(chats, Chat{
|
|
|
|
Portal: portal,
|
|
|
|
LastMessageTime: uint64(dm.UpdatedAt.ToTime().Unix()),
|
2023-09-07 19:01:41 +00:00
|
|
|
DM: dm,
|
2023-09-04 00:26:53 +00:00
|
|
|
})
|
|
|
|
}
|
2022-10-21 22:04:21 +00:00
|
|
|
|
2023-09-07 03:17:06 +00:00
|
|
|
//for _, chat := range chats {
|
2023-09-15 03:35:20 +00:00
|
|
|
// if user.IsInSpace(chat.Portal.Key) {
|
|
|
|
// user.MarkInSpace(chat.Portal.Key)
|
|
|
|
// if chat.Portal.IsPrivateChat() {
|
|
|
|
// user.addPortalToSpace(chat.Portal)
|
|
|
|
// }
|
2023-09-07 03:17:06 +00:00
|
|
|
// }
|
2023-09-15 03:35:20 +00:00
|
|
|
//
|
|
|
|
// portalKeys = append(portalKeys, chat.Portal.Key)
|
2023-09-07 03:17:06 +00:00
|
|
|
//}
|
2023-09-15 03:35:20 +00:00
|
|
|
user.log.Infoln("Read chat list, updating user-portal mapping")
|
2023-09-07 03:17:06 +00:00
|
|
|
|
|
|
|
err := user.SetPortalKeys(portalKeys)
|
2023-09-04 00:26:53 +00:00
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Failed to update user-portal mapping:", err)
|
|
|
|
}
|
|
|
|
sort.Sort(chats)
|
2023-09-07 03:17:06 +00:00
|
|
|
limit := 5 //user.bridge.Config.Bridge.HistorySync.MaxInitialConversations
|
2023-09-04 22:24:33 +00:00
|
|
|
if limit < 0 {
|
|
|
|
limit = len(chats)
|
|
|
|
}
|
2023-09-04 00:26:53 +00:00
|
|
|
user.log.Infoln("Syncing portals")
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i, chat := range chats {
|
2023-09-04 22:24:33 +00:00
|
|
|
|
2023-09-04 00:26:53 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(chat Chat, i int) {
|
2023-09-04 22:24:33 +00:00
|
|
|
create := (int64(chat.LastMessageTime) >= user.lastReconnection && user.lastReconnection > 0) || i < limit
|
2023-09-04 00:26:53 +00:00
|
|
|
if len(chat.Portal.MXID) > 0 || create || createAll {
|
2023-09-07 19:01:41 +00:00
|
|
|
if chat.Group != nil {
|
|
|
|
chat.Portal.SyncGroup(user, chat.Group)
|
|
|
|
} else {
|
|
|
|
chat.Portal.SyncDM(user, chat.DM)
|
|
|
|
}
|
2023-09-04 00:26:53 +00:00
|
|
|
//err := chat.Portal.BackfillHistory(user, chat.LastMessageTime)
|
|
|
|
if err != nil {
|
|
|
|
chat.Portal.log.Errorln("Error backfilling history:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(chat, i)
|
|
|
|
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
//TODO: handle leave from groupme side
|
|
|
|
user.UpdateDirectChats(nil)
|
|
|
|
user.log.Infoln("Finished syncing portals")
|
|
|
|
select {
|
|
|
|
case user.syncPortalsDone <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 03:35:20 +00:00
|
|
|
func (user *User) addPuppetToSpace(puppet *Puppet) bool {
|
|
|
|
bot := user.bridge.Bot
|
|
|
|
url := bot.BuildURL(mautrix.ClientURLPath{"groups", user.SpaceId, "admin", "users", "invite", puppet.MXID})
|
|
|
|
blankReqBody := map[string]interface{}{}
|
|
|
|
_, err := bot.MakeRequest(http.MethodPut, url, &blankReqBody, nil)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to invite %s to %s: %v", puppet.MXID, user.SpaceId, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
reqBody := map[string]map[string]string{
|
|
|
|
"m.visibility": {
|
|
|
|
"type": "private",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
url = bot.BuildURLWithQuery(mautrix.ClientURLPath{"groups", user.SpaceId, "self", "accept_invite"}, map[string]string{
|
|
|
|
"user_id": puppet.MXID.String(),
|
|
|
|
})
|
|
|
|
_, err = bot.MakeRequest(http.MethodPut, url, &reqBody, nil)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to join %s as %s: %v", user.SpaceId, puppet.MXID, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
user.log.Debugln("Added", puppet.MXID, "to", user.SpaceId)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) addPortalToSpace(portal *Portal) bool {
|
|
|
|
if portal.MXID == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, err := user.bridge.Bot.SendStateEvent(user.GetSpaceRoom(), event.StateSpaceChild, portal.MXID.String(), &event.SpaceChildEventContent{
|
|
|
|
Via: []string{user.bridge.AS.HomeserverDomain},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to add portal space. RoomId: ", portal.MXID.String())
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 10:07:55 +00:00
|
|
|
func (user *User) getDirectChats() map[id.UserID][]id.RoomID {
|
|
|
|
res := make(map[id.UserID][]id.RoomID)
|
2022-10-21 22:04:21 +00:00
|
|
|
privateChats := user.bridge.DB.Portal.FindPrivateChats(user.GMID)
|
2020-08-22 10:07:55 +00:00
|
|
|
for _, portal := range privateChats {
|
|
|
|
if len(portal.MXID) > 0 {
|
2022-10-21 19:02:33 +00:00
|
|
|
res[user.bridge.FormatPuppetMXID(portal.Key.GMID)] = []id.RoomID{portal.MXID}
|
2020-08-22 10:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-09-15 03:35:20 +00:00
|
|
|
func (user *User) getSpaceRoom(ptr *id.RoomID, name, topic string, parent id.RoomID) id.RoomID {
|
|
|
|
if len(*ptr) > 0 {
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
user.spaceCreateLock.Lock()
|
|
|
|
defer user.spaceCreateLock.Unlock()
|
|
|
|
if len(*ptr) > 0 {
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
initialState := []*event.Event{{
|
|
|
|
Type: event.StateRoomAvatar,
|
|
|
|
Content: event.Content{
|
|
|
|
Parsed: &event.RoomAvatarEventContent{
|
|
|
|
URL: user.bridge.Config.AppService.Bot.ParsedAvatar,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
if parent != "" {
|
|
|
|
parentIDStr := parent.String()
|
|
|
|
initialState = append(initialState, &event.Event{
|
|
|
|
Type: event.StateSpaceParent,
|
|
|
|
StateKey: &parentIDStr,
|
|
|
|
Content: event.Content{
|
|
|
|
Parsed: &event.SpaceParentEventContent{
|
|
|
|
Canonical: true,
|
|
|
|
Via: []string{user.bridge.AS.HomeserverDomain},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := user.bridge.Bot.CreateRoom(&mautrix.ReqCreateRoom{
|
|
|
|
Visibility: "private",
|
|
|
|
Name: name,
|
|
|
|
Topic: topic,
|
|
|
|
InitialState: initialState,
|
|
|
|
CreationContent: map[string]interface{}{
|
|
|
|
"type": event.RoomTypeSpace,
|
|
|
|
},
|
|
|
|
PowerLevelOverride: &event.PowerLevelsEventContent{
|
|
|
|
Users: map[id.UserID]int{
|
|
|
|
user.bridge.Bot.UserID: 9001,
|
|
|
|
user.MXID: 50,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to auto-create space room", err)
|
|
|
|
} else {
|
|
|
|
*ptr = resp.RoomID
|
|
|
|
user.Update()
|
|
|
|
user.ensureInvited(user.bridge.Bot, resp.RoomID, true)
|
|
|
|
|
|
|
|
//if parent != "" {
|
|
|
|
// _, err = user.bridge.Bot.SendStateEvent(parent, event.StateSpaceChild, resp.RoomID.String(), &event.SpaceChildEventContent{
|
|
|
|
// Via: []string{user.bridge.AS.HomeserverDomain},
|
|
|
|
// Order: " 0000",
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// user.log.Error().Err(err).
|
|
|
|
// Str("created_space_id", resp.RoomID.String()).
|
|
|
|
// Str("parent_space_id", parent.String()).
|
|
|
|
// Msg("Failed to add created space room to parent space")
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
|
2023-09-12 02:58:26 +00:00
|
|
|
func (user *User) updateAvatar(gmdi groupme.ID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool,
|
|
|
|
log log.Logger, intent *appservice.IntentAPI) bool {
|
2023-09-07 19:01:41 +00:00
|
|
|
return false
|
2018-08-28 21:40:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 13:52:16 +00:00
|
|
|
func (user *User) runMessageRingBuffer() {
|
|
|
|
for msg := range user.messageInput {
|
|
|
|
select {
|
|
|
|
case user.messageOutput <- msg:
|
2020-11-16 12:28:08 +00:00
|
|
|
user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput))
|
2020-11-06 13:52:16 +00:00
|
|
|
default:
|
2020-11-06 13:56:07 +00:00
|
|
|
dropped := <-user.messageOutput
|
|
|
|
user.log.Warnln("Buffer is full, dropping message in", dropped.chat)
|
2021-02-13 05:53:35 +00:00
|
|
|
user.messageOutput <- msg
|
2020-11-06 13:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|