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
|
|
|
|
2019-10-18 00:35:13 +00:00
|
|
|
var UserBlocks BlockStore
|
|
|
|
|
|
|
|
//var UserFriends FriendStore
|
|
|
|
|
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-18 00:35:13 +00:00
|
|
|
Remove(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-18 00:35:13 +00:00
|
|
|
remove *sql.Stmt
|
2019-10-13 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultBlockStore(acc *qgen.Accumulator) (*DefaultBlockStore, error) {
|
2019-10-19 10:33:59 +00:00
|
|
|
ub := "users_blocks"
|
2019-10-13 09:07:05 +00:00
|
|
|
return &DefaultBlockStore{
|
2019-10-19 10:33:59 +00:00
|
|
|
isBlocked: acc.Select(ub).Cols("blocker").Where("blocker = ? AND blockedUser = ?").Prepare(),
|
|
|
|
add: acc.Insert(ub).Columns("blocker,blockedUser").Fields("?,?").Prepare(),
|
|
|
|
remove: acc.Delete(ub).Where("blocker = ? AND blockedUser = ?").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)
|
2019-10-19 10:33:59 +00:00
|
|
|
if err == ErrNoRows {
|
|
|
|
return false, nil
|
2019-10-13 09:07:05 +00:00
|
|
|
}
|
2019-10-19 10:33:59 +00:00
|
|
|
return err == nil, err
|
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
|
|
|
}
|
|
|
|
|
2019-10-18 00:35:13 +00:00
|
|
|
func (s *DefaultBlockStore) Remove(blocker, blockee int) error {
|
|
|
|
_, err := s.remove.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{
|
2019-10-18 00:35:13 +00:00
|
|
|
addInvite: acc.Insert("users_friends_invites").Columns("requester,target").Fields("?,?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
confirm: acc.Insert("users_friends").Columns("uid,uid2").Fields("?,?").Prepare(),
|
2019-10-18 00:35:13 +00:00
|
|
|
getOwnSentInvites: acc.Select("users_friends_invites").Cols("requester,target").Where("requester = ?").Prepare(),
|
|
|
|
getOwnRecvInvites: acc.Select("users_friends_invites").Cols("requester,target").Where("target = ?").Prepare(),
|
2019-10-13 10:36:50 +00:00
|
|
|
}, 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
|
|
|
}
|