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

68 lines
1.4 KiB
Go
Raw 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-09-06 03:08:58 +02:00
ThreemaID *o3.ThreemaID
2019-06-01 01:43:48 +02:00
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,
2019-09-06 03:08:58 +02:00
ThreemaID: &tid,
threema: t,
}
2019-09-06 03:08:58 +02:00
session := o3.NewSessionContext(tid)
a.send, a.receive, err = 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
}