Add list followers api calls

This commit is contained in:
jaina heartles 2022-11-14 00:14:29 -08:00
parent ebf8b34e15
commit c49f1487a7
2 changed files with 58 additions and 1 deletions

View file

@ -11,6 +11,7 @@ const services = struct {
const auth = @import("./services/auth.zig");
const invites = @import("./services/invites.zig");
const notes = @import("./services/notes.zig");
const follows = @import("./services/follows.zig");
};
pub const RegistrationOptions = struct {
@ -90,6 +91,40 @@ pub const TimelineResult = struct {
next_page: TimelineArgs,
};
pub const FollowQueryArgs = struct {
pub const OrderBy = services.follows.QueryArgs.OrderBy;
pub const Direction = services.follows.QueryArgs.Direction;
pub const PageDirection = services.follows.QueryArgs.PageDirection;
pub const Prev = services.follows.QueryArgs.Prev;
max_items: usize = 20,
order_by: OrderBy = .created_at,
direction: Direction = .descending,
prev: ?Prev = null,
page_direction: PageDirection = .forward,
fn from(args: services.follows.QueryArgs) FollowQueryArgs {
return .{
.max_items = args.max_items,
.order_by = args.order_by,
.direction = args.direction,
.prev = args.prev,
.page_direction = args.page_direction,
};
}
};
pub const FollowQueryResult = struct {
items: []services.follows.Follow,
prev_page: FollowQueryArgs,
next_page: FollowQueryArgs,
};
pub fn isAdminSetup(db: sql.Db) !bool {
_ = services.communities.adminCommunityId(db) catch |err| switch (err) {
error.NotFound => return false,
@ -405,5 +440,27 @@ fn ApiConn(comptime DbConn: type) type {
.next_page = TimelineArgs.from(result.next_page),
};
}
pub fn getFollowers(self: *Self, user_id: Uuid, args: FollowQueryArgs) !FollowQueryResult {
var all_args = std.mem.zeroInit(services.follows.QueryArgs, args);
all_args.following_id = user_id;
const result = try services.follows.query(self.db, all_args, self.arena.allocator());
return TimelineResult{
.items = result.items,
.prev_page = FollowQueryArgs.from(result.prev_page),
.next_page = FollowQueryArgs.from(result.next_page),
};
}
pub fn getFollowing(self: *Self, user_id: Uuid, args: FollowQueryArgs) !FollowQueryResult {
var all_args = std.mem.zeroInit(services.follows.QueryArgs, args);
all_args.follower_id = user_id;
const result = try services.follows.query(self.db, all_args, self.arena.allocator());
return TimelineResult{
.items = result.items,
.prev_page = FollowQueryArgs.from(result.prev_page),
.next_page = FollowQueryArgs.from(result.next_page),
};
}
};
}

View file

@ -60,7 +60,7 @@ pub const QueryArgs = struct {
},
} = null,
page_direction: PageDirection = null,
page_direction: PageDirection = .forward,
};
pub const QueryResult = struct {