diff --git a/apps/topics.lua b/apps/topics.lua index f6a6b04..d9b1eec 100644 --- a/apps/topics.lua +++ b/apps/topics.lua @@ -40,6 +40,8 @@ app:get("all_topics", "", function(self) ) threads ON threads.topic_id = topics.id AND threads.rn = 1 LEFT JOIN users on users.id = threads.user_id + ORDER BY + topics.sort_order ASC ]]) self.me = util.get_logged_in_user_or_transient(self) return {render = "topics.topics"} @@ -68,10 +70,12 @@ app:post("topic_create", "/create", function(self) local time = os.time() local slug = lapis_util.slugify(topic_name) .. "-" .. time + local topic_count = Topics:count() local topic = Topics:create({ name = topic_name, description = topic_description, slug = slug, + sort_order = topic_count + 1, }) util.inject_infobox(self, "Topic created.") diff --git a/migrations.lua b/migrations.lua index 7ed91e9..00fca1c 100644 --- a/migrations.lua +++ b/migrations.lua @@ -57,5 +57,10 @@ return { db.query('DROP INDEX "idx_users_avatar"') schema.drop_column("users", "avatar_id") schema.add_column("users", "avatar_id", "REFERENCES avatars(id) DEFAULT 1") + end, + + [8] = function () + schema.add_column("topics", "sort_order", types.integer{default = 0}) + db.query("UPDATE topics SET sort_order = (SELECT COUNT(*) FROM topics t2 WHERE t2.ROWID <= topics.ROWID)") end }