Link shares to the user, not the collective

The user is required when searching because of folders (sadly), so the
share is connected to the user.
This commit is contained in:
eikek
2021-10-23 23:29:36 +02:00
parent 9009ebcb39
commit 2ac0b84e52
17 changed files with 268 additions and 110 deletions

View File

@ -1,6 +1,6 @@
CREATE TABLE "item_share" (
"id" varchar(254) not null primary key,
"cid" varchar(254) not null,
"user_id" varchar(254) not null,
"name" varchar(254),
"query" varchar(2000) not null,
"enabled" boolean not null,
@ -9,5 +9,5 @@ CREATE TABLE "item_share" (
"publish_until" timestamp not null,
"views" int not null,
"last_access" timestamp,
foreign key ("cid") references "collective"("cid") on delete cascade
foreign key ("user_id") references "user_"("uid") on delete cascade
)

View File

@ -1,6 +1,6 @@
CREATE TABLE `item_share` (
`id` varchar(254) not null primary key,
`cid` varchar(254) not null,
`user_id` varchar(254) not null,
`name` varchar(254),
`query` varchar(2000) not null,
`enabled` boolean not null,
@ -9,5 +9,5 @@ CREATE TABLE `item_share` (
`publish_until` timestamp not null,
`views` int not null,
`last_access` timestamp,
foreign key (`cid`) references `collective`(`cid`) on delete cascade
foreign key (`user_id`) references `user_`(`uid`) on delete cascade
)

View File

@ -1,6 +1,6 @@
CREATE TABLE "item_share" (
"id" varchar(254) not null primary key,
"cid" varchar(254) not null,
"user_id" varchar(254) not null,
"name" varchar(254),
"query" varchar(2000) not null,
"enabled" boolean not null,
@ -9,5 +9,5 @@ CREATE TABLE "item_share" (
"publish_until" timestamp not null,
"views" int not null,
"last_access" timestamp,
foreign key ("cid") references "collective"("cid") on delete cascade
foreign key ("user_id") references "user_"("uid") on delete cascade
)

View File

@ -18,7 +18,7 @@ import doobie.implicits._
final case class RShare(
id: Ident,
cid: Ident,
userId: Ident,
name: Option[String],
query: ItemQuery,
enabled: Boolean,
@ -35,7 +35,7 @@ object RShare {
val tableName = "item_share";
val id = Column[Ident]("id", this)
val cid = Column[Ident]("cid", this)
val userId = Column[Ident]("user_id", this)
val name = Column[String]("name", this)
val query = Column[ItemQuery]("query", this)
val enabled = Column[Boolean]("enabled", this)
@ -48,7 +48,7 @@ object RShare {
val all: NonEmptyList[Column[_]] =
NonEmptyList.of(
id,
cid,
userId,
name,
query,
enabled,
@ -67,7 +67,7 @@ object RShare {
DML.insert(
T,
T.all,
fr"${r.id},${r.cid},${r.name},${r.query},${r.enabled},${r.password},${r.publishAt},${r.publishUntil},${r.views},${r.lastAccess}"
fr"${r.id},${r.userId},${r.name},${r.query},${r.enabled},${r.password},${r.publishAt},${r.publishUntil},${r.views},${r.lastAccess}"
)
def incAccess(id: Ident): ConnectionIO[Int] =
@ -83,7 +83,7 @@ object RShare {
def updateData(r: RShare, removePassword: Boolean): ConnectionIO[Int] =
DML.update(
T,
T.id === r.id && T.cid === r.cid,
T.id === r.id && T.userId === r.userId,
DML.set(
T.name.setTo(r.name),
T.query.setTo(r.query),
@ -94,26 +94,41 @@ object RShare {
else Nil)
)
def findOne(id: Ident, cid: Ident): OptionT[ConnectionIO, RShare] =
def findOne(id: Ident, cid: Ident): OptionT[ConnectionIO, (RShare, RUser)] = {
val s = RShare.as("s")
val u = RUser.as("u")
OptionT(
Select(select(T.all), from(T), T.id === id && T.cid === cid).build
.query[RShare]
Select(
select(s.all, u.all),
from(s).innerJoin(u, u.uid === s.userId),
s.id === id && u.cid === cid
).build
.query[(RShare, RUser)]
.option
)
}
private def activeCondition(t: Table, id: Ident, current: Timestamp): Condition =
t.id === id && t.enabled === true && t.publishedUntil > current
def findActive(id: Ident, current: Timestamp): OptionT[ConnectionIO, RShare] =
def findActive(
id: Ident,
current: Timestamp
): OptionT[ConnectionIO, (RShare, RUser)] = {
val s = RShare.as("s")
val u = RUser.as("u")
OptionT(
Select(
select(T.all),
from(T),
activeCondition(T, id, current)
).build.query[RShare].option
select(s.all, u.all),
from(s).innerJoin(u, s.userId === u.uid),
activeCondition(s, id, current)
).build.query[(RShare, RUser)].option
)
}
def findCurrentActive(id: Ident): OptionT[ConnectionIO, RShare] =
def findCurrentActive(id: Ident): OptionT[ConnectionIO, (RShare, RUser)] =
OptionT.liftF(Timestamp.current[ConnectionIO]).flatMap(now => findActive(id, now))
def findActivePassword(id: Ident): OptionT[ConnectionIO, Option[Password]] =
@ -123,13 +138,30 @@ object RShare {
.option
})
def findAllByCollective(cid: Ident): ConnectionIO[List[RShare]] =
Select(select(T.all), from(T), T.cid === cid)
.orderBy(T.publishedAt.desc)
.build
.query[RShare]
.to[List]
def findAllByCollective(
cid: Ident,
ownerLogin: Option[Ident],
q: Option[String]
): ConnectionIO[List[(RShare, RUser)]] = {
val s = RShare.as("s")
val u = RUser.as("u")
def deleteByIdAndCid(id: Ident, cid: Ident): ConnectionIO[Int] =
DML.delete(T, T.id === id && T.cid === cid)
val ownerQ = ownerLogin.map(name => u.login === name)
val nameQ = q.map(n => s.name.like(s"%$n%"))
Select(
select(s.all, u.all),
from(s).innerJoin(u, u.uid === s.userId),
u.cid === cid &&? ownerQ &&? nameQ
)
.orderBy(s.publishedAt.desc)
.build
.query[(RShare, RUser)]
.to[List]
}
def deleteByIdAndCid(id: Ident, cid: Ident): ConnectionIO[Int] = {
val u = RUser.T
DML.delete(T, T.id === id && T.userId.in(Select(u.uid.s, from(u), u.cid === cid)))
}
}

View File

@ -26,7 +26,13 @@ case class RUser(
loginCount: Int,
lastLogin: Option[Timestamp],
created: Timestamp
) {}
) {
def accountId: AccountId =
AccountId(cid, login)
def idRef: IdRef =
IdRef(uid, login.id)
}
object RUser {