pull/50/head
sunface 4 years ago
parent 24d71a4188
commit 3295ac48ed

@ -16,7 +16,7 @@ import { requestApi } from "utils/axios/request"
import { addParamToUrl, removeParamFromUrl } from "utils/url"
const PostsSearchPage = () => {
let filter = SearchFilter.Best
let filter = SearchFilter.Favorites
const router = useRouter()
const q = router.query.q
@ -81,10 +81,10 @@ const PostsSearchPage = () => {
<Sidebar query={query ?{q:query} : null} routes={searchLinks} title="全站搜索" />
<Box ml="3" width={['100%', '100%', '100%', '70%']}>
<Card p="5">
<Input value={tempQuery} onChange={(e) => setTempQuery(e.currentTarget.value)} onKeyUp={(e) => startSearch(e)} size="lg" placeholder="type to search..." variant="unstyled" />
<Input value={tempQuery} onChange={(e) => setTempQuery(e.currentTarget.value)} onKeyUp={(e) => startSearch(e)} size="lg" placeholder="type and enter to search..." variant="unstyled" />
</Card>
<Card mt="2" p="0" pt="4" px="4">
<SearchFilters filters={getFilters()} onChange={onFilterChange}/>
<SearchFilters filters={getFilters()} onChange={onFilterChange} initFilter={filter}/>
<Divider mt="3"/>
{results.length === 0 && <Empty /> }
{results.length > 0 &&

@ -19,7 +19,7 @@ import UserCard from "components/users/user-card"
import Users from "components/users/users"
const PostsSearchPage = () => {
let filter = SearchFilter.Best
let filter = SearchFilter.Favorites
const router = useRouter()
const q = router.query.q
@ -87,7 +87,7 @@ const PostsSearchPage = () => {
<Input value={tempQuery} onChange={(e) => setTempQuery(e.currentTarget.value)} onKeyUp={(e) => startSearch(e)} size="lg" placeholder="type to search..." variant="unstyled" />
</Card>
<Card mt="2" p="0" pt="4" px="4">
<SearchFilters filters={getFilters()} onChange={onFilterChange}/>
<SearchFilters filters={getFilters()} onChange={onFilterChange} initFilter={filter}/>
<Divider mt="3"/>
{results.length === 0 ? <Empty /> : <Users users={results} p="2" highlight={query}/>}
</Card>

@ -3,6 +3,7 @@ package cache
import (
"time"
"github.com/imdotdev/im.dev/server/internal/interaction"
"github.com/imdotdev/im.dev/server/pkg/db"
"github.com/imdotdev/im.dev/server/pkg/log"
"github.com/imdotdev/im.dev/server/pkg/models"
@ -38,6 +39,8 @@ func Init() {
if user.Cover == "" {
user.Cover = models.DefaultCover
}
user.Follows = interaction.GetFollows(user.ID)
users = append(users, user)
}

@ -14,20 +14,26 @@ import (
var logger = log.RootLogger.New("logger", "search")
func Posts(user *models.User, filter, query string) models.Posts {
posts := make(models.Posts, 0)
func Posts(user *models.User, filter, query string) []*models.Post {
posts := make([]*models.Post, 0)
// postsMap := make(map[string]*models.Post)
// search by title
rows, err := db.Conn.Query("select id,slug,title,url,cover,brief,creator,created,updated from posts where title LIKE ?", "%"+query+"%")
sqlq := "%" + query + "%"
rows, err := db.Conn.Query("select id,slug,title,url,cover,brief,creator,created,updated from posts where title LIKE ? or brief LIKE ?", sqlq, sqlq)
if err != nil {
logger.Warn("get user posts error", "error", err)
return posts
}
posts = story.GetPosts(user, rows)
sort.Sort(posts)
if filter == models.FilterFavorites {
sort.Sort(models.FavorPosts(posts))
} else {
sort.Sort(models.Posts(posts))
}
return posts
}
@ -35,7 +41,7 @@ func Posts(user *models.User, filter, query string) models.Posts {
func Users(user *models.User, filter, query string) []*models.User {
allUsers := cache.Users
users := make([]*models.User, 0)
users := make(models.Users, 0)
for _, u := range allUsers {
if strings.Contains(strings.ToLower(u.Nickname), strings.ToLower(query)) {
users = append(users, u)
@ -52,5 +58,6 @@ func Users(user *models.User, filter, query string) []*models.User {
u.Followed = interaction.GetFollowed(u.ID, user.ID)
}
sort.Sort(users)
return users
}

@ -37,3 +37,11 @@ func (ar Posts) Swap(i, j int) { ar[i], ar[j] = ar[j], ar[i] }
func (ar Posts) Less(i, j int) bool {
return ar[i].Created.Unix() > ar[j].Created.Unix()
}
type FavorPosts []*Post
func (ar FavorPosts) Len() int { return len(ar) }
func (ar FavorPosts) Swap(i, j int) { ar[i], ar[j] = ar[j], ar[i] }
func (ar FavorPosts) Less(i, j int) bool {
return ar[i].Likes > ar[j].Likes
}

@ -37,6 +37,14 @@ type User struct {
Created time.Time `json:"created"`
}
type Users []*User
func (ar Users) Len() int { return len(ar) }
func (ar Users) Swap(i, j int) { ar[i], ar[j] = ar[j], ar[i] }
func (ar Users) Less(i, j int) bool {
return ar[i].Follows > ar[j].Follows
}
func (user *User) Query(id string, username string, email string) error {
err := db.Conn.QueryRow(`SELECT id,username,role,nickname,email,avatar,last_seen_at,created FROM user WHERE id=? or username=? or email=?`,
id, username, email).Scan(&user.ID, &user.Username, &user.Role, &user.Nickname, &user.Email, &user.Avatar, &user.LastSeenAt, &user.Created)

@ -5,13 +5,14 @@ import { getSvgIcon } from "components/svg-icon"
import { SearchFilter } from "src/types/search"
interface Props {
initFilter?: string
filters?: SearchFilter[]
onChange: any
}
export const SearchFilters = (props:Props) => {
const {filters=[SearchFilter.Best,SearchFilter.Featured,SearchFilter.Recent],onChange} = props
const [filter, setFilter] = useState(SearchFilter.Best)
const {initFilter=SearchFilter.Best,filters=[SearchFilter.Best,SearchFilter.Featured,SearchFilter.Recent],onChange} = props
const [filter, setFilter] = useState(initFilter)
const changeFilter = f => {
onChange(f)

@ -26,14 +26,14 @@ export const PostCard = (props: Props) => {
<PostAuthor post={post} showFooter={false} size="md" />
<Link href={`/${post.creator.username}/${post.id}`}>
<Layout alignItems={isLargeScreen ? "top" : "left"} cursor="pointer" pl="2" pt="1">
<VStack alignItems="left" spacing="3" width={isLargeScreen && type === "classic" ? "calc(100% - 18rem)" : '100%'}>
<Heading size="md"><Highlighter
<VStack alignItems="left" spacing={type==="classic"? 3 : 2} width={isLargeScreen && type === "classic" ? "calc(100% - 18rem)" : '100%'}>
<Heading size="md" fontSize={type==="classic" ? '1.4rem' : '1.2rem'}><Highlighter
highlightClassName="highlight-search-match"
textToHighlight={post.title}
searchWords={[props.highlight]}
/>
</Heading>
{type !== "classic" && <HStack>{post.rawTags.map(t => <Text layerStyle="textSecondary" fontSize="sm">#{t.name}</Text>)}</HStack>}
{type !== "classic" && <HStack>{post.rawTags.map(t => <Text layerStyle="textSecondary" fontSize="md">#{t.name}</Text>)}</HStack>}
<Text layerStyle={type === "classic" ? "textSecondary" : null}>
<Highlighter
highlightClassName="highlight-search-match"

@ -44,7 +44,11 @@ export const UserCard= ({user,highlight}:Props) =>{
</Text>}
</VStack>
</HStack>
<Follow followed={user.followed} targetID={user.id} size="sm"/>
<HStack>
<Text fontWeight="600" fontSize=".9rem">{user.follows} followers</Text>
<Follow followed={user.followed} targetID={user.id} size="sm"/>
</HStack>
</Flex>
)
}

@ -23,14 +23,14 @@ export const searchLinks: any[] = [{
path: `${ReserveUrls.Search}/posts`,
icon: getSvgIcon("post"),
disabled: false,
filters: [SearchFilter.Best,SearchFilter.Recent]
filters: [SearchFilter.Favorites,SearchFilter.Recent]
},
{
title: '用户',
path: `${ReserveUrls.Search}/users`,
icon: getSvgIcon('user','1.5rem'),
disabled: false,
filters: [SearchFilter.Best]
filters: [SearchFilter.Favorites]
}
]

Loading…
Cancel
Save