2019-08-13 02:15:11 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'mongo'
|
|
|
|
|
|
|
|
module NodeBB
|
|
|
|
class Mongo
|
|
|
|
attr_reader :mongo
|
|
|
|
|
|
|
|
::Mongo::Logger.logger.level = Logger::WARN
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
client = ::Mongo::Client.new(params)
|
|
|
|
@mongo = client[:objects]
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups
|
2019-08-13 05:10:58 +08:00
|
|
|
group_keys = mongo.find(_key: "groups:visible:createtime").pluck(:value)
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
group_keys.map { |group_key| group(group_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def group(id)
|
2019-08-13 05:10:58 +08:00
|
|
|
group = mongo.find(_key: "group:#{id}").first
|
2019-08-13 02:15:11 +08:00
|
|
|
group["createtime"] = timestamp_to_date(group["createtime"])
|
2019-08-13 05:10:58 +08:00
|
|
|
group["member_ids"] = mongo.find(_key: "group:#{id}:members").pluck(:value)
|
|
|
|
if mongo.find(_key: "group:#{id}:owners").first
|
|
|
|
group["owner_ids"] = mongo.find(_key: "group:#{id}:owners").first[:members]
|
2019-08-13 02:15:11 +08:00
|
|
|
else
|
|
|
|
group["owner_ids"] = []
|
|
|
|
end
|
|
|
|
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
|
|
|
def users
|
2019-08-13 05:10:58 +08:00
|
|
|
user_keys = mongo.find(_key: "users:joindate").pluck(:value)
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
user_keys.map { |user_key| user(user_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def user(id)
|
2019-08-13 05:10:58 +08:00
|
|
|
user = mongo.find(_key: "user:#{id}").first
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
user["joindate"] = timestamp_to_date(user["joindate"])
|
|
|
|
user["lastonline"] = timestamp_to_date(user["lastonline"])
|
|
|
|
user['banned'] = user['banned'].to_s
|
|
|
|
user['uid'] = user['uid'].to_s
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def categories
|
2019-08-13 05:10:58 +08:00
|
|
|
category_keys = mongo.find(_key: "categories:cid").pluck(:value)
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
{}.tap do |categories|
|
|
|
|
category_keys.each do |category_key|
|
2019-08-13 05:10:58 +08:00
|
|
|
category = mongo.find(_key: "category:#{category_key}").first
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
category['parentCid'] = category['parentCid'].to_s
|
|
|
|
category['disabled'] = category['disabled'].to_s
|
|
|
|
category['cid'] = category['cid'].to_s
|
|
|
|
|
|
|
|
categories[category['cid']] = category
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def topics(offset = 0, page_size = 2000)
|
2019-08-13 05:10:58 +08:00
|
|
|
topic_keys = mongo.find(_key: 'topics:tid').skip(offset).limit(page_size).pluck(:value)
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
topic_keys.map { |topic_key| topic(topic_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic(id)
|
2019-08-13 05:10:58 +08:00
|
|
|
topic = mongo.find(_key: "topic:#{id}").first
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
topic["lastposttime"] = timestamp_to_date(topic["lastposttime"])
|
|
|
|
topic["timestamp"] = timestamp_to_date(topic["timestamp"])
|
|
|
|
topic["mainpost"] = post(topic["mainPid"])
|
|
|
|
topic["mainPid"] = topic["mainPid"].to_s
|
|
|
|
topic["deleted"] = topic["deleted"].to_s
|
|
|
|
topic["pinned"] = topic["pinned"].to_s
|
|
|
|
topic["locked"] = topic["locked"].to_s
|
|
|
|
|
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_count
|
2019-08-13 05:10:58 +08:00
|
|
|
mongo.find(_key: 'topics:tid').count
|
2019-08-13 02:15:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def posts(offset = 0, page_size = 2000)
|
2019-08-13 05:10:58 +08:00
|
|
|
post_keys = mongo.find(_key: 'posts:pid').skip(offset).limit(page_size).pluck(:value)
|
2019-08-13 02:15:11 +08:00
|
|
|
|
|
|
|
post_keys.map { |post_key| post(post_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def post(id)
|
2019-08-13 05:10:58 +08:00
|
|
|
post = mongo.find(_key: "post:#{id}").first
|
2019-08-13 02:15:11 +08:00
|
|
|
post["timestamp"] = timestamp_to_date(post["timestamp"])
|
2019-08-13 05:10:58 +08:00
|
|
|
if post["upvoted_by"] = mongo.find(_key: "pid:#{id}:upvote").first
|
|
|
|
post["upvoted_by"] = mongo.find(_key: "pid:#{id}:upvote").first[:members]
|
2019-08-13 02:15:11 +08:00
|
|
|
else
|
|
|
|
post["upvoted_by"] = []
|
|
|
|
end
|
|
|
|
|
|
|
|
post["pid"] = post["pid"].to_s
|
|
|
|
post["deleted"] = post["deleted"].to_s
|
|
|
|
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_count
|
2019-08-13 05:10:58 +08:00
|
|
|
mongo.find(_key: 'posts:pid').count
|
2019-08-13 02:15:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def timestamp_to_date(createtime)
|
|
|
|
Time.at(createtime.to_s[0..-6].to_i).utc if createtime
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|