本文整理汇总了Golang中euphoria/io/heim/proto/snowflake.Snowflake.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Snowflake.String方法的具体用法?Golang Snowflake.String怎么用?Golang Snowflake.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类euphoria/io/heim/proto/snowflake.Snowflake
的用法示例。
在下文中一共展示了Snowflake.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetMessage
func (rb *RoomBinding) GetMessage(ctx scope.Context, id snowflake.Snowflake) (*proto.Message, error) {
var msg Message
nDays, err := rb.DbMap.SelectInt("SELECT retention_days FROM room WHERE name = $1", rb.Name)
if err != nil {
return nil, err
}
err = rb.DbMap.SelectOne(
&msg,
"SELECT room, id, previous_edit_id, parent, posted, edited, deleted,"+
" session_id, sender_id, sender_name, server_id, server_era, content, encryption_key_id"+
" FROM message WHERE room = $1 AND id = $2",
rb.Name, id.String())
if err != nil {
if err == sql.ErrNoRows {
return nil, proto.ErrMessageNotFound
}
return nil, err
}
if nDays > 0 {
threshold := time.Now().Add(time.Duration(-nDays) * 24 * time.Hour)
if msg.Posted.Before(threshold) {
return nil, proto.ErrMessageNotFound
}
}
m := msg.ToBackend()
return &m, nil
}
示例2: SetUserKey
func (b *AccountManagerBinding) SetUserKey(
ctx scope.Context, accountID snowflake.Snowflake, key *security.ManagedKey) error {
if !key.Encrypted() {
return security.ErrKeyMustBeEncrypted
}
res, err := b.DbMap.Exec(
"UPDATE account SET encrypted_user_key = $2 WHERE id = $1", accountID.String(), key.Ciphertext)
if err != nil {
if err == sql.ErrNoRows {
return proto.ErrAccountNotFound
}
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return proto.ErrAccountNotFound
}
return nil
}
示例3: getOTP
func (b *AccountManagerBinding) getOTP(db gorp.SqlExecutor, kms security.KMS, accountID snowflake.Snowflake) (*proto.OTP, error) {
encryptedOTP, err := b.getRawOTP(db, accountID)
if err != nil {
return nil, err
}
key := security.ManagedKey{
KeyType: OTPKeyType,
IV: encryptedOTP.IV,
Ciphertext: encryptedOTP.EncryptedKey,
ContextKey: "account",
ContextValue: accountID.String(),
}
if err := kms.DecryptKey(&key); err != nil {
return nil, err
}
uriBytes, err := security.DecryptGCM(&key, encryptedOTP.IV, encryptedOTP.Digest, encryptedOTP.EncryptedURI, nil)
if err != nil {
return nil, err
}
otp := &proto.OTP{
URI: string(uriBytes),
Validated: encryptedOTP.Validated,
}
return otp, nil
}
示例4: RevokeStaff
func (b *AccountManagerBinding) RevokeStaff(ctx scope.Context, accountID snowflake.Snowflake) error {
_, err := b.DbMap.Exec(
"DELETE FROM capability USING account"+
" WHERE account.id = $1 AND capability.id = account.staff_capability_id",
accountID.String())
return err
}
示例5: GrantStaff
func (m *accountManager) GrantStaff(
ctx scope.Context, accountID snowflake.Snowflake, kmsCred security.KMSCredential) error {
m.b.Lock()
defer m.b.Unlock()
account, ok := m.b.accounts[accountID.String()]
if !ok {
return proto.ErrAccountNotFound
}
memAcc := account.(*memAccount)
kms := kmsCred.KMS()
key := memAcc.sec.SystemKey.Clone()
if err := kms.DecryptKey(&key); err != nil {
return err
}
nonce, err := kms.GenerateNonce(key.KeyType.BlockSize())
if err != nil {
return err
}
capability, err := security.GrantSharedSecretCapability(&key, nonce, kmsCred.KMSType(), kmsCred)
if err != nil {
return err
}
memAcc.staffCapability = capability
return nil
}
示例6: NewMessage
func NewMessage(
roomName string, sessionView proto.SessionView, id, parent snowflake.Snowflake, keyID, content string) (
*Message, error) {
msg := &Message{
Room: roomName,
ID: id.String(),
Parent: parent.String(),
Posted: id.Time(),
Content: content,
SessionID: sessionView.SessionID,
SenderID: string(sessionView.ID),
SenderName: sessionView.Name,
ServerID: sessionView.ServerID,
ServerEra: sessionView.ServerEra,
SenderClientAddress: sessionView.ClientAddress,
SenderIsManager: sessionView.IsManager,
SenderIsStaff: sessionView.IsStaff,
}
if keyID != "" {
msg.EncryptionKeyID = sql.NullString{
String: keyID,
Valid: true,
}
}
return msg, nil
}
示例7: Room
func (t *PMTracker) Room(ctx scope.Context, kms security.KMS, pmID snowflake.Snowflake, client *proto.Client) (proto.Room, *security.ManagedKey, error) {
row, err := t.Backend.Get(PM{}, pmID.String())
if row == nil || err != nil {
if row == nil || err == sql.ErrNoRows {
return nil, nil, proto.ErrPMNotFound
}
}
pm := row.(*PM).ToBackend()
pmKey, modified, otherName, err := pm.Access(ctx, t.Backend, kms, client)
if err != nil {
return nil, nil, err
}
if modified {
_, err := t.Backend.DbMap.Exec(
"UPDATE pm SET receiver = $2, receiver_mac = $3, encrypted_receiver_key = $4 WHERE id = $1",
pm.ID.String(), string(pm.Receiver), pm.ReceiverMAC, pm.EncryptedReceiverKey.Ciphertext)
if err != nil {
return nil, nil, err
}
}
room := &PMRoomBinding{
RoomBinding: RoomBinding{
RoomName: fmt.Sprintf("pm:%s", pm.ID),
RoomTitle: fmt.Sprintf("%s (private chat)", otherName),
Backend: t.Backend,
},
pm: pm,
}
return room, pmKey, nil
}
示例8: NewMessage
func NewMessage(
room *Room, sessionView *proto.SessionView, id, parent snowflake.Snowflake, keyID, content string) (
*Message, error) {
msg := &Message{
Room: room.Name,
ID: id.String(),
Parent: parent.String(),
Posted: id.Time(),
Content: content,
}
if sessionView != nil {
msg.SessionID = sessionView.SessionID
msg.SenderID = string(sessionView.ID)
msg.SenderName = sessionView.Name
msg.ServerID = sessionView.ServerID
msg.ServerEra = sessionView.ServerEra
msg.SenderIsManager = sessionView.IsManager
msg.SenderIsStaff = sessionView.IsStaff
}
if keyID != "" {
msg.EncryptionKeyID = sql.NullString{
String: keyID,
Valid: true,
}
}
return msg, nil
}
示例9: latest
func (b *Backend) latest(ctx scope.Context, room *Room, n int, before snowflake.Snowflake) (
[]proto.Message, error) {
if n <= 0 {
return nil, nil
}
// TODO: define constant
if n > 1000 {
n = 1000
}
var query string
args := []interface{}{room.Name, n}
// Get the time before which messages will be expired
nDays, err := b.DbMap.SelectInt("SELECT retention_days FROM room WHERE name = $1", room.Name)
if err != nil {
return nil, err
}
if nDays == 0 {
if before.IsZero() {
query = ("SELECT room, id, previous_edit_id, parent, posted, edited, deleted," +
" session_id, sender_id, sender_name, server_id, server_era, content, encryption_key_id" +
" FROM message WHERE room = $1 AND deleted IS NULL ORDER BY id DESC LIMIT $2")
} else {
query = ("SELECT room, id, previous_edit_id, parent, posted, edited, deleted," +
" session_id, sender_id, sender_name, server_id, server_era, content, encryption_key_id" +
" FROM message WHERE room = $1 AND id < $3 AND deleted IS NULL ORDER BY id DESC LIMIT $2")
args = append(args, before.String())
}
} else {
threshold := time.Now().Add(time.Duration(-nDays) * 24 * time.Hour)
if before.IsZero() {
query = ("SELECT room, id, previous_edit_id, parent, posted, edited, deleted," +
" session_id, sender_id, sender_name, server_id, server_era, content, encryption_key_id" +
" FROM message WHERE room = $1 AND posted > $3 AND deleted IS NULL ORDER BY id DESC LIMIT $2")
} else {
query = ("SELECT room, id, previous_edit_id, parent, posted, edited, deleted," +
" session_id, sender_id, sender_name, server_id, server_era, content, encryption_key_id" +
" FROM message WHERE room = $1 AND id < $3 AND deleted IS NULL AND posted > $4 ORDER BY id DESC LIMIT $2")
args = append(args, before.String())
}
args = append(args, threshold)
}
msgs, err := b.DbMap.Select(Message{}, query, args...)
if err != nil {
return nil, err
}
results := make([]proto.Message, len(msgs))
for i, row := range msgs {
msg := row.(*Message)
results[len(msgs)-i-1] = msg.ToBackend()
}
return results, nil
}
示例10: ChangeClientKey
func (b *AccountManagerBinding) ChangeClientKey(
ctx scope.Context, accountID snowflake.Snowflake, oldKey, newKey *security.ManagedKey) error {
t, err := b.DbMap.Begin()
if err != nil {
return err
}
rollback := func() {
if err := t.Rollback(); err != nil {
backend.Logger(ctx).Printf("rollback error: %s", err)
}
}
var account Account
err = t.SelectOne(
&account,
"SELECT nonce, mac, encrypted_user_key, encrypted_private_key FROM account WHERE id = $1",
accountID.String())
if err != nil {
rollback()
if err == sql.ErrNoRows {
return proto.ErrAccountNotFound
}
return err
}
sec := account.Bind(b.Backend).accountSecurity()
if err := sec.ChangeClientKey(oldKey, newKey); err != nil {
rollback()
return err
}
res, err := t.Exec(
"UPDATE account SET mac = $2, encrypted_user_key = $3 WHERE id = $1",
accountID.String(), sec.MAC, sec.UserKey.Ciphertext)
if err != nil {
rollback()
return err
}
n, err := res.RowsAffected()
if err != nil {
rollback()
return err
}
if n == 0 {
rollback()
return proto.ErrAccountNotFound
}
if err := t.Commit(); err != nil {
return err
}
return nil
}
示例11: Get
func (m *accountManager) Get(ctx scope.Context, id snowflake.Snowflake) (proto.Account, error) {
m.b.Lock()
defer m.b.Unlock()
account, ok := m.b.accounts[id.String()]
if !ok {
return nil, proto.ErrAccountNotFound
}
return account, nil
}
示例12: getRawOTP
func (b *AccountManagerBinding) getRawOTP(db gorp.SqlExecutor, accountID snowflake.Snowflake) (*OTP, error) {
row, err := db.Get(OTP{}, accountID.String())
if row == nil || err != nil {
if row == nil || err == sql.ErrNoRows {
return nil, proto.ErrOTPNotEnrolled
}
return nil, err
}
return row.(*OTP), nil
}
示例13: latest
func (b *Backend) latest(ctx scope.Context, rb *RoomBinding, n int, before snowflake.Snowflake) (
[]proto.Message, error) {
if n <= 0 {
return nil, nil
}
// TODO: define constant
if n > 1000 {
n = 1000
}
var query string
args := []interface{}{rb.RoomName, n}
// Get the time before which messages will be expired
nDays, err := b.DbMap.SelectInt("SELECT retention_days FROM room WHERE name = $1", rb.RoomName)
if err != nil {
return nil, err
}
cols, err := allColumns(b.DbMap, Message{}, "")
if err != nil {
return nil, err
}
if nDays == 0 {
if before.IsZero() {
query = fmt.Sprintf("SELECT %s FROM message WHERE room = $1 AND deleted IS NULL ORDER BY id DESC LIMIT $2", cols)
} else {
query = fmt.Sprintf("SELECT %s FROM message WHERE room = $1 AND id < $3 AND deleted IS NULL ORDER BY id DESC LIMIT $2", cols)
args = append(args, before.String())
}
} else {
threshold := time.Now().Add(time.Duration(-nDays) * 24 * time.Hour)
if before.IsZero() {
query = fmt.Sprintf("SELECT %s FROM message WHERE room = $1 AND posted > $3 AND deleted IS NULL ORDER BY id DESC LIMIT $2", cols)
} else {
query = fmt.Sprintf(
"SELECT %s FROM message WHERE room = $1 AND id < $3 AND deleted IS NULL AND posted > $4 ORDER BY id DESC LIMIT $2", cols)
args = append(args, before.String())
}
args = append(args, threshold)
}
msgs, err := b.DbMap.Select(Message{}, query, args...)
if err != nil {
return nil, err
}
results := make([]proto.Message, len(msgs))
for i, row := range msgs {
msg := row.(*Message)
results[len(msgs)-i-1] = msg.ToTransmission()
}
return results, nil
}
示例14: getParentPostTime
func (rb *RoomBinding) getParentPostTime(id snowflake.Snowflake) (time.Time, error) {
var row struct {
Posted time.Time
}
err := rb.DbMap.SelectOne(&row,
"SELECT posted FROM message WHERE room = $1 AND id = $2",
rb.RoomName, id.String())
if err != nil {
return time.Time{}, err
}
return row.Posted, nil
}
示例15: RevokeStaff
func (m *accountManager) RevokeStaff(ctx scope.Context, accountID snowflake.Snowflake) error {
m.b.Lock()
defer m.b.Unlock()
account, ok := m.b.accounts[accountID.String()]
if !ok {
return proto.ErrAccountNotFound
}
memAcc := account.(*memAccount)
memAcc.staffCapability = nil
return nil
}