From 7a59df325e7484ded08cfba9c81a812240aec302 Mon Sep 17 00:00:00 2001
From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com>
Date: Fri, 20 Apr 2012 22:14:11 +0400
Subject: [PATCH] IPostParsingContext.OnUserMention implemented and used
---
FLocal.Common/BBCodes/User.cs | 1 +
.../BBCodes/helpers/IPostParsingContext.cs | 2 +-
FLocal.Common/UBBParser.cs | 8 ++++++--
FLocal.Common/dataobjects/Post.cs | 6 +++++-
FLocal.Common/dataobjects/Thread.cs | 6 +++++-
.../helpers/DelegatePostParsingContext.cs | 18 ++++++++++++++++--
6 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/FLocal.Common/BBCodes/User.cs b/FLocal.Common/BBCodes/User.cs
index 45f45d0..0b2d914 100644
--- a/FLocal.Common/BBCodes/User.cs
+++ b/FLocal.Common/BBCodes/User.cs
@@ -13,6 +13,7 @@ namespace FLocal.Common.BBCodes {
public override string Format(IPostParsingContext context, ITextFormatter formatter) {
var user = dataobjects.User.LoadByName(this.DefaultOrValue);
+ context.OnUserMention(user);
var url = new URL.users.user.Info(user.id.ToString(), null);
return String.Format("{2}", this.Safe(user.userGroup.name), url.canonical, this.Safe(user.name));
}
diff --git a/FLocal.Common/BBCodes/helpers/IPostParsingContext.cs b/FLocal.Common/BBCodes/helpers/IPostParsingContext.cs
index 038221f..7a3bf3b 100644
--- a/FLocal.Common/BBCodes/helpers/IPostParsingContext.cs
+++ b/FLocal.Common/BBCodes/helpers/IPostParsingContext.cs
@@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using FLocal.Common.dataobjects;
namespace FLocal.Common.BBCodes {
public interface IPostParsingContext {
+ void OnUserMention(dataobjects.User user);
}
}
diff --git a/FLocal.Common/UBBParser.cs b/FLocal.Common/UBBParser.cs
index 8968bc0..f85ce87 100644
--- a/FLocal.Common/UBBParser.cs
+++ b/FLocal.Common/UBBParser.cs
@@ -161,18 +161,22 @@ namespace FLocal.Common {
}
public string ParseQuote(string input) {
- string result = this.quotesParser.Parse(input).Format(new DelegatePostParsingContext(), this.simpleFormatter);
+ string result = this.quotesParser.Parse(input).Format(CreateStubContext(), this.simpleFormatter);
return result;
}
}
+ private static BBCodes.IPostParsingContext CreateStubContext() {
+ return new DelegatePostParsingContext(user => {});
+ }
+
public static string UBBToIntermediate(BBCodes.IPostParsingContext context, string UBB) {
return BBParserGateway.instance.Parse(context, UBB);
}
public static string UBBToIntermediate(string UBB) {
- return UBBToIntermediate(new DelegatePostParsingContext(), UBB);
+ return UBBToIntermediate(CreateStubContext(), UBB);
}
public static string ShallerToUBB(string shaller) {
diff --git a/FLocal.Common/dataobjects/Post.cs b/FLocal.Common/dataobjects/Post.cs
index 0387e04..dabc529 100644
--- a/FLocal.Common/dataobjects/Post.cs
+++ b/FLocal.Common/dataobjects/Post.cs
@@ -8,6 +8,7 @@ using Web.Core.DB;
using Web.Core.DB.conditions;
using FLocal.Common;
using FLocal.Common.actions;
+using FLocal.Common.helpers;
namespace FLocal.Common.dataobjects {
public class Post : SqlObject {
@@ -337,7 +338,10 @@ namespace FLocal.Common.dataobjects {
if(parentPost != null && parentPost.poster.id != poster.id) {
newMentionedUsersIds.Add(parentPost.poster.id);
}
- string newBodyIntermediate = UBBParser.UBBToIntermediate(newBody);
+ string newBodyIntermediate = UBBParser.UBBToIntermediate(
+ new DelegatePostParsingContext(mentionedUser => newMentionedUsersIds.Add(mentionedUser.id)),
+ newBody
+ );
List changes = new List {
new InsertChange(
diff --git a/FLocal.Common/dataobjects/Thread.cs b/FLocal.Common/dataobjects/Thread.cs
index 039f0f2..d07a26f 100644
--- a/FLocal.Common/dataobjects/Thread.cs
+++ b/FLocal.Common/dataobjects/Thread.cs
@@ -7,6 +7,7 @@ using Web.Core;
using Web.Core.DB;
using Web.Core.DB.conditions;
using FLocal.Common.actions;
+using FLocal.Common.helpers;
namespace FLocal.Common.dataobjects {
public class Thread : SqlObject {
@@ -420,7 +421,10 @@ namespace FLocal.Common.dataobjects {
//dirty hack
bodyIntermediate = body;
} else {
- bodyIntermediate = UBBParser.UBBToIntermediate(body);
+ bodyIntermediate = UBBParser.UBBToIntermediate(
+ new DelegatePostParsingContext(mentionedUser => mentionedUsersIds.Add(mentionedUser.id)),
+ body
+ );
}
var postInsertData = new Dictionary {
{ Post.TableSpec.FIELD_THREADID, new ScalarFieldValue(threadId.ToString()) },
diff --git a/FLocal.Common/helpers/DelegatePostParsingContext.cs b/FLocal.Common/helpers/DelegatePostParsingContext.cs
index 9116f52..4036e6f 100644
--- a/FLocal.Common/helpers/DelegatePostParsingContext.cs
+++ b/FLocal.Common/helpers/DelegatePostParsingContext.cs
@@ -3,9 +3,23 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLocal.Common.dataobjects;
-using FLocal.Common.BBCodes;
namespace FLocal.Common.helpers {
- class DelegatePostParsingContext : IPostParsingContext {
+ class DelegatePostParsingContext : BBCodes.IPostParsingContext {
+
+ private readonly Action onUserMention;
+
+ public DelegatePostParsingContext(Action onUserMention) {
+ this.onUserMention = onUserMention;
+ }
+
+ #region IPostParsingContext Members
+
+ public void OnUserMention(User user) {
+ this.onUserMention(user);
+ }
+
+ #endregion
+
}
}