2019-10-13 08:50:55 +00:00
|
|
|
package common
|
|
|
|
|
2019-10-13 09:07:05 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2019-10-13 08:50:55 +00:00
|
|
|
|
|
|
|
type BlockStore interface {
|
2019-10-13 10:36:50 +00:00
|
|
|
IsBlockedBy(blocker, blockee int) (bool, error)
|
|
|
|
Add(blocker, blockee int) error
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultBlockStore struct {
|
2019-10-13 09:07:05 +00:00
|
|
|
isBlocked *sql.Stmt
|
2019-10-13 10:36:50 +00:00
|
|
|
add *sql.Stmt
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultBlockStore(acc *qgen.Accumulator) (*DefaultBlockStore, error) {
|
2019-10-13 09:07:05 +00:00
|
|
|
return &DefaultBlockStore{
|
|
|
|
isBlocked: acc.Select("users_blocks").Cols("blocker").Where("blocker = ? AND blockedUser = ?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
add: acc.Insert("users_blocks").Columns("blocker,blockedUser").Fields("?,?").Prepare(),
|
2019-10-13 09:07:05 +00:00
|
|
|
}, acc.FirstError()
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultBlockStore) IsBlockedBy(blocker, blockee int) (bool, error) {
|
2019-10-13 09:07:05 +00:00
|
|
|
err := s.isBlocked.QueryRow(blocker, blockee).Scan(&blocker)
|
|
|
|
if err != nil && err != ErrNoRows {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return err != ErrNoRows, nil
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultBlockStore) Add(blocker, blockee int) error {
|
2019-10-13 10:50:15 +00:00
|
|
|
_, err := s.add.Exec(blocker, blockee)
|
|
|
|
return err
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FriendInvite struct {
|
|
|
|
Requester int
|
|
|
|
Target int
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:50:55 +00:00
|
|
|
type FriendStore interface {
|
2019-10-13 10:36:50 +00:00
|
|
|
AddInvite(requester, target int) error
|
|
|
|
Confirm(requester, target int) error
|
|
|
|
GetOwSentInvites(uid int) ([]FriendInvite, error)
|
|
|
|
GetOwnRecvInvites(uid int) ([]FriendInvite, error)
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultFriendStore struct {
|
2019-10-13 10:36:50 +00:00
|
|
|
addInvite *sql.Stmt
|
|
|
|
confirm *sql.Stmt
|
|
|
|
getOwnSentInvites *sql.Stmt
|
|
|
|
getOwnRecvInvites *sql.Stmt
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultFriendStore(acc *qgen.Accumulator) (*DefaultFriendStore, error) {
|
2019-10-13 10:36:50 +00:00
|
|
|
return &DefaultFriendStore{
|
|
|
|
addInvite: acc.Insert("users_friends_invites").Columns("requester, target").Fields("?,?").Prepare(),
|
|
|
|
confirm: acc.Insert("users_friends").Columns("uid,uid2").Fields("?,?").Prepare(),
|
|
|
|
getOwnSentInvites: acc.Select("users_friends_invites").Cols("requester, target").Where("requester = ?").Prepare(),
|
|
|
|
getOwnRecvInvites: acc.Select("users_friends_invites").Cols("requester, target").Where("target = ?").Prepare(),
|
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultFriendStore) AddInvite(requester, target int) error {
|
2019-10-13 10:50:15 +00:00
|
|
|
_, err := s.addInvite.Exec(requester, target)
|
|
|
|
return err
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
2019-10-13 10:50:15 +00:00
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultFriendStore) Confirm(requester, target int) error {
|
2019-10-13 10:50:15 +00:00
|
|
|
_, err := s.confirm.Exec(requester, target)
|
|
|
|
return err
|
2019-10-13 10:36:50 +00:00
|
|
|
}
|
2019-10-13 10:50:15 +00:00
|
|
|
|
2019-10-13 10:36:50 +00:00
|
|
|
func (s *DefaultFriendStore) GetOwnSentInvites(uid int) ([]FriendInvite, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (s *DefaultFriendStore) GetOwnRecvInvites(uid int) ([]FriendInvite, error) {
|
|
|
|
return nil, nil
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|