Commits

luastoned committed ecd2aa3

Added thread.getPage() for immediate page caching, not intended for long-term caching
Removed obsoleted error code returns in functions as a result of
thread.getPage() now being the primary page request function

  • Participants
  • Parent commits 90482db

Comments (0)

Files changed (1)

File facepunch/thread.lua

 -------------------------------------------------------------------------------
 local error = error
 local facepunch = require( "facepunch" )
+local http = require( "facepunch.http" )
 local member = require( "facepunch.member" )
 local post = require( "facepunch.post" )
 local pairs = pairs
 -- thread.getMembersInPage()
 -- Purpose: Returns all members that have posted on a given thread page, first
 --			returns 0 if there are no errors or 1 in case of errors
--- Input: threadPageURL - URL to a single page in the thread
--- Output: integer, table of members
+-- Input: threadPage - string of the requested page
+-- Output: table of members
 -------------------------------------------------------------------------------
-function getMembersInPage( threadPageURL )
-	local threadPage, returnCode = facepunch.http.get( threadPageURL )
-	if ( returnCode == 200 ) then
-		local t = {}
-		local matched = false
-		for username,
-			status,
-			displayedUsername,
-			usertitle,
-			avatar,
-			joinDate,
-			postCount,
-			links in string.gmatch( threadPage, threadPageMemberPattern ) do
-			for _, v in pairs( t ) do
-				if ( v.username == username ) then
-					matched = true
-				end
-			end
-			if ( not matched ) then
-				local member			= member()
-				member.username			= username
-				member.online			= status == "on"
-				if ( username == displayedUsername ) then
-					member.usergroup	= "Registered User"
-				elseif ( string.find( displayedUsername, "<font color=\"red\">" ) ) then
-					member.usergroup	= "Banned"
-				elseif ( string.find( displayedUsername, "#A06000" ) ) then
-					member.usergroup	= "Gold Member"
-				elseif ( string.find( displayedUsername, "#00aa00" ) ) then
-					member.usergroup	= "Moderator"
-				elseif ( string.find( displayedUsername, "<span class=\"boing\">") ) then
-					member.usergroup	= "Administrator"
-				end
-				member.usertitle		= string.gsub( usertitle, "^%s*(.-)%s*$", "%1" )
-				if ( string.find( member.usertitle, "<span" ) ) then
-					member.usertitle	= member.usertitle .. "</span>"
-				end
-				if ( member.usertitle == "" ) then
-					member.usertitle	= nil
-				end
-				avatar					= string.gsub( avatar, "^%s*(.-)%s*$", "%1" )
-				if ( avatar == "" ) then
-					member.avatar		= nil
-				else
-					for url in string.gmatch( avatar, ".-img src=\"(.-)\"" ) do
-						avatar			= url
-					end
-					member.avatar		= facepunch.rootURL .. avatar
-				end
-				member.joinDate			= string.gsub( joinDate, "^%s*(.-)%s*$", "%1" )
-				member.postCount		= postCount
-				member.postCount		= string.gsub( member.postCount, "^%s*(.-)%s*$", "%1" )
-				member.postCount		= tonumber( string.gsub( member.postCount, ",", "" ), 10 )
-
-				member.links = {}
-				local hasLinks = false
-				for url, name in string.gmatch( links, memberSocialLinkPattern ) do
-					if ( hasLinks == false ) then hasLinks = true end
-					member.links[ name ] = url
-				end
-				if ( not hasLinks ) then member.links = nil end
-				table.insert( t, member )
-			else
-				matched = false
+function getMembersInPage( threadPage )
+	local t = {}
+	local matched = false
+	for username,
+		status,
+		displayedUsername,
+		usertitle,
+		avatar,
+		joinDate,
+		postCount,
+		links in string.gmatch( threadPage, threadPageMemberPattern ) do
+		for _, v in pairs( t ) do
+			if ( v.username == username ) then
+				matched = true
 			end
 		end
-		return 0, t
+		if ( not matched ) then
+			local member			= member()
+			member.username			= username
+			member.online			= status == "on"
+			if ( username == displayedUsername ) then
+				member.usergroup	= "Registered User"
+			elseif ( string.find( displayedUsername, "<font color=\"red\">" ) ) then
+				member.usergroup	= "Banned"
+			elseif ( string.find( displayedUsername, "#A06000" ) ) then
+				member.usergroup	= "Gold Member"
+			elseif ( string.find( displayedUsername, "#00aa00" ) ) then
+				member.usergroup	= "Moderator"
+			elseif ( string.find( displayedUsername, "<span class=\"boing\">") ) then
+				member.usergroup	= "Administrator"
+			end
+			member.usertitle		= string.gsub( usertitle, "^%s*(.-)%s*$", "%1" )
+			if ( string.find( member.usertitle, "<span" ) ) then
+				member.usertitle	= member.usertitle .. "</span>"
+			end
+			if ( member.usertitle == "" ) then
+				member.usertitle	= nil
+			end
+			avatar					= string.gsub( avatar, "^%s*(.-)%s*$", "%1" )
+			if ( avatar == "" ) then
+				member.avatar		= nil
+			else
+				for url in string.gmatch( avatar, ".-img src=\"(.-)\"" ) do
+					avatar			= url
+				end
+				member.avatar		= facepunch.rootURL .. avatar
+			end
+			member.joinDate			= string.gsub( joinDate, "^%s*(.-)%s*$", "%1" )
+			member.postCount		= postCount
+			member.postCount		= string.gsub( member.postCount, "^%s*(.-)%s*$", "%1" )
+			member.postCount		= tonumber( string.gsub( member.postCount, ",", "" ), 10 )
+
+			member.links = {}
+			local hasLinks = false
+			for url, name in string.gmatch( links, memberSocialLinkPattern ) do
+				if ( hasLinks == false ) then hasLinks = true end
+				member.links[ name ] = url
+			end
+			if ( not hasLinks ) then member.links = nil end
+			table.insert( t, member )
+		else
+			matched = false
+		end
+	end
+	return t
+end
+
+-------------------------------------------------------------------------------
+-- thread.getMembersReading()
+-- Purpose: Returns all members reading a given thread
+-- Input: threadPage - string of the requested page
+-- Output: table of members
+-------------------------------------------------------------------------------
+function getMembersReading( threadPage )
+	error( "not yet implemented!", 2 )
+end
+
+-------------------------------------------------------------------------------
+-- thread.getPage()
+-- Purpose: Returns 0 if the page is retrieved successfully, then the thread
+--			page by ID and page number, if provided, otherwise it returns 1 and
+--			nil
+-- Input: threadID - ID of the thread to get
+--		  pageNumber - number of the page to get
+-- Output: error code, thread page
+-------------------------------------------------------------------------------
+function getPage( threadID, pageNumber )
+	pageNumber = pageNumber or ""
+	pageNumber = "/" .. pageNumber
+	local r, c = http.get( facepunch.baseURL .. "/threads/" .. threadID .. pageNumber )
+	if ( c == 200 ) then
+		return 0, r
 	else
 		return 1, nil
 	end
 end
 
 -------------------------------------------------------------------------------
--- thread.getMembersReading()
--- Purpose: Returns all members reading a given thread
--- Input: threadPageURL - URL to a single page in the thread
--- Output: table of members
--------------------------------------------------------------------------------
-function getMembersReading( threadPageURL )
-	error( "not yet implemented!", 2 )
-end
-
--------------------------------------------------------------------------------
 -- thread.getPostByID()
 -- Purpose: Returns 0 if the post is found, then the post object, otherwise it
 --			returns 1 and nil
--- Input: threadPageURL - URL to a single page in the thread
+-- Input: threadPage - string of the requested page
 --		  postID - number of post
--- Output: integer, post
+-- Output: error code, post
 -------------------------------------------------------------------------------
-function getPostByID( threadPageURL, postID )
+function getPostByID( threadPage, postID )
 	error( "not yet implemented!", 2 )
 end
 
 -- thread.getPostsInPage()
 -- Purpose: Returns all posts on a given thread page, first returns 0 if there
 --			are no errors or 1 in case of errors
--- Input: threadPageURL - URL to a single page in the thread
+-- Input: threadPage - string of the requested page
 -- Output: table of posts
 -------------------------------------------------------------------------------
-function getPostsInPage( threadPageURL )
-	local threadPage, returnCode = facepunch.http.get( threadPageURL )
-	if ( returnCode == 200 ) then
-		local t = {}
-		for fullPost,
-			postDate,
-			link,
-			postNumber
-			in string.gmatch( threadPage, threadPagePostPattern ) do
-			local post		= post()
-			post.postDate	= postDate
-			post.link		= facepunch.baseURL .. string.gsub( link, "&amp;", "&" )
-			post.postNumber	= postNumber
+function getPostsInPage( threadPage )
+	local t = {}
+	for fullPost,
+		postDate,
+		link,
+		postNumber
+		in string.gmatch( threadPage, threadPagePostPattern ) do
+		local post		= post()
+		post.postDate	= postDate
+		post.link		= facepunch.baseURL .. string.gsub( link, "&amp;", "&" )
+		post.postNumber	= postNumber
 
-			local postRatings = string.match( fullPost, postRatingResultSpanPattern )
-			if ( postRatings ) then
-				post.postRatings = {}
-				for name, amount in string.gmatch( postRatings, postRatingResultPattern ) do
-					post.postRatings[ name ] = tonumber( amount )
-				end
+		local postRatings = string.match( fullPost, postRatingResultSpanPattern )
+		if ( postRatings ) then
+			post.postRatings = {}
+			for name, amount in string.gmatch( postRatings, postRatingResultPattern ) do
+				post.postRatings[ name ] = tonumber( amount )
 			end
-			
-			local postRatingKeys = string.match( fullPost, postRatingKeyDivPattern )
-			if ( postRatingKeys ) then
-				post.postRatingKeys = {}
-				for key, rating in string.gmatch( postRatingKeys, postRatingKeyPattern ) do
-					post.postRatingKeys[ rating ] = key
-				end
+		end
+		
+		local postRatingKeys = string.match( fullPost, postRatingKeyDivPattern )
+		if ( postRatingKeys ) then
+			post.postRatingKeys = {}
+			for key, rating in string.gmatch( postRatingKeys, postRatingKeyPattern ) do
+				post.postRatingKeys[ rating ] = key
 			end
+		end
 
-			table.insert( t, post )
-		end
-		return 0, t
-	else
-		return 1, nil
+		table.insert( t, post )
 	end
+	return t
 end