Skip to content

Commit

Permalink
$ sed -ri s/TiebaUser/User/
Browse files Browse the repository at this point in the history
$ sed -ri s/tiebaUser/user/
$ git ls-files | grep TiebaUser | xargs -d'\n' -I{} bash -c 'mv {} $(echo {} | sed -e s/TiebaUser/User/)'
  • Loading branch information
n0099 committed Feb 13, 2024
1 parent d3eb4e2 commit 43e4a98
Show file tree
Hide file tree
Showing 25 changed files with 123 additions and 123 deletions.
4 changes: 2 additions & 2 deletions c#/crawler/src/Db/CrawlerDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public CrawlerDbContext() : this(fid: 0) { }
public delegate CrawlerDbContext New(Fid fid);

public Fid Fid { get; } = fid;
public DbSet<TiebaUser> Users => Set<TiebaUser>();
public DbSet<User> Users => Set<User>();
public DbSet<AuthorExpGradeRevision> AuthorExpGradeRevisions => Set<AuthorExpGradeRevision>();
public DbSet<ForumModeratorRevision> ForumModeratorRevisions => Set<ForumModeratorRevision>();
public DbSet<ThreadPost> Threads => Set<ThreadPost>();
Expand Down Expand Up @@ -65,7 +65,7 @@ protected override void OnModelCreating(ModelBuilder b)
{
base.OnModelCreating(b);
OnModelCreatingWithFid(b, Fid);
b.Entity<TiebaUser>().ToTable("tbmc_user");
b.Entity<User>().ToTable("tbmc_user");
b.Entity<ThreadPost>().ToTable($"tbmc_f{Fid}_thread");
b.Entity<ThreadMissingFirstReply>().ToTable("tbmc_thread_missingFirstReply");
b.Entity<ReplyPost>().ToTable($"tbmc_f{Fid}_reply");
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Db/TiebaUser.cs → c#/crawler/src/Db/User.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace tbm.Crawler.Db;

public class TiebaUser : ITimestampingEntity
public class User : ITimestampingEntity
{
[Key] public long Uid { get; set; }
public string? Name { get; set; }
Expand All @@ -14,6 +14,6 @@ public class TiebaUser : ITimestampingEntity
public uint CreatedAt { get; set; }
public uint? UpdatedAt { get; set; }

public static TiebaUser CreateLatestReplier(long uid, string? name, string? displayName) =>
public static User CreateLatestReplier(long uid, string? name, string? displayName) =>
new() {Uid = uid, Name = name, DisplayName = displayName};
}
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Facade/BaseCrawlFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public virtual void Dispose()
using var transaction = db.Database.BeginTransaction(IsolationLevel.ReadCommitted);
var saver = saverFactory(Posts);
var savedPosts = Posts.IsEmpty ? null : saver.SavePosts(db);
Users.SaveUsers(db, saver.PostType, saver.TiebaUserFieldChangeIgnorance);
Users.SaveUsers(db, saver.PostType, saver.UserFieldChangeIgnorance);
BeforeCommitSaveHook(db);
try
{
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ThreadCrawlFacade(
: BaseCrawlFacade<ThreadPost, BaseThreadRevision, ThreadResponse, Thread>
(crawler(forumName), parser, saver.Invoke, locks["thread"], new(fid), fid)
{
private readonly Dictionary<long, TiebaUser> _latestRepliers = new();
private readonly Dictionary<long, User> _latestRepliers = new();

Check failure on line 13 in c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs

View workflow job for this annotation

GitHub Actions / build (crawler)

'User' is an ambiguous reference between 'tbm.Crawler.Db.User' and 'TbClient.User'

public delegate ThreadCrawlFacade New(Fid fid, string forumName);

Expand All @@ -20,7 +20,7 @@ protected override void BeforeCommitSaveHook(CrawlerDbContext db)
// note this will bypass user revision detection since not invoking CommonInSavers.SavePostsOrUsers() but directly DbContext.AddRange()

// users has already been added into DbContext and tracking
var existingUsersId = db.ChangeTracker.Entries<TiebaUser>().Select(ee => ee.Entity.Uid);
var existingUsersId = db.ChangeTracker.Entries<User>().Select(ee => ee.Entity.Uid);
var newLatestRepliers = _latestRepliers
.ExceptBy(existingUsersId, pair => pair.Key)
.Select(pair => pair.Value)
Expand All @@ -45,7 +45,7 @@ protected void ParseLatestRepliers(IEnumerable<Thread> threads) =>
// some rare deleted thread but still visible in 6.0.2 response
// will have a latest replier uid=0 name="" nameShow=".*"
.Where(u => u.Uid != 0)
.Select(u => TiebaUser.CreateLatestReplier(u.Uid, u.Name.NullIfEmpty(),
.Select(u => User.CreateLatestReplier(u.Uid, u.Name.NullIfEmpty(),
u.Name == u.NameShow ? null : u.NameShow))
.ForEach(u => _latestRepliers[u.Uid] = u);

Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class BaseSaver<TPost, TBaseRevision>(
protected delegate void PostSaveEventHandler();
protected event PostSaveEventHandler PostSaveEvent = () => { };

public virtual FieldChangeIgnoranceDelegates TiebaUserFieldChangeIgnorance =>
public virtual FieldChangeIgnoranceDelegates UserFieldChangeIgnorance =>
throw new NotImplementedException();
public string PostType { get; } = postType;
protected ConcurrentDictionary<ulong, TPost> Posts { get; } = posts;
Expand All @@ -37,7 +37,7 @@ protected SaverChangeSet<TPost> SavePosts<TRevision>(
// deep copy before entities get mutated by CommonInSavers.SavePostsOrUsers()
var existingBeforeMerge = existingPostsKeyById.Select(pair => (TPost)pair.Value.Clone()).ToList();

SavePostsOrUsers(db, TiebaUserFieldChangeIgnorance, revisionFactory,
SavePostsOrUsers(db, UserFieldChangeIgnorance, revisionFactory,
Posts.Values.ToLookup(p => existingPostsKeyById.ContainsKey(postIdSelector(p))),
p => existingPostsKeyById[postIdSelector(p)]);
return new(existingBeforeMerge, Posts.Values, postIdSelector);
Expand Down
10 changes: 5 additions & 5 deletions c#/crawler/src/Tieba/Crawl/Saver/CommonInSavers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool IsTimestampingFieldName(string name) => name is nameof(IPost.LastSeenAt)
var revision = default(TRevision);
var revisionNullFieldsBitMask = 0;
var whichPostType = typeof(TPostOrUser);
var entryIsUser = whichPostType == typeof(TiebaUser);
var entryIsUser = whichPostType == typeof(User);
foreach (var p in entry.Properties)
{
var pName = p.Metadata.Name;
Expand Down Expand Up @@ -118,21 +118,21 @@ private static bool IsLatestReplierUser(string pName, PropertyEntry p, EntityEnt
// so we should ignore its revision update for all fields
// ignore entire record is not possible via GlobalFieldChangeIgnorance.Revision()
// since it can only determine one field at the time
if (pName != nameof(TiebaUser.Portrait) || p.OriginalValue is not "") return false;
if (pName != nameof(User.Portrait) || p.OriginalValue is not "") return false;

// invokes OriginalValues.ToObject() to get a new instance
// since postOrUserInTracking is reference to the changed one
var user = (TiebaUser)entry.OriginalValues.ToObject();
var user = (User)entry.OriginalValues.ToObject();

// create another user instance with only fields of latest replier filled
var latestReplier = TiebaUser.CreateLatestReplier(user.Uid, user.Name, user.DisplayName);
var latestReplier = User.CreateLatestReplier(user.Uid, user.Name, user.DisplayName);

// if they are same by fields values, the original one is a latest replier
// that previously generated by ParseLatestRepliers()
return IsSameUser(user, latestReplier);
}

private static bool IsSameUser(TiebaUser a, TiebaUser b) =>
private static bool IsSameUser(User a, User b) =>

Check failure on line 135 in c#/crawler/src/Tieba/Crawl/Saver/CommonInSavers.cs

View workflow job for this annotation

GitHub Actions / build (crawler)

'User' is an ambiguous reference between 'tbm.Crawler.Db.User' and 'TbClient.User'

Check failure on line 135 in c#/crawler/src/Tieba/Crawl/Saver/CommonInSavers.cs

View workflow job for this annotation

GitHub Actions / build (crawler)

'User' is an ambiguous reference between 'tbm.Crawler.Db.User' and 'TbClient.User'
(a.Uid, a.Name, a.DisplayName, a.Portrait, a.PortraitUpdatedAt, a.Gender, a.FansNickname, a.IpGeolocation)
== (b.Uid, b.Name, b.DisplayName, b.Portrait, b.PortraitUpdatedAt, b.Gender, b.FansNickname, b.IpGeolocation)
&& (a.Icon == b.Icon || (a.Icon != null && b.Icon != null && a.Icon.SequenceEqual(b.Icon)));
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/ReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public partial class ReplySaver(
{
public delegate ReplySaver New(ConcurrentDictionary<PostId, ReplyPost> posts);

public override FieldChangeIgnoranceDelegates TiebaUserFieldChangeIgnorance { get; } = new(
public override FieldChangeIgnoranceDelegates UserFieldChangeIgnorance { get; } = new(
Update: (_, propName, oldValue, newValue) => propName switch
{ // FansNickname in reply response will always be null
nameof(TiebaUser.FansNickname) when oldValue is not null && newValue is null => true,
nameof(User.FansNickname) when oldValue is not null && newValue is null => true,
_ => false
},
Revision: (_, propName, oldValue, newValue) => propName switch
{ // user icon will be null after UserParserAndSaver.ResetUsersIcon() get invoked
nameof(TiebaUser.Icon) when oldValue is null && newValue is not null => true,
nameof(User.Icon) when oldValue is null && newValue is not null => true,
_ => false
});

Expand Down
10 changes: 5 additions & 5 deletions c#/crawler/src/Tieba/Crawl/Saver/StaticCommonInSavers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public delegate bool FieldChangeIgnoranceDelegate(
protected static FieldChangeIgnoranceDelegates GlobalFieldChangeIgnorance { get; } = new(
Update: (whichPostType, propName, oldValue, newValue) =>
{
if (whichPostType == typeof(TiebaUser))
if (whichPostType == typeof(User))
{
switch (propName)
{ // possible randomly respond with null
case nameof(TiebaUser.IpGeolocation) when newValue is null:
case nameof(User.IpGeolocation) when newValue is null:
// possible clock drift across multiple response from tieba api
// they should sync their servers with NTP
/* following sql can track these drift
Expand All @@ -29,7 +29,7 @@ FROM tbmcr_user WHERE portraitUpdatedAt IS NOT NULL
WHERE portraitUpdatedAtDiff > -100 AND portraitUpdatedAtDiff < 100
GROUP BY portraitUpdatedAtDiff ORDER BY portraitUpdatedAtDiff;
*/
case nameof(TiebaUser.PortraitUpdatedAt)
case nameof(User.PortraitUpdatedAt)
when Math.Abs((newValue as int? ?? 0) - (oldValue as int? ?? 0)) <= 10:
return true;
}
Expand Down Expand Up @@ -74,8 +74,8 @@ when newValue is ""
},
Revision: (whichPostType, propName, oldValue, _) =>
{ // ignore revision that figures update existing old users that don't have ip geolocation
if (whichPostType == typeof(TiebaUser)
&& propName == nameof(TiebaUser.IpGeolocation) && oldValue is null) return true;
if (whichPostType == typeof(User)
&& propName == nameof(User.IpGeolocation) && oldValue is null) return true;
if (whichPostType == typeof(ThreadPost))
{
switch (propName)
Expand Down
8 changes: 4 additions & 4 deletions c#/crawler/src/Tieba/Crawl/Saver/SubReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public class SubReplySaver(
{
public delegate SubReplySaver New(ConcurrentDictionary<PostId, SubReplyPost> posts);

public override FieldChangeIgnoranceDelegates TiebaUserFieldChangeIgnorance { get; } = new(
public override FieldChangeIgnoranceDelegates UserFieldChangeIgnorance { get; } = new(
Update: (_, propName, oldValue, newValue) => propName switch
{ // always ignore updates on iconinfo due to some rare user will show some extra icons
// compare to reply response in the response of sub reply
nameof(TiebaUser.Icon) => true,
nameof(User.Icon) => true,
// FansNickname in sub reply response will always be null
nameof(TiebaUser.FansNickname) when oldValue is not null && newValue is null => true,
nameof(User.FansNickname) when oldValue is not null && newValue is null => true,
// DisplayName in users embedded in sub replies from response will be the legacy nick name
nameof(TiebaUser.DisplayName) => true,
nameof(User.DisplayName) => true,
_ => false
}, (_, _, _, _) => false);

Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/ThreadSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class ThreadSaver(
{
public delegate ThreadSaver New(ConcurrentDictionary<Tid, ThreadPost> posts);

public override FieldChangeIgnoranceDelegates TiebaUserFieldChangeIgnorance { get; } = new(
public override FieldChangeIgnoranceDelegates UserFieldChangeIgnorance { get; } = new(
Update: (_, propName, _, _) => propName switch
{ // Icon.SpriteInfo will be an empty array and the icon url is a smaller one
// so we should mark it as null temporarily
// note this will cause we can't record when did a user update its iconinfo to null
// since these null values have been ignored in reply and sub reply saver
nameof(TiebaUser.Icon) => true,
nameof(User.Icon) => true,
_ => false
}, (_, _, _, _) => false);

Expand Down
18 changes: 9 additions & 9 deletions c#/crawler/src/Tieba/Crawl/UserParserAndSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ protected override Dictionary<Type, RevisionUpsertDelegate>
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row")]
protected override ushort GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
{
nameof(TiebaUser.Name) => 1,
nameof(TiebaUser.Gender) => 1 << 3,
nameof(TiebaUser.Icon) => 1 << 5,
nameof(User.Name) => 1,
nameof(User.Gender) => 1 << 3,
nameof(User.Icon) => 1 << 5,
_ => 0
};
}
Expand All @@ -38,7 +38,7 @@ public partial class UserParserAndSaver(ILogger<UserParserAndSaver> logger)
{
private static readonly HashSet<Uid> UserIdLocks = new();
private readonly List<Uid> _savedUsersId = new();
private readonly ConcurrentDictionary<Uid, TiebaUser> _users = new();
private readonly ConcurrentDictionary<Uid, User> _users = new();

Check failure on line 41 in c#/crawler/src/Tieba/Crawl/UserParserAndSaver.cs

View workflow job for this annotation

GitHub Actions / build (crawler)

'User' is an ambiguous reference between 'tbm.Crawler.Db.User' and 'TbClient.User'

public void ParseUsers(IEnumerable<User> users) =>

Check failure on line 43 in c#/crawler/src/Tieba/Crawl/UserParserAndSaver.cs

View workflow job for this annotation

GitHub Actions / build (crawler)

'User' is an ambiguous reference between 'tbm.Crawler.Db.User' and 'TbClient.User'
users.Select(el =>
Expand All @@ -65,7 +65,7 @@ public void ParseUsers(IEnumerable<User> users) =>
// will be an empty string when the user hasn't set a username for their baidu account yet
var name = el.Name.NullIfEmpty();
var nameShow = el.NameShow.NullIfEmpty();
var u = new TiebaUser();
var u = new User();
try
{
u.Uid = uid;
Expand All @@ -85,25 +85,25 @@ public void ParseUsers(IEnumerable<User> users) =>
e.Data["raw"] = Helper.UnescapedJsonSerialize(el);
throw new InvalidDataException("User parse error.", e);
}
}).OfType<TiebaUser>().ForEach(u => _users[u.Uid] = u);
}).OfType<User>().ForEach(u => _users[u.Uid] = u);

public void ResetUsersIcon() => _users.Values.ForEach(u => u.Icon = null);

public void SaveUsers
(CrawlerDbContext db, string postType, FieldChangeIgnoranceDelegates tiebaUserFieldChangeIgnorance)
(CrawlerDbContext db, string postType, FieldChangeIgnoranceDelegates userFieldChangeIgnorance)
{
if (_users.IsEmpty) return;
lock (UserIdLocks)
{
var usersExceptLocked = new Dictionary<Uid, TiebaUser>(_users.ExceptBy(UserIdLocks, pair => pair.Key));
var usersExceptLocked = new Dictionary<Uid, User>(_users.ExceptBy(UserIdLocks, pair => pair.Key));
if (!usersExceptLocked.Any()) return;
_savedUsersId.AddRange(usersExceptLocked.Keys);
UserIdLocks.UnionWith(_savedUsersId);

var existingUsersKeyByUid = (from user in db.Users.AsTracking().ForUpdate()
where usersExceptLocked.Keys.Contains(user.Uid)
select user).ToDictionary(u => u.Uid);
SavePostsOrUsers(db, tiebaUserFieldChangeIgnorance,
SavePostsOrUsers(db, userFieldChangeIgnorance,
u => new UserRevision
{
TakenAt = u.UpdatedAt ?? u.CreatedAt,
Expand Down
10 changes: 5 additions & 5 deletions fe/src/api/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Reply, SubReply, Thread } from './post';
import type { TiebaUser, TiebaUserGenderQueryParam } from './user';
import type { SelectTiebaUserParams } from '@/components/widgets/selectTiebaUser';
import type { User, UserGenderQueryParam } from './user';
import type { SelectUserParams } from '@/components/widgets/selectUser';
import type { BoolInt, Fid, Float, PostType, UInt, UnixTimestamp } from '@/shared';
import type { Mix } from '@/shared/groupBytimeGranularityUtcPlus8';

Expand Down Expand Up @@ -54,8 +54,8 @@ interface CursorPagination {
}

export type ApiUsers = Api<
CursorPagination & { users: TiebaUser[] },
CursorPaginationQueryParam & SelectTiebaUserParams & { gender?: TiebaUserGenderQueryParam }
CursorPagination & { users: User[] },
CursorPaginationQueryParam & SelectUserParams & { gender?: UserGenderQueryParam }
>;

export type JsonString = string;
Expand All @@ -70,5 +70,5 @@ export type ApiPosts = Api<CursorPagination<{
subReplies: SubReply[]
}>
}>,
users: TiebaUser[]
users: User[]
}, CursorPaginationQueryParam & { query: JsonString }>;
8 changes: 4 additions & 4 deletions fe/src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export type ForumModeratorType = 'assist'
| 'videoadmin'
| 'voiceadmin';
export type AuthorExpGrade = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18;
export type TiebaUserGender = 0 | 1 | 2 | null;
export type TiebaUserGenderQueryParam = '0' | '1' | '2' | 'NULL';
export type UserGender = 0 | 1 | 2 | null;
export type UserGenderQueryParam = '0' | '1' | '2' | 'NULL';

export interface TiebaUser extends TimestampFields {
export interface User extends TimestampFields {
uid: BaiduUserID,
name: string | null,
displayName: string | null,
portrait: string,
portraitUpdatedAt: UInt | null,
gender: TiebaUserGender,
gender: UserGender,
fansNickname: string | null,
icon: ObjUnknown[] | null,
ipGeolocation: string | null,
Expand Down
4 changes: 2 additions & 2 deletions fe/src/components/Post/badges/BadgeUser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
</template>

<script setup lang="ts">
import type { BaiduUserID, ForumModeratorType, TiebaUser } from '@/api/user';
import type { BaiduUserID, ForumModeratorType, User } from '@/api/user';
import type { BootstrapColor } from '@/shared';
import { computed } from 'vue';
import * as _ from 'lodash-es';
const props = defineProps<{
user: TiebaUser,
user: User,
threadAuthorUid?: BaiduUserID,
replyAuthorUid?: BaiduUserID
}>();
Expand Down
6 changes: 3 additions & 3 deletions fe/src/components/Post/renderers/RendererList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<div v-for="author in [getUser(reply.authorUid)]" :key="author.uid"
class="reply-author col-auto text-center sticky-top shadow-sm badge bg-light">
<RouterLink :to="userRoute(author.uid)" class="d-block">
<img :src="toTiebaUserPortraitImageUrl(author.portrait)"
<img :src="toUserPortraitImageUrl(author.portrait)"
loading="lazy" class="tieba-user-portrait-large" />
<p class="my-0">{{ author.name }}</p>
<p v-if="author.displayName !== null && author.name !== null">{{ author.displayName }}</p>
Expand All @@ -120,7 +120,7 @@
<RouterLink v-if="subReplyGroup[subReplyIndex - 1] === undefined"
:to="userRoute(author.uid)"
class="sub-reply-author text-wrap badge bg-light">
<img :src="toTiebaUserPortraitImageUrl(author.portrait)"
<img :src="toUserPortraitImageUrl(author.portrait)"
loading="lazy" class="tieba-user-portrait-small" />
<span class="mx-2 align-middle link-dark">
{{ renderUsername(subReply.authorUid) }}
Expand Down Expand Up @@ -164,7 +164,7 @@ import type { Reply, SubReply, Thread } from '@/api/post';
import type { BaiduUserID } from '@/api/user';
import { compareRouteIsNewQuery, setComponentCustomScrollBehaviour } from '@/router';
import type { Modify } from '@/shared';
import { convertRemToPixels, isElementNode, toTiebaUserPortraitImageUrl } from '@/shared';
import { convertRemToPixels, isElementNode, toUserPortraitImageUrl } from '@/shared';
import { initialTippy } from '@/shared/tippy';
import { useElementRefsStore } from '@/stores/elementRefs';
import '@/styles/bootstrapCallout.css';
Expand Down
Loading

0 comments on commit 43e4a98

Please sign in to comment.