Refactor flag & mention repos
This commit is contained in:
@@ -2,11 +2,9 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
@@ -37,7 +35,7 @@ func Mention(ctx context.Context, db *factory.DB) MentionRepository {
|
||||
return (&mention{}).With(ctx, db)
|
||||
}
|
||||
|
||||
func (r *mention) With(ctx context.Context, db *factory.DB) MentionRepository {
|
||||
func (r mention) With(ctx context.Context, db *factory.DB) MentionRepository {
|
||||
return &mention{
|
||||
repository: r.repository.With(ctx, db),
|
||||
}
|
||||
@@ -47,40 +45,45 @@ func (r mention) table() string {
|
||||
return "messaging_mention"
|
||||
}
|
||||
|
||||
func (r *mention) FindByUserIDs(IDs ...uint64) (types.MentionSet, error) {
|
||||
return r.findByIDs("rel_user", IDs...)
|
||||
}
|
||||
|
||||
func (r *mention) FindByMessageIDs(IDs ...uint64) (types.MentionSet, error) {
|
||||
return r.findByIDs("rel_message", IDs...)
|
||||
}
|
||||
|
||||
func (r *mention) findByIDs(col string, IDs ...uint64) (mm types.MentionSet, err error) {
|
||||
mm = types.MentionSet{}
|
||||
|
||||
if len(IDs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s IN (?)`, r.table(), col)
|
||||
|
||||
if sql, args, err := sqlx.In(sql, IDs); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return mm, r.db().Select(&mm, sql, args...)
|
||||
func (r mention) columns() []string {
|
||||
return []string{
|
||||
"mm.id",
|
||||
"mm.rel_message",
|
||||
"mm.rel_channel",
|
||||
"mm.rel_user",
|
||||
"mm.rel_mentioned_by",
|
||||
"mm.created_at",
|
||||
}
|
||||
}
|
||||
|
||||
func (r *mention) Create(m *types.Mention) (*types.Mention, error) {
|
||||
func (r mention) query() squirrel.SelectBuilder {
|
||||
return squirrel.
|
||||
Select(r.columns()...).
|
||||
From(r.table() + " AS mm")
|
||||
}
|
||||
|
||||
func (r mention) FindByUserIDs(IDs ...uint64) (types.MentionSet, error) {
|
||||
return r.findAllBy(squirrel.Eq{"rel_user": IDs})
|
||||
}
|
||||
|
||||
func (r mention) FindByMessageIDs(IDs ...uint64) (types.MentionSet, error) {
|
||||
return r.findAllBy(squirrel.Eq{"rel_message": IDs})
|
||||
}
|
||||
|
||||
func (r mention) findAllBy(cnd squirrel.Sqlizer) (mm types.MentionSet, err error) {
|
||||
return mm, rh.FetchAll(r.db(), r.query().Where(cnd), &mm)
|
||||
}
|
||||
|
||||
func (r mention) Create(m *types.Mention) (*types.Mention, error) {
|
||||
m.ID = factory.Sonyflake.NextID()
|
||||
m.CreatedAt = time.Now()
|
||||
return m, r.db().Insert(r.table(), m)
|
||||
}
|
||||
|
||||
func (r *mention) DeleteByMessageID(ID uint64) error {
|
||||
func (r mention) DeleteByMessageID(ID uint64) error {
|
||||
return rh.Delete(r.db(), r.table(), squirrel.Eq{"rel_message": ID})
|
||||
}
|
||||
|
||||
func (r *mention) DeleteByID(ID uint64) error {
|
||||
func (r mention) DeleteByID(ID uint64) error {
|
||||
return rh.Delete(r.db(), r.table(), squirrel.Eq{"id": ID})
|
||||
}
|
||||
|
||||
@@ -56,17 +56,17 @@ func (r messageFlag) query() squirrel.SelectBuilder {
|
||||
From(r.table() + " AS mf")
|
||||
}
|
||||
|
||||
func (r *messageFlag) With(ctx context.Context, db *factory.DB) MessageFlagRepository {
|
||||
func (r messageFlag) With(ctx context.Context, db *factory.DB) MessageFlagRepository {
|
||||
return &messageFlag{
|
||||
repository: r.repository.With(ctx, db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *messageFlag) FindByID(ID uint64) (*types.MessageFlag, error) {
|
||||
func (r messageFlag) FindByID(ID uint64) (*types.MessageFlag, error) {
|
||||
return r.findOneBy(squirrel.Eq{"id": ID})
|
||||
}
|
||||
|
||||
func (r *messageFlag) FindByFlag(messageID, userID uint64, flag string) (*types.MessageFlag, error) {
|
||||
func (r messageFlag) FindByFlag(messageID, userID uint64, flag string) (*types.MessageFlag, error) {
|
||||
cnd := squirrel.Eq{
|
||||
"rel_message": messageID,
|
||||
"flag": flag,
|
||||
@@ -99,7 +99,7 @@ func (r messageFlag) findOneBy(cnd squirrel.Sqlizer) (*types.MessageFlag, error)
|
||||
}
|
||||
|
||||
// FindByMessageIDs returns all flags by message id range
|
||||
func (r *messageFlag) FindByMessageIDs(IDs ...uint64) (set types.MessageFlagSet, err error) {
|
||||
func (r messageFlag) FindByMessageIDs(IDs ...uint64) (set types.MessageFlagSet, err error) {
|
||||
if len(IDs) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -107,12 +107,12 @@ func (r *messageFlag) FindByMessageIDs(IDs ...uint64) (set types.MessageFlagSet,
|
||||
return set, rh.FetchAll(r.db(), r.query().Where(squirrel.Eq{"rel_message": IDs}), &set)
|
||||
}
|
||||
|
||||
func (r *messageFlag) Create(mod *types.MessageFlag) (*types.MessageFlag, error) {
|
||||
func (r messageFlag) Create(mod *types.MessageFlag) (*types.MessageFlag, error) {
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now()
|
||||
return mod, r.db().Insert(r.table(), mod)
|
||||
}
|
||||
|
||||
func (r *messageFlag) DeleteByID(ID uint64) error {
|
||||
func (r messageFlag) DeleteByID(ID uint64) error {
|
||||
return rh.Delete(r.db(), r.table(), squirrel.Eq{"id": ID})
|
||||
}
|
||||
|
||||
@@ -580,14 +580,16 @@ func (svc message) flag(messageID uint64, flag string, remove bool) error {
|
||||
}
|
||||
|
||||
f, err = svc.mflag.FindByFlag(messageID, flagOwnerId, flag)
|
||||
if f.ID == 0 && remove {
|
||||
if f == nil && remove {
|
||||
// Skip removing, flag does not exists
|
||||
return nil
|
||||
}
|
||||
if f.ID > 0 && !remove {
|
||||
|
||||
if f != nil && f.ID > 0 && !remove {
|
||||
// Skip adding, flag already exists
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil && err != repository.ErrMessageFlagNotFound {
|
||||
// Other errors, exit
|
||||
return
|
||||
@@ -595,13 +597,17 @@ func (svc message) flag(messageID uint64, flag string, remove bool) error {
|
||||
|
||||
if msg, err = svc.message.FindByID(messageID); err != nil {
|
||||
return
|
||||
} else if ch, err = svc.findChannelByID(msg.ChannelID); err != nil {
|
||||
}
|
||||
|
||||
if ch, err = svc.findChannelByID(msg.ChannelID); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !svc.ac.CanReadChannel(svc.ctx, ch) {
|
||||
return ErrNoPermissions.withStack()
|
||||
}
|
||||
if f.IsReaction() && !svc.ac.CanReactMessage(svc.ctx, ch) {
|
||||
|
||||
if f != nil && f.IsReaction() && !svc.ac.CanReactMessage(svc.ctx, ch) {
|
||||
return ErrNoPermissions.withStack()
|
||||
}
|
||||
|
||||
|
||||
@@ -23,14 +23,14 @@ const (
|
||||
MessageFlagBookmarkedMessage string = "bookmark"
|
||||
)
|
||||
|
||||
func (f *MessageFlag) IsReaction() bool {
|
||||
func (f MessageFlag) IsReaction() bool {
|
||||
return f.Flag != MessageFlagPinnedToChannel && f.Flag != MessageFlagBookmarkedMessage
|
||||
}
|
||||
|
||||
func (f *MessageFlag) IsPin() bool {
|
||||
func (f MessageFlag) IsPin() bool {
|
||||
return f.Flag == MessageFlagPinnedToChannel
|
||||
}
|
||||
|
||||
func (f *MessageFlag) IsBookmark() bool {
|
||||
func (f MessageFlag) IsBookmark() bool {
|
||||
return f.Flag == MessageFlagBookmarkedMessage
|
||||
}
|
||||
|
||||
@@ -36,6 +36,5 @@ func (h helper) repoMsgExistingLoad(ID uint64) *types.Message {
|
||||
func (h helper) repoMsgFlagLoad(ID uint64) types.MessageFlagSet {
|
||||
ff, err := h.repoMessageFlag().FindByMessageIDs(ID)
|
||||
h.a.NoError(err)
|
||||
h.a.NotNil(ff)
|
||||
return ff
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user