This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/threema/account.go

67 lines
1.4 KiB
Go
Raw Permalink Normal View History

2019-05-31 14:36:18 +02:00
package threema
2019-05-31 18:26:15 +02:00
import (
"errors"
2019-05-31 19:39:29 +02:00
2019-05-31 18:26:15 +02:00
"github.com/o3ma/o3"
2019-05-31 14:36:18 +02:00
2019-05-31 18:26:15 +02:00
"dev.sum7.eu/genofire/golang-lib/database"
2019-08-08 15:48:43 +02:00
"dev.sum7.eu/sum7/thrempp/models"
2019-05-31 18:26:15 +02:00
)
2019-05-31 14:36:18 +02:00
2019-05-31 18:26:15 +02:00
type Account struct {
models.AccountThreema
threema *Threema
2019-06-01 01:43:48 +02:00
Session o3.SessionContext
send chan<- o3.Message
receive <-chan o3.ReceivedMsg
2019-06-01 01:43:48 +02:00
deliveredMSG map[uint64]string
2019-06-01 02:32:34 +02:00
readedMSG map[uint64]string
2019-05-31 14:36:18 +02:00
}
2019-06-01 15:21:49 +02:00
func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
2019-05-31 18:26:15 +02:00
if a, ok := t.accountJID[jid.String()]; ok {
2019-06-01 15:21:49 +02:00
return a, nil
2019-05-31 18:26:15 +02:00
}
account := models.AccountThreema{}
if database.Read == nil {
return nil, errors.New("no database connection")
}
2019-05-31 18:26:15 +02:00
database.Read.Where("xmpp_id = (?)",
database.Read.Table(jid.TableName()).Select("id").Where(map[string]interface{}{
"local": jid.Local,
"domain": jid.Domain,
}).QueryExpr()).First(&account)
var lsk [32]byte
copy(lsk[:], account.LSK[:])
tid, err := o3.NewThreemaID(string(account.TID), lsk, o3.AddressBook{})
if err != nil {
2019-06-01 15:21:49 +02:00
return nil, err
2019-05-31 14:36:18 +02:00
}
2019-05-31 18:26:15 +02:00
tid.Nick = o3.NewPubNick("xmpp:" + jid.String())
a := &Account{
AccountThreema: account,
Session: o3.NewSessionContext(tid),
threema: t,
}
a.send, a.receive, err = a.Session.Run()
2019-05-31 18:26:15 +02:00
if err != nil {
2019-06-01 15:21:49 +02:00
return nil, err
2019-05-31 18:26:15 +02:00
}
2019-06-01 15:21:49 +02:00
a.XMPP = *jid
a.deliveredMSG = make(map[uint64]string)
a.readedMSG = make(map[uint64]string)
go a.receiver(t.out)
2019-05-31 19:39:29 +02:00
2019-05-31 18:26:15 +02:00
t.accountJID[jid.String()] = a
2019-06-01 15:21:49 +02:00
return a, nil
2019-05-31 18:26:15 +02:00
}