Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo-1
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo-1
Commits
555bcc11
Unverified
Commit
555bcc11
authored
Mar 03, 2019
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🎨
#12690
parent
a63475a9
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
21 additions
and
493 deletions
+21
-493
src/main/java/org/b3log/solo/cache/UserCache.java
src/main/java/org/b3log/solo/cache/UserCache.java
+1
-27
src/main/java/org/b3log/solo/model/Comment.java
src/main/java/org/b3log/solo/model/Comment.java
+1
-6
src/main/java/org/b3log/solo/repository/UserRepository.java
src/main/java/org/b3log/solo/repository/UserRepository.java
+0
-23
src/main/java/org/b3log/solo/service/CommentMgmtService.java
src/main/java/org/b3log/solo/service/CommentMgmtService.java
+9
-170
src/main/java/org/b3log/solo/service/UpgradeService.java
src/main/java/org/b3log/solo/service/UpgradeService.java
+4
-258
src/main/java/org/b3log/solo/upgrade/V310_320.java
src/main/java/org/b3log/solo/upgrade/V310_320.java
+2
-1
src/main/java/org/b3log/solo/util/Solos.java
src/main/java/org/b3log/solo/util/Solos.java
+4
-7
src/test/java/org/b3log/solo/AbstractTestCase.java
src/test/java/org/b3log/solo/AbstractTestCase.java
+0
-1
No files found.
src/main/java/org/b3log/solo/cache/UserCache.java
View file @
555bcc11
...
...
@@ -20,7 +20,6 @@ package org.b3log.solo.cache;
import
org.b3log.latke.Keys
;
import
org.b3log.latke.ioc.Singleton
;
import
org.b3log.latke.model.Role
;
import
org.b3log.latke.model.User
;
import
org.b3log.solo.util.Solos
;
import
org.json.JSONObject
;
...
...
@@ -31,7 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
* User cache.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.
1, Sep 25, 2018
* @version 1.1.0.
2, Mar 3, 2019
* @since 2.3.0
*/
@Singleton
...
...
@@ -42,11 +41,6 @@ public class UserCache {
*/
private
final
Map
<
String
,
JSONObject
>
idCache
=
new
ConcurrentHashMap
<>();
/**
* Email, User.
*/
private
final
Map
<
String
,
JSONObject
>
emailCache
=
new
ConcurrentHashMap
<>();
/**
* Admin user.
*/
...
...
@@ -85,21 +79,6 @@ public class UserCache {
return
Solos
.
clone
(
user
);
}
/**
* Gets a user by the specified user email.
*
* @param userEmail the specified user email
* @return user, returns {@code null} if not found
*/
public
JSONObject
getUserByEmail
(
final
String
userEmail
)
{
final
JSONObject
user
=
emailCache
.
get
(
userEmail
);
if
(
null
==
user
)
{
return
null
;
}
return
Solos
.
clone
(
user
);
}
/**
* Adds or updates the specified user.
*
...
...
@@ -107,7 +86,6 @@ public class UserCache {
*/
public
void
putUser
(
final
JSONObject
user
)
{
idCache
.
put
(
user
.
optString
(
Keys
.
OBJECT_ID
),
Solos
.
clone
(
user
));
emailCache
.
put
(
user
.
optString
(
User
.
USER_EMAIL
),
Solos
.
clone
(
user
));
}
/**
...
...
@@ -122,9 +100,6 @@ public class UserCache {
}
idCache
.
remove
(
id
);
final
String
email
=
user
.
optString
(
User
.
USER_EMAIL
);
emailCache
.
remove
(
email
);
}
/**
...
...
@@ -132,7 +107,6 @@ public class UserCache {
*/
public
void
clear
()
{
idCache
.
clear
();
emailCache
.
clear
();
adminCache
.
clear
();
}
}
src/main/java/org/b3log/solo/model/Comment.java
View file @
555bcc11
...
...
@@ -24,7 +24,7 @@ import org.json.JSONObject;
* This class defines all comment model relevant keys.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.0.
0, Sep 21, 2018
* @version 1.2.0.
1, Mar 3, 2019
* @since 0.3.1
*/
public
final
class
Comment
{
...
...
@@ -49,11 +49,6 @@ public final class Comment {
*/
public
static
final
String
COMMENT_NAME
=
"commentName"
;
/**
* Key of comment email.
*/
public
static
final
String
COMMENT_EMAIL
=
"commentEmail"
;
/**
* Key of comment URL.
*/
...
...
src/main/java/org/b3log/solo/repository/UserRepository.java
View file @
555bcc11
...
...
@@ -96,29 +96,6 @@ public class UserRepository extends AbstractRepository {
return
getFirst
(
new
Query
().
setFilter
(
new
PropertyFilter
(
User
.
USER_NAME
,
FilterOperator
.
EQUAL
,
userName
)));
}
/**
* Gets a user by the specified email.
*
* @param email the specified email
* @return user, returns {@code null} if not found
* @throws RepositoryException repository exception
*/
public
JSONObject
getByEmail
(
final
String
email
)
throws
RepositoryException
{
JSONObject
ret
=
userCache
.
getUserByEmail
(
email
);
if
(
null
!=
ret
)
{
return
ret
;
}
ret
=
getFirst
(
new
Query
().
setFilter
(
new
PropertyFilter
(
User
.
USER_EMAIL
,
FilterOperator
.
EQUAL
,
email
.
toLowerCase
().
trim
())));
if
(
null
==
ret
)
{
return
null
;
}
userCache
.
putUser
(
ret
);
return
ret
;
}
/**
* Gets the administrator user.
*
...
...
src/main/java/org/b3log/solo/service/CommentMgmtService.java
View file @
555bcc11
...
...
@@ -17,11 +17,9 @@
*/
package
org
.
b3log
.
solo
.
service
;
import
jodd.http.HttpRequest
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.commons.lang.time.DateFormatUtils
;
import
org.b3log.latke.Keys
;
import
org.b3log.latke.Latkes
;
import
org.b3log.latke.event.Event
;
import
org.b3log.latke.event.EventManager
;
import
org.b3log.latke.ioc.Inject
;
...
...
@@ -44,42 +42,28 @@ import org.b3log.solo.repository.PageRepository;
import
org.b3log.solo.repository.UserRepository
;
import
org.b3log.solo.util.Emotions
;
import
org.b3log.solo.util.Markdowns
;
import
org.b3log.solo.util.Solos
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.jsoup.Jsoup
;
import
org.jsoup.safety.Whitelist
;
import
javax.servlet.http.HttpServletResponse
;
import
java.net.URL
;
import
java.util.Date
;
/**
* Comment management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.3.
5, Feb 21
, 2019
* @version 1.3.3.
6, Mar 3
, 2019
* @since 0.3.5
*/
@Service
public
class
CommentMgmtService
{
/**
* Comment mail HTML body.
*/
public
static
final
String
COMMENT_MAIL_HTML_BODY
=
"<p>{articleOrPage} [<a href=\""
+
"{articleOrPageURL}\">"
+
"{title}</a>]"
+
" received a new comment:</p>"
+
"{commenter}: <span><a href=\"{commentSharpURL}\">"
+
"{commentContent}</a></span>"
;
/**
* Logger.
*/
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
CommentMgmtService
.
class
);
/**
* Default user thumbnail.
*/
private
static
final
String
DEFAULT_USER_THUMBNAIL
=
"default-user-thumbnail.png"
;
/**
* Minimum length of comment name.
*/
...
...
@@ -159,98 +143,6 @@ public class CommentMgmtService {
*/
private
MailService
mailService
=
MailServiceFactory
.
getMailService
();
/**
* Sends a notification mail to administrator for notifying the specified article or page received the specified
* comment and original comment.
*
* @param articleOrPage the specified article or page
* @param comment the specified comment
* @param originalComment original comment, if not exists, set it as {@code null}
* @param preference the specified preference
* @throws Exception exception
*/
public
void
sendNotificationMail
(
final
JSONObject
articleOrPage
,
final
JSONObject
comment
,
final
JSONObject
originalComment
,
final
JSONObject
preference
)
throws
Exception
{
if
(!
Solos
.
isMailConfigured
())
{
return
;
}
final
String
commentEmail
=
comment
.
getString
(
Comment
.
COMMENT_EMAIL
);
final
String
commentId
=
comment
.
getString
(
Keys
.
OBJECT_ID
);
final
String
commentContent
=
comment
.
getString
(
Comment
.
COMMENT_CONTENT
);
final
String
adminEmail
=
preference
.
getString
(
Option
.
ID_C_ADMIN_EMAIL
);
if
(
adminEmail
.
equalsIgnoreCase
(
commentEmail
))
{
LOGGER
.
log
(
Level
.
DEBUG
,
"Do not send comment notification mail to admin itself[{0}]"
,
adminEmail
);
return
;
}
if
(
Latkes
.
getServePath
().
contains
(
"localhost"
)
||
Strings
.
isIPv4
(
Latkes
.
getServePath
()))
{
LOGGER
.
log
(
Level
.
INFO
,
"Solo runs on local server, so should not send mail"
);
return
;
}
if
(
null
!=
originalComment
&&
comment
.
has
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
))
{
final
String
originalEmail
=
originalComment
.
getString
(
Comment
.
COMMENT_EMAIL
);
if
(
originalEmail
.
equalsIgnoreCase
(
adminEmail
))
{
LOGGER
.
log
(
Level
.
DEBUG
,
"Do not send comment notification mail to admin while the specified comment[{0}] is an reply"
,
commentId
);
return
;
}
}
final
String
blogTitle
=
preference
.
getString
(
Option
.
ID_C_BLOG_TITLE
);
boolean
isArticle
=
true
;
String
title
=
articleOrPage
.
optString
(
Article
.
ARTICLE_TITLE
);
if
(
StringUtils
.
isBlank
(
title
))
{
title
=
articleOrPage
.
getString
(
Page
.
PAGE_TITLE
);
isArticle
=
false
;
}
final
String
commentSharpURL
=
comment
.
getString
(
Comment
.
COMMENT_SHARP_URL
);
final
MailService
.
Message
message
=
new
MailService
.
Message
();
message
.
setFrom
(
adminEmail
);
message
.
addRecipient
(
adminEmail
);
String
mailSubject
;
String
articleOrPageURL
;
String
mailBody
;
if
(
isArticle
)
{
mailSubject
=
blogTitle
+
": New comment on article ["
+
title
+
"]"
;
articleOrPageURL
=
Latkes
.
getServePath
()
+
articleOrPage
.
getString
(
Article
.
ARTICLE_PERMALINK
);
mailBody
=
COMMENT_MAIL_HTML_BODY
.
replace
(
"{articleOrPage}"
,
"Article"
);
}
else
{
mailSubject
=
blogTitle
+
": New comment on page ["
+
title
+
"]"
;
articleOrPageURL
=
Latkes
.
getServePath
()
+
articleOrPage
.
getString
(
Page
.
PAGE_PERMALINK
);
mailBody
=
COMMENT_MAIL_HTML_BODY
.
replace
(
"{articleOrPage}"
,
"Page"
);
}
message
.
setSubject
(
mailSubject
);
final
String
commentName
=
comment
.
getString
(
Comment
.
COMMENT_NAME
);
final
String
commentURL
=
comment
.
getString
(
Comment
.
COMMENT_URL
);
String
commenter
;
if
(!
"http://"
.
equals
(
commentURL
))
{
commenter
=
"<a target=\"_blank\" "
+
"href=\""
+
commentURL
+
"\">"
+
commentName
+
"</a>"
;
}
else
{
commenter
=
commentName
;
}
mailBody
=
mailBody
.
replace
(
"{articleOrPageURL}"
,
articleOrPageURL
).
replace
(
"{title}"
,
title
).
replace
(
"{commentContent}"
,
commentContent
).
replace
(
"{commentSharpURL}"
,
Latkes
.
getServePath
()
+
commentSharpURL
).
replace
(
"{commenter}"
,
commenter
);
message
.
setHtmlBody
(
mailBody
);
LOGGER
.
log
(
Level
.
DEBUG
,
"Sending a mail[mailSubject={0}, mailBody=[{1}] to admin[email={2}]"
,
mailSubject
,
mailBody
,
adminEmail
);
mailService
.
send
(
message
);
}
/**
* Checks the specified comment adding request.
* <p>
...
...
@@ -356,7 +248,6 @@ public class CommentMgmtService {
* {
* "oId": "", // page id
* "commentName": "",
* "commentEmail": "",
* "commentURL": "", // optional
* "commentContent": "",
* "commentOriginalCommentId": "" // optional
...
...
@@ -391,7 +282,6 @@ public class CommentMgmtService {
final
JSONObject
page
=
pageRepository
.
get
(
pageId
);
ret
.
put
(
Page
.
PAGE
,
page
);
final
String
commentName
=
requestJSONObject
.
getString
(
Comment
.
COMMENT_NAME
);
final
String
commentEmail
=
requestJSONObject
.
getString
(
Comment
.
COMMENT_EMAIL
).
trim
().
toLowerCase
();
final
String
commentURL
=
requestJSONObject
.
optString
(
Comment
.
COMMENT_URL
);
final
String
commentContent
=
requestJSONObject
.
getString
(
Comment
.
COMMENT_CONTENT
);
...
...
@@ -406,7 +296,6 @@ public class CommentMgmtService {
JSONObject
originalComment
=
null
;
comment
.
put
(
Comment
.
COMMENT_NAME
,
commentName
);
comment
.
put
(
Comment
.
COMMENT_EMAIL
,
commentEmail
);
comment
.
put
(
Comment
.
COMMENT_URL
,
commentURL
);
comment
.
put
(
Comment
.
COMMENT_CONTENT
,
commentContent
);
final
JSONObject
preference
=
preferenceQueryService
.
getPreference
();
...
...
@@ -450,11 +339,6 @@ public class CommentMgmtService {
comment
.
put
(
Keys
.
OBJECT_ID
,
commentId
);
commentRepository
.
add
(
comment
);
incPageCommentCount
(
pageId
);
try
{
sendNotificationMail
(
page
,
comment
,
originalComment
,
preference
);
}
catch
(
final
Exception
e
)
{
LOGGER
.
log
(
Level
.
WARN
,
"Send mail failed"
,
e
);
}
final
JSONObject
eventData
=
new
JSONObject
();
eventData
.
put
(
Comment
.
COMMENT
,
comment
);
...
...
@@ -480,7 +364,6 @@ public class CommentMgmtService {
* {
* "oId": "", // article id
* "commentName": "",
* "commentEmail": "",
* "commentURL": "", // optional
* "commentContent": "",
* "commentOriginalCommentId": "" // optional
...
...
@@ -515,7 +398,6 @@ public class CommentMgmtService {
final
JSONObject
article
=
articleRepository
.
get
(
articleId
);
ret
.
put
(
Article
.
ARTICLE
,
article
);
final
String
commentName
=
requestJSONObject
.
getString
(
Comment
.
COMMENT_NAME
);
final
String
commentEmail
=
requestJSONObject
.
getString
(
Comment
.
COMMENT_EMAIL
).
trim
().
toLowerCase
();
final
String
commentURL
=
requestJSONObject
.
optString
(
Comment
.
COMMENT_URL
);
final
String
commentContent
=
requestJSONObject
.
getString
(
Comment
.
COMMENT_CONTENT
);
...
...
@@ -530,7 +412,6 @@ public class CommentMgmtService {
JSONObject
originalComment
=
null
;
comment
.
put
(
Comment
.
COMMENT_NAME
,
commentName
);
comment
.
put
(
Comment
.
COMMENT_EMAIL
,
commentEmail
);
comment
.
put
(
Comment
.
COMMENT_URL
,
commentURL
);
comment
.
put
(
Comment
.
COMMENT_CONTENT
,
commentContent
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
requestJSONObject
.
optString
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
));
...
...
@@ -582,12 +463,6 @@ public class CommentMgmtService {
commentRepository
.
add
(
comment
);
articleMgmtService
.
incArticleCommentCount
(
articleId
);
try
{
sendNotificationMail
(
article
,
comment
,
originalComment
,
preference
);
}
catch
(
final
Exception
e
)
{
LOGGER
.
log
(
Level
.
WARN
,
"Send mail failed"
,
e
);
}
final
JSONObject
eventData
=
new
JSONObject
();
eventData
.
put
(
Comment
.
COMMENT
,
comment
);
eventData
.
put
(
Article
.
ARTICLE
,
article
);
...
...
@@ -637,8 +512,7 @@ public class CommentMgmtService {
* @param commentId the given comment id
* @throws ServiceException service exception
*/
public
void
removeArticleComment
(
final
String
commentId
)
throws
ServiceException
{
public
void
removeArticleComment
(
final
String
commentId
)
throws
ServiceException
{
final
Transaction
transaction
=
commentRepository
.
beginTransaction
();
try
{
...
...
@@ -665,8 +539,7 @@ public class CommentMgmtService {
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
public
void
incPageCommentCount
(
final
String
pageId
)
throws
JSONException
,
RepositoryException
{
public
void
incPageCommentCount
(
final
String
pageId
)
throws
JSONException
,
RepositoryException
{
final
JSONObject
page
=
pageRepository
.
get
(
pageId
);
final
JSONObject
newPage
=
new
JSONObject
(
page
,
JSONObject
.
getNames
(
page
));
final
int
commentCnt
=
page
.
getInt
(
Page
.
PAGE_COMMENT_COUNT
);
...
...
@@ -682,8 +555,7 @@ public class CommentMgmtService {
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
private
void
decArticleCommentCount
(
final
String
articleId
)
throws
JSONException
,
RepositoryException
{
private
void
decArticleCommentCount
(
final
String
articleId
)
throws
JSONException
,
RepositoryException
{
final
JSONObject
article
=
articleRepository
.
get
(
articleId
);
final
JSONObject
newArticle
=
new
JSONObject
(
article
,
JSONObject
.
getNames
(
article
));
final
int
commentCnt
=
article
.
getInt
(
Article
.
ARTICLE_COMMENT_COUNT
);
...
...
@@ -700,8 +572,7 @@ public class CommentMgmtService {
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
private
void
decPageCommentCount
(
final
String
pageId
)
throws
JSONException
,
RepositoryException
{
private
void
decPageCommentCount
(
final
String
pageId
)
throws
JSONException
,
RepositoryException
{
final
JSONObject
page
=
pageRepository
.
get
(
pageId
);
final
JSONObject
newPage
=
new
JSONObject
(
page
,
JSONObject
.
getNames
(
page
));
final
int
commentCnt
=
page
.
getInt
(
Page
.
PAGE_COMMENT_COUNT
);
...
...
@@ -712,46 +583,14 @@ public class CommentMgmtService {
/**
* Sets commenter thumbnail URL for the specified comment.
* <p>
* Try to set thumbnail URL using:
* <ol>
* <li>User avatar</li>
* <li>Gravatar service</li>
* <ol>
* </p>
*
* @param comment the specified comment
* @throws Exception exception
*/
public
void
setCommentThumbnailURL
(
final
JSONObject
comment
)
throws
Exception
{
final
String
commentEmail
=
comment
.
getString
(
Comment
.
COMMENT_EMAIL
);
// 1. user avatar
final
JSONObject
user
=
userRepository
.
getByEmail
(
commentEmail
);
if
(
null
!=
user
)
{
final
String
avatar
=
user
.
optString
(
UserExt
.
USER_AVATAR
);
if
(
StringUtils
.
isNotBlank
(
avatar
))
{
comment
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
avatar
);
return
;
}
}
// 2. Gravatar
String
thumbnailURL
=
Solos
.
getGravatarURL
(
commentEmail
.
toLowerCase
(),
"128"
);
final
URL
gravatarURL
=
new
URL
(
thumbnailURL
);
int
statusCode
=
HttpServletResponse
.
SC_OK
;
try
{
statusCode
=
HttpRequest
.
get
(
thumbnailURL
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
send
().
statusCode
();
}
catch
(
final
Exception
e
)
{
LOGGER
.
log
(
Level
.
DEBUG
,
"Can not fetch thumbnail from Gravatar [commentEmail={0}]"
,
commentEmail
);
}
finally
{
if
(
HttpServletResponse
.
SC_OK
!=
statusCode
)
{
thumbnailURL
=
Latkes
.
getStaticServePath
()
+
"/images/"
+
DEFAULT_USER_THUMBNAIL
;
}
}
comment
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
thumbnailURL
);
final
String
commenterName
=
comment
.
optString
(
Comment
.
COMMENT_NAME
);
final
JSONObject
commenter
=
userRepository
.
getByUserName
(
commenterName
);
final
String
avatarURL
=
commenter
.
optString
(
UserExt
.
USER_AVATAR
);
comment
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
avatarURL
);
}
}
src/main/java/org/b3log/solo/service/UpgradeService.java
View file @
555bcc11
...
...
@@ -17,48 +17,22 @@
*/
package
org
.
b3log
.
solo
.
service
;
import
org.apache.commons.lang.StringUtils
;
import
org.b3log.latke.Keys
;
import
org.b3log.latke.Latkes
;
import
org.b3log.latke.ioc.Inject
;
import
org.b3log.latke.logging.Level
;
import
org.b3log.latke.logging.Logger
;
import
org.b3log.latke.model.User
;
import
org.b3log.latke.repository.Query
;
import
org.b3log.latke.repository.Transaction
;
import
org.b3log.latke.repository.jdbc.util.Connections
;
import
org.b3log.latke.service.LangPropsService
;
import
org.b3log.latke.service.annotation.Service
;
import
org.b3log.solo.SoloServletListener
;
import
org.b3log.solo.cache.ArticleCache
;
import
org.b3log.solo.cache.CommentCache
;
import
org.b3log.solo.mail.MailService
;
import
org.b3log.solo.mail.MailServiceFactory
;
import
org.b3log.solo.model.Article
;
import
org.b3log.solo.model.Comment
;
import
org.b3log.solo.model.Option
;
import
org.b3log.solo.model.UserExt
;
import
org.b3log.solo.repository.ArticleRepository
;
import
org.b3log.solo.repository.CommentRepository
;
import
org.b3log.solo.repository.OptionRepository
;
import
org.b3log.solo.repository.UserRepository
;
import
org.b3log.solo.upgrade.V299_300
;
import
org.b3log.solo.upgrade.V300_310
;
import
org.b3log.solo.util.Solos
;
import
org.json.JSONArray
;
import
org.b3log.solo.upgrade.V310_320
;
import
org.json.JSONObject
;
import
java.sql.Connection
;
import
java.sql.Statement
;
import
java.util.Date
;
import
java.util.List
;
/**
* Upgrade service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="https://hacpai.com/member/e">Dongxu Wang</a>
* @version 1.2.1.1, Feb 28, 2019
* @version 1.2.1.2, Mar 3, 2019
* @since 1.2.0
*/
@Service
...
...
@@ -69,70 +43,12 @@ public class UpgradeService {
*/
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
UpgradeService
.
class
);
/**
* Step for article updating.
*/
private
static
final
int
STEP
=
50
;
/**
* Mail Service.
*/
private
static
final
MailService
MAIL_SVC
=
MailServiceFactory
.
getMailService
();
/**
* Article repository.
*/
@Inject
private
ArticleRepository
articleRepository
;
/**
* Comment repository.
*/
@Inject
private
CommentRepository
commentRepository
;
/**
* User repository.
*/
@Inject
private
UserRepository
userRepository
;
/**
* Option repository.
*/
@Inject
private
OptionRepository
optionRepository
;
/**
* Preference Query Service.
*/
@Inject
private
PreferenceQueryService
preferenceQueryService
;
/**
* Statistic query service.
*/
@Inject
private
StatisticQueryService
statisticQueryService
;
/**
* Language service.
*/
@Inject
private
LangPropsService
langPropsService
;
/**
* Article cache.
*/
@Inject
private
ArticleCache
articleCache
;
/**
* Comment cache.
*/
@Inject
private
CommentCache
commentCache
;
/**
* Upgrades if need.
*/
...
...
@@ -153,6 +69,8 @@ public class UpgradeService {
V299_300
.
perform
();
case
"3.0.0"
:
V300_310
.
perform
();
case
"3.1.0"
:
V310_320
.
perform
();
break
;
default
:
...
...
@@ -165,176 +83,4 @@ public class UpgradeService {
System
.
exit
(-
1
);
}
}
private
void
alterTables
()
throws
Exception
{
final
Connection
connection
=
Connections
.
getConnection
();
final
Statement
statement
=
connection
.
createStatement
();
final
String
tablePrefix
=
Latkes
.
getLocalProperty
(
"jdbc.tablePrefix"
)
+
"_"
;
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"archiveDate` RENAME TO `"
+
tablePrefix
+
"archivedate1`"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"archivedate1` RENAME TO `"
+
tablePrefix
+
"archivedate`"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"archiveDate_article` RENAME TO `"
+
tablePrefix
+
"archivedate_article1`"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"archivedate_article1` RENAME TO `"
+
tablePrefix
+
"archivedate_article`"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"article` ADD `articleAuthorId` VARCHAR(19) DEFAULT '' NOT NULL"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"article` ADD `articleCreated` BIGINT DEFAULT 0 NOT NULL"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"article` ADD `articleUpdated` BIGINT DEFAULT 0 NOT NULL"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"comment` ADD `commentCreated` BIGINT DEFAULT 0 NOT NULL"
);
statement
.
close
();
connection
.
commit
();
connection
.
close
();
}
private
void
dropTables
()
throws
Exception
{
final
Connection
connection
=
Connections
.
getConnection
();
final
Statement
statement
=
connection
.
createStatement
();
final
String
tablePrefix
=
Latkes
.
getLocalProperty
(
"jdbc.tablePrefix"
)
+
"_"
;
statement
.
execute
(
"DROP TABLE `"
+
tablePrefix
+
"statistic`"
);
statement
.
close
();
connection
.
commit
();
connection
.
close
();
}
private
void
upgradeUsers
()
throws
Exception
{
final
JSONArray
users
=
userRepository
.
get
(
new
Query
()).
getJSONArray
(
Keys
.
RESULTS
);
for
(
int
i
=
0
;
i
<
users
.
length
();
i
++)
{
final
JSONObject
user
=
users
.
getJSONObject
(
i
);
final
String
email
=
user
.
optString
(
User
.
USER_EMAIL
);
user
.
put
(
UserExt
.
USER_AVATAR
,
Solos
.
getGravatarURL
(
email
,
"128"
));
userRepository
.
update
(
user
.
optString
(
Keys
.
OBJECT_ID
),
user
);
LOGGER
.
log
(
Level
.
INFO
,
"Updated user[email={0}]"
,
email
);
}
}
private
void
upgradeArticles
()
throws
Exception
{
final
List
<
JSONObject
>
articles
=
articleRepository
.
getList
(
new
Query
().
select
(
Keys
.
OBJECT_ID
,
Article
.
ARTICLE_T_CREATE_DATE
,
Article
.
ARTICLE_T_UPDATE_DATE
,
Article
.
ARTICLE_T_AUTHOR_EMAIL
));
if
(
articles
.
isEmpty
())
{
LOGGER
.
log
(
Level
.
TRACE
,
"No articles"
);
return
;
}
Transaction
transaction
=
null
;
try
{
for
(
int
i
=
0
;
i
<
articles
.
size
();
i
++)
{
if
(
0
==
i
%
STEP
||
!
transaction
.
isActive
())
{
transaction
=
userRepository
.
beginTransaction
();
}
final
String
articleId
=
articles
.
get
(
i
).
optString
(
Keys
.
OBJECT_ID
);
final
JSONObject
article
=
articleRepository
.
get
(
articleId
);
String
authorEmail
=
article
.
optString
(
Article
.
ARTICLE_T_AUTHOR_EMAIL
);
if
(
StringUtils
.
isBlank
(
authorEmail
))
{
// H2
authorEmail
=
article
.
optString
(
Article
.
ARTICLE_T_AUTHOR_EMAIL
.
toUpperCase
());
}
JSONObject
author
=
userRepository
.
getByEmail
(
authorEmail
);
if
(
null
==
author
)
{
author
=
userRepository
.
getAdmin
();
}
article
.
put
(
Article
.
ARTICLE_AUTHOR_ID
,
author
.
optString
(
Keys
.
OBJECT_ID
));
Date
createDate
=
(
Date
)
article
.
opt
(
Article
.
ARTICLE_T_CREATE_DATE
);
if
(
null
==
createDate
)
{
// H2
createDate
=
(
Date
)
article
.
opt
(
Article
.
ARTICLE_T_CREATE_DATE
.
toUpperCase
());
}
article
.
put
(
Article
.
ARTICLE_CREATED
,
createDate
.
getTime
());
Date
updateDate
=
(
Date
)
article
.
opt
(
Article
.
ARTICLE_T_UPDATE_DATE
);
if
(
null
==
updateDate
)
{
// H2
updateDate
=
(
Date
)
article
.
opt
(
Article
.
ARTICLE_T_UPDATE_DATE
.
toUpperCase
());
}
article
.
put
(
Article
.
ARTICLE_UPDATED
,
updateDate
.
getTime
());
articleRepository
.
update
(
articleId
,
article
);
if
(
0
==
i
%
STEP
)
{
transaction
.
commit
();
LOGGER
.
log
(
Level
.
INFO
,
"Updated some articles ["
+
i
+
"]"
);
}
}
if
(
transaction
.
isActive
())
{
transaction
.
commit
();
}
LOGGER
.
log
(
Level
.
INFO
,
"Updated all articles"
);
}
catch
(
final
Exception
e
)
{
if
(
transaction
.
isActive
())
{
transaction
.
rollback
();
}
throw
e
;
}
}
private
void
upgradeComments
()
throws
Exception
{
final
List
<
JSONObject
>
comments
=
commentRepository
.
getList
(
new
Query
());
if
(
comments
.
isEmpty
())
{
LOGGER
.
log
(
Level
.
TRACE
,
"No comments"
);
return
;
}
Transaction
transaction
=
null
;
try
{
for
(
int
i
=
0
;
i
<
comments
.
size
();
i
++)
{
if
(
0
==
i
%
STEP
||
!
transaction
.
isActive
())
{
transaction
=
userRepository
.
beginTransaction
();
}
final
JSONObject
comment
=
comments
.
get
(
i
);
final
String
commentId
=
comment
.
optString
(
Keys
.
OBJECT_ID
);
Date
createDate
=
(
Date
)
comment
.
opt
(
Comment
.
COMMENT_T_DATE
);
if
(
null
==
createDate
)
{
// H2
createDate
=
(
Date
)
comment
.
opt
(
Comment
.
COMMENT_T_DATE
.
toUpperCase
());
}
comment
.
put
(
Comment
.
COMMENT_CREATED
,
createDate
.
getTime
());
commentRepository
.
update
(
commentId
,
comment
);
if
(
0
==
i
%
STEP
)
{
transaction
.
commit
();
LOGGER
.
log
(
Level
.
INFO
,
"Updated some comments ["
+
i
+
"]"
);
}
}
if
(
transaction
.
isActive
())
{
transaction
.
commit
();
}
LOGGER
.
log
(
Level
.
INFO
,
"Updated all comments"
);
}
catch
(
final
Exception
e
)
{
if
(
transaction
.
isActive
())
{
transaction
.
rollback
();
}
throw
e
;
}
}
/**
* Send an email to the user who upgrades Solo with a discontinuous version.
*
* @throws Exception exception
*/
private
void
notifyUserByEmail
()
throws
Exception
{
if
(!
Solos
.
isMailConfigured
())
{
return
;
}
final
String
adminEmail
=
preferenceQueryService
.
getPreference
().
getString
(
Option
.
ID_C_ADMIN_EMAIL
);
final
MailService
.
Message
message
=
new
MailService
.
Message
();
message
.
setFrom
(
adminEmail
);
message
.
addRecipient
(
adminEmail
);
message
.
setSubject
(
langPropsService
.
get
(
"skipVersionMailSubject"
));
message
.
setHtmlBody
(
langPropsService
.
get
(
"skipVersionMailBody"
));
MAIL_SVC
.
send
(
message
);
LOGGER
.
info
(
"Send an email to the user who upgrades Solo with a discontinuous version."
);
}
}
src/main/java/org/b3log/solo/upgrade/V310_320.java
View file @
555bcc11
...
...
@@ -64,9 +64,10 @@ public final class V310_320 {
Connection
connection
=
Connections
.
getConnection
();
Statement
statement
=
connection
.
createStatement
();
//
删除 userEmail 字段,
移除邮件相关功能 https://github.com/b3log/solo/issues/12690
// 移除邮件相关功能 https://github.com/b3log/solo/issues/12690
final
String
tablePrefix
=
Latkes
.
getLocalProperty
(
"jdbc.tablePrefix"
)
+
"_"
;
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"article` DROP COLUMN `userEmail`"
);
statement
.
executeUpdate
(
"ALTER TABLE `"
+
tablePrefix
+
"comment` DROP COLUMN `commentEmail`"
);
statement
.
close
();
connection
.
commit
();
connection
.
close
();
...
...
src/main/java/org/b3log/solo/util/Solos.java
View file @
555bcc11
...
...
@@ -19,7 +19,6 @@ package org.b3log.solo.util;
import
jodd.http.HttpRequest
;
import
jodd.http.HttpResponse
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.commons.lang.RandomStringUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.b3log.latke.Keys
;
...
...
@@ -474,14 +473,12 @@ public final class Solos {
}
/**
* Gets the
Gravatar URL for the specified email with the specified size
.
* Gets the
default user avatar URL.
.
*
* @param email the specified email
* @param size the specified size
* @return the Gravatar URL
* @return default user avatar URL
*/
public
static
String
get
GravatarURL
(
final
String
email
,
final
String
size
)
{
return
GRAVATAR
+
DigestUtils
.
md5Hex
(
email
)
+
"?s="
+
size
;
public
static
String
get
DefaultAvatar
(
)
{
return
Latkes
.
getStaticServePath
()
+
"/images/default-user-thumbnail.png"
;
}
/**
...
...
src/test/java/org/b3log/solo/AbstractTestCase.java
View file @
555bcc11
...
...
@@ -122,7 +122,6 @@ public abstract class AbstractTestCase {
public
void
init
()
throws
Exception
{
final
InitService
initService
=
getInitService
();
final
JSONObject
requestJSONObject
=
new
JSONObject
();
requestJSONObject
.
put
(
User
.
USER_EMAIL
,
"test@gmail.com"
);
requestJSONObject
.
put
(
User
.
USER_NAME
,
"Solo"
);
requestJSONObject
.
put
(
UserExt
.
USER_B3_KEY
,
"pass"
);
initService
.
init
(
requestJSONObject
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment