Rename follower/followee
This commit is contained in:
parent
81c705d519
commit
721bf7e61a
4 changed files with 19 additions and 19 deletions
|
@ -461,7 +461,7 @@ fn ApiConn(comptime DbConn: type) type {
|
||||||
|
|
||||||
pub fn queryFollowers(self: *Self, user_id: Uuid, args: FollowerQueryArgs) !FollowerQueryResult {
|
pub fn queryFollowers(self: *Self, user_id: Uuid, args: FollowerQueryArgs) !FollowerQueryResult {
|
||||||
var all_args = std.mem.zeroInit(services.follows.QueryArgs, args);
|
var all_args = std.mem.zeroInit(services.follows.QueryArgs, args);
|
||||||
all_args.following_id = user_id;
|
all_args.followee_id = user_id;
|
||||||
const result = try services.follows.query(self.db, all_args, self.arena.allocator());
|
const result = try services.follows.query(self.db, all_args, self.arena.allocator());
|
||||||
return FollowerQueryResult{
|
return FollowerQueryResult{
|
||||||
.items = result.items,
|
.items = result.items,
|
||||||
|
@ -472,7 +472,7 @@ fn ApiConn(comptime DbConn: type) type {
|
||||||
|
|
||||||
pub fn queryFollowing(self: *Self, user_id: Uuid, args: FollowingQueryArgs) !FollowingQueryResult {
|
pub fn queryFollowing(self: *Self, user_id: Uuid, args: FollowingQueryArgs) !FollowingQueryResult {
|
||||||
var all_args = std.mem.zeroInit(services.follows.QueryArgs, args);
|
var all_args = std.mem.zeroInit(services.follows.QueryArgs, args);
|
||||||
all_args.follower_id = user_id;
|
all_args.followed_by_id = user_id;
|
||||||
const result = try services.follows.query(self.db, all_args, self.arena.allocator());
|
const result = try services.follows.query(self.db, all_args, self.arena.allocator());
|
||||||
return FollowingQueryResult{
|
return FollowingQueryResult{
|
||||||
.items = result.items,
|
.items = result.items,
|
||||||
|
|
|
@ -10,21 +10,21 @@ const DateTime = util.DateTime;
|
||||||
pub const Follow = struct {
|
pub const Follow = struct {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
|
|
||||||
follower_id: Uuid,
|
followed_by_id: Uuid,
|
||||||
following_id: Uuid,
|
followee_id: Uuid,
|
||||||
|
|
||||||
created_at: DateTime,
|
created_at: DateTime,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn create(db: anytype, follower_id: Uuid, following_id: Uuid, alloc: std.mem.Allocator) !void {
|
pub fn create(db: anytype, followed_by_id: Uuid, followee_id: Uuid, alloc: std.mem.Allocator) !void {
|
||||||
if (Uuid.eql(follower_id, following_id)) return error.SelfFollow;
|
if (Uuid.eql(followed_by_id, followee_id)) return error.SelfFollow;
|
||||||
const now = DateTime.now();
|
const now = DateTime.now();
|
||||||
const id = Uuid.randV4(util.getThreadPrng());
|
const id = Uuid.randV4(util.getThreadPrng());
|
||||||
|
|
||||||
db.insert("follow", .{
|
db.insert("follow", .{
|
||||||
.id = id,
|
.id = id,
|
||||||
.follower_id = follower_id,
|
.followed_by_id = followed_by_id,
|
||||||
.following_id = following_id,
|
.followee_id = followee_id,
|
||||||
.created_at = now,
|
.created_at = now,
|
||||||
}, alloc) catch |err| return switch (err) {
|
}, alloc) catch |err| return switch (err) {
|
||||||
error.ForeignKeyViolation => error.NotFound,
|
error.ForeignKeyViolation => error.NotFound,
|
||||||
|
@ -46,8 +46,8 @@ pub const QueryArgs = struct {
|
||||||
|
|
||||||
max_items: usize = 20,
|
max_items: usize = 20,
|
||||||
|
|
||||||
follower_id: ?Uuid = null,
|
followed_by_id: ?Uuid = null,
|
||||||
following_id: ?Uuid = null,
|
followee_id: ?Uuid = null,
|
||||||
|
|
||||||
order_by: OrderBy = .created_at,
|
order_by: OrderBy = .created_at,
|
||||||
|
|
||||||
|
@ -75,13 +75,13 @@ pub fn query(db: anytype, args: QueryArgs, alloc: std.mem.Allocator) !QueryResul
|
||||||
defer builder.deinit();
|
defer builder.deinit();
|
||||||
|
|
||||||
try builder.appendSlice(
|
try builder.appendSlice(
|
||||||
\\SELECT follow.id, follow.follower_id, follow.following_id, follow.created_at
|
\\SELECT follow.id, follow.followed_by_id, follow.followee_id, follow.created_at
|
||||||
\\FROM follow
|
\\FROM follow
|
||||||
\\
|
\\
|
||||||
);
|
);
|
||||||
|
|
||||||
if (args.follower_id != null) try builder.andWhere("follow.follower_id = $1");
|
if (args.followed_by_id != null) try builder.andWhere("follow.followed_by_id = $1");
|
||||||
if (args.following_id != null) try builder.andWhere("follow.following_id = $2");
|
if (args.followee_id != null) try builder.andWhere("follow.followee_id = $2");
|
||||||
|
|
||||||
if (args.prev != null) {
|
if (args.prev != null) {
|
||||||
try builder.andWhere("(follow.id, follow.created_at)");
|
try builder.andWhere("(follow.id, follow.created_at)");
|
||||||
|
@ -101,8 +101,8 @@ pub fn query(db: anytype, args: QueryArgs, alloc: std.mem.Allocator) !QueryResul
|
||||||
|
|
||||||
const max_items = if (args.max_items > max_max_items) max_max_items else args.max_items;
|
const max_items = if (args.max_items > max_max_items) max_max_items else args.max_items;
|
||||||
const query_args = .{
|
const query_args = .{
|
||||||
args.follower_id,
|
args.followed_by_id,
|
||||||
args.following_id,
|
args.followee_id,
|
||||||
if (args.prev) |p| p.id else null,
|
if (args.prev) |p| p.id else null,
|
||||||
if (args.prev) |p| p.order_val else null,
|
if (args.prev) |p| p.order_val else null,
|
||||||
max_items,
|
max_items,
|
||||||
|
|
|
@ -98,7 +98,7 @@ pub fn query(db: anytype, args: QueryArgs, alloc: std.mem.Allocator) !QueryResul
|
||||||
|
|
||||||
if (args.followed_by != null) try builder.appendSlice(
|
if (args.followed_by != null) try builder.appendSlice(
|
||||||
\\ JOIN follow ON
|
\\ JOIN follow ON
|
||||||
\\ follow.follower_id = $7 AND follow.following_id = note.author_id
|
\\ follow.followed_by_id = $7 AND follow.followee_id = note.author_id
|
||||||
\\
|
\\
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -195,12 +195,12 @@ const migrations: []const Migration = &.{
|
||||||
\\CREATE TABLE follow(
|
\\CREATE TABLE follow(
|
||||||
\\ id UUID NOT NULL PRIMARY KEY,
|
\\ id UUID NOT NULL PRIMARY KEY,
|
||||||
\\
|
\\
|
||||||
\\ follower_id UUID NOT NULL,
|
\\ followed_by_id UUID NOT NULL,
|
||||||
\\ following_id UUID NOT NULL,
|
\\ followee_id UUID NOT NULL,
|
||||||
\\
|
\\
|
||||||
\\ created_at TIMESTAMPTZ NOT NULL,
|
\\ created_at TIMESTAMPTZ NOT NULL,
|
||||||
\\
|
\\
|
||||||
\\ UNIQUE(follower_id, following_id)
|
\\ UNIQUE(followed_by_id, followee_id)
|
||||||
\\);
|
\\);
|
||||||
,
|
,
|
||||||
.down = "DROP TABLE follow",
|
.down = "DROP TABLE follow",
|
||||||
|
|
Loading…
Reference in a new issue