Add local timeline

This commit is contained in:
jaina heartles 2022-11-12 05:23:55 -08:00
parent 5630c6160f
commit 8694516180
4 changed files with 30 additions and 3 deletions

View file

@ -356,5 +356,14 @@ fn ApiConn(comptime DbConn: type) type {
const result = try services.notes.query(self.db, .{}, self.arena.allocator());
return result.items;
}
pub fn localTimeline(self: *Self) ![]services.notes.Note {
const result = try services.notes.query(
self.db,
.{ .community_id = self.community.id },
self.arena.allocator(),
);
return result.items;
}
};
}

View file

@ -43,7 +43,7 @@ const selectStarFromNote = std.fmt.comptimePrint(
\\SELECT {s}
\\FROM note
\\
, .{util.comptimeJoin(",", std.meta.fieldNames(Note))});
, .{util.comptimeJoinWithPrefix(",", "note.", std.meta.fieldNames(Note))});
pub fn get(db: anytype, id: Uuid, alloc: std.mem.Allocator) GetError!Note {
return db.queryRow(
Note,
@ -69,6 +69,7 @@ pub const QueryArgs = struct {
created_before: ?DateTime = null,
created_after: ?DateTime = null,
community_id: ?Uuid = null,
prev: ?struct {
id: Uuid,
@ -89,7 +90,10 @@ pub fn query(db: anytype, args: QueryArgs, alloc: std.mem.Allocator) !QueryResul
var builder = sql.QueryBuilder.init(alloc);
defer builder.deinit();
try builder.appendSlice(selectStarFromNote);
try builder.appendSlice(selectStarFromNote ++
\\ JOIN actor ON actor.id = note.author_id
\\
);
if (args.created_before != null) try builder.andWhere("note.created_at < $1");
if (args.created_after != null) try builder.andWhere("note.created_at > $2");
@ -102,11 +106,12 @@ pub fn query(db: anytype, args: QueryArgs, alloc: std.mem.Allocator) !QueryResul
}
try builder.appendSlice("($3, $4)");
}
if (args.community_id != null) try builder.andWhere("actor.community_id = $5");
try builder.appendSlice(
\\
\\ORDER BY note.created_at DESC
\\LIMIT $5
\\LIMIT $6
\\
);
@ -121,6 +126,7 @@ pub fn query(db: anytype, args: QueryArgs, alloc: std.mem.Allocator) !QueryResul
args.created_after,
prev_created_at,
prev_id,
args.community_id,
max_items,
};
};

View file

@ -44,6 +44,7 @@ const routes = .{
notes.get,
streaming.streaming,
timelines.global,
timelines.local,
};
pub fn Context(comptime Route: type) type {

View file

@ -8,3 +8,14 @@ pub const global = struct {
try res.json(.ok, results);
}
};
pub const local = struct {
pub const method = .GET;
pub const path = "/timelines/local";
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
const results = try srv.localTimeline();
try res.json(.ok, results);
}
};